Skip to content

Commit

Permalink
amLocale pipe implemented (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
FallenRiteMonk authored and urish committed Jul 14, 2017
1 parent a0978d4 commit 2d33056
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 2 deletions.
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
locale.pipe.js
locale.pipe.js.map
locale.pipe.d.ts
*.spec.js
*.spec.js.map
*.spec.d.ts
Expand Down
15 changes: 15 additions & 0 deletions 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`

## amLocale pipe

To be used with amDateFormat pipe in order to change locale.

``` typescript
@Component({
selector: 'app',
template: `
Last updated: {{'2016-01-24 14:23:45' | amLocale:'en' | amDateFormat:'MMMM Do YYYY, h:mm:ss a'}}
`
})
```

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


## amFromUnix pipe

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",
"locale.pipe.js",
"locale.pipe.js.map",
"locale.pipe.d.ts",
"locale.pipe.metadata.json",
"CHANGELOG.md"
],
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export { MomentModule } from './moment.module';
export { SubtractPipe } from './subtract.pipe';
export { TimeAgoPipe } from './time-ago.pipe';
export { UtcPipe } from './utc.pipe';
export { LocalePipe } from './locale.pipe';
44 changes: 44 additions & 0 deletions src/locale.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'es6-shim';
import 'reflect-metadata';
import * as moment from 'moment';
import { DateFormatPipe } from './date-format.pipe';
import { LocalePipe } from './locale.pipe';

describe('LocalePipe', () => {

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

let localePipe: LocalePipe;

beforeEach(() => {
localePipe = new LocalePipe();
});

it('should output a moment object for a string date', () => {
const datetimeString = '2016-01-24 01:23:45';
const langKeyString1 = 'en';
const langKeyString2 = 'de';
const parsedMoment1 = localePipe.transform(datetimeString, langKeyString1);
const parsedMoment2 = localePipe.transform(datetimeString, langKeyString2);
expect(parsedMoment1).toEqual(jasmine.any(moment));
expect(parsedMoment2).toEqual(jasmine.any(moment));
expect(parsedMoment1.isValid()).toBe(true);
expect(parsedMoment2.isValid()).toBe(true);
});

it('should be pipeable to amDateFormat', () => {
const amDateFormat = new DateFormatPipe();
const datetimeString = '2016-01-24 14:23:45';
const langKeyString1 = 'en';
const langKeyString2 = 'de';
const momentFormatString1 = 'MMMM Do YYYY, h:mm:ss a';
const momentFormatString2 = 'MMMM Do YYYY, HH:mm:ss';
const parseOutput1 = localePipe.transform(datetimeString, langKeyString1);
const parseOutput2 = localePipe.transform(datetimeString, langKeyString2);
expect(amDateFormat.transform(parseOutput1, momentFormatString1)).toEqual('January 24th 2016, 2:23:45 pm');
expect(amDateFormat.transform(parseOutput2, momentFormatString2)).toEqual('Januar 24. 2016, 14:23:45');
});

});

});
12 changes: 12 additions & 0 deletions src/locale.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: 'amLocale' })
export class LocalePipe implements PipeTransform {
transform(value: string, locale: string): moment.Moment {
return moment(value).locale(locale);
}
}
4 changes: 3 additions & 1 deletion 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 { LocalePipe } from './locale.pipe';

const ANGULAR_MOMENT_PIPES = [
AddPipe,
Expand All @@ -21,7 +22,8 @@ const ANGULAR_MOMENT_PIPES = [
ParsePipe,
SubtractPipe,
TimeAgoPipe,
UtcPipe
UtcPipe,
LocalePipe
];

@NgModule({
Expand Down
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
"src/time-ago.pipe.ts",
"src/time-ago.pipe.spec.ts",
"src/utc.pipe.ts",
"src/utc.pipe.spec.ts"
"src/utc.pipe.spec.ts",
"src/locale.pipe.ts",
"src/locale.pipe.spec.ts"
],
"angularCompilerOptions": {
"genDir": "compiled"
Expand Down

0 comments on commit 2d33056

Please sign in to comment.