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 Jul 31, 2019
1 parent 6237442 commit d40becb
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 11 deletions.
45 changes: 38 additions & 7 deletions src/from-utc.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -50,18 +50,49 @@ 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');
});

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);
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);
});

});

});
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 moment.utc(value, formats);
}
}
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 d40becb

Please sign in to comment.