diff --git a/.gitignore b/.gitignore index d91535f..ff8505f 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/README.md b/README.md index e1dffbe..3aa0f25 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package.json b/package.json index 14869a7..1737c64 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/index.ts b/src/index.ts index 0e0f4dc..c670166 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; diff --git a/src/locale.pipe.spec.ts b/src/locale.pipe.spec.ts new file mode 100644 index 0000000..20eb528 --- /dev/null +++ b/src/locale.pipe.spec.ts @@ -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'); + }); + + }); + +}); diff --git a/src/locale.pipe.ts b/src/locale.pipe.ts new file mode 100644 index 0000000..3039c24 --- /dev/null +++ b/src/locale.pipe.ts @@ -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 = (moment).default || moment; + +@Pipe({ name: 'amLocale' }) +export class LocalePipe implements PipeTransform { + transform(value: string, locale: string): moment.Moment { + return moment(value).locale(locale); + } +} diff --git a/src/moment.module.ts b/src/moment.module.ts index c68f76d..5db7fee 100644 --- a/src/moment.module.ts +++ b/src/moment.module.ts @@ -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, @@ -21,7 +22,8 @@ const ANGULAR_MOMENT_PIPES = [ ParsePipe, SubtractPipe, TimeAgoPipe, - UtcPipe + UtcPipe, + LocalePipe ]; @NgModule({ diff --git a/tsconfig.json b/tsconfig.json index 3c4ef48..e88df43 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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"