-
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.
Add ParsePipe to enable parsing of custom-formatted date string (#148)
- Loading branch information
Showing
8 changed files
with
80 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
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,40 @@ | ||
import 'es6-shim'; | ||
import 'reflect-metadata'; | ||
import * as moment from 'moment'; | ||
import { DateFormatPipe } from './date-format.pipe'; | ||
import { ParsePipe } from './parse.pipe'; | ||
|
||
describe('ParsePipe', () => { | ||
|
||
describe('#transform', () => { | ||
|
||
let parsePipe: ParsePipe; | ||
|
||
beforeEach(() => { | ||
parsePipe = new ParsePipe(); | ||
}); | ||
|
||
it('should output a moment object for a string date', () => { | ||
const dateString = '2015#09#13'; | ||
const formatInputString = 'YYYY#MM#DD'; | ||
const parsedMoment = parsePipe.transform(dateString, formatInputString); | ||
expect(parsedMoment).toEqual(jasmine.any(moment)); | ||
expect(parsedMoment.isValid()).toBe(true); | ||
|
||
expect(parsedMoment.year()).toBe(2015); | ||
expect(parsedMoment.month()).toBe(8); | ||
expect(parsedMoment.date()).toBe(13); | ||
}); | ||
|
||
it('should be pipeable to amDateFormat', () => { | ||
const amDateFormat = new DateFormatPipe(); | ||
const datetimeString = '01/02/2016'; | ||
const formatInputString = 'DD/MM/YYYY'; | ||
const momentFormatString = 'YYYY-MM-DD'; | ||
const parseOutput = parsePipe.transform(datetimeString, formatInputString); | ||
expect(amDateFormat.transform(parseOutput, momentFormatString)).toEqual('2016-02-01'); | ||
}); | ||
|
||
}); | ||
|
||
}); |
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,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: 'amParse' }) | ||
export class ParsePipe implements PipeTransform { | ||
transform(value: string, format: string): moment.Moment { | ||
return moment(value, format); | ||
} | ||
} |
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