Skip to content

Commit

Permalink
Merge pull request #54 from josx/master
Browse files Browse the repository at this point in the history
feat(difference): add amDifference pipe
  • Loading branch information
urish authored Jul 29, 2016
2 parents 90506c0 + 287e4ac commit 01614e0
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ import {DurationPipe} from 'angular2-moment';

Prints `Uptime: 6 minutes`

## amDifference pipe

``` typescript
import {DifferencePipe} from 'angular2-moment';

@Component({
selector: 'app',
pipes: [DifferencePipe],
template: `
Expiration: <time>{{nextDay | amDifference: today :'days' : true}}</time> days
`
})
```
Prints `Expiration: 1 day`

Complete Example
----------------

Expand Down
41 changes: 41 additions & 0 deletions src/DifferencePipe.spec.ts
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('-');
});

});
});
18 changes: 18 additions & 0 deletions src/DifferencePipe.ts
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);
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './DateFormatPipe';
export * from './DurationPipe';
export * from './FromUnixPipe';
export * from './TimeAgoPipe';
export * from './DifferencePipe';

0 comments on commit 01614e0

Please sign in to comment.