From 8a2e4ffc79a5218535481c9561fef9aa232e06b5 Mon Sep 17 00:00:00 2001 From: Stanislaus Date: Wed, 24 Feb 2016 14:05:51 -0800 Subject: [PATCH 1/2] feat(FromUnixPipe):Add unix pipe to parse unix timestamp to moment --- FromUnixPipe.spec.ts | 28 ++++++++++++++++++++++++++++ FromUnixPipe.ts | 14 ++++++++++++++ README.md | 16 ++++++++++++++++ package.json | 2 ++ 4 files changed, 60 insertions(+) create mode 100644 FromUnixPipe.spec.ts create mode 100644 FromUnixPipe.ts diff --git a/FromUnixPipe.spec.ts b/FromUnixPipe.spec.ts new file mode 100644 index 0000000..3f9bdbb --- /dev/null +++ b/FromUnixPipe.spec.ts @@ -0,0 +1,28 @@ +import 'es6-shim'; +import 'reflect-metadata'; +import * as moment from 'moment'; +import {FromUnixPipe} from './FromUnixPipe'; +import {DateFormatPipe} from './DateFormatPipe'; + +describe('FromUnixPipe', () => { + describe('#transform', () => { + it('should parse a unix timestamp number to moment', () => { + const pipe = new FromUnixPipe(); + const result = pipe.transform(1456263980); + expect(result).toEqual(moment.unix(1456263980)); + }); + + it('should parse a unix timestamp string to moment', () => { + const pipe = new FromUnixPipe(); + const result = pipe.transform('1456263980'); + expect(result).toEqual(moment.unix(1456263980)); + }); + + it('should format a unix timestamp', () => { + const unixPipe = new FromUnixPipe(), + datePipe = new DateFormatPipe(); + const result = datePipe.transform(unixPipe.transform(1456263980), ['hh:mmA']); + expect(result).toBe('01:46PM'); + }); + }); +}); diff --git a/FromUnixPipe.ts b/FromUnixPipe.ts new file mode 100644 index 0000000..e7f2893 --- /dev/null +++ b/FromUnixPipe.ts @@ -0,0 +1,14 @@ +/* angular2-moment (c) 2015, 2016 Uri Shaked / MIT Licence */ + +import {Pipe, ChangeDetectorRef, PipeTransform} from 'angular2/core'; +import * as moment from 'moment'; + +@Pipe({ name: 'amFromUnix', pure: false }) +export class FromUnixPipe implements PipeTransform { + transform(value: any, args?: string[]): any { + if (typeof value === 'string') { + value = +value; + } + return moment.unix(value); + } +} diff --git a/README.md b/README.md index 04fb2df..74c739f 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,22 @@ import {DateFormatPipe} from 'angular2-moment'; Prints `Last updated: January 24, 2016` +## amFromUnix pipe + +``` typescript +import {DateFormatPipe, FromUnixPipe} from 'angular2-moment'; + +@Component({ + selector: 'app', + pipes: [DateFormatPipe, FromUnixPipe], + template: ` + Last updated: + ` +}) +``` + +Prints `Last updated: 01:46PM` + Complete Example ---------------- diff --git a/package.json b/package.json index ff68074..56b0298 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,8 @@ "TimeAgoPipe.js.map", "TimeAgoPipe.d.ts", "TimeAgoPipe.ts", + "FromUnixPipe.d.ts", + "FromUnixPipe.ts", "CHANGELOG.md" ], "scripts": { From 3380fab298b435ab450bfd8640b577f9ea6a0471 Mon Sep 17 00:00:00 2001 From: Stanislaus Date: Wed, 24 Feb 2016 14:20:34 -0800 Subject: [PATCH 2/2] fix(FromUnixPipe.spec): unit test locale issue --- FromUnixPipe.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FromUnixPipe.spec.ts b/FromUnixPipe.spec.ts index 3f9bdbb..5febf57 100644 --- a/FromUnixPipe.spec.ts +++ b/FromUnixPipe.spec.ts @@ -22,7 +22,7 @@ describe('FromUnixPipe', () => { const unixPipe = new FromUnixPipe(), datePipe = new DateFormatPipe(); const result = datePipe.transform(unixPipe.transform(1456263980), ['hh:mmA']); - expect(result).toBe('01:46PM'); + expect(result.length).toEqual(7); }); }); });