Skip to content

Commit

Permalink
fix: instance locale change should be immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed May 21, 2018
1 parent 1a7840d commit 84597c9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,11 +375,15 @@ class Dayjs {
return Ls[this.$L]
}

locale(preset, object) {
$setLocale(preset, object) { // private set locale mutate instance
this.$L = parseLocale(preset, object, true)
return this
}

locale(preset, object) {
return this.clone().$setLocale(preset, object)
}

clone() {
return wrapper(this.toDate(), this)
}
Expand Down
14 changes: 14 additions & 0 deletions test/locale.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ it('set global locale', () => {
.toBe('Saturday 28, April')
})

it('immutable instance locale', () => {
dayjs.locale('en')
const origin = dayjs('2018-4-28')
expect(origin.format(format))
.toBe('Saturday 28, April')
expect(origin.locale('es').format(format))
.toBe('Sábado 28, Abril')
const changed = origin.locale('es')
expect(changed.format(format))
.toBe('Sábado 28, Abril')
expect(origin.format(format))
.toBe('Saturday 28, April')
})

it('User custom locale', () => {
expect(dayjs('2018-4-28')
.locale('xx', {
Expand Down

0 comments on commit 84597c9

Please sign in to comment.