-
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.
* Parse input as UTC formatted date using moment.utc()
- Loading branch information
1 parent
2dae7ef
commit 253a041
Showing
8 changed files
with
107 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
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,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'); | ||
}); | ||
|
||
}); | ||
|
||
}); |
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,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); | ||
} | ||
} |
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
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