-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:urish/ngx-moment
- Loading branch information
Showing
13 changed files
with
256 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ node_modules | |
.ng_pkg_build | ||
*.log | ||
dist | ||
/package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,36 @@ | ||
import {Pipe, PipeTransform} from '@angular/core'; | ||
import * as moment from 'moment'; | ||
|
||
import { Inject, Optional, Pipe, PipeTransform } from '@angular/core'; | ||
import { NGX_MOMENT_OPTIONS, NgxMomentOptions } 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); | ||
} | ||
|
||
transform(value: any, ...args: string[]): string { | ||
if (typeof args === 'undefined' || args.length !== 1) { | ||
throw new Error('DurationPipe: missing required time unit argument'); | ||
} | ||
return moment.duration(value, args[0] as moment.unitOfTime.DurationConstructor).humanize(); | ||
} | ||
|
||
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]); | ||
}); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* tslint:disable:no-unused-variable */ | ||
import { IsAfterPipe } from './is-after.pipe'; | ||
|
||
describe('IsAfterPipe', () => { | ||
let pipe: IsAfterPipe; | ||
|
||
beforeEach(() => pipe = new IsAfterPipe()); | ||
|
||
describe('#transform', () => { | ||
|
||
it('should return true if value is after otherValue', () => { | ||
const test = new Date(2018, 11, 15, 0, 0, 0); | ||
const testDate1 = new Date(2018, 11, 13, 0, 0, 0); | ||
expect(pipe.transform(test, testDate1)).toBeTruthy(); | ||
}); | ||
|
||
it('should support passing "year", "month", "week", "day", etc as a unit parameter', () => { | ||
const test = new Date(2019, 11, 13, 12, 45, 45); | ||
const testDate1 = new Date(2018, 0, 13, 12, 45, 45); | ||
expect(pipe.transform(test, testDate1, 'year')).toBe(true); | ||
const testDate2 = new Date(2018, 1, 13, 12, 45, 45); | ||
expect(pipe.transform(test, testDate2, 'month')).toBe(true); | ||
const testDate3 = new Date(2018, 1, 10, 12, 45, 45); | ||
expect(pipe.transform(test, testDate3, 'day')).toBe(true); | ||
}); | ||
|
||
it('should return false if value is before otherValue', () => { | ||
const test = new Date(2018, 11, 13, 0, 0, 0); | ||
const testDate1 = new Date(2018, 11, 15, 0, 0, 0); | ||
expect(pipe.transform(test, testDate1)).toBeFalsy(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as moment from 'moment'; | ||
|
||
import { Pipe, PipeTransform } from '@angular/core'; | ||
|
||
const momentConstructor = moment; | ||
|
||
@Pipe({ | ||
name: 'amIsAfter' | ||
}) | ||
export class IsAfterPipe implements PipeTransform { | ||
|
||
transform(value: Date | moment.Moment, | ||
otherValue: Date | moment.Moment, | ||
unit?: moment.unitOfTime.StartOf): boolean { | ||
return momentConstructor(value).isAfter(momentConstructor(otherValue), unit); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { IsBeforePipe } from './is-before.pipe'; | ||
|
||
describe('IsBeforePipe', () => { | ||
let pipe: IsBeforePipe; | ||
|
||
beforeEach(() => pipe = new IsBeforePipe()); | ||
|
||
describe('#transform', () => { | ||
|
||
it('should return true if value is before otherValue', () => { | ||
const test = new Date(2018, 11, 13, 0, 0, 0); | ||
const testDate1 = new Date(2018, 11, 15, 0, 0, 0); | ||
expect(pipe.transform(test, testDate1)).toBeTruthy(); | ||
}); | ||
|
||
it('should support passing "year", "month", "week", "day", etc as a unit parameter', () => { | ||
const test = new Date(2018, 0, 13, 12, 45, 45); | ||
const testDate1 = new Date(2019, 0, 13, 12, 45, 45); | ||
expect(pipe.transform(test, testDate1, 'year')).toBe(true); | ||
const testDate2 = new Date(2018, 1, 13, 12, 45, 45); | ||
expect(pipe.transform(test, testDate2, 'month')).toBe(true); | ||
const testDate3 = new Date(2018, 1, 13, 12, 45, 45); | ||
expect(pipe.transform(test, testDate3, 'day')).toBe(true); | ||
}); | ||
|
||
it('should return false if value is after otherValue', () => { | ||
const test = new Date(2018, 11, 15, 0, 0, 0); | ||
const testDate1 = new Date(2018, 11, 13, 0, 0, 0); | ||
expect(pipe.transform(test, testDate1)).toBeFalsy(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as moment from 'moment'; | ||
|
||
import { Pipe, PipeTransform } from '@angular/core'; | ||
|
||
const momentConstructor = moment; | ||
|
||
@Pipe({ | ||
name: 'amIsBefore' | ||
}) | ||
export class IsBeforePipe implements PipeTransform { | ||
|
||
transform(value: Date | moment.Moment, | ||
otherValue: Date | moment.Moment, | ||
unit?: moment.unitOfTime.StartOf): boolean { | ||
return momentConstructor(value).isBefore(momentConstructor(otherValue), unit); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
|
||
export const NGX_MOMENT_OPTIONS: InjectionToken<NgxMomentOptions> = new InjectionToken<NgxMomentOptions>('NGX_MOMENT_OPTIONS'); | ||
|
||
export interface NgxMomentOptions { | ||
/** | ||
* relativeTimeThresholdOptions | ||
* @description Provides the `relativeTimeThreshold` units allowing a pipe to set the `moment.relativeTimeThreshold` values. | ||
* The `key` is a unit defined as one of `ss`, `s`, `m`, `h`, `d`, `M`. | ||
* @see https://momentjs.com/docs/#/customization/relative-time-threshold/ | ||
* @example by default more than 45 seconds is considered a minute, more than 22 hours is considered a day and so on. | ||
* So settings the unit 'm' to `59` will adjust the `relativeTimeThreshold` and consider more than 59 minutes | ||
* to be an hour (default is `45 minutes`) | ||
*/ | ||
relativeTimeThresholdOptions: { [key: string]: number }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters