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

fix: set relativeTimeThreshold for amTimeAgo #247

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
18 changes: 2 additions & 16 deletions src/duration.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import * as moment from 'moment';

import { Inject, Optional, Pipe, PipeTransform } from '@angular/core';
import { NGX_MOMENT_OPTIONS, NgxMomentOptions } from './moment-options';
import { NGX_MOMENT_OPTIONS, NgxMomentOptions, applyOptions } from './moment-options';

@Pipe({ name: 'amDuration' })
export class DurationPipe implements PipeTransform {
allowedUnits: Array<string> = ['ss', 's', 'm', 'h', 'd', 'M'];

constructor(@Optional() @Inject(NGX_MOMENT_OPTIONS) momentOptions?: NgxMomentOptions) {
this._applyOptions(momentOptions);
}
Expand All @@ -19,18 +17,6 @@ export class DurationPipe implements PipeTransform {
}

private _applyOptions(momentOptions: NgxMomentOptions): void {
if (!momentOptions) {
return;
}

if (!!momentOptions.relativeTimeThresholdOptions) {
const units: Array<string> = Object.keys(momentOptions.relativeTimeThresholdOptions);
const filteredUnits: Array<string> = units.filter(
(unit) => this.allowedUnits.indexOf(unit) !== -1,
);
filteredUnits.forEach((unit) => {
moment.relativeTimeThreshold(unit, momentOptions.relativeTimeThresholdOptions[unit]);
});
}
applyOptions(momentOptions);
}
}
23 changes: 20 additions & 3 deletions src/moment-options.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { InjectionToken } from '@angular/core';
import * as moment from 'moment';

export const NGX_MOMENT_OPTIONS: InjectionToken<NgxMomentOptions> = new InjectionToken<
NgxMomentOptions
>('NGX_MOMENT_OPTIONS');
export const NGX_MOMENT_OPTIONS: InjectionToken<NgxMomentOptions> = new InjectionToken<NgxMomentOptions>(
'NGX_MOMENT_OPTIONS',
);

export interface NgxMomentOptions {
/**
Expand All @@ -16,3 +17,19 @@ export interface NgxMomentOptions {
*/
relativeTimeThresholdOptions: { [key: string]: number };
}

export function applyOptions(momentOptions: NgxMomentOptions): void {
const allowedUnits: Array<string> = ['ss', 's', 'm', 'h', 'd', 'M'];

if (!momentOptions) {
return;
}

if (!!momentOptions.relativeTimeThresholdOptions) {
const units: Array<string> = Object.keys(momentOptions.relativeTimeThresholdOptions);
const filteredUnits: Array<string> = units.filter((unit) => allowedUnits.indexOf(unit) !== -1);
filteredUnits.forEach((unit) => {
moment.relativeTimeThreshold(unit, momentOptions.relativeTimeThresholdOptions[unit]);
});
}
}
19 changes: 19 additions & 0 deletions src/time-ago.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NgZone } from '@angular/core';
import { TimeAgoPipe } from './time-ago.pipe';
import * as moment from 'moment';
import 'moment/min/locales';
import { NgxMomentOptions } from './moment-options';

declare var global: any;

Expand Down Expand Up @@ -130,5 +131,23 @@ describe('TimeAgoPipe', () => {
expect(pipe.transform(moment().add(1, 'h'), false, formatFnMock)).toBe('');
expect(pipe.transform(moment().add(3, 'h'), false, formatFnMock)).toBe('');
});

it(`should transform '50 minutes before' to 'an hour ago' with default 'relativeTimeThreshold'`, () => {
const pipe = new TimeAgoPipe(null, new NgZoneMock() as NgZone);
expect(pipe.transform(moment().subtract(50, 'minutes'))).toEqual('an hour ago');
});
});

describe('ctor with NgxMomentOptions', () => {
const momentOptions: NgxMomentOptions = {
relativeTimeThresholdOptions: {
m: 59,
},
};

it(`should transform '50 minutes before' to '50 minutes ago' when relativeTimeThreshold for 'm' unit is set to 59`, () => {
const pipe = new TimeAgoPipe(null, new NgZoneMock() as NgZone, momentOptions);
expect(pipe.transform(moment().subtract(50, 'minutes'))).toEqual('50 minutes ago');
});
});
});
23 changes: 21 additions & 2 deletions src/time-ago.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
/* ngx-moment (c) 2015, 2016 Uri Shaked / MIT Licence */

import { Pipe, ChangeDetectorRef, PipeTransform, OnDestroy, NgZone } from '@angular/core';
import {
Pipe,
ChangeDetectorRef,
PipeTransform,
OnDestroy,
NgZone,
Optional,
Inject,
} from '@angular/core';
import * as moment from 'moment';
import { applyOptions, NgxMomentOptions, NGX_MOMENT_OPTIONS } from './moment-options';

const momentConstructor = moment;

Expand All @@ -16,7 +25,13 @@ export class TimeAgoPipe implements PipeTransform, OnDestroy {
private lastText: string;
private formatFn: (m: moment.Moment) => string;

constructor(private cdRef: ChangeDetectorRef, private ngZone: NgZone) {}
constructor(
private cdRef: ChangeDetectorRef,
private ngZone: NgZone,
@Optional() @Inject(NGX_MOMENT_OPTIONS) momentOptions?: NgxMomentOptions,
) {
this._applyOptions(momentOptions);
}

format(m: moment.Moment) {
return m.from(momentConstructor(), this.lastOmitSuffix);
Expand Down Expand Up @@ -110,4 +125,8 @@ export class TimeAgoPipe implements PipeTransform, OnDestroy {
private getLocale(value: moment.MomentInput): string | null {
return moment.isMoment(value) ? value.locale() : moment.locale();
}

private _applyOptions(momentOptions: NgxMomentOptions): void {
applyOptions(momentOptions);
}
}