refactor(common): use private names rather than aliases internally (#33523)

PR Close #33523
This commit is contained in:
Pete Bacon Darwin 2019-11-04 15:06:30 +00:00 committed by atscott
parent cd6ab209b6
commit 5b21b71c9a
4 changed files with 106 additions and 104 deletions

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {ɵLocaleDataIndex as LocaleDataIndex, ɵfindLocaleData as findLocaleData, ɵgetLocalePluralCase} from '@angular/core'; import {ɵLocaleDataIndex, ɵfindLocaleData, ɵgetLocalePluralCase} from '@angular/core';
import {CURRENCIES_EN, CurrenciesSymbols} from './currencies'; import {CURRENCIES_EN, CurrenciesSymbols} from './currencies';
import {CurrencyIndex, ExtraLocaleDataIndex} from './locale_data'; import {CurrencyIndex, ExtraLocaleDataIndex} from './locale_data';
@ -217,7 +217,7 @@ export enum WeekDay {
* @publicApi * @publicApi
*/ */
export function getLocaleId(locale: string): string { export function getLocaleId(locale: string): string {
return findLocaleData(locale)[LocaleDataIndex.LocaleId]; return ɵfindLocaleData(locale)[ɵLocaleDataIndex.LocaleId];
} }
/** /**
@ -233,10 +233,10 @@ export function getLocaleId(locale: string): string {
*/ */
export function getLocaleDayPeriods( export function getLocaleDayPeriods(
locale: string, formStyle: FormStyle, width: TranslationWidth): [string, string] { locale: string, formStyle: FormStyle, width: TranslationWidth): [string, string] {
const data = findLocaleData(locale); const data = ɵfindLocaleData(locale);
const amPmData = <[ const amPmData = <[
string, string string, string
][][]>[data[LocaleDataIndex.DayPeriodsFormat], data[LocaleDataIndex.DayPeriodsStandalone]]; ][][]>[data[ɵLocaleDataIndex.DayPeriodsFormat], data[ɵLocaleDataIndex.DayPeriodsStandalone]];
const amPm = getLastDefinedValue(amPmData, formStyle); const amPm = getLastDefinedValue(amPmData, formStyle);
return getLastDefinedValue(amPm, width); return getLastDefinedValue(amPm, width);
} }
@ -255,9 +255,9 @@ export function getLocaleDayPeriods(
*/ */
export function getLocaleDayNames( export function getLocaleDayNames(
locale: string, formStyle: FormStyle, width: TranslationWidth): string[] { locale: string, formStyle: FormStyle, width: TranslationWidth): string[] {
const data = findLocaleData(locale); const data = ɵfindLocaleData(locale);
const daysData = const daysData =
<string[][][]>[data[LocaleDataIndex.DaysFormat], data[LocaleDataIndex.DaysStandalone]]; <string[][][]>[data[ɵLocaleDataIndex.DaysFormat], data[ɵLocaleDataIndex.DaysStandalone]];
const days = getLastDefinedValue(daysData, formStyle); const days = getLastDefinedValue(daysData, formStyle);
return getLastDefinedValue(days, width); return getLastDefinedValue(days, width);
} }
@ -276,9 +276,9 @@ export function getLocaleDayNames(
*/ */
export function getLocaleMonthNames( export function getLocaleMonthNames(
locale: string, formStyle: FormStyle, width: TranslationWidth): string[] { locale: string, formStyle: FormStyle, width: TranslationWidth): string[] {
const data = findLocaleData(locale); const data = ɵfindLocaleData(locale);
const monthsData = const monthsData =
<string[][][]>[data[LocaleDataIndex.MonthsFormat], data[LocaleDataIndex.MonthsStandalone]]; <string[][][]>[data[ɵLocaleDataIndex.MonthsFormat], data[ɵLocaleDataIndex.MonthsStandalone]];
const months = getLastDefinedValue(monthsData, formStyle); const months = getLastDefinedValue(monthsData, formStyle);
return getLastDefinedValue(months, width); return getLastDefinedValue(months, width);
} }
@ -296,8 +296,8 @@ export function getLocaleMonthNames(
* @publicApi * @publicApi
*/ */
export function getLocaleEraNames(locale: string, width: TranslationWidth): [string, string] { export function getLocaleEraNames(locale: string, width: TranslationWidth): [string, string] {
const data = findLocaleData(locale); const data = ɵfindLocaleData(locale);
const erasData = <[string, string][]>data[LocaleDataIndex.Eras]; const erasData = <[string, string][]>data[ɵLocaleDataIndex.Eras];
return getLastDefinedValue(erasData, width); return getLastDefinedValue(erasData, width);
} }
@ -313,8 +313,8 @@ export function getLocaleEraNames(locale: string, width: TranslationWidth): [str
* @publicApi * @publicApi
*/ */
export function getLocaleFirstDayOfWeek(locale: string): WeekDay { export function getLocaleFirstDayOfWeek(locale: string): WeekDay {
const data = findLocaleData(locale); const data = ɵfindLocaleData(locale);
return data[LocaleDataIndex.FirstDayOfWeek]; return data[ɵLocaleDataIndex.FirstDayOfWeek];
} }
/** /**
@ -327,8 +327,8 @@ export function getLocaleFirstDayOfWeek(locale: string): WeekDay {
* @publicApi * @publicApi
*/ */
export function getLocaleWeekEndRange(locale: string): [WeekDay, WeekDay] { export function getLocaleWeekEndRange(locale: string): [WeekDay, WeekDay] {
const data = findLocaleData(locale); const data = ɵfindLocaleData(locale);
return data[LocaleDataIndex.WeekendRange]; return data[ɵLocaleDataIndex.WeekendRange];
} }
/** /**
@ -343,8 +343,8 @@ export function getLocaleWeekEndRange(locale: string): [WeekDay, WeekDay] {
* @publicApi * @publicApi
*/ */
export function getLocaleDateFormat(locale: string, width: FormatWidth): string { export function getLocaleDateFormat(locale: string, width: FormatWidth): string {
const data = findLocaleData(locale); const data = ɵfindLocaleData(locale);
return getLastDefinedValue(data[LocaleDataIndex.DateFormat], width); return getLastDefinedValue(data[ɵLocaleDataIndex.DateFormat], width);
} }
/** /**
@ -359,8 +359,8 @@ export function getLocaleDateFormat(locale: string, width: FormatWidth): string
* @publicApi * @publicApi
*/ */
export function getLocaleTimeFormat(locale: string, width: FormatWidth): string { export function getLocaleTimeFormat(locale: string, width: FormatWidth): string {
const data = findLocaleData(locale); const data = ɵfindLocaleData(locale);
return getLastDefinedValue(data[LocaleDataIndex.TimeFormat], width); return getLastDefinedValue(data[ɵLocaleDataIndex.TimeFormat], width);
} }
/** /**
@ -375,8 +375,8 @@ export function getLocaleTimeFormat(locale: string, width: FormatWidth): string
* @publicApi * @publicApi
*/ */
export function getLocaleDateTimeFormat(locale: string, width: FormatWidth): string { export function getLocaleDateTimeFormat(locale: string, width: FormatWidth): string {
const data = findLocaleData(locale); const data = ɵfindLocaleData(locale);
const dateTimeFormatData = <string[]>data[LocaleDataIndex.DateTimeFormat]; const dateTimeFormatData = <string[]>data[ɵLocaleDataIndex.DateTimeFormat];
return getLastDefinedValue(dateTimeFormatData, width); return getLastDefinedValue(dateTimeFormatData, width);
} }
@ -391,13 +391,13 @@ export function getLocaleDateTimeFormat(locale: string, width: FormatWidth): str
* @publicApi * @publicApi
*/ */
export function getLocaleNumberSymbol(locale: string, symbol: NumberSymbol): string { export function getLocaleNumberSymbol(locale: string, symbol: NumberSymbol): string {
const data = findLocaleData(locale); const data = ɵfindLocaleData(locale);
const res = data[LocaleDataIndex.NumberSymbols][symbol]; const res = data[ɵLocaleDataIndex.NumberSymbols][symbol];
if (typeof res === 'undefined') { if (typeof res === 'undefined') {
if (symbol === NumberSymbol.CurrencyDecimal) { if (symbol === NumberSymbol.CurrencyDecimal) {
return data[LocaleDataIndex.NumberSymbols][NumberSymbol.Decimal]; return data[ɵLocaleDataIndex.NumberSymbols][NumberSymbol.Decimal];
} else if (symbol === NumberSymbol.CurrencyGroup) { } else if (symbol === NumberSymbol.CurrencyGroup) {
return data[LocaleDataIndex.NumberSymbols][NumberSymbol.Group]; return data[ɵLocaleDataIndex.NumberSymbols][NumberSymbol.Group];
} }
} }
return res; return res;
@ -439,8 +439,8 @@ export function getLocaleNumberSymbol(locale: string, symbol: NumberSymbol): str
* @publicApi * @publicApi
*/ */
export function getLocaleNumberFormat(locale: string, type: NumberFormatStyle): string { export function getLocaleNumberFormat(locale: string, type: NumberFormatStyle): string {
const data = findLocaleData(locale); const data = ɵfindLocaleData(locale);
return data[LocaleDataIndex.NumberFormats][type]; return data[ɵLocaleDataIndex.NumberFormats][type];
} }
/** /**
@ -455,8 +455,8 @@ export function getLocaleNumberFormat(locale: string, type: NumberFormatStyle):
* @publicApi * @publicApi
*/ */
export function getLocaleCurrencySymbol(locale: string): string|null { export function getLocaleCurrencySymbol(locale: string): string|null {
const data = findLocaleData(locale); const data = ɵfindLocaleData(locale);
return data[LocaleDataIndex.CurrencySymbol] || null; return data[ɵLocaleDataIndex.CurrencySymbol] || null;
} }
/** /**
@ -470,8 +470,8 @@ export function getLocaleCurrencySymbol(locale: string): string|null {
* @publicApi * @publicApi
*/ */
export function getLocaleCurrencyName(locale: string): string|null { export function getLocaleCurrencyName(locale: string): string|null {
const data = findLocaleData(locale); const data = ɵfindLocaleData(locale);
return data[LocaleDataIndex.CurrencyName] || null; return data[ɵLocaleDataIndex.CurrencyName] || null;
} }
/** /**
@ -481,8 +481,8 @@ export function getLocaleCurrencyName(locale: string): string|null {
* @see [Internationalization (i18n) Guide](https://angular.io/guide/i18n) * @see [Internationalization (i18n) Guide](https://angular.io/guide/i18n)
*/ */
function getLocaleCurrencies(locale: string): {[code: string]: CurrenciesSymbols} { function getLocaleCurrencies(locale: string): {[code: string]: CurrenciesSymbols} {
const data = findLocaleData(locale); const data = ɵfindLocaleData(locale);
return data[LocaleDataIndex.Currencies]; return data[ɵLocaleDataIndex.Currencies];
} }
/** /**
@ -493,9 +493,9 @@ export const getLocalePluralCase: (locale: string) => ((value: number) => Plural
ɵgetLocalePluralCase; ɵgetLocalePluralCase;
function checkFullData(data: any) { function checkFullData(data: any) {
if (!data[LocaleDataIndex.ExtraData]) { if (!data[ɵLocaleDataIndex.ExtraData]) {
throw new Error( throw new Error(
`Missing extra locale data for the locale "${data[LocaleDataIndex.LocaleId]}". Use "registerLocaleData" to load new data. See the "I18n guide" on angular.io to know more.`); `Missing extra locale data for the locale "${data[ɵLocaleDataIndex.LocaleId]}". Use "registerLocaleData" to load new data. See the "I18n guide" on angular.io to know more.`);
} }
} }
@ -522,9 +522,9 @@ function checkFullData(data: any) {
* @publicApi * @publicApi
*/ */
export function getLocaleExtraDayPeriodRules(locale: string): (Time | [Time, Time])[] { export function getLocaleExtraDayPeriodRules(locale: string): (Time | [Time, Time])[] {
const data = findLocaleData(locale); const data = ɵfindLocaleData(locale);
checkFullData(data); checkFullData(data);
const rules = data[LocaleDataIndex.ExtraData][ExtraLocaleDataIndex.ExtraDayPeriodsRules] || []; const rules = data[ɵLocaleDataIndex.ExtraData][ExtraLocaleDataIndex.ExtraDayPeriodsRules] || [];
return rules.map((rule: string | [string, string]) => { return rules.map((rule: string | [string, string]) => {
if (typeof rule === 'string') { if (typeof rule === 'string') {
return extractTime(rule); return extractTime(rule);
@ -552,11 +552,11 @@ export function getLocaleExtraDayPeriodRules(locale: string): (Time | [Time, Tim
*/ */
export function getLocaleExtraDayPeriods( export function getLocaleExtraDayPeriods(
locale: string, formStyle: FormStyle, width: TranslationWidth): string[] { locale: string, formStyle: FormStyle, width: TranslationWidth): string[] {
const data = findLocaleData(locale); const data = ɵfindLocaleData(locale);
checkFullData(data); checkFullData(data);
const dayPeriodsData = <string[][][]>[ const dayPeriodsData = <string[][][]>[
data[LocaleDataIndex.ExtraData][ExtraLocaleDataIndex.ExtraDayPeriodFormats], data[ɵLocaleDataIndex.ExtraData][ExtraLocaleDataIndex.ExtraDayPeriodFormats],
data[LocaleDataIndex.ExtraData][ExtraLocaleDataIndex.ExtraDayPeriodStandalone] data[ɵLocaleDataIndex.ExtraData][ExtraLocaleDataIndex.ExtraDayPeriodStandalone]
]; ];
const dayPeriods = getLastDefinedValue(dayPeriodsData, formStyle) || []; const dayPeriods = getLastDefinedValue(dayPeriodsData, formStyle) || [];
return getLastDefinedValue(dayPeriods, width) || []; return getLastDefinedValue(dayPeriods, width) || [];

View File

@ -15,7 +15,7 @@ import localeHu from '@angular/common/locales/hu';
import localeSr from '@angular/common/locales/sr'; import localeSr from '@angular/common/locales/sr';
import localeTh from '@angular/common/locales/th'; import localeTh from '@angular/common/locales/th';
import {isDate, toDate, formatDate} from '@angular/common/src/i18n/format_date'; import {isDate, toDate, formatDate} from '@angular/common/src/i18n/format_date';
import {ɵDEFAULT_LOCALE_ID as DEFAULT_LOCALE_ID} from '@angular/core'; import {ɵDEFAULT_LOCALE_ID} from '@angular/core';
describe('Format date', () => { describe('Format date', () => {
describe('toDate', () => { describe('toDate', () => {
@ -52,7 +52,8 @@ describe('Format date', () => {
// Check the transformation of a date into a pattern // Check the transformation of a date into a pattern
function expectDateFormatAs(date: Date | string, pattern: any, output: string): void { function expectDateFormatAs(date: Date | string, pattern: any, output: string): void {
expect(formatDate(date, pattern, DEFAULT_LOCALE_ID)).toEqual(output, `pattern: "${pattern}"`); expect(formatDate(date, pattern, ɵDEFAULT_LOCALE_ID))
.toEqual(output, `pattern: "${pattern}"`);
} }
beforeAll(() => { beforeAll(() => {
@ -209,7 +210,7 @@ describe('Format date', () => {
}; };
Object.keys(dateFixtures).forEach((pattern: string) => { Object.keys(dateFixtures).forEach((pattern: string) => {
expect(formatDate(date, pattern, DEFAULT_LOCALE_ID, '+0430')) expect(formatDate(date, pattern, ɵDEFAULT_LOCALE_ID, '+0430'))
.toMatch(dateFixtures[pattern]); .toMatch(dateFixtures[pattern]);
}); });
}); });
@ -254,22 +255,22 @@ describe('Format date', () => {
}; };
Object.keys(dateFixtures).forEach((pattern: string) => { Object.keys(dateFixtures).forEach((pattern: string) => {
expect(formatDate(date, pattern, DEFAULT_LOCALE_ID)).toMatch(dateFixtures[pattern]); expect(formatDate(date, pattern, ɵDEFAULT_LOCALE_ID)).toMatch(dateFixtures[pattern]);
}); });
}); });
it('should format invalid in IE ISO date', it('should format invalid in IE ISO date',
() => expect(formatDate('2017-01-11T12:00:00.014-0500', defaultFormat, DEFAULT_LOCALE_ID)) () => expect(formatDate('2017-01-11T12:00:00.014-0500', defaultFormat, ɵDEFAULT_LOCALE_ID))
.toEqual('Jan 11, 2017')); .toEqual('Jan 11, 2017'));
it('should format invalid in Safari ISO date', it('should format invalid in Safari ISO date',
() => expect(formatDate('2017-01-20T12:00:00+0000', defaultFormat, DEFAULT_LOCALE_ID)) () => expect(formatDate('2017-01-20T12:00:00+0000', defaultFormat, ɵDEFAULT_LOCALE_ID))
.toEqual('Jan 20, 2017')); .toEqual('Jan 20, 2017'));
// https://github.com/angular/angular/issues/9524 // https://github.com/angular/angular/issues/9524
// https://github.com/angular/angular/issues/9524 // https://github.com/angular/angular/issues/9524
it('should format correctly with iso strings that contain time', it('should format correctly with iso strings that contain time',
() => expect(formatDate('2017-05-07T22:14:39', 'dd-MM-yyyy HH:mm', DEFAULT_LOCALE_ID)) () => expect(formatDate('2017-05-07T22:14:39', 'dd-MM-yyyy HH:mm', ɵDEFAULT_LOCALE_ID))
.toMatch(/07-05-2017 \d{2}:\d{2}/)); .toMatch(/07-05-2017 \d{2}:\d{2}/));
// https://github.com/angular/angular/issues/21491 // https://github.com/angular/angular/issues/21491
@ -277,22 +278,22 @@ describe('Format date', () => {
// this test only works if the timezone is not in UTC // this test only works if the timezone is not in UTC
// which is the case for BrowserStack when we test Safari // which is the case for BrowserStack when we test Safari
if (new Date().getTimezoneOffset() !== 0) { if (new Date().getTimezoneOffset() !== 0) {
expect(formatDate('2018-01-11T13:00:00', 'HH', DEFAULT_LOCALE_ID)) expect(formatDate('2018-01-11T13:00:00', 'HH', ɵDEFAULT_LOCALE_ID))
.not.toEqual(formatDate('2018-01-11T13:00:00Z', 'HH', DEFAULT_LOCALE_ID)); .not.toEqual(formatDate('2018-01-11T13:00:00Z', 'HH', ɵDEFAULT_LOCALE_ID));
} }
}); });
// https://github.com/angular/angular/issues/16624 // https://github.com/angular/angular/issues/16624
// https://github.com/angular/angular/issues/17478 // https://github.com/angular/angular/issues/17478
it('should show the correct time when the timezone is fixed', () => { it('should show the correct time when the timezone is fixed', () => {
expect(formatDate('2017-06-13T10:14:39+0000', 'shortTime', DEFAULT_LOCALE_ID, '+0000')) expect(formatDate('2017-06-13T10:14:39+0000', 'shortTime', ɵDEFAULT_LOCALE_ID, '+0000'))
.toEqual('10:14 AM'); .toEqual('10:14 AM');
expect(formatDate('2017-06-13T10:14:39+0000', 'h:mm a', DEFAULT_LOCALE_ID, '+0000')) expect(formatDate('2017-06-13T10:14:39+0000', 'h:mm a', ɵDEFAULT_LOCALE_ID, '+0000'))
.toEqual('10:14 AM'); .toEqual('10:14 AM');
}); });
it('should remove bidi control characters', it('should remove bidi control characters',
() => expect(formatDate(date, 'MM/dd/yyyy', DEFAULT_LOCALE_ID) !.length).toEqual(10)); () => expect(formatDate(date, 'MM/dd/yyyy', ɵDEFAULT_LOCALE_ID) !.length).toEqual(10));
it(`should format the date correctly in various locales`, () => { it(`should format the date correctly in various locales`, () => {
expect(formatDate(date, 'short', 'de')).toEqual('15.06.15, 09:03'); expect(formatDate(date, 'short', 'de')).toEqual('15.06.15, 09:03');

View File

@ -12,7 +12,7 @@ import localeFr from '@angular/common/locales/fr';
import localeAr from '@angular/common/locales/ar'; import localeAr from '@angular/common/locales/ar';
import {formatCurrency, formatNumber, formatPercent, registerLocaleData} from '@angular/common'; import {formatCurrency, formatNumber, formatPercent, registerLocaleData} from '@angular/common';
import {describe, expect, it} from '@angular/core/testing/src/testing_internal'; import {describe, expect, it} from '@angular/core/testing/src/testing_internal';
import {ɵDEFAULT_LOCALE_ID as DEFAULT_LOCALE_ID} from '@angular/core'; import {ɵDEFAULT_LOCALE_ID} from '@angular/core';
describe('Format number', () => { describe('Format number', () => {
beforeAll(() => { beforeAll(() => {
@ -25,18 +25,18 @@ describe('Format number', () => {
describe('Number', () => { describe('Number', () => {
describe('transform', () => { describe('transform', () => {
it('should return correct value for numbers', () => { it('should return correct value for numbers', () => {
expect(formatNumber(12345, DEFAULT_LOCALE_ID)).toEqual('12,345'); expect(formatNumber(12345, ɵDEFAULT_LOCALE_ID)).toEqual('12,345');
expect(formatNumber(123, DEFAULT_LOCALE_ID, '.2')).toEqual('123.00'); expect(formatNumber(123, ɵDEFAULT_LOCALE_ID, '.2')).toEqual('123.00');
expect(formatNumber(1, DEFAULT_LOCALE_ID, '3.')).toEqual('001'); expect(formatNumber(1, ɵDEFAULT_LOCALE_ID, '3.')).toEqual('001');
expect(formatNumber(1.1, DEFAULT_LOCALE_ID, '3.4-5')).toEqual('001.1000'); expect(formatNumber(1.1, ɵDEFAULT_LOCALE_ID, '3.4-5')).toEqual('001.1000');
expect(formatNumber(1.123456, DEFAULT_LOCALE_ID, '3.4-5')).toEqual('001.12346'); expect(formatNumber(1.123456, ɵDEFAULT_LOCALE_ID, '3.4-5')).toEqual('001.12346');
expect(formatNumber(1.1234, DEFAULT_LOCALE_ID)).toEqual('1.123'); expect(formatNumber(1.1234, ɵDEFAULT_LOCALE_ID)).toEqual('1.123');
expect(formatNumber(1.123456, DEFAULT_LOCALE_ID, '.2')).toEqual('1.123'); expect(formatNumber(1.123456, ɵDEFAULT_LOCALE_ID, '.2')).toEqual('1.123');
expect(formatNumber(1.123456, DEFAULT_LOCALE_ID, '.4')).toEqual('1.1235'); expect(formatNumber(1.123456, ɵDEFAULT_LOCALE_ID, '.4')).toEqual('1.1235');
}); });
it('should throw if minFractionDigits is explicitly higher than maxFractionDigits', () => { it('should throw if minFractionDigits is explicitly higher than maxFractionDigits', () => {
expect(() => formatNumber(1.1, DEFAULT_LOCALE_ID, '3.4-2')) expect(() => formatNumber(1.1, ɵDEFAULT_LOCALE_ID, '3.4-2'))
.toThrowError(/is higher than the maximum/); .toThrowError(/is higher than the maximum/);
}); });
}); });
@ -50,27 +50,27 @@ describe('Format number', () => {
describe('Percent', () => { describe('Percent', () => {
describe('transform', () => { describe('transform', () => {
it('should return correct value for numbers', () => { it('should return correct value for numbers', () => {
expect(formatPercent(1.23, DEFAULT_LOCALE_ID)).toEqual('123%'); expect(formatPercent(1.23, ɵDEFAULT_LOCALE_ID)).toEqual('123%');
expect(formatPercent(1.2, DEFAULT_LOCALE_ID, '.2')).toEqual('120.00%'); expect(formatPercent(1.2, ɵDEFAULT_LOCALE_ID, '.2')).toEqual('120.00%');
expect(formatPercent(1.2, DEFAULT_LOCALE_ID, '4.2')).toEqual('0,120.00%'); expect(formatPercent(1.2, ɵDEFAULT_LOCALE_ID, '4.2')).toEqual('0,120.00%');
expect(formatPercent(1.2, 'fr', '4.2')).toEqual('0 120,00 %'); expect(formatPercent(1.2, 'fr', '4.2')).toEqual('0 120,00 %');
expect(formatPercent(1.2, 'ar', '4.2')).toEqual('0,120.00%'); expect(formatPercent(1.2, 'ar', '4.2')).toEqual('0,120.00%');
// see issue #20136 // see issue #20136
expect(formatPercent(0.12345674, DEFAULT_LOCALE_ID, '0.0-10')).toEqual('12.345674%'); expect(formatPercent(0.12345674, ɵDEFAULT_LOCALE_ID, '0.0-10')).toEqual('12.345674%');
expect(formatPercent(0, DEFAULT_LOCALE_ID, '0.0-10')).toEqual('0%'); expect(formatPercent(0, ɵDEFAULT_LOCALE_ID, '0.0-10')).toEqual('0%');
expect(formatPercent(0.00, DEFAULT_LOCALE_ID, '0.0-10')).toEqual('0%'); expect(formatPercent(0.00, ɵDEFAULT_LOCALE_ID, '0.0-10')).toEqual('0%');
expect(formatPercent(1, DEFAULT_LOCALE_ID, '0.0-10')).toEqual('100%'); expect(formatPercent(1, ɵDEFAULT_LOCALE_ID, '0.0-10')).toEqual('100%');
expect(formatPercent(0.1, DEFAULT_LOCALE_ID, '0.0-10')).toEqual('10%'); expect(formatPercent(0.1, ɵDEFAULT_LOCALE_ID, '0.0-10')).toEqual('10%');
expect(formatPercent(0.12, DEFAULT_LOCALE_ID, '0.0-10')).toEqual('12%'); expect(formatPercent(0.12, ɵDEFAULT_LOCALE_ID, '0.0-10')).toEqual('12%');
expect(formatPercent(0.123, DEFAULT_LOCALE_ID, '0.0-10')).toEqual('12.3%'); expect(formatPercent(0.123, ɵDEFAULT_LOCALE_ID, '0.0-10')).toEqual('12.3%');
expect(formatPercent(12.3456, DEFAULT_LOCALE_ID, '0.0-10')).toEqual('1,234.56%'); expect(formatPercent(12.3456, ɵDEFAULT_LOCALE_ID, '0.0-10')).toEqual('1,234.56%');
expect(formatPercent(12.345600, DEFAULT_LOCALE_ID, '0.0-10')).toEqual('1,234.56%'); expect(formatPercent(12.345600, ɵDEFAULT_LOCALE_ID, '0.0-10')).toEqual('1,234.56%');
expect(formatPercent(12.345699999, DEFAULT_LOCALE_ID, '0.0-6')).toEqual('1,234.57%'); expect(formatPercent(12.345699999, ɵDEFAULT_LOCALE_ID, '0.0-6')).toEqual('1,234.57%');
expect(formatPercent(12.345699999, DEFAULT_LOCALE_ID, '0.4-6')).toEqual('1,234.5700%'); expect(formatPercent(12.345699999, ɵDEFAULT_LOCALE_ID, '0.4-6')).toEqual('1,234.5700%');
expect(formatPercent(100, DEFAULT_LOCALE_ID, '0.4-6')).toEqual('10,000.0000%'); expect(formatPercent(100, ɵDEFAULT_LOCALE_ID, '0.4-6')).toEqual('10,000.0000%');
expect(formatPercent(100, DEFAULT_LOCALE_ID, '0.0-10')).toEqual('10,000%'); expect(formatPercent(100, ɵDEFAULT_LOCALE_ID, '0.0-10')).toEqual('10,000%');
expect(formatPercent(1.5e2, DEFAULT_LOCALE_ID)).toEqual('15,000%'); expect(formatPercent(1.5e2, ɵDEFAULT_LOCALE_ID)).toEqual('15,000%');
expect(formatPercent(1e100, DEFAULT_LOCALE_ID)).toEqual('1E+102%'); expect(formatPercent(1e100, ɵDEFAULT_LOCALE_ID)).toEqual('1E+102%');
}); });
}); });
}); });
@ -79,16 +79,16 @@ describe('Format number', () => {
const defaultCurrencyCode = 'USD'; const defaultCurrencyCode = 'USD';
describe('transform', () => { describe('transform', () => {
it('should return correct value for numbers', () => { it('should return correct value for numbers', () => {
expect(formatCurrency(123, DEFAULT_LOCALE_ID, '$')).toEqual('$123.00'); expect(formatCurrency(123, ɵDEFAULT_LOCALE_ID, '$')).toEqual('$123.00');
expect(formatCurrency(12, DEFAULT_LOCALE_ID, 'EUR', 'EUR', '.1')).toEqual('EUR12.0'); expect(formatCurrency(12, ɵDEFAULT_LOCALE_ID, 'EUR', 'EUR', '.1')).toEqual('EUR12.0');
expect(formatCurrency( expect(formatCurrency(
5.1234, DEFAULT_LOCALE_ID, defaultCurrencyCode, defaultCurrencyCode, '.0-3')) 5.1234, ɵDEFAULT_LOCALE_ID, defaultCurrencyCode, defaultCurrencyCode, '.0-3'))
.toEqual('USD5.123'); .toEqual('USD5.123');
expect(formatCurrency(5.1234, DEFAULT_LOCALE_ID, defaultCurrencyCode)).toEqual('USD5.12'); expect(formatCurrency(5.1234, ɵDEFAULT_LOCALE_ID, defaultCurrencyCode)).toEqual('USD5.12');
expect(formatCurrency(5.1234, DEFAULT_LOCALE_ID, '$')).toEqual('$5.12'); expect(formatCurrency(5.1234, ɵDEFAULT_LOCALE_ID, '$')).toEqual('$5.12');
expect(formatCurrency(5.1234, DEFAULT_LOCALE_ID, 'CA$')).toEqual('CA$5.12'); expect(formatCurrency(5.1234, ɵDEFAULT_LOCALE_ID, 'CA$')).toEqual('CA$5.12');
expect(formatCurrency(5.1234, DEFAULT_LOCALE_ID, '$')).toEqual('$5.12'); expect(formatCurrency(5.1234, ɵDEFAULT_LOCALE_ID, '$')).toEqual('$5.12');
expect(formatCurrency(5.1234, DEFAULT_LOCALE_ID, '$', defaultCurrencyCode, '5.2-2')) expect(formatCurrency(5.1234, ɵDEFAULT_LOCALE_ID, '$', defaultCurrencyCode, '5.2-2'))
.toEqual('$00,005.12'); .toEqual('$00,005.12');
expect(formatCurrency(5.1234, 'fr', '$', defaultCurrencyCode, '5.2-2')) expect(formatCurrency(5.1234, 'fr', '$', defaultCurrencyCode, '5.2-2'))
.toEqual('00 005,12 $'); .toEqual('00 005,12 $');
@ -97,21 +97,22 @@ describe('Format number', () => {
it('should support any currency code name', () => { it('should support any currency code name', () => {
// currency code is unknown, default formatting options will be used // currency code is unknown, default formatting options will be used
expect(formatCurrency(5.1234, DEFAULT_LOCALE_ID, 'unexisting_ISO_code')) expect(formatCurrency(5.1234, ɵDEFAULT_LOCALE_ID, 'unexisting_ISO_code'))
.toEqual('unexisting_ISO_code5.12'); .toEqual('unexisting_ISO_code5.12');
// currency code is USD, the pipe will format based on USD but will display "Custom name" // currency code is USD, the pipe will format based on USD but will display "Custom name"
expect(formatCurrency(5.1234, DEFAULT_LOCALE_ID, 'Custom name')).toEqual('Custom name5.12'); expect(formatCurrency(5.1234, ɵDEFAULT_LOCALE_ID, 'Custom name'))
.toEqual('Custom name5.12');
}); });
it('should round to the default number of digits if no digitsInfo', () => { it('should round to the default number of digits if no digitsInfo', () => {
// IDR has a default number of digits of 0 // IDR has a default number of digits of 0
expect(formatCurrency(5.1234, DEFAULT_LOCALE_ID, 'IDR', 'IDR')).toEqual('IDR5'); expect(formatCurrency(5.1234, ɵDEFAULT_LOCALE_ID, 'IDR', 'IDR')).toEqual('IDR5');
expect(formatCurrency(5.1234, DEFAULT_LOCALE_ID, 'IDR', 'IDR', '.2')).toEqual('IDR5.12'); expect(formatCurrency(5.1234, ɵDEFAULT_LOCALE_ID, 'IDR', 'IDR', '.2')).toEqual('IDR5.12');
expect(formatCurrency(5.1234, DEFAULT_LOCALE_ID, 'Custom name', 'IDR')) expect(formatCurrency(5.1234, ɵDEFAULT_LOCALE_ID, 'Custom name', 'IDR'))
.toEqual('Custom name5'); .toEqual('Custom name5');
// BHD has a default number of digits of 3 // BHD has a default number of digits of 3
expect(formatCurrency(5.1234, DEFAULT_LOCALE_ID, 'BHD', 'BHD')).toEqual('BHD5.123'); expect(formatCurrency(5.1234, ɵDEFAULT_LOCALE_ID, 'BHD', 'BHD')).toEqual('BHD5.123');
expect(formatCurrency(5.1234, DEFAULT_LOCALE_ID, 'BHD', 'BHD', '.1-2')).toEqual('BHD5.12'); expect(formatCurrency(5.1234, ɵDEFAULT_LOCALE_ID, 'BHD', 'BHD', '.1-2')).toEqual('BHD5.12');
}); });
}); });
}); });

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {ɵfindLocaleData as findLocaleData} from '@angular/core'; import {ɵfindLocaleData} from '@angular/core';
import localeCaESVALENCIA from '@angular/common/locales/ca-ES-VALENCIA'; import localeCaESVALENCIA from '@angular/common/locales/ca-ES-VALENCIA';
import localeEn from '@angular/common/locales/en'; import localeEn from '@angular/common/locales/en';
import localeFr from '@angular/common/locales/fr'; import localeFr from '@angular/common/locales/fr';
@ -32,28 +32,28 @@ import {getCurrencySymbol, getLocaleDateFormat, FormatWidth, getNumberOfCurrency
describe('findLocaleData', () => { describe('findLocaleData', () => {
it('should throw if the LOCALE_DATA for the chosen locale or its parent locale is not available', it('should throw if the LOCALE_DATA for the chosen locale or its parent locale is not available',
() => { () => {
expect(() => findLocaleData('pt-AO')) expect(() => ɵfindLocaleData('pt-AO'))
.toThrowError(/Missing locale data for the locale "pt-AO"/); .toThrowError(/Missing locale data for the locale "pt-AO"/);
}); });
it('should return english data if the locale is en-US', it('should return english data if the locale is en-US',
() => { expect(findLocaleData('en-US')).toEqual(localeEn); }); () => { expect(ɵfindLocaleData('en-US')).toEqual(localeEn); });
it('should return the exact LOCALE_DATA if it is available', it('should return the exact LOCALE_DATA if it is available',
() => { expect(findLocaleData('fr-CA')).toEqual(localeFrCA); }); () => { expect(ɵfindLocaleData('fr-CA')).toEqual(localeFrCA); });
it('should return the parent LOCALE_DATA if it exists and exact locale is not available', it('should return the parent LOCALE_DATA if it exists and exact locale is not available',
() => { expect(findLocaleData('fr-BE')).toEqual(localeFr); }); () => { expect(ɵfindLocaleData('fr-BE')).toEqual(localeFr); });
it(`should find the LOCALE_DATA even if the locale id is badly formatted`, () => { it(`should find the LOCALE_DATA even if the locale id is badly formatted`, () => {
expect(findLocaleData('ca-ES-VALENCIA')).toEqual(localeCaESVALENCIA); expect(ɵfindLocaleData('ca-ES-VALENCIA')).toEqual(localeCaESVALENCIA);
expect(findLocaleData('CA_es_Valencia')).toEqual(localeCaESVALENCIA); expect(ɵfindLocaleData('CA_es_Valencia')).toEqual(localeCaESVALENCIA);
}); });
it(`should find the LOCALE_DATA if the locale id was registered`, () => { it(`should find the LOCALE_DATA if the locale id was registered`, () => {
expect(findLocaleData('fake-id')).toEqual(localeFr); expect(ɵfindLocaleData('fake-id')).toEqual(localeFr);
expect(findLocaleData('fake_iD')).toEqual(localeFr); expect(ɵfindLocaleData('fake_iD')).toEqual(localeFr);
expect(findLocaleData('fake-id2')).toEqual(localeFrCA); expect(ɵfindLocaleData('fake-id2')).toEqual(localeFrCA);
}); });
}); });