diff --git a/README.md b/README.md index 867818305..f764b2099 100644 --- a/README.md +++ b/README.md @@ -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)
diff --git a/docs/pt-br/README-pt-br.md b/docs/pt-br/README-pt-br.md index 33ba4c36e..5cf73613e 100644 --- a/docs/pt-br/README-pt-br.md +++ b/docs/pt-br/README-pt-br.md @@ -1,5 +1,3 @@ -[English](../../README.md) | [简体中文](../../README.zh-CN.md) | Português Brasileiro - diff --git a/docs/zh-cn/I18n.md b/docs/zh-cn/I18n.md index 413ee5e0c..a13b87272 100644 --- a/docs/zh-cn/I18n.md +++ b/docs/zh-cn/I18n.md @@ -29,9 +29,9 @@ dayjs.locale(customizedLocaleObject) // 使用自定义语言 * 改变全局语言并不会影响在此之前生成的实例 -#### 改变局部语言 +#### 改变当前实例的语言 -* 返回 **当前** `Dayjs` 对象 +* 改变语言并返回新的`Dayjs` 对象 用法与`dayjs#locale`一致, 但只影响当前实例的语言设置 diff --git a/README.zh-CN.md b/docs/zh-cn/README.zh-CN.md similarity index 90% rename from README.zh-CN.md rename to docs/zh-cn/README.zh-CN.md index e0f4ef24a..224a84772 100644 --- a/README.zh-CN.md +++ b/docs/zh-cn/README.zh-CN.md @@ -1,5 +1,3 @@ -[English](./README.md) | 简体中文 | [日本語](./docs/ja/README-ja.md) | [Português Brasileiro](./docs/pt-br/README-pt-br.md) - @@ -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 @@ -63,7 +61,7 @@ dayjs().add(1, 'year') // 处理 dayjs().isBefore(dayjs()) // 查询 ``` -📚[API 参考](./docs/zh-cn/API-reference.md) +📚[API 参考](./API-reference.md) ### 国际化 I18n @@ -78,7 +76,7 @@ dayjs.locale('es') // 全局使用西班牙语 dayjs('2018-05-05').locale('zh-cn').format() // 在这个实例上使用简体中文 ``` -📚[国际化 I18n](./docs/zh-cn/I18n.md) +📚[国际化 I18n](./I18n.md) ### 插件 @@ -91,7 +89,7 @@ dayjs.extend(AdvancedFormat) // 使用插件 dayjs().format('Q Do k kk X x') // 使用扩展后的API ``` -📚[插件列表](./docs/zh-cn/Plugin.md) +📚[插件列表](./Plugin.md) ## 开源协议 diff --git a/src/index.js b/src/index.js index 37f398079..996695d70 100644 --- a/src/index.js +++ b/src/index.js @@ -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() { diff --git a/src/plugin/advancedFormat.js b/src/plugin/advancedFormat.js index 1bd6ef296..0820ee25a 100644 --- a/src/plugin/advancedFormat.js +++ b/src/plugin/advancedFormat.js @@ -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) { diff --git a/test/locale.test.js b/test/locale.test.js index 88d34e7cf..5570a062c 100644 --- a/test/locale.test.js +++ b/test/locale.test.js @@ -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', { diff --git a/test/plugin/advancedFormat.test.js b/test/plugin/advancedFormat.test.js index dbc809dff..144a25878 100644 --- a/test/plugin/advancedFormat.test.js +++ b/test/plugin/advancedFormat.test.js @@ -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')) })