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

Adds amFromUtc pipe #163

Merged
merged 1 commit into from
Aug 15, 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 @@ -34,6 +34,9 @@ subtract.pipe.d.ts
utc.pipe.js
utc.pipe.js.map
utc.pipe.d.ts
from-utc.pipe.js
from-utc.pipe.js.map
from-utc.pipe.d.ts
parse.pipe.js
parse.pipe.js.map
parse.pipe.d.ts
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,21 @@ Prints `Expiration: 2017-03-17 18:55`
```
Prints `Last updated: 2012-03-17 16:55`

## amFromUtc pipe

Parses the date as UTC and enables mode for subsequent moment operations (such as displaying the time in UTC). This can be combined with `amLocal` to display a UTC date in local time.

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

Prints `Last updated: 2017-01-01`

## amUtc pipe

Enables UTC mode for subsequent moment operations (such as displaying the time in UTC).
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
"utc.pipe.js.map",
"utc.pipe.d.ts",
"utc.pipe.metadata.json",
"from-utc.pipe.js",
"from-utc.pipe.js.map",
"from-utc.pipe.d.ts",
"from-utc.pipe.metadata.json",
"parse.pipe.js",
"parse.pipe.js.map",
"parse.pipe.d.ts",
Expand Down
69 changes: 69 additions & 0 deletions src/from-utc.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import 'es6-shim';
import 'reflect-metadata';
import * as moment from 'moment';
import { DateFormatPipe } from './date-format.pipe';
import { FromUtcPipe } from './from-utc.pipe';

describe('UtcPipe', () => {

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

let utcDatePipe: FromUtcPipe;

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

it('should output an invalid momemt object for a null input', () => {
const utcDate = utcDatePipe.transform(null);
expect(utcDate).toEqual(expect.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(expect.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(expect.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(expect.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(expect.any(moment));
expect(utcDate.isValid()).toBe(true);
});

it('should parse with provided timezone and format to UTC', () => {
const amDateFormat = new DateFormatPipe();
const datetimeString = '2016-12-31T23:00:00.000-01:00';
const momentFormatString = 'YYYY-MM-DD';
const utcOutput = utcDatePipe.transform(datetimeString);
expect(amDateFormat.transform(utcOutput, momentFormatString)).toEqual('2017-01-01');
});

it('should parse as UTC without provided timezone', () => {
const amDateFormat = new DateFormatPipe();
const datetimeString = '2016-12-31T23:00:00.000';
const momentFormatString = 'YYYY-MM-DD';
const utcOutput = utcDatePipe.transform(datetimeString);
expect(amDateFormat.transform(utcOutput, momentFormatString)).toEqual('2016-12-31');
});

});

});
11 changes: 11 additions & 0 deletions src/from-utc.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* angular2-moment (c) 2015, 2016 Uri Shaked / MIT Licence */

import {Pipe, PipeTransform} from '@angular/core';
import * as moment from 'moment';

@Pipe({ name: 'amFromUtc' })
export class FromUtcPipe implements PipeTransform {
transform(value: any, ...args: string[]): any {
return moment.utc(value);
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export { MomentModule } from './moment.module';
export { SubtractPipe } from './subtract.pipe';
export { TimeAgoPipe } from './time-ago.pipe';
export { UtcPipe } from './utc.pipe';
import { FromUtcPipe } from './from-utc.pipe';
export { LocalTimePipe } from './local.pipe';
export { LocalePipe } from './locale.pipe';
2 changes: 2 additions & 0 deletions src/moment.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ParsePipe } from './parse.pipe';
import { SubtractPipe } from './subtract.pipe';
import { TimeAgoPipe } from './time-ago.pipe';
import { UtcPipe } from './utc.pipe';
import { FromUtcPipe } from './from-utc.pipe';
import { LocalTimePipe } from './local.pipe';
import { LocalePipe } from './locale.pipe';

Expand All @@ -24,6 +25,7 @@ const ANGULAR_MOMENT_PIPES = [
SubtractPipe,
TimeAgoPipe,
UtcPipe,
FromUtcPipe,
LocalTimePipe,
LocalePipe
];
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"src/time-ago.pipe.spec.ts",
"src/utc.pipe.ts",
"src/utc.pipe.spec.ts",
"src/from-utc.pipe.ts",
"src/from-utc.pipe.spec.ts",
"src/local.pipe.ts",
"src/local.pipe.spec.ts",
"src/locale.pipe.ts",
Expand Down