Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
feat(formats): allow fractions in percentage formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
skolmer committed Apr 12, 2019
1 parent b0131d8 commit 8b5b47f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ The following standard format strings can be applied to a template expression of
console.log(i18n`Hello ${name}, the percentage is ${0.01}:p.`)
// Hello Steffen, the percentage is 1%.

console.log(i18n`Hello ${name}, the percentage is ${0.005}:p(1).`)
// Hello Steffen, the percentage is 0.5%.

i18nConfig({
locales: 'de-DE'
})
Expand Down
2 changes: 2 additions & 0 deletions __tests__/__snapshots__/es2015-i18n-tag.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ exports[`es2015-i18n-tag should format fractionals 1`] = `"Hello Steffen, the nu

exports[`es2015-i18n-tag should format percentage 1`] = `"Hello Steffen, the percentage is 10%."`;

exports[`es2015-i18n-tag should format percentage with fractions 1`] = `"Hello Steffen, the percentage is 0.1%."`;

exports[`es2015-i18n-tag should ignore missing standard formatters 1`] = `"The date is 20.12.2012, 18:00:00 test123."`;

exports[`es2015-i18n-tag should ignore unknown custom number formatters 1`] = `"Hello Steffen, the number is 0.137."`;
Expand Down
12 changes: 12 additions & 0 deletions __tests__/es2015-i18n-tag.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,18 @@ describe('es2015-i18n-tag', () => {
expect(actual).toMatchSnapshot()
})

it('should format percentage with fractions', () => {
const name = 'Steffen'
const percentage = 0.001

i18nConfig({
locales: 'en-US'
})

const actual = i18n`Hello ${name}, the percentage is ${percentage}:p(1).`
expect(actual).toMatchSnapshot()
})

it('should format currency', () => {
const name = 'Steffen'
const amount = 0.1
Expand Down
7 changes: 5 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,14 @@ class Tag {
Object.assign({}, config.number, { style: numberStyleCurrency })
)
},
p /*percent*/: (config, v) => {
p /*percent*/: (config, v, minimumFractionDigits) => {
if((typeof v) !== 'number') {
throw Error(`value is not a number. type: ${typeof v}`)
}
return v.toLocaleString(config.locales, Object.assign({}, config.number, { style: numberStylePercent }))
return v.toLocaleString(config.locales, (minimumFractionDigits) ?
Object.assign({}, config.number, { style: numberStylePercent, minimumFractionDigits }) :
Object.assign({}, config.number, { style: numberStylePercent })
)
}
}
this.i18n = this.i18n.bind(this)
Expand Down

0 comments on commit 8b5b47f

Please sign in to comment.