Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(difference): add amDifference pipe #54

Merged
merged 1 commit into from
Jul 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';