-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
066077b
commit 6ffe468
Showing
11 changed files
with
426 additions
and
0 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
6 changes: 6 additions & 0 deletions
6
projects/angular-ecmascript-intl/src/lib/duration/intl-duration-pipe-default-options.ts
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,6 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
import { IntlDurationPipeOptions } from './intl-duration.pipe'; | ||
|
||
export const INTL_DURATION_PIPE_DEFAULT_OPTIONS = new InjectionToken< | ||
Omit<IntlDurationPipeOptions, 'locale'> | ||
>('IntlDurationPipeDefaultOptions'); |
145 changes: 145 additions & 0 deletions
145
projects/angular-ecmascript-intl/src/lib/duration/intl-duration.pipe.spec.ts
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,145 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { INTL_LOCALES } from '../locale'; | ||
import { INTL_DURATION_PIPE_DEFAULT_OPTIONS } from './intl-duration-pipe-default-options'; | ||
import { IntlDurationPipe } from './intl-duration.pipe'; | ||
|
||
describe('IntlDurationPipe', () => { | ||
let testUnit: IntlDurationPipe; | ||
|
||
describe('parsing', () => { | ||
beforeEach(() => { | ||
testUnit = new IntlDurationPipe('en-US'); | ||
}); | ||
|
||
it('should create an instance', () => { | ||
expect(testUnit).toBeTruthy(); | ||
}); | ||
|
||
it('should handle null values', () => { | ||
expect(testUnit.transform(null)).toBeNull(); | ||
}); | ||
|
||
it('should handle undefined values', () => { | ||
expect(testUnit.transform(undefined)).toBeNull(); | ||
}); | ||
|
||
it('should transform durations', () => { | ||
expect( | ||
testUnit.transform({ | ||
years: 2, | ||
months: 11, | ||
weeks: 2, | ||
days: 1, | ||
hours: 0, | ||
minutes: 55, | ||
seconds: 19, | ||
milliseconds: 940, | ||
microseconds: 10, | ||
nanoseconds: 3, | ||
}), | ||
).toEqual( | ||
'2 yrs, 11 mths, 2 wks, 1 day, 55 min, 19 sec, 940 ms, 10 μs, 3 ns', | ||
); | ||
}); | ||
|
||
it('should handle missing Intl.NumberFormat browser API', () => { | ||
// @ts-expect-error Intl APIs are not expected to be undefined | ||
spyOn(Intl, 'DurationFormat').and.returnValue(undefined); | ||
const consoleError = spyOn(console, 'error'); | ||
expect(testUnit.transform({ years: 1 })).toBeNull(); | ||
|
||
expect(consoleError).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
describe('internationalization', () => { | ||
it('should respect the set locale', () => { | ||
TestBed.configureTestingModule({ | ||
providers: [ | ||
IntlDurationPipe, | ||
{ | ||
provide: INTL_LOCALES, | ||
useValue: 'de-DE', | ||
}, | ||
], | ||
}); | ||
testUnit = TestBed.inject(IntlDurationPipe); | ||
|
||
expect(testUnit.transform({ years: 1 })).toEqual('1 J'); | ||
}); | ||
|
||
it('should fall back to the browser default locale', () => { | ||
TestBed.configureTestingModule({ providers: [IntlDurationPipe] }); | ||
|
||
const result1 = TestBed.inject(IntlDurationPipe).transform({ years: 1 }); | ||
const result2 = new IntlDurationPipe(navigator.language).transform({ | ||
years: 1, | ||
}); | ||
|
||
expect(result1).toEqual(result2); | ||
}); | ||
}); | ||
|
||
describe('options', () => { | ||
it('should respect the setting from default config', () => { | ||
TestBed.configureTestingModule({ | ||
providers: [ | ||
IntlDurationPipe, | ||
{ | ||
provide: INTL_LOCALES, | ||
useValue: 'en-US', | ||
}, | ||
{ | ||
provide: INTL_DURATION_PIPE_DEFAULT_OPTIONS, | ||
useValue: { | ||
style: 'long', | ||
}, | ||
}, | ||
], | ||
}); | ||
testUnit = TestBed.inject(IntlDurationPipe); | ||
|
||
expect(testUnit.transform({ years: 1 })).toEqual('1 year'); | ||
}); | ||
|
||
it('should give the user options a higher priority', () => { | ||
TestBed.configureTestingModule({ | ||
providers: [ | ||
IntlDurationPipe, | ||
{ | ||
provide: INTL_LOCALES, | ||
useValue: 'en-US', | ||
}, | ||
{ | ||
provide: INTL_DURATION_PIPE_DEFAULT_OPTIONS, | ||
useValue: { | ||
style: 'long', | ||
}, | ||
}, | ||
], | ||
}); | ||
testUnit = TestBed.inject(IntlDurationPipe); | ||
|
||
expect(testUnit.transform({ years: 1 }, { style: 'narrow' })).toEqual( | ||
'1y', | ||
); | ||
}); | ||
}); | ||
|
||
it('should respect locale option', () => { | ||
TestBed.configureTestingModule({ | ||
providers: [ | ||
IntlDurationPipe, | ||
{ | ||
provide: INTL_LOCALES, | ||
useValue: 'en-US', | ||
}, | ||
], | ||
}); | ||
testUnit = TestBed.inject(IntlDurationPipe); | ||
|
||
expect(testUnit.transform({ years: 1 }, { locale: 'de-DE' })).toEqual( | ||
'1 J', | ||
); | ||
}); | ||
}); |
93 changes: 93 additions & 0 deletions
93
projects/angular-ecmascript-intl/src/lib/duration/intl-duration.pipe.ts
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,93 @@ | ||
import { Inject, Optional, Pipe, PipeTransform } from '@angular/core'; | ||
import { IntlPipeOptions } from '../intl-pipe-options'; | ||
import { INTL_LOCALES } from '../locale'; | ||
import { INTL_DURATION_PIPE_DEFAULT_OPTIONS } from './intl-duration-pipe-default-options'; | ||
|
||
// ToDo: remove once TypeScript includes official typings | ||
// eslint-disable-next-line @typescript-eslint/no-namespace | ||
declare namespace Intl { | ||
export class DurationFormat { | ||
constructor(locale?: string[] | string, options?: DurationFormatOptions); | ||
|
||
format(duration: Duration): string; | ||
} | ||
|
||
export interface DurationFormatOptions { | ||
style?: DurationItemStyle | 'digital'; | ||
years?: DurationItemStyle; | ||
yearsDisplay?: DurationItemDisplay; | ||
months?: DurationItemStyle; | ||
monthsDisplay?: DurationItemDisplay; | ||
weeks?: DurationItemStyle; | ||
weeksDisplay?: DurationItemDisplay; | ||
days?: DurationItemStyle; | ||
daysDisplay?: DurationItemDisplay; | ||
hours?: DurationItemStyle | 'numeric' | '2-digit'; | ||
hoursDisplay?: DurationItemDisplay; | ||
minutes?: DurationItemStyle | 'numeric' | '2-digit'; | ||
minutesDisplay?: DurationItemDisplay; | ||
seconds?: DurationItemStyle | 'numeric' | '2-digit'; | ||
secondsDisplay?: DurationItemDisplay; | ||
milliseconds?: DurationItemStyle | 'numeric' | '2-digit'; | ||
millisecondsDisplay?: DurationItemDisplay; | ||
microseconds?: DurationItemStyle | 'numeric'; | ||
microsecondsDisplay?: DurationItemDisplay; | ||
nanoseconds?: DurationItemStyle | 'numeric'; | ||
nanosecondsDisplay?: DurationItemDisplay; | ||
fractionalDigits?: number; | ||
} | ||
|
||
export type DurationItemStyle = 'long' | 'short' | 'narrow'; | ||
export type DurationItemDisplay = 'always' | 'auto'; | ||
|
||
export interface Duration { | ||
years?: number; | ||
months?: number; | ||
weeks?: number; | ||
days?: number; | ||
hours?: number; | ||
minutes?: number; | ||
seconds?: number; | ||
milliseconds?: number; | ||
microseconds?: number; | ||
nanoseconds?: number; | ||
} | ||
} | ||
|
||
export type IntlDurationPipeOptions = Partial<Intl.DurationFormatOptions> & | ||
IntlPipeOptions; | ||
|
||
@Pipe({ | ||
name: 'intlDuration', | ||
standalone: true, | ||
}) | ||
export class IntlDurationPipe implements PipeTransform { | ||
constructor( | ||
@Optional() | ||
@Inject(INTL_LOCALES) | ||
readonly locale?: string | string[] | null, | ||
@Optional() | ||
@Inject(INTL_DURATION_PIPE_DEFAULT_OPTIONS) | ||
readonly defaultOptions?: Omit<IntlDurationPipeOptions, 'locale'> | null, | ||
) {} | ||
transform( | ||
value: Intl.Duration | null | undefined, | ||
options?: IntlDurationPipeOptions, | ||
): string | null { | ||
if (!value) { | ||
return null; | ||
} | ||
|
||
const { locale, ...intlOptions } = options ?? {}; | ||
|
||
try { | ||
return new Intl.DurationFormat(locale ?? this.locale ?? undefined, { | ||
...this.defaultOptions, | ||
...intlOptions, | ||
}).format(value); | ||
} catch (e) { | ||
console.error('Error while transforming the duration value', e); | ||
return 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
Oops, something went wrong.