Skip to content

Commit

Permalink
amTimeAgo pipe updates it's output when locale changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz.kusnierz committed Mar 11, 2018
1 parent 3cbf666 commit 4808635
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/time-ago.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'reflect-metadata';
import { NgZone } from '@angular/core';
import { TimeAgoPipe } from './time-ago.pipe';
import * as moment from 'moment';
import 'moment/min/locales';

declare var global: any;

Expand Down Expand Up @@ -65,6 +66,22 @@ describe('TimeAgoPipe', () => {
expect(pipe.transform(moment(0))).toBe('46 years ago');
});

it('should update the text when moment locale changes', () => {
const changeDetectorMock = { markForCheck: jest.fn() };
const pipe = new TimeAgoPipe(changeDetectorMock as any, new NgZoneMock() as NgZone);
fakeDate('2016-05-01');
expect(pipe.transform(moment(0))).toBe('46 years ago');
expect(pipe.transform(moment(0).locale('pl'))).toBe('46 lat temu');
});

it('should reset language when localized moment changes to Date', () => {
const changeDetectorMock = { markForCheck: jest.fn() };
const pipe = new TimeAgoPipe(changeDetectorMock as any, new NgZoneMock() as NgZone);
fakeDate('2016-05-01');
expect(pipe.transform(moment(0).locale('pl'))).toBe('46 lat temu');
expect(pipe.transform(new Date(0))).toBe('46 years ago');
});

it('should update the text when the date instance time is updated', () => {
const changeDetectorMock = { markForCheck: jest.fn() };
const pipe = new TimeAgoPipe(changeDetectorMock as any, new NgZoneMock() as NgZone);
Expand Down
10 changes: 9 additions & 1 deletion src/time-ago.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export class TimeAgoPipe implements PipeTransform, OnDestroy {
private lastTime: Number;
private lastValue: Date | moment.Moment;
private lastOmitSuffix: boolean;
private lastLocale?: string;
private lastText: string;

constructor(private cdRef: ChangeDetectorRef, private ngZone: NgZone) {
Expand All @@ -23,6 +24,7 @@ export class TimeAgoPipe implements PipeTransform, OnDestroy {
this.lastTime = this.getTime(value);
this.lastValue = value;
this.lastOmitSuffix = omitSuffix;
this.lastLocale = this.getLocale(value);
this.removeTimer();
this.createTimer();
this.lastText = momentConstructor(value).from(momentConstructor(), omitSuffix);
Expand Down Expand Up @@ -80,7 +82,9 @@ export class TimeAgoPipe implements PipeTransform, OnDestroy {
}

private hasChanged(value: Date | moment.Moment, omitSuffix?: boolean) {
return this.getTime(value) !== this.lastTime || omitSuffix !== this.lastOmitSuffix;
return this.getTime(value) !== this.lastTime
|| this.getLocale(value) !== this.lastLocale
|| omitSuffix !== this.lastOmitSuffix;
}

private getTime(value: Date | moment.Moment) {
Expand All @@ -92,4 +96,8 @@ export class TimeAgoPipe implements PipeTransform, OnDestroy {
return momentConstructor(value).valueOf();
}
}

private getLocale(value: Date | moment.Moment): string {
return moment.isMoment(value) ? value.locale() : null;
}
}

0 comments on commit 4808635

Please sign in to comment.