From 48086358cf8995bfe34951c6b9c085a9880e7ad8 Mon Sep 17 00:00:00 2001 From: "lukasz.kusnierz" Date: Sun, 11 Mar 2018 09:21:01 +0100 Subject: [PATCH] amTimeAgo pipe updates it's output when locale changes --- src/time-ago.pipe.spec.ts | 17 +++++++++++++++++ src/time-ago.pipe.ts | 10 +++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/time-ago.pipe.spec.ts b/src/time-ago.pipe.spec.ts index 81c7223..6e2b363 100644 --- a/src/time-ago.pipe.spec.ts +++ b/src/time-ago.pipe.spec.ts @@ -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; @@ -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); diff --git a/src/time-ago.pipe.ts b/src/time-ago.pipe.ts index 023adb0..6eff024 100644 --- a/src/time-ago.pipe.ts +++ b/src/time-ago.pipe.ts @@ -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) { @@ -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); @@ -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) { @@ -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; + } }