Skip to content

Commit 2cce729

Browse files
authored
fix(): Changing locales locally is immutable from this release
immutable locale, docs update Merge pull request #182 from iamkun/feature/iamkun
2 parents c72dac0 + 7e63e20 commit 2cce729

File tree

10 files changed

+32
-22
lines changed

10 files changed

+32
-22
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
English | [简体中文](./README.zh-CN.md) | [日本語](./docs/ja/README-ja.md) | [Português Brasileiro](./docs/pt-br/README-pt-br.md)
1+
English | [简体中文](./docs/zh-cn/README.zh-CN.md) | [日本語](./docs/ja/README-ja.md) | [Português Brasileiro](./docs/pt-br/README-pt-br.md)
22

33
<p align="center"><a href="#" target="_blank" rel="noopener noreferrer"><img width="550"
44
src="https://user-images.githubusercontent.com/17680888/39081119-3057bbe2-456e-11e8-862c-646133ad4b43.png"

docs/en/I18n.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ dayjs.locale(customizedLocaleObject) // use customize locale
3131

3232
#### Changing locales locally
3333

34-
* Returns CURRENT `Dayjs` object.
34+
* Returns a new `Dayjs` object by switching to new locale.
3535

3636
Exactly the same as `dayjs#locale`, but only use locale in a specific instance.
3737

docs/ja/README-ja.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
[English](../../README.md) | [简体中文](../../README.zh-CN.md) | 日本語
2-
31
<p align="center"><a href="#" target="_blank" rel="noopener noreferrer"><img width="550"
42
src="https://user-images.githubusercontent.com/17680888/39081119-3057bbe2-456e-11e8-862c-646133ad4b43.png"
53
alt="Day.js"></a></p>

docs/pt-br/README-pt-br.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
[English](../../README.md) | [简体中文](../../README.zh-CN.md) | Português Brasileiro
2-
31
<p align="center"><a href="#" target="_blank" rel="noopener noreferrer"><img width="550"
42
src="https://user-images.githubusercontent.com/17680888/39081119-3057bbe2-456e-11e8-862c-646133ad4b43.png"
53
alt="Day.js"></a></p>

docs/zh-cn/I18n.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ dayjs.locale(customizedLocaleObject) // 使用自定义语言
2929

3030
* 改变全局语言并不会影响在此之前生成的实例
3131

32-
#### 改变局部语言
32+
#### 改变当前实例的语言
3333

34-
* 返回 **当前** `Dayjs` 对象
34+
* 改变语言并返回新的`Dayjs` 对象
3535

3636
用法与`dayjs#locale`一致, 但只影响当前实例的语言设置
3737

README.zh-CN.md renamed to docs/zh-cn/README.zh-CN.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
[English](./README.md) | 简体中文 | [日本語](./docs/ja/README-ja.md) | [Português Brasileiro](./docs/pt-br/README-pt-br.md)
2-
31
<p align="center"><a href="#" target="_blank" rel="noopener noreferrer"><img width="550"
42
src="https://user-images.githubusercontent.com/17680888/39081119-3057bbe2-456e-11e8-862c-646133ad4b43.png"
53
alt="Day.js"></a></p>
@@ -45,7 +43,7 @@ dayjs().startOf('month').add(1, 'day').set('year', 2018).format('YYYY-MM-DD HH:m
4543
npm install dayjs --save
4644
```
4745

48-
📚[安装指南](./docs/zh-cn/Installation.md)
46+
📚[安装指南](./Installation.md)
4947

5048
### API
5149

@@ -63,7 +61,7 @@ dayjs().add(1, 'year') // 处理
6361
dayjs().isBefore(dayjs()) // 查询
6462
```
6563

66-
📚[API 参考](./docs/zh-cn/API-reference.md)
64+
📚[API 参考](./API-reference.md)
6765

6866
### 国际化 I18n
6967

@@ -78,7 +76,7 @@ dayjs.locale('es') // 全局使用西班牙语
7876

7977
dayjs('2018-05-05').locale('zh-cn').format() // 在这个实例上使用简体中文
8078
```
81-
📚[国际化 I18n](./docs/zh-cn/I18n.md)
79+
📚[国际化 I18n](./I18n.md)
8280

8381
### 插件
8482

@@ -91,7 +89,7 @@ dayjs.extend(AdvancedFormat) // 使用插件
9189

9290
dayjs().format('Q Do k kk X x') // 使用扩展后的API
9391
```
94-
📚[插件列表](./docs/zh-cn/Plugin.md)
92+
📚[插件列表](./Plugin.md)
9593

9694
## 开源协议
9795

src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,9 @@ class Dayjs {
376376
}
377377

378378
locale(preset, object) {
379-
this.$L = parseLocale(preset, object, true)
380-
return this
379+
const that = this.clone()
380+
that.$L = parseLocale(preset, object, true)
381+
return that
381382
}
382383

383384
clone() {

src/plugin/advancedFormat.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@ import { FORMAT_DEFAULT } from '../constant'
33
export default (o, c, d) => { // locale needed later
44
const proto = c.prototype
55
const oldFormat = proto.format
6-
// eslint-disable-next-line no-nested-ternary
76
d.en.ordinal = (number) => {
8-
let suffix = 'th'
9-
if (![11, 12].includes(number)) {
10-
suffix = ['st', 'nd', 'rd'][(number % 10) - 1] || suffix
11-
}
12-
return `${number}[${suffix}]`
7+
const s = ['th', 'st', 'nd', 'rd']
8+
const v = number % 100
9+
return `[${number}${(s[(v - 20) % 10] || s[v] || s[0])}]`
1310
}
1411
// extend en locale here
1512
proto.format = function (formatStr, localeObject) {

test/locale.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ it('set global locale', () => {
4949
.toBe('Saturday 28, April')
5050
})
5151

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+
5266
it('User custom locale', () => {
5367
expect(dayjs('2018-4-28')
5468
.locale('xx', {

test/plugin/advancedFormat.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ it('Format Day of Month Do 1 - 31', () => {
3838
expect(dayjs(d).format('Do')).toBe(moment(d).format('Do'))
3939
d = '2018-05-11'
4040
expect(dayjs(d).format('Do')).toBe(moment(d).format('Do'))
41+
d = '2018-05-12'
42+
expect(dayjs(d).format('Do')).toBe(moment(d).format('Do'))
43+
d = '2018-05-13'
44+
expect(dayjs(d).format('Do')).toBe(moment(d).format('Do'))
4145
d = '2018-05-22'
4246
expect(dayjs(d).format('Do')).toBe(moment(d).format('Do'))
4347
})

0 commit comments

Comments
 (0)