Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(FromUnixPipe):Add unix pipe to parse unix timestamp to moment #16

Merged
merged 2 commits into from
Feb 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions FromUnixPipe.spec.ts
Original file line number Diff line number Diff line change
@@ -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.length).toEqual(7);
});
});
});
14 changes: 14 additions & 0 deletions FromUnixPipe.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: <time>{{ (1456263980 | amFromUnix) | amDateFormat:'hh:mmA'}}</time>
`
})
```

Prints `Last updated: 01:46PM`

Complete Example
----------------

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"TimeAgoPipe.js.map",
"TimeAgoPipe.d.ts",
"TimeAgoPipe.ts",
"FromUnixPipe.d.ts",
"FromUnixPipe.ts",
"CHANGELOG.md"
],
"scripts": {
Expand Down