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

Add UtcPipe to enable UTC mode in pipe chain. #121

Merged
merged 1 commit into from
Jan 20, 2017
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ add.pipe.d.ts
subtract.pipe.js
subtract.pipe.js.map
subtract.pipe.d.ts
utc.pipe.js
utc.pipe.js.map
utc.pipe.d.ts
*.spec.js
*.spec.js.map
*.spec.d.ts
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,21 @@ Use these pipes to perform date arithmetics. See [Momnet.js documentation](http:

*Example for amAdd/amSubtract is needed here, Pull Requests are welcome*

## amUtc pipe

Enables UTC mode for subsequent moment operations (such as displaying the time in UTC).

``` typescript
@Component({
selector: 'app',
template: `
Last updated: {{ '2016-12-31T23:00:00.000-01:00' | amUtc | amDateFormat: 'YYYY-MM-DD' }}
`
})
```

Prints `Last updated: 2017-01-01`

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

Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
"subtract.pipe.js.map",
"subtract.pipe.d.ts",
"subtract.pipe.metadata.json",
"utc.pipe.js",
"utc.pipe.js.map",
"utc.pipe.d.ts",
"utc.pipe.metadata.json",
"CHANGELOG.md"
],
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export { DurationPipe } from './duration.pipe';
export { FromUnixPipe } from './from-unix.pipe';
export { MomentModule } from './moment.module';
export { TimeAgoPipe } from './time-ago.pipe';
export { UtcPipe } from './utc.pipe';
4 changes: 3 additions & 1 deletion src/moment.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import { DifferencePipe } from './difference.pipe';
import { DurationPipe } from './duration.pipe';
import { FromUnixPipe } from './from-unix.pipe';
import { TimeAgoPipe } from './time-ago.pipe';
import { UtcPipe } from './utc.pipe';

const ANGULAR_MOMENT_PIPES = [CalendarPipe, DateFormatPipe, DifferencePipe, DurationPipe, FromUnixPipe, TimeAgoPipe];
const ANGULAR_MOMENT_PIPES = [CalendarPipe, DateFormatPipe, DifferencePipe, DurationPipe, FromUnixPipe, TimeAgoPipe,
UtcPipe];

@NgModule({
declarations: ANGULAR_MOMENT_PIPES,
Expand Down
61 changes: 61 additions & 0 deletions src/utc.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import 'es6-shim';
import 'reflect-metadata';
import * as moment from 'moment';
import { DateFormatPipe } from './date-format.pipe';
import { UtcPipe } from './utc.pipe';

describe('UtcPipe', () => {

describe('#transform', () => {

let utcDatePipe: UtcPipe;

beforeEach(() => {
utcDatePipe = new UtcPipe();
});

it('should output an invalid momemt object for a null input', () => {
const utcDate = utcDatePipe.transform(null);
expect(utcDate).toEqual(jasmine.any(moment));
expect(utcDate.isValid()).toBe(false);
});

it('should output a moment object for a moment input', () => {
const momentDate = moment();
const utcDate = utcDatePipe.transform(momentDate);
expect(utcDate).toEqual(jasmine.any(moment));
expect(utcDate.isValid()).toBe(true);
});

it('should output a moment object for a date input', () => {
const date = new Date();
const utcDate = utcDatePipe.transform(date);
expect(utcDate).toEqual(jasmine.any(moment));
expect(utcDate.isValid()).toBe(true);
});

it('should output a moment object for a string date', () => {
const dateString = '2016-01-01';
const utcDate = utcDatePipe.transform(dateString);
expect(utcDate).toEqual(jasmine.any(moment));
expect(utcDate.isValid()).toBe(true);
});

it('should output a moment object for a timestamp', () => {
const timestamp: number = Date.now();
const utcDate = utcDatePipe.transform(timestamp);
expect(utcDate).toEqual(jasmine.any(moment));
expect(utcDate.isValid()).toBe(true);
});

it('should be pipeable to amDateFormat', () => {
const amDateFormat = new DateFormatPipe();
const datetimeString = '2015-12-31T23:00:00.000-01:00';
const momentFormatString = 'YYYY-MM-DD';
const utcOutput = utcDatePipe.transform(datetimeString);
expect(amDateFormat.transform(utcOutput, momentFormatString)).toEqual('2016-01-01');
});

});

});
12 changes: 12 additions & 0 deletions src/utc.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Pipe, 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: 'amUtc' })
export class UtcPipe implements PipeTransform {
transform(value: Date | moment.Moment | string | number): moment.Moment {
return moment(value).utc();
}
}
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"src/duration.pipe.spec.ts",
"src/time-ago.pipe.ts",
"src/time-ago.pipe.spec.ts",
"src/utc.pipe.ts",
"src/utc.pipe.spec.ts",
"src/moment.module.ts",
"src/index.ts"
],
Expand Down