Skip to content

Commit

Permalink
Merge pull request #1031 from iamkun/dev
Browse files Browse the repository at this point in the history
D2M
  • Loading branch information
iamkun committed Sep 2, 2020
2 parents e1890c9 + 1533a2c commit 8fca859
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/locale/nl.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const locale = {
monthsShort: 'jan_feb_mrt_apr_mei_jun_jul_aug_sep_okt_nov_dec'.split('_'),
ordinal: n => `${n}.`,
weekStart: 1,
yearStart: 4,
formats: {
LT: 'HH:mm',
LTS: 'HH:mm:ss',
Expand Down
1 change: 1 addition & 0 deletions src/plugin/localeData/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default (o, c, dayjs) => { // locale needed later
monthsShort: instance =>
(instance ? instance.format('MMM') : getShort(this, 'monthsShort', 'months', 3)),
firstDayOfWeek: () => this.$locale().weekStart || 0,
weekdays: instance => (instance ? instance.format('dddd') : getShort(this, 'weekdays')),
weekdaysMin: instance =>
(instance ? instance.format('dd') : getShort(this, 'weekdaysMin', 'weekdays', 2)),
weekdaysShort: instance =>
Expand Down
4 changes: 2 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const monthDiff = (a, b) => {
// function from moment.js in order to keep the same result
if (a.date() < b.date()) return -monthDiff(b, a)
const wholeMonthDiff = ((b.year() - a.year()) * 12) + (b.month() - a.month())
const anchor = a.add(wholeMonthDiff, C.M)
const anchor = a.clone().add(wholeMonthDiff, C.M)
const c = b - anchor < 0
const anchor2 = a.add(wholeMonthDiff + (c ? -1 : 1), C.M)
const anchor2 = a.clone().add(wholeMonthDiff + (c ? -1 : 1), C.M)
return +(-(wholeMonthDiff + ((b - anchor) / (c ? (anchor - anchor2) :
(anchor2 - anchor)))) || 0)
}
Expand Down
10 changes: 10 additions & 0 deletions test/plugin/badMutable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ it('Locale', () => {
expect(d.format(format)).toBe(m.format(format))
})

it('Diff', () => {
const d = dayjs()
const m = moment()
const unit = 'year'
const d2 = d.clone().add(1, unit)
const m2 = m.clone().add(1, unit)
expect(d.diff(d2, unit)).toBe(-1)
expect(m.diff(m2, unit)).toBe(-1)
})

it('isAfter isBefore isSame', () => {
const d = dayjs()
const format = dayjs().format()
Expand Down
50 changes: 30 additions & 20 deletions test/plugin/localeData.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import localeData from '../../src/plugin/localeData'
import localizedFormat from '../../src/plugin/localizedFormat'
import '../../src/locale/fr'
import '../../src/locale/zh-cn'
import '../../src/locale/ru'
import '../../src/locale/zh-cn'
import localeData from '../../src/plugin/localeData'
import localizedFormat from '../../src/plugin/localizedFormat'

dayjs.extend(localizedFormat)
dayjs.extend(localeData)
Expand All @@ -18,24 +18,34 @@ afterEach(() => {
MockDate.reset()
})

it('Instance localeData', () => {
const d = dayjs()
const m = moment()
const dayjsLocaleData = dayjs().localeData()
const momentLocaleData = moment().localeData()
expect(dayjsLocaleData.firstDayOfWeek()).toBe(momentLocaleData.firstDayOfWeek())
expect(dayjsLocaleData.months(d)).toBe(momentLocaleData.months(m))
expect(dayjsLocaleData.months()).toEqual(momentLocaleData.months())
expect(dayjsLocaleData.monthsShort(d)).toBe(momentLocaleData.monthsShort(m))
expect(dayjsLocaleData.monthsShort()).toEqual(momentLocaleData.monthsShort())
expect(dayjsLocaleData.weekdaysMin(d)).toBe(momentLocaleData.weekdaysMin(m))
expect(dayjsLocaleData.weekdaysMin()).toEqual(momentLocaleData.weekdaysMin())
expect(dayjsLocaleData.weekdaysShort(d)).toBe(momentLocaleData.weekdaysShort(m))
expect(dayjsLocaleData.weekdaysShort()).toEqual(momentLocaleData.weekdaysShort())
const longDateFormats = ['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL']
longDateFormats.forEach((f) => {
expect(dayjsLocaleData.longDateFormat(f)).toEqual(momentLocaleData.longDateFormat(f))
describe('Instance localeData', () => {
['zh-cn', 'en', 'fr'].forEach((lo) => {
it(`Locale: ${lo}`, () => {
dayjs.locale(lo)
moment.locale(lo)
const d = dayjs()
const m = moment()
const dayjsLocaleData = dayjs().localeData()
const momentLocaleData = moment().localeData()
expect(dayjsLocaleData.firstDayOfWeek()).toBe(momentLocaleData.firstDayOfWeek())
expect(dayjsLocaleData.months(d)).toBe(momentLocaleData.months(m))
expect(dayjsLocaleData.months()).toEqual(momentLocaleData.months())
expect(dayjsLocaleData.monthsShort(d)).toBe(momentLocaleData.monthsShort(m))
expect(dayjsLocaleData.monthsShort()).toEqual(momentLocaleData.monthsShort())
expect(dayjsLocaleData.weekdays(d)).toBe(momentLocaleData.weekdays(m))
expect(dayjsLocaleData.weekdays()).toEqual(momentLocaleData.weekdays())
expect(dayjsLocaleData.weekdaysMin(d)).toBe(momentLocaleData.weekdaysMin(m))
expect(dayjsLocaleData.weekdaysMin()).toEqual(momentLocaleData.weekdaysMin())
expect(dayjsLocaleData.weekdaysShort(d)).toBe(momentLocaleData.weekdaysShort(m))
expect(dayjsLocaleData.weekdaysShort()).toEqual(momentLocaleData.weekdaysShort())
const longDateFormats = ['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL']
longDateFormats.forEach((f) => {
expect(dayjsLocaleData.longDateFormat(f)).toEqual(momentLocaleData.longDateFormat(f))
})
})
})
dayjs.locale('en')
moment.locale('en')
})


Expand Down

0 comments on commit 8fca859

Please sign in to comment.