Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow optional fields on currency format #219

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions accounting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions tests/qunit/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:""});
Expand Down