Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix dayjs.locale() returns current global locale #602

Merged
merged 2 commits into from
May 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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())
})