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

Adding amLocal pipe, per code found here: https://github.com/reberinf… #153

Merged
merged 7 commits into from
Jul 17, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ utc.pipe.d.ts
parse.pipe.js
parse.pipe.js.map
parse.pipe.d.ts
local.pipe.js
local.pipe.js.map
local.pipe.d.ts
locale.pipe.js
locale.pipe.js.map
locale.pipe.d.ts
Expand Down
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,21 @@ Parses a custom-formatted date into a moment object that can be used with the ot

Prints `Last updated: January 24, 2016`

## amLocal pipe

Converts UTC time to local time.

``` typescript
@Component({
selector: 'app',
template: `
Last updated: {{mydate | amLocal | amDateFormat: 'YYYY-MM-DD HH:mm'}}
`
})
```

Prints `Last updated 2016-01-24 12:34`

## amLocale pipe

To be used with amDateFormat pipe in order to change locale.
Expand All @@ -176,7 +191,6 @@ To be used with amDateFormat pipe in order to change locale.

Prints `Last updated: January 24th 2016, 2:23:45 pm`


## amFromUnix pipe

``` typescript
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
"parse.pipe.js.map",
"parse.pipe.d.ts",
"parse.pipe.metadata.json",
"local.pipe.js",
"local.pipe.js.map",
"local.pipe.d.ts",
"local.pipe.metadata.json",
"locale.pipe.js",
"locale.pipe.js.map",
"locale.pipe.d.ts",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export { MomentModule } from './moment.module';
export { SubtractPipe } from './subtract.pipe';
export { TimeAgoPipe } from './time-ago.pipe';
export { UtcPipe } from './utc.pipe';
export { LocalTimePipe } from './local.pipe';
export { LocalePipe } from './locale.pipe';
61 changes: 61 additions & 0 deletions src/local.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import 'es6-shim';
import 'reflect-metadata';
import * as moment from 'moment';
import { DateFormatPipe } from './date-format.pipe';
import { LocalTimePipe } from './local.pipe';

describe('LocalPipe', () => {

describe('#transform', () => {

let localPipe: LocalTimePipe;

beforeEach(() => {
localPipe = new LocalTimePipe();
});

it('should output an invalid momemt object for a null input', () => {
const localDate = localPipe.transform(null);
expect(localDate).toEqual(jasmine.any(moment));
expect(localDate.isValid()).toBe(false);
});

it('should output a moment object for a moment input', () => {
const momentDate = moment();
const localDate = localPipe.transform(momentDate);
expect(localDate).toEqual(jasmine.any(moment));
expect(localDate.isValid()).toBe(true);
});

it('should output a moment object for a date input', () => {
const date = new Date();
const localDate = localPipe.transform(date);
expect(localDate).toEqual(jasmine.any(moment));
expect(localDate.isValid()).toBe(true);
});

it('should output a moment object for a string date', () => {
const dateString = '2016-01-01';
const localDate = localPipe.transform(dateString);
expect(localDate).toEqual(jasmine.any(moment));
expect(localDate.isValid()).toBe(true);
});

it('should output a moment object for a timestamp', () => {
const timestamp: number = Date.now();
const localDate = localPipe.transform(timestamp);
expect(localDate).toEqual(jasmine.any(moment));
expect(localDate.isValid()).toBe(true);
});

it('should be pipeable to amDateFormat', () => {
const amDateFormat = new DateFormatPipe();
const datetimeString = '2015-12-31T23:00:00.000-01:00';
const momentFormatString = 'YYYY-MM-DD';
const localOutput = localPipe.transform(datetimeString);
expect(amDateFormat.transform(localOutput, momentFormatString)).toEqual('2016-01-01');
});

});

});
12 changes: 12 additions & 0 deletions src/local.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Pipe, PipeTransform } from '@angular/core';
import * as moment from 'moment';

// under systemjs, moment is actually exported as the default export, so we account for that
const momentConstructor: (value?: any) => moment.Moment = (<any>moment).default || moment;

@Pipe({ name: 'amLocal' })
export class LocalTimePipe implements PipeTransform {
transform(value: Date | moment.Moment | string | number): moment.Moment {
return moment(value).local();
}
}
2 changes: 2 additions & 0 deletions src/moment.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ParsePipe } from './parse.pipe';
import { SubtractPipe } from './subtract.pipe';
import { TimeAgoPipe } from './time-ago.pipe';
import { UtcPipe } from './utc.pipe';
import { LocalTimePipe } from './local.pipe';
import { LocalePipe } from './locale.pipe';

const ANGULAR_MOMENT_PIPES = [
Expand All @@ -23,6 +24,7 @@ const ANGULAR_MOMENT_PIPES = [
SubtractPipe,
TimeAgoPipe,
UtcPipe,
LocalTimePipe,
LocalePipe
];

Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"src/time-ago.pipe.spec.ts",
"src/utc.pipe.ts",
"src/utc.pipe.spec.ts",
"src/local.pipe.ts",
"src/local.pipe.spec.ts",
"src/locale.pipe.ts",
"src/locale.pipe.spec.ts"
],
Expand Down