-
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 #208 from StickNitro/master
feat: add amIsBefore and amIsAfter pipes for #118
- Loading branch information
Showing
7 changed files
with
140 additions
and
7 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
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,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(); | ||
}); | ||
}); | ||
}); |
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,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); | ||
} | ||
|
||
} |
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,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(); | ||
}); | ||
}); | ||
}); |
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,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); | ||
} | ||
|
||
} |
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