fix(pipes): remove bidi control chars (#10870)

Fix inconsistent results in Edge vs. other browsers.

Closes #10080.
This commit is contained in:
John-David Dalton 2016-08-18 13:31:33 -07:00 committed by Kara
parent 2f41b5c8a0
commit 91980382e8
2 changed files with 10 additions and 5 deletions

View File

@ -97,6 +97,9 @@ export function main() {
expect(pipe.transform(date, 'mediumTime')).toEqual('9:03:01 AM'); expect(pipe.transform(date, 'mediumTime')).toEqual('9:03:01 AM');
expect(pipe.transform(date, 'shortTime')).toEqual('9:03 AM'); expect(pipe.transform(date, 'shortTime')).toEqual('9:03 AM');
}); });
it('should remove bidi control characters',
() => { expect(pipe.transform(date, 'MM/dd/yyyy').length).toEqual(10); });
}); });
} }
}); });

View File

@ -140,11 +140,15 @@ function hourExtracter(inner: (date: Date, locale: string) => string): (
}; };
} }
function intlDateFormat(date: Date, locale: string, options: Intl.DateTimeFormatOptions): string {
return new Intl.DateTimeFormat(locale, options).format(date).replace(/[\u200e\u200f]/g, '');
}
function timeZoneGetter(timezone: string): (date: Date, locale: string) => string { function timeZoneGetter(timezone: string): (date: Date, locale: string) => string {
// To workaround `Intl` API restriction for single timezone let format with 24 hours // To workaround `Intl` API restriction for single timezone let format with 24 hours
const format = {hour: '2-digit', hour12: false, timeZoneName: timezone}; const options = {hour: '2-digit', hour12: false, timeZoneName: timezone};
return function(date: Date, locale: string): string { return function(date: Date, locale: string): string {
const result = new Intl.DateTimeFormat(locale, format).format(date); const result = intlDateFormat(date, locale, options);
// Then extract first 3 letters that related to hours // Then extract first 3 letters that related to hours
return result ? result.substring(3) : ''; return result ? result.substring(3) : '';
}; };
@ -177,9 +181,7 @@ function combine(options: Intl.DateTimeFormatOptions[]): Intl.DateTimeFormatOpti
function datePartGetterFactory(ret: Intl.DateTimeFormatOptions): (date: Date, locale: string) => function datePartGetterFactory(ret: Intl.DateTimeFormatOptions): (date: Date, locale: string) =>
string { string {
return function(date: Date, locale: string): string { return (date: Date, locale: string): string => intlDateFormat(date, locale, ret);
return new Intl.DateTimeFormat(locale, ret).format(date);
};
} }