@@ -13,18 +13,30 @@ import {
1313import { EN } from '../i18n/en' ;
1414
1515export const LOCALE_DEFAULT = 'en' ;
16- export const FIRST_DAY_OF_WEEK : number = 0 ;
16+
17+ export interface NativeDateServiceOptions {
18+ // 0 for Sunday, 1 for Monday, etc
19+ startDayOfWeek ?: 0 | 1 | 2 | 3 | 4 | 5 | 6 ;
20+ i18n ?: I18nConfig ;
21+ }
22+
23+ const DEFAULT_OPTIONS : NativeDateServiceOptions = {
24+ startDayOfWeek : 0 ,
25+ } ;
1726
1827/**
1928 * The `NativeDateService` is basic implementation of `DateService` using
2029 * native js date objects.
2130 */
2231export class NativeDateService extends DateService < Date > {
2332
24- constructor ( locale : string = LOCALE_DEFAULT , i18n ?: I18nConfig ) {
33+ protected options : NativeDateServiceOptions ;
34+
35+ constructor ( locale : string = LOCALE_DEFAULT , options ?: NativeDateServiceOptions ) {
2536 super ( ) ;
26- super . setLocale ( i18n ? locale : LOCALE_DEFAULT ) ;
27- this . setFechaLocaleData ( i18n || EN ) ;
37+ this . options = { ...DEFAULT_OPTIONS , ...options } ;
38+ super . setLocale ( this . options . i18n ? locale : LOCALE_DEFAULT ) ;
39+ this . setFechaLocaleData ( this . options . i18n || EN ) ;
2840 }
2941
3042 public setLocale ( locale : string ) {
@@ -60,7 +72,7 @@ export class NativeDateService extends DateService<Date> {
6072 * and 0 if from sunday and so on.
6173 * */
6274 public getFirstDayOfWeek ( ) : number {
63- return FIRST_DAY_OF_WEEK ;
75+ return this . options . startDayOfWeek ;
6476 }
6577
6678 public getMonthName ( date : Date , style : TranslationWidth = TranslationWidth . SHORT ) : string {
@@ -74,7 +86,10 @@ export class NativeDateService extends DateService<Date> {
7486 }
7587
7688 public getDayOfWeekNames ( style : TranslationWidth = TranslationWidth . SHORT ) : string [ ] {
77- return this . getFechaDayNames ( style ) ;
89+ const dayNames : string [ ] = this . getFechaDayNames ( style ) ;
90+
91+ // avoid mutation of source array
92+ return this . shiftDayOfWeekNames ( [ ...dayNames ] , this . options . startDayOfWeek ) ;
7893 }
7994
8095 public format ( date : Date , format : string ) : string {
@@ -172,7 +187,11 @@ export class NativeDateService extends DateService<Date> {
172187 return 'native' ;
173188 }
174189
175- private getFechaDayNames ( style : TranslationWidth ) {
190+ protected shiftDayOfWeekNames < T > ( value : T [ ] , offset : number ) : T [ ] {
191+ return value . splice ( offset ) . concat ( value ) ;
192+ }
193+
194+ private getFechaDayNames ( style : TranslationWidth ) : string [ ] {
176195 switch ( style ) {
177196 case TranslationWidth . SHORT :
178197 return fecha . i18n . dayNamesShort ;
@@ -181,7 +200,7 @@ export class NativeDateService extends DateService<Date> {
181200 }
182201 }
183202
184- private getFechaMonthNames ( style : TranslationWidth ) {
203+ private getFechaMonthNames ( style : TranslationWidth ) : string [ ] {
185204 switch ( style ) {
186205 case TranslationWidth . SHORT :
187206 return fecha . i18n . monthNamesShort ;
0 commit comments