From 2d3be758f7bbc2c26e1d1bbfa941f66764025362 Mon Sep 17 00:00:00 2001 From: aaron-hardin Date: Mon, 28 Sep 2020 17:48:38 -0500 Subject: [PATCH 1/2] Add test for optional format field --- tests/qunit/methods.js | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/qunit/methods.js b/tests/qunit/methods.js index 19e354b..4d56566 100644 --- a/tests/qunit/methods.js +++ b/tests/qunit/methods.js @@ -58,6 +58,7 @@ $(document).ready(function() { equals(accounting.formatMoney(0, { symbol: "GBP", format:format}), "GBP --", "`format` parameter provided given as an object with `zero` format, correctly observed in string output"); equals(accounting.formatMoney(-1000, { symbol: "GBP", format:format}), "GBP (1,000.00)", "`format` parameter provided given as an object with `neg` format, correctly observed in string output"); equals(accounting.formatMoney(1000, { symbol: "GBP", format:{neg:"--%v %s"}}), "GBP1,000.00", "`format` parameter provided, but only `neg` value provided - positive value should be formatted by default format (%s%v)"); + equals(accounting.formatMoney(-1000, { symbol: "GBP", format:{pos:"%s%v"}}), "GBP-1,000.00", "`format` parameter provided, but only `pos` value provided - negative and zero should be optional"); accounting.settings.currency.format = "%s%v"; accounting.formatMoney(0, {format:""}); From 70f36cc36a963bc209f19bc8a8ca018c397f7d4c Mon Sep 17 00:00:00 2001 From: aaron-hardin Date: Mon, 28 Sep 2020 17:49:21 -0500 Subject: [PATCH 2/2] Update checkCurrencyFormat to populate optional fields --- accounting.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/accounting.js b/accounting.js index fd181fd..fe3ee62 100644 --- a/accounting.js +++ b/accounting.js @@ -156,6 +156,17 @@ zero : defaults }; + // If format is an object then neg and zero should be optional + } else if ( isObject( format ) && ( !format.neg || !format.zero ) ) { + + // If the negative or zero value is missing, populate based on positive + if ( !format.neg ) { + format.neg = format.pos.replace("%v", "-%v"); + } + if ( !format.zero ) { + format.zero = format.pos; + } + } // Otherwise, assume format was fine: return format;