Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

amTimeAgo pipe updates it's output when locale changes #188

Merged
merged 1 commit into from
Mar 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}