-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
340 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const { formatCurrency } = require('../../util/currency-format') | ||
|
||
describe('formatCurrency', () => { | ||
test('no arguments passed - should throw error', () => { | ||
expect(() => formatCurrency()).toThrow() | ||
}) | ||
|
||
describe('currency as', () => { | ||
test('"NOK"', () => { | ||
const formatted = formatCurrency({ number: 69, options: { currency: 'NOK' } }) | ||
expect(formatted === 'NOK 69.00').toBeTruthy() | ||
}) | ||
|
||
test('"USD"', () => { | ||
const formatted = formatCurrency({ number: 69, options: { currency: 'USD' } }) | ||
expect(formatted === '$69.00').toBeTruthy() | ||
}) | ||
}) | ||
|
||
describe('minimumFractionDigits', () => { | ||
test('as 5', () => { | ||
const formatted = formatCurrency({ number: 69, options: { currency: 'NOK', minimumFractionDigits: 5 } }) | ||
expect(formatted === 'NOK 69.00000').toBeTruthy() | ||
}) | ||
|
||
test('as 0', () => { | ||
const formatted = formatCurrency({ number: 69, options: { currency: 'NOK', minimumFractionDigits: 0 } }) | ||
expect(formatted === 'NOK 69').toBeTruthy() | ||
}) | ||
}) | ||
|
||
describe('minimumSignificantDigits', () => { | ||
test('as 5', () => { | ||
const formatted = formatCurrency({ number: 69, options: { currency: 'NOK', minimumSignificantDigits: 5 } }) | ||
expect(formatted === 'NOK 69.000').toBeTruthy() | ||
}) | ||
|
||
test('as 1', () => { | ||
const formatted = formatCurrency({ number: 69, options: { currency: 'NOK', minimumSignificantDigits: 1 } }) | ||
expect(formatted === 'NOK 69').toBeTruthy() | ||
}) | ||
}) | ||
|
||
describe('minimumIntegerDigits', () => { | ||
test('as 5', () => { | ||
const formatted = formatCurrency({ number: 69, options: { currency: 'NOK', minimumIntegerDigits: 5 } }) | ||
expect(formatted === 'NOK 00,069.00').toBeTruthy() | ||
}) | ||
|
||
test('as 1', () => { | ||
const formatted = formatCurrency({ number: 69, options: { currency: 'NOK', minimumIntegerDigits: 1 } }) | ||
expect(formatted === 'NOK 69.00').toBeTruthy() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
export function formatCurrency({ number, locale, options }: FormatCurrencyOptions): string; | ||
export type FormatCurrencyOptions = { | ||
/** | ||
* Number to format | ||
*/ | ||
number: number; | ||
/** | ||
* Locale to format number into (Default = 'en') | ||
*/ | ||
locale?: string; | ||
/** | ||
* CurrencyFormat options | ||
*/ | ||
options?: CurrencyFormatOptions; | ||
}; | ||
/** | ||
* CurrencyFormat options | ||
*/ | ||
export type CurrencyFormatOptions = { | ||
/** | ||
* Only used when notation is "compact" (Default = "short") | ||
*/ | ||
compactDisplay?: "long" | "short"; | ||
/** | ||
* The currency to use in currency formatting. Possible values are the ISO 4217 currency codes, such as "USD" for the US dollar, "EUR" | ||
*/ | ||
currency: string; | ||
/** | ||
* In many locales, accounting format means to wrap the number with parentheses instead of appending a minus sign. You can enable this formatting by setting the currencySign option to "accounting". The default value is "standard" | ||
*/ | ||
currencySign?: "standard" | "accounting"; | ||
/** | ||
* The minimum number of fraction digits to use. Possible values are from 0 to 20 (Default = 0) | ||
*/ | ||
minimumFractionDigits?: number; | ||
/** | ||
* The maximum number of fraction digits to use. Possible values are from 0 to 20 (the default for plain number formatting is the larger of minimumFractionDigits and 3) | ||
*/ | ||
maximumFractionDigits?: number; | ||
/** | ||
* The minimum number of significant digits to use. Possible values are from 1 to 21 (Default = 1) | ||
*/ | ||
minimumSignificantDigits?: number; | ||
/** | ||
* The maximum number of significant digits to use. Possible values are from 1 to 21 (Default = 21) | ||
*/ | ||
maximumSignificantDigits?: number; | ||
/** | ||
* The minimum number of integer digits to use. Possible values are from 1 to 21 (Default = 1) | ||
*/ | ||
minimumIntegerDigits?: number; | ||
/** | ||
* The formatting that should be displayed for the number (Default = "standard") | ||
*/ | ||
notation: "compact" | "standard" | "scientific" | "engineering"; | ||
/** | ||
* When to display the sign for the number (Default = "auto") | ||
*/ | ||
signDisplay: "always" | "exceptZero" | "auto" | "never"; | ||
/** | ||
* The unit formatting style to use in unit formatting (Default = "short") | ||
*/ | ||
unitDisplay: "long" | "short" | "narrow"; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* @typedef {Object} FormatCurrencyOptions | ||
* @property {Number} number Number to format | ||
* @property {String} [locale] Locale to format number into (Default = 'en') | ||
* @property {CurrencyFormatOptions} [options] CurrencyFormat options | ||
*/ | ||
|
||
/** | ||
* CurrencyFormat options | ||
* @typedef {Object} CurrencyFormatOptions | ||
* @property {"long"|"short"} [compactDisplay] Only used when notation is "compact" (Default = "short") | ||
* @property {String} currency The currency to use in currency formatting. Possible values are the ISO 4217 currency codes, such as "USD" for the US dollar, "EUR" | ||
* @property {"standard"|"accounting"} [currencySign] In many locales, accounting format means to wrap the number with parentheses instead of appending a minus sign. You can enable this formatting by setting the currencySign option to "accounting". The default value is "standard" | ||
* @property {Number} [minimumFractionDigits] The minimum number of fraction digits to use. Possible values are from 0 to 20 (Default = 0) | ||
* @property {Number} [maximumFractionDigits] The maximum number of fraction digits to use. Possible values are from 0 to 20 (the default for plain number formatting is the larger of minimumFractionDigits and 3) | ||
* @property {Number} [minimumSignificantDigits] The minimum number of significant digits to use. Possible values are from 1 to 21 (Default = 1) | ||
* @property {Number} [maximumSignificantDigits] The maximum number of significant digits to use. Possible values are from 1 to 21 (Default = 21) | ||
* @property {Number} [minimumIntegerDigits] The minimum number of integer digits to use. Possible values are from 1 to 21 (Default = 1) | ||
* @property {"compact"|"standard"|"scientific"|"engineering"} notation The formatting that should be displayed for the number (Default = "standard") | ||
* @property {"always"|"exceptZero"|"auto"|"never"} signDisplay When to display the sign for the number (Default = "auto") | ||
* @property {"long"|"short"|"narrow"} unitDisplay The unit formatting style to use in unit formatting (Default = "short") | ||
*/ | ||
|
||
/** | ||
* Format currency | ||
* @param {String} locale Locale to format currency into | ||
* @param {CurrencyFormatOptions} options CurrencyFormat options | ||
* @returns {String} | ||
*/ | ||
const getCurrencyFormatter = (locale, options) => { | ||
return new Intl.NumberFormat(locale, { ...options, style: 'currency' }) | ||
} | ||
|
||
/** | ||
* Format number in currency | ||
* @param {FormatCurrencyOptions} options FormatCurrency options | ||
* @returns {String} The formatted string | ||
*/ | ||
module.exports.formatCurrency = ({ number, locale = 'en', options = undefined }) => { | ||
if (Number.isNaN(number)) throw new Error('Pass a number') | ||
|
||
const FORMATTER = getCurrencyFormatter(locale, options) | ||
return FORMATTER.format(number).replace('\xa0', ' ') // replace char code 160 ( ) with regular space | ||
} |