Skip to content

Commit

Permalink
fix: injection of date pipe not working in timeago pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
fynnfeldpausch committed Aug 9, 2022
1 parent 2d91e8f commit 371adf2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
15 changes: 5 additions & 10 deletions projects/ngx-intl/src/lib/intl-timeago.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -46,34 +45,30 @@ 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' });
expect(result).toEqual('2 mo. ago');
});

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' });
expect(result).toEqual('2 months ago');
});

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 });
Expand Down
10 changes: 7 additions & 3 deletions projects/ngx-intl/src/lib/intl-timeago.pipe.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand Down

0 comments on commit 371adf2

Please sign in to comment.