Skip to content

Commit

Permalink
fix(): Changing locales locally is immutable from this release
Browse files Browse the repository at this point in the history
immutable locale, docs update
Merge pull request #182 from iamkun/feature/iamkun
  • Loading branch information
iamkun authored May 21, 2018
2 parents c72dac0 + 7e63e20 commit 2cce729
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
English | [简体中文](./README.zh-CN.md) | [日本語](./docs/ja/README-ja.md) | [Português Brasileiro](./docs/pt-br/README-pt-br.md)
English | [简体中文](./docs/zh-cn/README.zh-CN.md) | [日本語](./docs/ja/README-ja.md) | [Português Brasileiro](./docs/pt-br/README-pt-br.md)

<p align="center"><a href="#" target="_blank" rel="noopener noreferrer"><img width="550"
src="https://user-images.githubusercontent.com/17680888/39081119-3057bbe2-456e-11e8-862c-646133ad4b43.png"
Expand Down
2 changes: 1 addition & 1 deletion docs/en/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dayjs.locale(customizedLocaleObject) // use customize locale

#### Changing locales locally

* Returns CURRENT `Dayjs` object.
* Returns a new `Dayjs` object by switching to new locale.

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

Expand Down
2 changes: 0 additions & 2 deletions docs/ja/README-ja.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
[English](../../README.md) | [简体中文](../../README.zh-CN.md) | 日本語

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

<p align="center"><a href="#" target="_blank" rel="noopener noreferrer"><img width="550"
src="https://user-images.githubusercontent.com/17680888/39081119-3057bbe2-456e-11e8-862c-646133ad4b43.png"
alt="Day.js"></a></p>
Expand Down
4 changes: 2 additions & 2 deletions docs/zh-cn/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ dayjs.locale(customizedLocaleObject) // 使用自定义语言

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

#### 改变局部语言
#### 改变当前实例的语言

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

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

Expand Down
10 changes: 4 additions & 6 deletions README.zh-CN.md → docs/zh-cn/README.zh-CN.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
[English](./README.md) | 简体中文 | [日本語](./docs/ja/README-ja.md) | [Português Brasileiro](./docs/pt-br/README-pt-br.md)

<p align="center"><a href="#" target="_blank" rel="noopener noreferrer"><img width="550"
src="https://user-images.githubusercontent.com/17680888/39081119-3057bbe2-456e-11e8-862c-646133ad4b43.png"
alt="Day.js"></a></p>
Expand Down Expand Up @@ -45,7 +43,7 @@ dayjs().startOf('month').add(1, 'day').set('year', 2018).format('YYYY-MM-DD HH:m
npm install dayjs --save
```

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

### API

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

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

### 国际化 I18n

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

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

### 插件

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

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

## 开源协议

Expand Down
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,9 @@ class Dayjs {
}

locale(preset, object) {
this.$L = parseLocale(preset, object, true)
return this
const that = this.clone()
that.$L = parseLocale(preset, object, true)
return that
}

clone() {
Expand Down
9 changes: 3 additions & 6 deletions src/plugin/advancedFormat.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ import { FORMAT_DEFAULT } from '../constant'
export default (o, c, d) => { // locale needed later
const proto = c.prototype
const oldFormat = proto.format
// eslint-disable-next-line no-nested-ternary
d.en.ordinal = (number) => {
let suffix = 'th'
if (![11, 12].includes(number)) {
suffix = ['st', 'nd', 'rd'][(number % 10) - 1] || suffix
}
return `${number}[${suffix}]`
const s = ['th', 'st', 'nd', 'rd']
const v = number % 100
return `[${number}${(s[(v - 20) % 10] || s[v] || s[0])}]`
}
// extend en locale here
proto.format = function (formatStr, localeObject) {
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
4 changes: 4 additions & 0 deletions test/plugin/advancedFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ it('Format Day of Month Do 1 - 31', () => {
expect(dayjs(d).format('Do')).toBe(moment(d).format('Do'))
d = '2018-05-11'
expect(dayjs(d).format('Do')).toBe(moment(d).format('Do'))
d = '2018-05-12'
expect(dayjs(d).format('Do')).toBe(moment(d).format('Do'))
d = '2018-05-13'
expect(dayjs(d).format('Do')).toBe(moment(d).format('Do'))
d = '2018-05-22'
expect(dayjs(d).format('Do')).toBe(moment(d).format('Do'))
})
Expand Down

0 comments on commit 2cce729

Please sign in to comment.