Skip to content

Commit

Permalink
refactor(common): use private names rather than aliases internally (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin authored and mohaxspb committed Nov 7, 2019
1 parent 544b8a1 commit 2e208b5
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 104 deletions.
78 changes: 39 additions & 39 deletions packages/common/src/i18n/locale_data_api.ts
Expand Up @@ -6,7 +6,7 @@
* 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 {CurrencyIndex, ExtraLocaleDataIndex} from './locale_data';

Expand Down Expand Up @@ -217,7 +217,7 @@ export enum WeekDay {
* @publicApi
*/
export function getLocaleId(locale: string): string {
return findLocaleData(locale)[LocaleDataIndex.LocaleId];
return ɵfindLocaleData(locale)[ɵLocaleDataIndex.LocaleId];
}

/**
Expand All @@ -233,10 +233,10 @@ export function getLocaleId(locale: string): string {
*/
export function getLocaleDayPeriods(
locale: string, formStyle: FormStyle, width: TranslationWidth): [string, string] {
const data = findLocaleData(locale);
const data = ɵfindLocaleData(locale);
const amPmData = <[
string, string
][][]>[data[LocaleDataIndex.DayPeriodsFormat], data[LocaleDataIndex.DayPeriodsStandalone]];
][][]>[data[ɵLocaleDataIndex.DayPeriodsFormat], data[ɵLocaleDataIndex.DayPeriodsStandalone]];
const amPm = getLastDefinedValue(amPmData, formStyle);
return getLastDefinedValue(amPm, width);
}
Expand All @@ -255,9 +255,9 @@ export function getLocaleDayPeriods(
*/
export function getLocaleDayNames(
locale: string, formStyle: FormStyle, width: TranslationWidth): string[] {
const data = findLocaleData(locale);
const data = ɵfindLocaleData(locale);
const daysData =
<string[][][]>[data[LocaleDataIndex.DaysFormat], data[LocaleDataIndex.DaysStandalone]];
<string[][][]>[data[ɵLocaleDataIndex.DaysFormat], data[ɵLocaleDataIndex.DaysStandalone]];
const days = getLastDefinedValue(daysData, formStyle);
return getLastDefinedValue(days, width);
}
Expand All @@ -276,9 +276,9 @@ export function getLocaleDayNames(
*/
export function getLocaleMonthNames(
locale: string, formStyle: FormStyle, width: TranslationWidth): string[] {
const data = findLocaleData(locale);
const data = ɵfindLocaleData(locale);
const monthsData =
<string[][][]>[data[LocaleDataIndex.MonthsFormat], data[LocaleDataIndex.MonthsStandalone]];
<string[][][]>[data[ɵLocaleDataIndex.MonthsFormat], data[ɵLocaleDataIndex.MonthsStandalone]];
const months = getLastDefinedValue(monthsData, formStyle);
return getLastDefinedValue(months, width);
}
Expand All @@ -296,8 +296,8 @@ export function getLocaleMonthNames(
* @publicApi
*/
export function getLocaleEraNames(locale: string, width: TranslationWidth): [string, string] {
const data = findLocaleData(locale);
const erasData = <[string, string][]>data[LocaleDataIndex.Eras];
const data = ɵfindLocaleData(locale);
const erasData = <[string, string][]>data[ɵLocaleDataIndex.Eras];
return getLastDefinedValue(erasData, width);
}

Expand All @@ -313,8 +313,8 @@ export function getLocaleEraNames(locale: string, width: TranslationWidth): [str
* @publicApi
*/
export function getLocaleFirstDayOfWeek(locale: string): WeekDay {
const data = findLocaleData(locale);
return data[LocaleDataIndex.FirstDayOfWeek];
const data = ɵfindLocaleData(locale);
return data[ɵLocaleDataIndex.FirstDayOfWeek];
}

/**
Expand All @@ -327,8 +327,8 @@ export function getLocaleFirstDayOfWeek(locale: string): WeekDay {
* @publicApi
*/
export function getLocaleWeekEndRange(locale: string): [WeekDay, WeekDay] {
const data = findLocaleData(locale);
return data[LocaleDataIndex.WeekendRange];
const data = ɵfindLocaleData(locale);
return data[ɵLocaleDataIndex.WeekendRange];
}

/**
Expand All @@ -343,8 +343,8 @@ export function getLocaleWeekEndRange(locale: string): [WeekDay, WeekDay] {
* @publicApi
*/
export function getLocaleDateFormat(locale: string, width: FormatWidth): string {
const data = findLocaleData(locale);
return getLastDefinedValue(data[LocaleDataIndex.DateFormat], width);
const data = ɵfindLocaleData(locale);
return getLastDefinedValue(data[ɵLocaleDataIndex.DateFormat], width);
}

/**
Expand All @@ -359,8 +359,8 @@ export function getLocaleDateFormat(locale: string, width: FormatWidth): string
* @publicApi
*/
export function getLocaleTimeFormat(locale: string, width: FormatWidth): string {
const data = findLocaleData(locale);
return getLastDefinedValue(data[LocaleDataIndex.TimeFormat], width);
const data = ɵfindLocaleData(locale);
return getLastDefinedValue(data[ɵLocaleDataIndex.TimeFormat], width);
}

/**
Expand All @@ -375,8 +375,8 @@ export function getLocaleTimeFormat(locale: string, width: FormatWidth): string
* @publicApi
*/
export function getLocaleDateTimeFormat(locale: string, width: FormatWidth): string {
const data = findLocaleData(locale);
const dateTimeFormatData = <string[]>data[LocaleDataIndex.DateTimeFormat];
const data = ɵfindLocaleData(locale);
const dateTimeFormatData = <string[]>data[ɵLocaleDataIndex.DateTimeFormat];
return getLastDefinedValue(dateTimeFormatData, width);
}

Expand All @@ -391,13 +391,13 @@ export function getLocaleDateTimeFormat(locale: string, width: FormatWidth): str
* @publicApi
*/
export function getLocaleNumberSymbol(locale: string, symbol: NumberSymbol): string {
const data = findLocaleData(locale);
const res = data[LocaleDataIndex.NumberSymbols][symbol];
const data = ɵfindLocaleData(locale);
const res = data[ɵLocaleDataIndex.NumberSymbols][symbol];
if (typeof res === 'undefined') {
if (symbol === NumberSymbol.CurrencyDecimal) {
return data[LocaleDataIndex.NumberSymbols][NumberSymbol.Decimal];
return data[ɵLocaleDataIndex.NumberSymbols][NumberSymbol.Decimal];
} else if (symbol === NumberSymbol.CurrencyGroup) {
return data[LocaleDataIndex.NumberSymbols][NumberSymbol.Group];
return data[ɵLocaleDataIndex.NumberSymbols][NumberSymbol.Group];
}
}
return res;
Expand Down Expand Up @@ -439,8 +439,8 @@ export function getLocaleNumberSymbol(locale: string, symbol: NumberSymbol): str
* @publicApi
*/
export function getLocaleNumberFormat(locale: string, type: NumberFormatStyle): string {
const data = findLocaleData(locale);
return data[LocaleDataIndex.NumberFormats][type];
const data = ɵfindLocaleData(locale);
return data[ɵLocaleDataIndex.NumberFormats][type];
}

/**
Expand All @@ -455,8 +455,8 @@ export function getLocaleNumberFormat(locale: string, type: NumberFormatStyle):
* @publicApi
*/
export function getLocaleCurrencySymbol(locale: string): string|null {
const data = findLocaleData(locale);
return data[LocaleDataIndex.CurrencySymbol] || null;
const data = ɵfindLocaleData(locale);
return data[ɵLocaleDataIndex.CurrencySymbol] || null;
}

/**
Expand All @@ -470,8 +470,8 @@ export function getLocaleCurrencySymbol(locale: string): string|null {
* @publicApi
*/
export function getLocaleCurrencyName(locale: string): string|null {
const data = findLocaleData(locale);
return data[LocaleDataIndex.CurrencyName] || null;
const data = ɵfindLocaleData(locale);
return data[ɵLocaleDataIndex.CurrencyName] || null;
}

/**
Expand All @@ -481,8 +481,8 @@ export function getLocaleCurrencyName(locale: string): string|null {
* @see [Internationalization (i18n) Guide](https://angular.io/guide/i18n)
*/
function getLocaleCurrencies(locale: string): {[code: string]: CurrenciesSymbols} {
const data = findLocaleData(locale);
return data[LocaleDataIndex.Currencies];
const data = ɵfindLocaleData(locale);
return data[ɵLocaleDataIndex.Currencies];
}

/**
Expand All @@ -493,9 +493,9 @@ export const getLocalePluralCase: (locale: string) => ((value: number) => Plural
ɵgetLocalePluralCase;

function checkFullData(data: any) {
if (!data[LocaleDataIndex.ExtraData]) {
if (!data[ɵLocaleDataIndex.ExtraData]) {
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.`);
}
}

Expand All @@ -522,9 +522,9 @@ function checkFullData(data: any) {
* @publicApi
*/
export function getLocaleExtraDayPeriodRules(locale: string): (Time | [Time, Time])[] {
const data = findLocaleData(locale);
const data = ɵfindLocaleData(locale);
checkFullData(data);
const rules = data[LocaleDataIndex.ExtraData][ExtraLocaleDataIndex.ExtraDayPeriodsRules] || [];
const rules = data[ɵLocaleDataIndex.ExtraData][ExtraLocaleDataIndex.ExtraDayPeriodsRules] || [];
return rules.map((rule: string | [string, string]) => {
if (typeof rule === 'string') {
return extractTime(rule);
Expand Down Expand Up @@ -552,11 +552,11 @@ export function getLocaleExtraDayPeriodRules(locale: string): (Time | [Time, Tim
*/
export function getLocaleExtraDayPeriods(
locale: string, formStyle: FormStyle, width: TranslationWidth): string[] {
const data = findLocaleData(locale);
const data = ɵfindLocaleData(locale);
checkFullData(data);
const dayPeriodsData = <string[][][]>[
data[LocaleDataIndex.ExtraData][ExtraLocaleDataIndex.ExtraDayPeriodFormats],
data[LocaleDataIndex.ExtraData][ExtraLocaleDataIndex.ExtraDayPeriodStandalone]
data[ɵLocaleDataIndex.ExtraData][ExtraLocaleDataIndex.ExtraDayPeriodFormats],
data[ɵLocaleDataIndex.ExtraData][ExtraLocaleDataIndex.ExtraDayPeriodStandalone]
];
const dayPeriods = getLastDefinedValue(dayPeriodsData, formStyle) || [];
return getLastDefinedValue(dayPeriods, width) || [];
Expand Down
25 changes: 13 additions & 12 deletions packages/common/test/i18n/format_date_spec.ts
Expand Up @@ -15,7 +15,7 @@ import localeHu from '@angular/common/locales/hu';
import localeSr from '@angular/common/locales/sr';
import localeTh from '@angular/common/locales/th';
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('toDate', () => {
Expand Down Expand Up @@ -52,7 +52,8 @@ describe('Format date', () => {

// Check the transformation of a date into a pattern
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(() => {
Expand Down Expand Up @@ -209,7 +210,7 @@ describe('Format date', () => {
};

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]);
});
});
Expand Down Expand Up @@ -254,45 +255,45 @@ describe('Format date', () => {
};

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',
() => 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'));

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'));

// https://github.com/angular/angular/issues/9524
// https://github.com/angular/angular/issues/9524
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}/));

// https://github.com/angular/angular/issues/21491
it('should not assume UTC for iso strings in Safari if the timezone is not defined', () => {
// this test only works if the timezone is not in UTC
// which is the case for BrowserStack when we test Safari
if (new Date().getTimezoneOffset() !== 0) {
expect(formatDate('2018-01-11T13:00:00', 'HH', DEFAULT_LOCALE_ID))
.not.toEqual(formatDate('2018-01-11T13:00:00Z', '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));
}
});

// https://github.com/angular/angular/issues/16624
// https://github.com/angular/angular/issues/17478
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');
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');
});

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`, () => {
expect(formatDate(date, 'short', 'de')).toEqual('15.06.15, 09:03');
Expand Down

0 comments on commit 2e208b5

Please sign in to comment.