Skip to content

Commit

Permalink
feat: add format|formats to from-utc pipe and parse-pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
gigadie committed Aug 6, 2019
1 parent 6237442 commit 4790ced
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
31 changes: 31 additions & 0 deletions src/from-utc.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,37 @@ describe('UtcPipe', () => {
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);
expect(utcDate).toEqual(expect.any(moment));
expect(utcDate.isValid()).toBe(false);
});

});

});
4 changes: 2 additions & 2 deletions src/from-utc.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 formats ? moment.utc(value, formats) : moment.utc(value);
}
}
12 changes: 12 additions & 0 deletions src/parse.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

});

});
4 changes: 2 additions & 2 deletions src/parse.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 4790ced

Please sign in to comment.