Skip to content

Commit

Permalink
fix: Fix dayjs.locale() returns current global locale (#602)
Browse files Browse the repository at this point in the history
* fix: Fix dayjs.locale() returns current global locale

* update localeData plugin
  • Loading branch information
iamkun committed May 22, 2019
1 parent 1761eba commit 790cd1a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/index.js
Expand Up @@ -10,7 +10,7 @@ const isDayjs = d => d instanceof Dayjs // eslint-disable-line no-use-before-def

const parseLocale = (preset, object, isLocal) => {
let l
if (!preset) return null
if (!preset) return L
if (typeof preset === 'string') {
if (Ls[preset]) {
l = preset
Expand Down Expand Up @@ -66,7 +66,7 @@ const parseDate = (cfg) => {

class Dayjs {
constructor(cfg) {
this.$L = this.$L || parseLocale(cfg.locale, null, true) || L
this.$L = this.$L || parseLocale(cfg.locale, null, true)
this.parse(cfg) // for plugin
}

Expand Down
9 changes: 8 additions & 1 deletion src/plugin/localeData/index.js
@@ -1,4 +1,4 @@
export default (o, c) => { // locale needed later
export default (o, c, dayjs) => { // locale needed later
const proto = c.prototype
const localeData = function () {
return {
Expand All @@ -12,5 +12,12 @@ export default (o, c) => { // locale needed later
proto.localeData = function () {
return localeData.bind(this)()
}

dayjs.localeData = () => {
const localeObject = dayjs.Ls[dayjs.locale()]
return {
firstDayOfWeek: () => localeObject.weekStart || 0
}
}
}

10 changes: 10 additions & 0 deletions test/locale.test.js
Expand Up @@ -110,4 +110,14 @@ describe('Instance locale inheritance', () => {
expect(esDayjs.add(1, 'minute').format(format))
.toBe('sábado 28, Abril')
})

it('dayjs.locale() returns locale name', () => {
dayjs.locale(es)
moment.locale('es')
expect(dayjs.locale()).toBe(moment.locale())

dayjs.locale('en')
moment.locale('en')
expect(dayjs.locale()).toBe(moment.locale())
})
})
16 changes: 15 additions & 1 deletion test/plugin/localeData.test.js
Expand Up @@ -2,6 +2,7 @@ import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import localeData from '../../src/plugin/localeData'
import '../../src/locale/zh-cn'

dayjs.extend(localeData)

Expand All @@ -13,7 +14,7 @@ afterEach(() => {
MockDate.reset()
})

it('localeData', () => {
it('instance localeData', () => {
const d = dayjs()
const m = moment()
const dayjsLocaleData = dayjs().localeData()
Expand All @@ -24,3 +25,16 @@ it('localeData', () => {
expect(dayjsLocaleData.weekdaysMin(d)).toBe(momentLocaleData.weekdaysMin(m))
expect(dayjsLocaleData.weekdaysShort(d)).toBe(momentLocaleData.weekdaysShort(m))
})

it('global localeData', () => {
dayjs.locale('zh-cn')
moment.locale('zh-cn')
let dayjsLocaleData = dayjs.localeData()
let momentLocaleData = moment.localeData()
expect(dayjsLocaleData.firstDayOfWeek()).toBe(momentLocaleData.firstDayOfWeek())
dayjs.locale('en')
moment.locale('en')
dayjsLocaleData = dayjs.localeData()
momentLocaleData = moment.localeData()
expect(dayjsLocaleData.firstDayOfWeek()).toBe(momentLocaleData.firstDayOfWeek())
})

0 comments on commit 790cd1a

Please sign in to comment.