-
Notifications
You must be signed in to change notification settings - Fork 154
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
issue #202 ability to provide options to the MomentModule #209
Merged
+104
−4
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3b5f2b4
refactor: add package-lock.json to gitignore
old-guy-coder 7f6a8c4
feat: ability to specify relativeTimeThreshold for issue #202
old-guy-coder 9892011
fix: remove redundant @type JSDoc which caused build failure
old-guy-coder d69c14b
fix(#209 pr): update README with details of changes
old-guy-coder 63055b0
fix(#209 pr): replace triple negation with single negation
old-guy-coder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`) | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for adding this doc comment. Can you please also add a note to the README explaining this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. README updated in next commit |
||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍