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

added type support for passing date as a number #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions time-ago.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {Pipe, PipeTransform, NgZone, ChangeDetectorRef, OnDestroy} from "@angula
export class TimeAgoPipe implements PipeTransform, OnDestroy {
private timer: number;
constructor(private changeDetectorRef: ChangeDetectorRef, private ngZone: NgZone) {}
transform(value:string) {
transform(value:string|number) {
this.removeTimer();
let d = new Date(value);
let d = typeof value === 'string' ? new Date(value) : new Date(value);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition here is required unfortunately (at least in TypeScript 2.7.2) - this ensures the type guards treat the first Date(value) as string and the second as number. Without it, TypeScript compiler complains (it won't auto select the matching constructor unfortunately)

let now = new Date();
let seconds = Math.round(Math.abs((now.getTime() - d.getTime())/1000));
let timeToUpdate = (Number.isNaN(seconds)) ? 1000 : this.getSecondsUntilUpdate(seconds) *1000;
Expand Down Expand Up @@ -74,4 +74,4 @@ export class TimeAgoPipe implements PipeTransform, OnDestroy {
return 3600;
}
}
}
}