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

Allow using all supported Moment inputs with TimeAgoPipe #206

Merged
merged 2 commits into from
Dec 9, 2018
Merged
Changes from 1 commit
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
Prev Previous commit
Allow using all supported Moment inputs with TimeAgoPipe
Fixes #204.
theodorejb committed Dec 9, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit ad58c20f79af77d1c0b49488df2789a171667ad8
6 changes: 6 additions & 0 deletions src/time-ago.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
15 changes: 7 additions & 8 deletions src/time-ago.pipe.ts
Original file line number Diff line number Diff line change
@@ -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;
@@ -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;
}
}