-
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 pull request #209 from StickNitro/master
issue #202 ability to provide options to the MomentModule
- Loading branch information
Showing
7 changed files
with
104 additions
and
4 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,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