Skip to content

Commit

Permalink
Merge pull request #206 from theodorejb/support-full-template-type-check
Browse files Browse the repository at this point in the history
Allow using all supported Moment inputs with TimeAgoPipe
  • Loading branch information
urish committed Dec 9, 2018
2 parents ef73d52 + ad58c20 commit f0dc4aa
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 26 deletions.
8 changes: 0 additions & 8 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions src/time-ago.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
15 changes: 7 additions & 8 deletions src/time-ago.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ 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;

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;
Expand All @@ -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(() => {
Expand All @@ -61,7 +61,6 @@ export class TimeAgoPipe implements PipeTransform, OnDestroy {
});
}


private removeTimer() {
if (this.currentTimer) {
window.clearTimeout(this.currentTimer);
Expand All @@ -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)) {
Expand All @@ -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;
}
}
3 changes: 1 addition & 2 deletions tsconfig.lint.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"types": []
},
"exclude": [
"src/test.ts",
"**/*.spec.ts"
"src/*.spec.ts"
]
}
10 changes: 3 additions & 7 deletions tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -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"
]
}

0 comments on commit f0dc4aa

Please sign in to comment.