From 4c476bbef18f93fc94427e8ffc187a503a5974dc Mon Sep 17 00:00:00 2001 From: StickNitro Date: Thu, 13 Dec 2018 11:09:36 +0000 Subject: [PATCH 1/3] feat: add amIsBefore and amIsAfter pipes for #118 --- README.md | 27 +++++++++++++++++++++++++++ src/index.ts | 2 ++ src/is-after.pipe.spec.ts | 33 +++++++++++++++++++++++++++++++++ src/is-after.pipe.ts | 18 ++++++++++++++++++ src/is-before.pipe.spec.ts | 32 ++++++++++++++++++++++++++++++++ src/is-before.pipe.ts | 18 ++++++++++++++++++ src/moment.module.ts | 23 ++++++++++++++--------- 7 files changed, 144 insertions(+), 9 deletions(-) create mode 100644 src/is-after.pipe.spec.ts create mode 100644 src/is-after.pipe.ts create mode 100644 src/is-before.pipe.spec.ts create mode 100644 src/is-before.pipe.ts diff --git a/README.md b/README.md index 4ee581a..80f7147 100644 --- a/README.md +++ b/README.md @@ -305,6 +305,33 @@ Parses a string but keeps the resulting Moment object in a fixed-offset timezone Prints `Last updated: Saturday, December 31, 2016 11:00 PM (-03:00)` +## amIsBefore and amIsAfter pipe + +Check if a moment is before another moment. Supports limiting granularity to a unit other than milliseconds, pass the units as second parameter + +```typescript +@Component({ + selector: 'app', + template: ` + Today is before tomorrow: {{ today | amIsBefore:tomorrow:'day' }} + ` +}) +``` + +Prints `Today is before tomorrow: true` + +```typescript +@Component({ + selector: 'app', + template: ` + Tomorrow is after today: {{ tomorrow | amIsAfter:today:'day' }} + ` +}) +``` + +Prints `Tomorrow is after today: true` + + Complete Example ---------------- diff --git a/src/index.ts b/src/index.ts index 8bec771..b9e011d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,3 +13,5 @@ export { FromUtcPipe } from './from-utc.pipe'; export { LocalTimePipe } from './local.pipe'; export { LocalePipe } from './locale.pipe'; export { ParseZonePipe } from './parse-zone.pipe'; +export { IsBeforePipe } from './is-before.pipe'; +export { IsAfterPipe } from './is-after.pipe'; diff --git a/src/is-after.pipe.spec.ts b/src/is-after.pipe.spec.ts new file mode 100644 index 0000000..695cf8b --- /dev/null +++ b/src/is-after.pipe.spec.ts @@ -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(); + }); + }); +}); diff --git a/src/is-after.pipe.ts b/src/is-after.pipe.ts new file mode 100644 index 0000000..3364df1 --- /dev/null +++ b/src/is-after.pipe.ts @@ -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); + } + +} diff --git a/src/is-before.pipe.spec.ts b/src/is-before.pipe.spec.ts new file mode 100644 index 0000000..3cd9a72 --- /dev/null +++ b/src/is-before.pipe.spec.ts @@ -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(); + }); + }); +}); diff --git a/src/is-before.pipe.ts b/src/is-before.pipe.ts new file mode 100644 index 0000000..d8f53ac --- /dev/null +++ b/src/is-before.pipe.ts @@ -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); + } + +} diff --git a/src/moment.module.ts b/src/moment.module.ts index 9a2229c..f494d5c 100644 --- a/src/moment.module.ts +++ b/src/moment.module.ts @@ -1,19 +1,20 @@ -import { NgModule } from '@angular/core'; - import { AddPipe } from './add.pipe'; import { CalendarPipe } from './calendar.pipe'; import { DateFormatPipe } from './date-format.pipe'; import { DifferencePipe } from './difference.pipe'; import { DurationPipe } from './duration.pipe'; import { FromUnixPipe } from './from-unix.pipe'; -import { ParsePipe } from './parse.pipe'; -import { SubtractPipe } from './subtract.pipe'; -import { TimeAgoPipe } from './time-ago.pipe'; -import { UtcPipe } from './utc.pipe'; import { FromUtcPipe } from './from-utc.pipe'; +import { IsAfterPipe } from './is-after.pipe'; +import { IsBeforePipe } from './is-before.pipe'; import { LocalTimePipe } from './local.pipe'; import { LocalePipe } from './locale.pipe'; +import { NgModule } from '@angular/core'; +import { ParsePipe } from './parse.pipe'; import { ParseZonePipe } from './parse-zone.pipe'; +import { SubtractPipe } from './subtract.pipe'; +import { TimeAgoPipe } from './time-ago.pipe'; +import { UtcPipe } from './utc.pipe'; const ANGULAR_MOMENT_PIPES = [ AddPipe, @@ -29,11 +30,15 @@ const ANGULAR_MOMENT_PIPES = [ FromUtcPipe, LocalTimePipe, LocalePipe, - ParseZonePipe + ParseZonePipe, + IsBeforePipe, + IsAfterPipe ]; @NgModule({ - declarations: ANGULAR_MOMENT_PIPES, - exports: ANGULAR_MOMENT_PIPES + declarations: [ + ANGULAR_MOMENT_PIPES, + exports + ] }) export class MomentModule { } From cc241e6cae3cf913126c7228e635b12037ceb362 Mon Sep 17 00:00:00 2001 From: StickNitro Date: Thu, 13 Dec 2018 11:20:06 +0000 Subject: [PATCH 2/3] fix: broken module exports --- src/moment.module.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/moment.module.ts b/src/moment.module.ts index f494d5c..e5da0ea 100644 --- a/src/moment.module.ts +++ b/src/moment.module.ts @@ -37,8 +37,10 @@ const ANGULAR_MOMENT_PIPES = [ @NgModule({ declarations: [ - ANGULAR_MOMENT_PIPES, - exports + ANGULAR_MOMENT_PIPES + ], + exports: [ + ANGULAR_MOMENT_PIPES ] }) export class MomentModule { } From d6b492c935b5c464d61726a3d200192c01221c54 Mon Sep 17 00:00:00 2001 From: StickNitro Date: Mon, 17 Dec 2018 11:31:48 +0000 Subject: [PATCH 3/3] revert: undo change in module declaration --- src/moment.module.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/moment.module.ts b/src/moment.module.ts index e5da0ea..cf0503f 100644 --- a/src/moment.module.ts +++ b/src/moment.module.ts @@ -36,11 +36,7 @@ const ANGULAR_MOMENT_PIPES = [ ]; @NgModule({ - declarations: [ - ANGULAR_MOMENT_PIPES - ], - exports: [ - ANGULAR_MOMENT_PIPES - ] + declarations: ANGULAR_MOMENT_PIPES, + exports: ANGULAR_MOMENT_PIPES }) export class MomentModule { }