diff --git a/angular.json b/angular.json index ed70e84..2204089 100644 --- a/angular.json +++ b/angular.json @@ -18,14 +18,6 @@ } } }, - "test": { - "builder": "@angular-devkit/build-angular:karma", - "options": { - "main": "src/test.ts", - "tsConfig": "tsconfig.spec.json", - "karmaConfig": "karma.conf.js" - } - }, "lint": { "builder": "@angular-devkit/build-angular:tslint", "options": { diff --git a/package.json b/package.json index 90a2322..e9a4068 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Moment.JS pipes for Angular (timeago and more)", "scripts": { "build": "ng build", - "test": "ng lint && jest", + "test": "ng lint && tsc -p tsconfig.spec.json && jest", "test:watch": "jest --watch", "test:coverage": "jest --coverage", "test:debug": "node --inspect-brk --inspect ./node_modules/jest/bin/jest.js --runInBand", diff --git a/src/time-ago.pipe.spec.ts b/src/time-ago.pipe.spec.ts index adb02e0..0a9f011 100644 --- a/src/time-ago.pipe.spec.ts +++ b/src/time-ago.pipe.spec.ts @@ -37,6 +37,12 @@ describe('TimeAgoPipe', () => { expect(pipe.transform(new Date())).toBe('a few seconds ago'); }); + it('should support string dates', () => { + const pipe = new TimeAgoPipe(null, new NgZoneMock() as NgZone); + const dateStr = new Date().toISOString(); + expect(pipe.transform(dateStr)).toBe('a few seconds ago'); + }); + it('should omit the suffix if second parameter is truthy', () => { const pipe = new TimeAgoPipe(null, new NgZoneMock() as NgZone); expect(pipe.transform(new Date(new Date().getTime() + 60000), true)).toBe('a minute'); diff --git a/src/time-ago.pipe.ts b/src/time-ago.pipe.ts index 751b4eb..d0d4840 100644 --- a/src/time-ago.pipe.ts +++ b/src/time-ago.pipe.ts @@ -10,7 +10,7 @@ export class TimeAgoPipe implements PipeTransform, OnDestroy { private currentTimer: number | null; private lastTime: Number; - private lastValue: Date | moment.Moment; + private lastValue: moment.MomentInput; private lastOmitSuffix: boolean; private lastLocale?: string; private lastText: string; @@ -18,7 +18,7 @@ export class TimeAgoPipe implements PipeTransform, OnDestroy { constructor(private cdRef: ChangeDetectorRef, private ngZone: NgZone) { } - transform(value: Date | moment.Moment, omitSuffix?: boolean): string { + transform(value: moment.MomentInput, omitSuffix?: boolean): string { if (this.hasChanged(value, omitSuffix)) { this.lastTime = this.getTime(value); this.lastValue = value; @@ -39,14 +39,14 @@ export class TimeAgoPipe implements PipeTransform, OnDestroy { this.removeTimer(); } - private createTimer() { if (this.currentTimer) { return; } - const momentInstance = momentConstructor(this.lastValue); + const momentInstance = momentConstructor(this.lastValue); const timeToUpdate = this.getSecondsUntilUpdate(momentInstance) * 1000; + this.currentTimer = this.ngZone.runOutsideAngular(() => { if (typeof window !== 'undefined') { return window.setTimeout(() => { @@ -61,7 +61,6 @@ export class TimeAgoPipe implements PipeTransform, OnDestroy { }); } - private removeTimer() { if (this.currentTimer) { window.clearTimeout(this.currentTimer); @@ -82,13 +81,13 @@ export class TimeAgoPipe implements PipeTransform, OnDestroy { } } - private hasChanged(value: Date | moment.Moment, omitSuffix?: boolean) { + private hasChanged(value: moment.MomentInput, omitSuffix?: boolean): boolean { return this.getTime(value) !== this.lastTime || this.getLocale(value) !== this.lastLocale || omitSuffix !== this.lastOmitSuffix; } - private getTime(value: Date | moment.Moment) { + private getTime(value: moment.MomentInput): number { if (moment.isDate(value)) { return value.getTime(); } else if (moment.isMoment(value)) { @@ -98,7 +97,7 @@ export class TimeAgoPipe implements PipeTransform, OnDestroy { } } - private getLocale(value: Date | moment.Moment): string { + private getLocale(value: moment.MomentInput): string | null { return moment.isMoment(value) ? value.locale() : null; } } diff --git a/tsconfig.lint.json b/tsconfig.lint.json index 05a58ed..b6960b3 100644 --- a/tsconfig.lint.json +++ b/tsconfig.lint.json @@ -6,7 +6,6 @@ "types": [] }, "exclude": [ - "src/test.ts", - "**/*.spec.ts" + "src/*.spec.ts" ] } diff --git a/tsconfig.spec.json b/tsconfig.spec.json index c36cfa9..d03fa77 100644 --- a/tsconfig.spec.json +++ b/tsconfig.spec.json @@ -1,17 +1,13 @@ { "extends": "./tsconfig.json", "compilerOptions": { - "outDir": "./out-tsc/spec", + "noEmit": true, "types": [ - "jasmine", + "jest", "node" ] }, - "files": [ - "src/test.ts" - ], "include": [ - "**/*.spec.ts", - "**/*.d.ts" + "src/*.spec.ts" ] }