File tree 2 files changed +19
-1
lines changed
2 files changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -375,11 +375,15 @@ class Dayjs {
375
375
return Ls [ this . $L ]
376
376
}
377
377
378
- locale ( preset , object ) {
378
+ $setLocale ( preset , object ) { // private set locale mutate instance
379
379
this . $L = parseLocale ( preset , object , true )
380
380
return this
381
381
}
382
382
383
+ locale ( preset , object ) {
384
+ return this . clone ( ) . $setLocale ( preset , object )
385
+ }
386
+
383
387
clone ( ) {
384
388
return wrapper ( this . toDate ( ) , this )
385
389
}
Original file line number Diff line number Diff line change @@ -49,6 +49,20 @@ it('set global locale', () => {
49
49
. toBe ( 'Saturday 28, April' )
50
50
} )
51
51
52
+ it ( 'immutable instance locale' , ( ) => {
53
+ dayjs . locale ( 'en' )
54
+ const origin = dayjs ( '2018-4-28' )
55
+ expect ( origin . format ( format ) )
56
+ . toBe ( 'Saturday 28, April' )
57
+ expect ( origin . locale ( 'es' ) . format ( format ) )
58
+ . toBe ( 'Sábado 28, Abril' )
59
+ const changed = origin . locale ( 'es' )
60
+ expect ( changed . format ( format ) )
61
+ . toBe ( 'Sábado 28, Abril' )
62
+ expect ( origin . format ( format ) )
63
+ . toBe ( 'Saturday 28, April' )
64
+ } )
65
+
52
66
it ( 'User custom locale' , ( ) => {
53
67
expect ( dayjs ( '2018-4-28' )
54
68
. locale ( 'xx' , {
You can’t perform that action at this time.
0 commit comments