-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ONL-4443: fix: Fixed the way how decimal separator and group separato…
…r are detected (#156) * ONL-4443: fix: Fixed the way how decimal separator and group separator are detected. * ONL-4443: pr: Changed se to sv. * ONL-4443: pr: Updated README. * 0.1.87
- Loading branch information
Showing
9 changed files
with
111 additions
and
22 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -212,4 +212,3 @@ describe('EcAmountInput', () => { | |
expect(wrapper.vm.valueAmount).toBe(null); | ||
}); | ||
}); | ||
|
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,46 @@ | ||
export function getDecimalSeparator(locale) { | ||
// we could just use formatToParts function but it's not supported on IE11 | ||
// and there's no polyfill for NumberFormat, only for DateTimeFormat | ||
// | ||
// return new Intl.NumberFormat(locale) | ||
// .formatToParts(1111.1) | ||
// .find(part => part.type === 'decimal') | ||
// .value; | ||
// | ||
// so we have to improvised and force the formatter to format number 0.1 in the format | ||
// 0<decimal separator>1 and then get the 2nd character from the string | ||
|
||
const formatted = new Intl.NumberFormat(locale, { | ||
useGrouping: false, | ||
minimumIntegerDigits: 1, | ||
minimumFractionDigits: 1, | ||
maximumFractionDigits: 1, | ||
minimumSignificantDigits: 1, | ||
maximumSignificantDigits: 1, | ||
}).format(0.1); | ||
return formatted[1]; | ||
} | ||
|
||
export function getGroupingSeparator(locale) { | ||
// we could just use formatToParts function but it's not supported on IE11 | ||
// and there's no polyfill for NumberFormat, only for DateTimeFormat | ||
// | ||
// return new Intl.NumberFormat(locale) | ||
// .formatToParts(1111.1) | ||
// .find(part => part.type === 'group') | ||
// .value; | ||
// | ||
// so we have to improvised and force the formatter to format number 1000 in the format | ||
// 1<group separator>000 and then get the 2nd character from the string | ||
|
||
const formatted = new Intl.NumberFormat(locale, { | ||
useGrouping: true, | ||
minimumIntegerDigits: 5, | ||
minimumFractionDigits: 0, | ||
maximumFractionDigits: 0, | ||
minimumSignificantDigits: 5, | ||
maximumSignificantDigits: 5, | ||
}).format(10000); | ||
return formatted[2]; | ||
} | ||
|
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,38 @@ | ||
import fs from 'fs'; | ||
import { getDecimalSeparator, getGroupingSeparator } from './number-format'; | ||
|
||
describe('Utils', () => { | ||
// get list of all locales in the world from intl polyfill. | ||
const locales = fs.readdirSync('./node_modules/intl/locale-data/jsonp') | ||
.filter(fileName => fileName.match(/\.js$/)) | ||
.map(fileName => fileName.replace(/\.js$/, '')); | ||
|
||
function getExpectedSeparator(locale, type) { | ||
return new Intl.NumberFormat(locale) | ||
.formatToParts(1111.1) | ||
.find(part => part.type === type) | ||
.value; | ||
} | ||
|
||
describe('getDecimalSeparator ', () => { | ||
for (const locale of locales) { | ||
it(`should get decimal separator for locale ${locale}`, () => { | ||
const expected = getExpectedSeparator(locale, 'decimal'); // get the decimal separator using method that works in every modern browser | ||
const separator = getDecimalSeparator(locale); // get the decimal separator using our method | ||
expect(separator.length).toBe(1); | ||
expect(separator).toBe(expected); | ||
}); | ||
} | ||
}); | ||
|
||
describe('getGroupingSeparator', () => { | ||
for (const locale of locales) { | ||
it(`should get group separator for locale ${locale}`, () => { | ||
const expected = getExpectedSeparator(locale, 'group'); // get the group separator using method that works in every modern browser | ||
const separator = getGroupingSeparator(locale); // get the group separator using our method | ||
expect(separator.length).toBe(1); | ||
expect(separator).toBe(expected); | ||
}); | ||
} | ||
}); | ||
}); |
ea3cc65
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to following URLs: