diff --git a/.gitignore b/.gitignore index 766c1a7..d91535f 100644 --- a/.gitignore +++ b/.gitignore @@ -34,6 +34,9 @@ subtract.pipe.d.ts utc.pipe.js utc.pipe.js.map utc.pipe.d.ts +parse.pipe.js +parse.pipe.js.map +parse.pipe.d.ts *.spec.js *.spec.js.map *.spec.d.ts diff --git a/README.md b/README.md index 00f151c..e1dffbe 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,22 @@ Prints `Last updated: Same Day at 2:00 PM` Prints `Last updated: January 24, 2016` +## amParse pipe + +Parses a custom-formatted date into a moment object that can be used with the other pipes. + +``` typescript +@Component({ + selector: 'app', + template: ` + Last updated: {{'24/01/2014' | amParse:'DD/MM/YYYY' | amDateFormat:'LL'}} + ` +}) +``` + +Prints `Last updated: January 24, 2016` + + ## amFromUnix pipe ``` typescript diff --git a/package.json b/package.json index 337c44b..f806314 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,10 @@ "utc.pipe.js.map", "utc.pipe.d.ts", "utc.pipe.metadata.json", + "parse.pipe.js", + "parse.pipe.js.map", + "parse.pipe.d.ts", + "parse.pipe.metadata.json", "CHANGELOG.md" ], "scripts": { diff --git a/src/index.ts b/src/index.ts index 30c3346..0e0f4dc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,7 @@ export { DateFormatPipe } from './date-format.pipe'; export { DifferencePipe } from './difference.pipe'; export { DurationPipe } from './duration.pipe'; export { FromUnixPipe } from './from-unix.pipe'; +export { ParsePipe } from './parse.pipe'; export { MomentModule } from './moment.module'; export { SubtractPipe } from './subtract.pipe'; export { TimeAgoPipe } from './time-ago.pipe'; diff --git a/src/moment.module.ts b/src/moment.module.ts index 0cc8133..c68f76d 100644 --- a/src/moment.module.ts +++ b/src/moment.module.ts @@ -6,6 +6,7 @@ import { DateFormatPipe } from './date-format.pipe'; import { DifferencePipe } from './difference.pipe'; import { DurationPipe } from './duration.pipe'; import { FromUnixPipe } from './from-unix.pipe'; +import { ParsePipe } from './parse.pipe'; import { SubtractPipe } from './subtract.pipe'; import { TimeAgoPipe } from './time-ago.pipe'; import { UtcPipe } from './utc.pipe'; @@ -17,6 +18,7 @@ const ANGULAR_MOMENT_PIPES = [ DifferencePipe, DurationPipe, FromUnixPipe, + ParsePipe, SubtractPipe, TimeAgoPipe, UtcPipe diff --git a/src/parse.pipe.spec.ts b/src/parse.pipe.spec.ts new file mode 100644 index 0000000..ead7720 --- /dev/null +++ b/src/parse.pipe.spec.ts @@ -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'); + }); + + }); + +}); diff --git a/src/parse.pipe.ts b/src/parse.pipe.ts new file mode 100644 index 0000000..6764904 --- /dev/null +++ b/src/parse.pipe.ts @@ -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 = (moment).default || moment; + +@Pipe({ name: 'amParse' }) +export class ParsePipe implements PipeTransform { + transform(value: string, format: string): moment.Moment { + return moment(value, format); + } +} diff --git a/tsconfig.json b/tsconfig.json index 195abe9..3c4ef48 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -28,6 +28,8 @@ "src/from-unix.pipe.ts", "src/from-unix.pipe.spec.ts", "src/index.ts", + "src/parse.pipe.ts", + "src/parse.pipe.spec.ts", "src/moment.module.ts", "src/subtract.pipe.ts", "src/subtract.pipe.spec.ts",