Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Commit

Permalink
Add omitZeroDecimals option. Fixes #31
Browse files Browse the repository at this point in the history
  • Loading branch information
edorivai committed Apr 30, 2017
1 parent b8cc4f3 commit 9992fdb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
16 changes: 12 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// TODO: #31
// TODO: Dynamic loading of currencies

const formatMapping = {
Expand Down Expand Up @@ -42,8 +41,13 @@ export function setLocale(locale) {
defaultLocale = locale
}

let defaultOverrides = {}
export function setOverrides(overrides) {
defaultOverrides = overrides;
}

export function format(value, ...options) {
const defaultOption = Object.assign({}, defaultCurrency, defaultLocale)
const defaultOption = Object.assign({}, defaultCurrency, defaultLocale, defaultOverrides)
const formatOption = Object.assign(defaultOption, ...options)
const spaceBetweenAmountAndSymbol = formatOption.spaceBetweenAmountAndSymbol
? 'spaceBetweenAmountAndSymbol'
Expand All @@ -52,10 +56,14 @@ export function format(value, ...options) {
const symbolOnLeft = formatOption.symbolOnLeft
? 'symbolOnLeft'
: 'symbolOnRight'

const decimalDigits = (formatOption.omitZeroDecimals && (value % 1 === 0))
? 0
: formatOption.decimalDigits

const formattedValue = new Intl.NumberFormat(formatOption.localeCode, {
minimumFractionDigits: formatOption.decimalDigits,
maximumFractionDigits: formatOption.decimalDigits
minimumFractionDigits: decimalDigits,
maximumFractionDigits: decimalDigits
}).format(Math.abs(value))

const formatPatternMapping = formatMapping[symbolOnLeft][spaceBetweenAmountAndSymbol]
Expand Down
40 changes: 36 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import nl_NL from '../locales/nl-NL'
// Polyfill the Intl object, because Node only contains English locale data
Intl.NumberFormat = IntlPolyfill.NumberFormat;

import { format, setCurrency, setLocale } from '../src'
import { format, setCurrency, setLocale, setOverrides } from '../src'

describe('format when', () => {
context('no default is set', () => {
Expand Down Expand Up @@ -97,7 +97,7 @@ describe('format when', () => {
})
})

// Symbol righr - no space
// Symbol right - no space
context('for ERN', () => {
it('should return -1.00Nfk for -1', () => {
const result = format(-1, ERN)
Expand Down Expand Up @@ -137,8 +137,8 @@ describe('format when', () => {

// Symbol right - with space
context('for EUR', () => {
before(() => setLocale({ localeCode: 'de-DE' }))
after(() => setLocale({}))
before(() => setLocale({ localeCode: 'de-DE' }))
after(() => setLocale({}))

it('should return -1,00 € for -1', () => {
const result = format(-1, EUR)
Expand Down Expand Up @@ -225,6 +225,19 @@ describe('format when', () => {

result.should.equal('1,000,000.0000 @')
})

it("should allow globally setting default overrides", () => {
setCurrency(USD);
setOverrides({
symbol: '@'
});
const result = format(1000000)
result.should.equal('@1,000,000.00')

// cleanup
setCurrency({});
setOverrides({})
});

it("should allow empty symbol", () => {
const result = format(1000000, USD, {
Expand All @@ -249,5 +262,24 @@ describe('format when', () => {

result.should.equal('$1,000,000')
})

context("omitZeroDecimals", () => {
it("should render $123 for 123", () => {
const result = format(123, USD, { omitZeroDecimals: true });
result.should.equal("$123");
})

it("should render -$123 for -123", () => {
const result = format(-123, USD, { omitZeroDecimals: true });
result.should.equal("-$123");
})

it("should render $123.45 for 123.45", () => {
const result = format(123.45, USD, { omitZeroDecimals: true });
result.should.equal("$123.45");
})

})

})
})

0 comments on commit 9992fdb

Please sign in to comment.