intl-js-sdk-dev
Version:
This is I18n level 2 common library for javascript based clients like g11n-angular-client,g11n-js-client
95 lines (94 loc) • 4.41 kB
JavaScript
;
/*
* Copyright 2019 VMware, Inc.
* SPDX-License-Identifier: EPL-2.0
*/
Object.defineProperty(exports, "__esModule", { value: true });
const locale_de_1 = require("./locale_de");
const locale_en_1 = require("../src/data/locale_en");
const index_1 = require("../index");
describe('number formatter', () => {
index_1.I18n.registerLocaleData('en', locale_en_1.default.categories);
index_1.I18n.registerLocaleData('de', locale_de_1.default.categories);
const numberFormater = (value, type, locale) => {
if (type === 'currencies') {
const currencyCode = locale === 'en' ? 'USD' : 'EUR';
const option = { numberFormatType: type, currencyCode: currencyCode };
const numberFormat = index_1.I18n.NumberFormat.getInstance(locale, option);
return numberFormat.format(value);
}
else if (type === 'plural') {
const option = { numberFormatType: type, currencyCode: '' };
const numberFormat = index_1.I18n.NumberFormat.getInstance(locale, option);
return numberFormat.format(value);
}
else {
const option = { numberFormatType: type, currencyCode: '' };
const numberFormat = index_1.I18n.NumberFormat.getInstance(locale, option);
return numberFormat.format(value);
}
};
const pluralEN = index_1.I18n.PluralRules.getInstance('en');
const pluralDE = index_1.I18n.PluralRules.getInstance('de');
//test singleton mode.
const pluralDE1 = index_1.I18n.PluralRules.getInstance('de');
describe('decimal format', () => {
it('in English', () => {
expect(numberFormater(12345, 'decimal', 'en')).toEqual('12,345');
expect(numberFormater(123.7892, 'decimal', 'en')).toEqual('123.789');
expect(numberFormater(.23, 'decimal', 'en')).toEqual('0.23');
});
it('in German', () => {
expect(numberFormater(12345, 'decimal', 'de')).toEqual('12.345');
expect(numberFormater(123.7892, 'decimal', 'de')).toEqual('123,789');
expect(numberFormater(.23, 'decimal', 'de')).toEqual('0,23');
});
});
describe('percent format', () => {
it('in English', () => {
expect(numberFormater(0.123, 'percent', 'en')).toEqual('12%');
expect(numberFormater(1234, 'percent', 'en')).toEqual('123,400%');
expect(numberFormater(.23, 'percent', 'en')).toEqual('23%');
});
it('in German', () => {
expect(numberFormater(0.123, 'percent', 'de')).toEqual('12 %');
expect(numberFormater(1234, 'percent', 'de')).toEqual('123.400 %');
expect(numberFormater(.23, 'percent', 'de')).toEqual('23 %');
});
});
describe('plural format', () => {
it('in English', () => {
expect(numberFormater(0.123, 'plural', 'en')).toEqual('0.123');
expect(numberFormater(1234, 'plural', 'en')).toEqual('1234');
expect(numberFormater(.23, 'plural', 'en')).toEqual('0.23');
});
it('in German', () => {
expect(numberFormater(0.123, 'plural', 'de')).toEqual('0.123');
expect(numberFormater(1234, 'plural', 'de')).toEqual('1234');
expect(numberFormater(.23, 'plural', 'de')).toEqual('0.23');
});
});
describe('currency format', () => {
it('in English', () => {
expect(numberFormater(0.123, 'currencies', 'en')).toEqual('$0.12');
expect(numberFormater(1234.56, 'currencies', 'en')).toEqual('$1,234.56');
expect(numberFormater(.23, 'currencies', 'en')).toEqual('$0.23');
});
it('in German', () => {
expect(numberFormater(0.123, 'currencies', 'de')).toEqual('0,12 €');
expect(numberFormater(1234.56, 'currencies', 'de')).toEqual('1.234,56 €');
expect(numberFormater(.23, 'currencies', 'de')).toEqual('0,23 €');
});
});
describe('plural select', () => {
it('in English', () => {
expect(pluralEN.select(1)).toEqual('one');
expect(pluralEN.select(2)).toEqual('other');
});
it('in German', () => {
expect(pluralDE.select(1)).toEqual('one');
expect(pluralDE.select(2)).toEqual('other');
expect(pluralDE1.select(3)).toEqual('other');
});
});
});