-
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 #54 from josx/master
feat(difference): add amDifference pipe
- Loading branch information
Showing
4 changed files
with
75 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import {DifferencePipe} from './DifferencePipe'; | ||
import * as moment from 'moment'; | ||
|
||
describe('DifferencePipe', () => { | ||
var pipe: DifferencePipe; | ||
|
||
beforeEach(() => pipe = new DifferencePipe()); | ||
|
||
describe('#transform', () => { | ||
|
||
it('should take the difference of two dates in milliseconds', () => { | ||
let today = new Date(2012, 0, 22, 0, 0, 0); | ||
let testDate = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 13, 33, 33); | ||
expect(pipe.transform(testDate, today)).toBe(48813000); | ||
}); | ||
|
||
it('should support passing "years", "months", "days", etc as a units parameter', () => { | ||
let test = new Date(2012, 0, 22, 4, 46, 54); | ||
let testDate1 = new Date(2013, 0, 22, 4, 46, 54); | ||
expect(pipe.transform(testDate1, test, 'years')).toBe(1); | ||
let testDate2 = new Date(2012, 1, 22, 4, 46, 54); | ||
expect(pipe.transform(testDate2, test, 'months')).toBe(1); | ||
let testDate3 = new Date(2012, 0, 23, 4, 46, 54); | ||
expect(pipe.transform(testDate3, test, 'days')).toBe(1); | ||
}); | ||
|
||
it('should allow rounding to be disabled via parameter', () => { | ||
let test = new Date(2012, 0, 22, 4, 46, 54); | ||
let testDate1 = new Date(test.getFullYear() + 1, test.getMonth() + 6, test.getDate()); | ||
expect(pipe.transform(testDate1, test, 'years')).toBe(1); | ||
expect(pipe.transform(testDate1, test, 'years', true)).toBeCloseTo(1.5, 1); | ||
}); | ||
|
||
it('dates from the future should return negative values', () => { | ||
let today = new Date(2012, 0, 22, 4, 46, 54); | ||
let testDate = new Date(2013, 0, 22, 4, 46, 54); | ||
expect(String(pipe.transform(today, testDate))).toContain('-'); | ||
}); | ||
|
||
}); | ||
}); |
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 @@ | ||
/* angular2-moment (c) 2015, 2016 Uri Shaked / MIT Licence */ | ||
|
||
import {Pipe, ChangeDetectorRef, PipeTransform} from '@angular/core'; | ||
import * as moment from 'moment'; | ||
|
||
// under systemjs, moment is actually exported as the default export, so we account for that | ||
const momentConstructor: (value?: any) => moment.Moment = (<any>moment).default || moment; | ||
|
||
@Pipe({ name: 'amDifference', pure: false }) | ||
export class DifferencePipe implements PipeTransform { | ||
transform(value: Date | moment.Moment, otherValue: Date | moment.Moment, unit?: string, precision?: boolean): number { | ||
|
||
let date = momentConstructor(value); | ||
let date2 = (otherValue !== null) ? momentConstructor(otherValue) : momentConstructor(); | ||
|
||
return date.diff(date2, unit, precision); | ||
} | ||
} |
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