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 { }