Skip to content

Commit

Permalink
feat: amLocal pipe
Browse files Browse the repository at this point in the history
* Adding amLocal pipe, per code found here: https://github.com/reberinformatik/angular2-moment/commit/0d545cc9a5e14913bddcb00221faaa7c4529ef17\#diff-bd4173df3baced7437261256a7041b36

* Fixing name in spec file.

* Fixing bug found in Local Pipe code.

* Fixing spec test.

* Fixing more class names.

* Ending file with new line per linter.
  • Loading branch information
benwilkins authored and urish committed Jul 17, 2017
1 parent 54fa255 commit 2936214
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 1 deletion.
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', () => {

This comment has been minimized.

Copy link
@connormlewis

connormlewis Aug 15, 2017

Contributor

Should this test pass locally? When I ran it, the expect failed and I got an output of 2015-12-31.

This comment has been minimized.

Copy link
@urish

urish Aug 16, 2017

Owner

probably a timezone issue - I guess the test should be fixed. What timezone are you at?

This comment has been minimized.

Copy link
@connormlewis

connormlewis Aug 16, 2017

Contributor

EDT -04:00

This comment has been minimized.

Copy link
@urish

urish Aug 20, 2017

Owner

Thanks, I will check it. Just found out how to configure TravisCI for different TimeZones:
https://stackoverflow.com/a/27052708/830623

I will try to set it up so in the future we'll know if some tests breaks on a specific timezone

This comment has been minimized.

Copy link
@urish

urish Aug 21, 2017

Owner

yes, issue reproduces on travis as well:

https://travis-ci.org/urish/angular2-moment/jobs/266749668

I will have a look into it now

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

0 comments on commit 2936214

Please sign in to comment.