Skip to content

Commit

Permalink
Add currencySymbol & currencyPosition to format
Browse files Browse the repository at this point in the history
Fix #344

* src/validating.js:
* src/formatting.js: add support for `currencyPosition` and
`currencySymbol`.
* tests/src/numbro-tests.js: add regression test.
  • Loading branch information
BenjaminVanRyseghem committed Mar 21, 2018
1 parent d21d347 commit 0d99ddd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/formatting.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,15 @@ function formatCurrency(instance, providedFormat, state) {
let decimalSeparator = undefined;
let space = "";
let average = !!options.totalLength || !!options.forceAverage || options.average;
let position = providedFormat.currencyPosition || currentCurrency.position;
let symbol = providedFormat.currencySymbol || currentCurrency.symbol;

if (options.spaceSeparated) {
space = " ";
}

if (currentCurrency.position === "infix") {
decimalSeparator = space + currentCurrency.symbol + space;
if (position === "infix") {
decimalSeparator = space + symbol + space;
}

let output = formatNumber({
Expand All @@ -300,17 +302,17 @@ function formatCurrency(instance, providedFormat, state) {
decimalSeparator
});

if (currentCurrency.position === "prefix") {
if (position === "prefix") {
if (instance._value < 0 && options.negative === "sign") {
output = `-${space}${currentCurrency.symbol}${output.slice(1)}`;
output = `-${space}${symbol}${output.slice(1)}`;
} else {
output = currentCurrency.symbol + space + output;
output = symbol + space + output;
}
}

if (!currentCurrency.position || currentCurrency.position === "postfix") {
if (!position || position === "postfix") {
space = average ? "" : space;
output = output + space + currentCurrency.symbol;
output = output + space + symbol;
}

return output;
Expand Down
11 changes: 11 additions & 0 deletions src/validating.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ const validForceAverageValues = [
"thousand"
];

const validCurrencyPosition = [
"prefix",
"infix",
"postfix"
];

const validNegativeValues = [
"sign",
"parenthesis"
Expand Down Expand Up @@ -109,6 +115,11 @@ const validFormat = {
validValues: validForceAverageValues
},
average: "boolean",
currencyPosition: {
type: "string",
validValues: validCurrencyPosition
},
currencySymbol: "string",
totalLength: {
type: "number",
restrictions: [
Expand Down
9 changes: 9 additions & 0 deletions tests/src/numbro-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,5 +327,14 @@ describe("numbro", () => {
let result = numbro("1000").format({ mantissa: 2 });
expect(result).toBe("1000.00");
});

it("Issue 344", () => {
let result = numbro("1000").formatCurrency({
currencyPosition: "infix",
currencySymbol: "FOO",
mantissa: 2
});
expect(result).toBe("1000FOO00");
});
});
});

0 comments on commit 0d99ddd

Please sign in to comment.