diff --git a/projects/ngx-intl/src/lib/intl-timeago.pipe.spec.ts b/projects/ngx-intl/src/lib/intl-timeago.pipe.spec.ts index ca83c98..cdbe561 100644 --- a/projects/ngx-intl/src/lib/intl-timeago.pipe.spec.ts +++ b/projects/ngx-intl/src/lib/intl-timeago.pipe.spec.ts @@ -5,8 +5,7 @@ describe('IntlTimeagoPipe', () => { let pipe: IntlTimeagoPipe; beforeEach(() => { - const datePipe = new IntlDatePipe('en-US', null, null); - pipe = new IntlTimeagoPipe('en-US', null, datePipe); + pipe = new IntlTimeagoPipe('en-US', null, null, null); }); it('should create instance', () => { @@ -46,16 +45,14 @@ describe('IntlTimeagoPipe', () => { }); it('should use preset by string', () => { - const datePipe = new IntlDatePipe('en-US', null, null); - pipe = new IntlTimeagoPipe('en-US', {presets: {custom: {style: 'short'}}}, datePipe); + pipe = new IntlTimeagoPipe('en-US', {presets: {custom: {style: 'short'}}}, null, null); const then = new Date(new Date().getTime() - 1 * 60 * 60 * 24 * 365 / 12 * 2 * 1000); const result = pipe.transform(then, 'custom'); expect(result).toEqual('2 mo. ago'); }); it('should use preset by options', () => { - const datePipe = new IntlDatePipe('en-US', null, null); - pipe = new IntlTimeagoPipe('en-US', {presets: {custom: {style: 'short'}}}, datePipe); + pipe = new IntlTimeagoPipe('en-US', {presets: {custom: {style: 'short'}}}, null, null); const now = new Date(Date.UTC(2020, 11, 21)); const then = new Date(Date.UTC(2020, 9, 21)); const result = pipe.transform(then, { now, preset: 'custom' }); @@ -63,8 +60,7 @@ describe('IntlTimeagoPipe', () => { }); it('should use overrides', () => { - const datePipe = new IntlDatePipe('en-US', null, null); - pipe = new IntlTimeagoPipe('en-US', {presets: {custom: {style: 'short'}}}, datePipe); + pipe = new IntlTimeagoPipe('en-US', {presets: {custom: {style: 'short'}}}, null, null); const now = new Date(Date.UTC(2020, 11, 21)); const then = new Date(Date.UTC(2020, 9, 21)); const result = pipe.transform(then, { now, preset: 'custom', style: 'long' }); @@ -72,8 +68,7 @@ describe('IntlTimeagoPipe', () => { }); it('should use default', () => { - const datePipe = new IntlDatePipe('en-US', null, null); - pipe = new IntlTimeagoPipe('en-US', {presets: {custom: {style: 'short'}}, defaultPreset: 'custom'}, datePipe); + pipe = new IntlTimeagoPipe('en-US', {presets: {custom: {style: 'short'}}, defaultPreset: 'custom'}, null, null); const now = new Date(Date.UTC(2020, 11, 21)); const then = new Date(Date.UTC(2020, 9, 21)); const result = pipe.transform(then, { now }); diff --git a/projects/ngx-intl/src/lib/intl-timeago.pipe.ts b/projects/ngx-intl/src/lib/intl-timeago.pipe.ts index 3c0cbcc..14c1643 100644 --- a/projects/ngx-intl/src/lib/intl-timeago.pipe.ts +++ b/projects/ngx-intl/src/lib/intl-timeago.pipe.ts @@ -1,5 +1,5 @@ import { Inject, InjectionToken, LOCALE_ID, Optional, Pipe, PipeTransform } from '@angular/core'; -import { IntlDateLocalOptions, IntlDatePipe } from './intl-date.pipe'; +import { IntlDateGlobalOptions, IntlDateLocalOptions, IntlDatePipe, INTL_DATE_OPTIONS, INTL_DATE_TIMEZONE } from './intl-date.pipe'; const UNITS: {[key in Intl.RelativeTimeFormatUnit]: number} = { year: 1 * 60 * 60 * 24 * 365, @@ -55,6 +55,7 @@ export const INTL_TIMEAGO_PRESET_LONG: IntlTimeagoOptions = standalone: true }) export class IntlTimeagoPipe implements PipeTransform { + private readonly intlDatePipe: IntlDatePipe private static readonly DEFAULT_OPTIONS: IntlTimeagoGlobalOptions = { presets: { short: INTL_TIMEAGO_PRESET_SHORT, @@ -65,8 +66,11 @@ export class IntlTimeagoPipe implements PipeTransform { constructor( @Inject(LOCALE_ID) private readonly locale: string, @Inject(INTL_TIMEAGO_OPTIONS) @Optional() private readonly options: IntlTimeagoGlobalOptions | null, - private readonly intlDatePipe: IntlDatePipe - ) {} + @Inject(INTL_DATE_OPTIONS) @Optional() dateOptions: IntlDateGlobalOptions | null, + @Inject(INTL_DATE_TIMEZONE) @Optional() dateTimezone: string | null + ) { + this.intlDatePipe = new IntlDatePipe(locale, dateOptions, dateTimezone); + } transform(value?: Date | number | null, options?: string | IntlTimeagoLocalOptions, ...locales: string[]): string | null { if (value === null) {