Skip to content
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
merged 5 commits into from
Jan 13, 2019

Conversation

StickNitro
Copy link

Add ability to provide options to the moment module using the forRoot convention to supply options globally or per pipe

add `forRoot` to `MomentModule` to allow passing of `NgxMomentOptions`, can be easily extended to support other options (both global and local to pipes)
added optional parameter to constructor of DurationPipe to provide options and allows pipe to specify `relativeTimeThreshold` units
added tests to support
Copy link
Owner

@urish urish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution! Please see my review comments in the code :)

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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why triple negation? I'd rather use single negation

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I felt triple negation was safer, equivalent to if (!(!!momentOptions)). If you prefer single negation ill make the change

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change made

beforeEach(() => pipe = new DurationPipe(momentOptions));

it(`should convert '50 minutes' to '50 minutes' when relativeTimeThreshold for 'm' unit is set to 59`, () => {
expect(pipe.transform(50, 'minutes')).toEqual('50 minutes');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

* @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`)
*/
Copy link
Owner

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

README updated in next commit

@@ -0,0 +1,16 @@
import { InjectionToken } from '@angular/core';

export const NGX_MOMENT_OPTIONS: InjectionToken<string> = new InjectionToken<string>('NGX_MOMENT_OPTIONS');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to docs, it seems like the type parameter for InjectionToken should be the interface name (i.e. NgxMomentOptions):

https://angular.io/api/core/InjectionToken

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, my bad and good spot, updated in next commit.

@urish urish merged commit 163348e into urish:master Jan 13, 2019
@urish
Copy link
Owner

urish commented Jan 13, 2019

Thank you!

@deeg
Copy link

deeg commented Mar 1, 2019

@urish , also looking for this. Any chance you could cut a new release to include this?

@urish
Copy link
Owner

urish commented Mar 2, 2019

@deeg I plan to, when I get a chance, I'm currently on a long vacation in Japan

@deeg
Copy link

deeg commented Mar 4, 2019

@urish , no rush, have fun on the vacation! I hear soon all the cherry blossoms will be in full force. Will you get to enjoy that?

@urish
Copy link
Owner

urish commented Mar 7, 2019

@deeg Hopefully so! You can read about our journey so far in my blog post.

Regarding this PR - released as part of 3.4.0

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants