-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(locales): Added global locale configuration options
Implemented provider `DateFnsConfigurationService` to work with global locale configurations BREAKING CHANGE: * To import the module you have to use `DateFnsModule.forRoot()`. re #135
- Loading branch information
1 parent
ed1e0b1
commit 7f50125
Showing
14 changed files
with
291 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,49 @@ | ||
import { Component } from '@angular/core'; | ||
import * as esLocale from 'date-fns/locale/es/index.js'; | ||
import { Component } from "@angular/core"; | ||
import * as esLocale from "date-fns/locale/es/index.js"; | ||
import * as deLocale from "date-fns/locale/de/index.js"; | ||
import { DateFnsConfigurationService } from "../lib/src/date-fns-configuration.service"; | ||
|
||
@Component({ | ||
selector: 'app-root', | ||
selector: "app-root", | ||
template: ` | ||
<p> | ||
{{ dateOne | dfnsFormat : 'YYYY/MM/DD' }} | ||
</p> | ||
<p> | ||
{{ [dateOne, dateTwo] | dfnsMin }} | ||
{{ [dateOne, dateTwo] | dfnsMin | dfnsFormat : 'ddd MMM D YYYY' }} | ||
</p> | ||
<p> | ||
{{ [dateOne, dateTwo] | dfnsMax | dfnsFormat : 'YYYY/MM/DD' }} | ||
{{ [dateOne, dateTwo] | dfnsMax | dfnsFormat : 'ddd MMM D YYYY' }} | ||
</p> | ||
<p> | ||
{{ dateThree | dfnsDistanceInWordsToNow : options }} | ||
{{ dateThree | dfnsDistanceInWordsToNow : options }} - (Explicit 'es' locale) | ||
</p> | ||
<hr> | ||
Set default locale to: <a href="#" (click)="changeToGerman()">German</a>, <a href="#" (click)="changeToSpanish()">Spanish</a>. | ||
<p *ngFor="let d of dates"> | ||
{{ d | dfnsDistanceInWordsToNow }} | ||
</p> | ||
` | ||
}) | ||
export class AppComponent { | ||
dateOne = new Date(2016, 0, 1); | ||
dateTwo = new Date(2017, 0, 1); | ||
dateThree; | ||
dateThree: Date; | ||
dates: Date[]; | ||
options = { | ||
locale: esLocale | ||
} | ||
constructor() { | ||
}; | ||
constructor(public config: DateFnsConfigurationService) { | ||
this.dateThree = new Date(); | ||
this.dateThree.setDate(-6); | ||
this.dates = new Array(6) | ||
.fill(new Date()) | ||
.map((d, i) => d.setDate(d.getDate() - Math.pow(5, i) )); | ||
} | ||
changeToGerman() { | ||
this.config.setLocale(deLocale); | ||
} | ||
changeToSpanish() { | ||
this.config.setLocale(esLocale); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Component } from "@angular/core"; | ||
import { TestBed, ComponentFixture, async } from "@angular/core/testing"; | ||
import { DateFnsConfigurationService } from "./date-fns-configuration.service"; | ||
import { DateFnsModule } from "."; | ||
import * as esLocale from "date-fns/locale/es/index.js"; | ||
import * as frLocale from "date-fns/locale/fr/index.js"; | ||
|
||
@Component({ | ||
template: ` | ||
<p class="dfns">{{ date | dfnsFormat : 'ddd MMM D YYYY' }}</p> | ||
<p class="explicit">{{ date | dfnsFormat : 'ddd MMM D YYYY' : options }}</p> | ||
` | ||
}) | ||
class TestHostComponent { | ||
date: Date = new Date(); | ||
options = { locale: frLocale }; | ||
} | ||
|
||
describe('DateFnsConfigurationService', () => { | ||
|
||
const mockConfig = new DateFnsConfigurationService(); | ||
|
||
beforeEach(async (() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [ TestHostComponent ], | ||
imports: [ DateFnsModule.forRoot() ], | ||
providers: [ {provide: DateFnsConfigurationService, useValue: mockConfig} ] | ||
}).compileComponents(); | ||
})); | ||
|
||
let component: TestHostComponent; | ||
let fixture: ComponentFixture<TestHostComponent>; | ||
let element: HTMLElement; | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(TestHostComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should display English date by default', () => { | ||
component.date = new Date(2017, 11, 31); | ||
element = fixture.nativeElement.querySelector('.dfns'); | ||
fixture.detectChanges(); | ||
expect(element.textContent).toBe('Sun Dec 31 2017'); | ||
}); | ||
|
||
it('should display Spanish date when changing config locale to Spanish', () => { | ||
mockConfig.setLocale(esLocale); | ||
component.date = new Date(2017, 11, 31); | ||
element = fixture.nativeElement.querySelector('.dfns'); | ||
fixture.detectChanges(); | ||
expect(element.textContent).toBe('dom dic 31 2017'); | ||
}); | ||
|
||
it('should display French date even when changing config locale to Spanish if locale has been explicitly set via options', () => { | ||
mockConfig.setLocale(esLocale); | ||
component.date = new Date(2017, 11, 31); | ||
element = fixture.nativeElement.querySelector('.explicit'); | ||
fixture.detectChanges(); | ||
expect(element.textContent).toBe('dim. déc. 31 2017'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Injectable } from "@angular/core"; | ||
|
||
export interface DateFnsConfiguration { | ||
/** | ||
* Returns the default locale used by date-fns | ||
*/ | ||
locale(): Object; | ||
|
||
/** | ||
* Sets the default locale used by date-fns | ||
*/ | ||
setLocale(locale: Object): void; | ||
} | ||
|
||
@Injectable() | ||
export class DateFnsConfigurationService implements DateFnsConfiguration { | ||
private _locale: Object; | ||
|
||
locale(): Object { | ||
return this._locale; | ||
} | ||
|
||
setLocale(locale: Object): void { | ||
this._locale = locale; | ||
} | ||
} | ||
|
||
/** | ||
* Helper function used by all pipes to calculate locale | ||
*/ | ||
export const calculateLocale = ( | ||
options: { locale?: Object }, | ||
config: DateFnsConfiguration | ||
): Object => { | ||
|
||
const configLocale = config.locale(); | ||
|
||
if(!options && configLocale) { | ||
return {locale: configLocale}; | ||
} | ||
|
||
if(options && !options.locale && configLocale) { | ||
return { | ||
...options, | ||
locale: configLocale | ||
}; | ||
} | ||
|
||
return options; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,33 @@ | ||
import {Pipe, PipeTransform} from '@angular/core'; | ||
import {distanceInWordsStrict} from 'date-fns'; | ||
import { Pipe, PipeTransform } from "@angular/core"; | ||
import { distanceInWordsStrict } from "date-fns"; | ||
import { | ||
DateFnsConfigurationService, | ||
calculateLocale | ||
} from "./date-fns-configuration.service"; | ||
|
||
|
||
@Pipe({ name: 'dfnsDistanceInWordsStrict' }) | ||
@Pipe({ name: "dfnsDistanceInWordsStrict", pure: false }) | ||
export class DistanceInWordsStrictPipe implements PipeTransform { | ||
static readonly NO_ARGS_ERROR = 'dfnsDistanceInWordsStrict: missing required arguments'; | ||
static readonly NO_ARGS_ERROR = "dfnsDistanceInWordsStrict: missing required arguments"; | ||
|
||
constructor(public config: DateFnsConfigurationService) {} | ||
|
||
transform( | ||
dateToCompare: string | number | Date, | ||
date: string | number | Date, | ||
options?: { | ||
addSuffix?: boolean, | ||
unit?: 's' | 'm' | 'h' | 'd' | 'M' | 'Y', | ||
partialMethod?: 'floor' | 'ceil' | 'round', | ||
locale?: Object | ||
addSuffix?: boolean; | ||
unit?: "s" | "m" | "h" | "d" | "M" | "Y"; | ||
partialMethod?: "floor" | "ceil" | "round"; | ||
locale?: Object; | ||
} | ||
): string { | ||
if (!dateToCompare || !date) { | ||
throw new Error(DistanceInWordsStrictPipe.NO_ARGS_ERROR); | ||
throw new Error(DistanceInWordsStrictPipe.NO_ARGS_ERROR); | ||
} | ||
return distanceInWordsStrict(dateToCompare, date, options); | ||
return distanceInWordsStrict( | ||
dateToCompare, | ||
date, | ||
calculateLocale(options, this.config) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.