From d40becb273b51ec2596bfb3f0caf69e80c364489 Mon Sep 17 00:00:00 2001 From: Diego Fernandez Date: Wed, 31 Jul 2019 17:50:55 +0100 Subject: [PATCH] feat: add format|formats to from-utc pipe and parse-pipe --- src/from-utc.pipe.spec.ts | 45 +++++++++++++++++++++++++++++++++------ src/from-utc.pipe.ts | 4 ++-- src/parse.pipe.spec.ts | 12 +++++++++++ src/parse.pipe.ts | 4 ++-- 4 files changed, 54 insertions(+), 11 deletions(-) diff --git a/src/from-utc.pipe.spec.ts b/src/from-utc.pipe.spec.ts index cae5ecf..5e47c6c 100644 --- a/src/from-utc.pipe.spec.ts +++ b/src/from-utc.pipe.spec.ts @@ -13,35 +13,35 @@ describe('UtcPipe', () => { }); it('should output an invalid momemt object for a null input', () => { - const utcDate = utcDatePipe.transform(null); + const utcDate = utcDatePipe.transform(null, 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); + const utcDate = utcDatePipe.transform(momentDate, null); 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); + const utcDate = utcDatePipe.transform(date, null); 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); + const utcDate = utcDatePipe.transform(dateString, null); 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); + const utcDate = utcDatePipe.transform(timestamp, null); expect(utcDate).toEqual(expect.any(moment)); expect(utcDate.isValid()).toBe(true); }); @@ -50,7 +50,7 @@ describe('UtcPipe', () => { const amDateFormat = new DateFormatPipe(); const datetimeString = '2016-12-31T23:00:00.000-01:00'; const momentFormatString = 'YYYY-MM-DD'; - const utcOutput = utcDatePipe.transform(datetimeString); + const utcOutput = utcDatePipe.transform(datetimeString, null); expect(amDateFormat.transform(utcOutput, momentFormatString)).toEqual('2017-01-01'); }); @@ -58,10 +58,41 @@ describe('UtcPipe', () => { const amDateFormat = new DateFormatPipe(); const datetimeString = '2016-12-31T23:00:00.000'; const momentFormatString = 'YYYY-MM-DD'; - const utcOutput = utcDatePipe.transform(datetimeString); + const utcOutput = utcDatePipe.transform(datetimeString, null); expect(amDateFormat.transform(utcOutput, momentFormatString)).toEqual('2016-12-31'); }); + it('should parse as UTC with a provided format', () => { + const datetimeString = '31/12/2016, 23:02:00'; + const momentFormatString = 'DD/MM/YYYY, HH:mm:ss'; + const utcOutput = utcDatePipe.transform(datetimeString, momentFormatString); + expect(utcOutput).toEqual(expect.any(moment)); + expect(utcOutput.isValid()).toBe(true); + + expect(utcOutput.year()).toBe(2016); + expect(utcOutput.month()).toBe(11); + expect(utcOutput.date()).toBe(31); + }); + + it('should parse as UTC with an array of provided formats', () => { + const datetimeString = '31st 12/2016'; + const momentFormatStrings = ['DD/MM/YYYY, HH:mm:ss', 'Do MM/YYYY']; + const utcOutput = utcDatePipe.transform(datetimeString, momentFormatStrings); + expect(utcOutput).toEqual(expect.any(moment)); + expect(utcOutput.isValid()).toBe(true); + + expect(utcOutput.year()).toBe(2016); + expect(utcOutput.month()).toBe(11); + expect(utcOutput.date()).toBe(31); + }); + + it('should output an invalid moment object for a different formatted input', () => { + const datetimeString = '31/12/2016, 23:02:00'; + const utcDate = utcDatePipe.transform(datetimeString, null); + expect(utcDate).toEqual(expect.any(moment)); + expect(utcDate.isValid()).toBe(false); + }); + }); }); diff --git a/src/from-utc.pipe.ts b/src/from-utc.pipe.ts index 5dde5a1..a9b478c 100644 --- a/src/from-utc.pipe.ts +++ b/src/from-utc.pipe.ts @@ -5,7 +5,7 @@ import * as moment from 'moment'; @Pipe({ name: 'amFromUtc' }) export class FromUtcPipe implements PipeTransform { - transform(value: any, ...args: string[]): any { - return moment.utc(value); + transform(value: any, formats: string|string[], ...args: string[]): any { + return moment.utc(value, formats); } } diff --git a/src/parse.pipe.spec.ts b/src/parse.pipe.spec.ts index d0743d5..43d98a5 100644 --- a/src/parse.pipe.spec.ts +++ b/src/parse.pipe.spec.ts @@ -33,6 +33,18 @@ describe('ParsePipe', () => { expect(amDateFormat.transform(parseOutput, momentFormatString)).toEqual('2016-02-01'); }); + it('should output a moment object for a string date with array of formats', () => { + const dateString = '15--09//13'; + const formatInputStrings = ['YYYY#MM#DD', 'YY--MM//DD']; + const parsedMoment = parsePipe.transform(dateString, formatInputStrings); + expect(parsedMoment).toEqual(expect.any(moment)); + expect(parsedMoment.isValid()).toBe(true); + + expect(parsedMoment.year()).toBe(2015); + expect(parsedMoment.month()).toBe(8); + expect(parsedMoment.date()).toBe(13); + }); + }); }); diff --git a/src/parse.pipe.ts b/src/parse.pipe.ts index 0b1ad25..edd5b58 100644 --- a/src/parse.pipe.ts +++ b/src/parse.pipe.ts @@ -5,7 +5,7 @@ const momentConstructor = moment; @Pipe({ name: 'amParse' }) export class ParsePipe implements PipeTransform { - transform(value: string, format: string): moment.Moment { - return momentConstructor(value, format); + transform(value: string, formats: string|string[]): moment.Moment { + return momentConstructor(value, formats); } }