Skip to content

Commit

Permalink
Merge pull request #891 from iamkun/dev
Browse files Browse the repository at this point in the history
D2M
  • Loading branch information
iamkun committed Apr 30, 2020
2 parents bcea067 + 058d624 commit 9fbb62b
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 22 deletions.
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ const parseLocale = (preset, object, isLocal) => {
return l || (!isLocal && L)
}

const dayjs = (date, c, pl) => {
const dayjs = function (date, c) {
if (isDayjs(date)) {
return date.clone()
}
// eslint-disable-next-line no-nested-ternary
const cfg = c ? (typeof c === 'string' ? { format: c, pl } : c) : {}
const cfg = typeof c === 'object' ? c : {}
cfg.date = date
cfg.args = arguments// eslint-disable-line prefer-rest-params
return new Dayjs(cfg) // eslint-disable-line no-use-before-define
}

Expand Down
22 changes: 16 additions & 6 deletions src/plugin/customParseFormat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,26 @@ export default (o, C, d) => {
proto.parse = function (cfg) {
const {
date,
format,
pl,
utc
utc,
args
} = cfg
this.$u = utc
if (format) {
locale = pl ? d.Ls[pl] : this.$locale()
const format = args[1]
if (typeof format === 'string') {
const isStrictWithoutLocale = args[2] === true
const isStrictWithLocale = args[3] === true
const isStrict = isStrictWithoutLocale || isStrictWithLocale
let pl = args[2]
if (isStrictWithLocale) [,, pl] = args
if (!isStrictWithoutLocale) {
locale = pl ? d.Ls[pl] : this.$locale()
}
this.$d = parseFormattedInput(date, format, utc)
this.init(cfg)
if (pl) this.$L = pl
if (isStrict && date !== this.format(format)) {
this.$d = new Date('')
}
if (pl && pl !== true) this.$L = pl
} else {
oldParse.call(this, cfg)
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class Duration {
seconds += this.$d.milliseconds / 1000
}
const S = seconds ? `${seconds}S` : ''
const T = (H || M || S) ? 'T' : ''
const T = (H || m || S) ? 'T' : ''
const result = `P${Y}${M}${D}${T}${H}${m}${S}`
return result === 'P' ? 'P0D' : result
}
Expand Down
5 changes: 3 additions & 2 deletions src/plugin/relativeTime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as C from '../../constant'
export default (o, c, d) => {
o = o || {}
const proto = c.prototype
d.en.relativeTime = {
const relObj = {
future: 'in %s',
past: '%s ago',
s: 'a few seconds',
Expand All @@ -18,8 +18,9 @@ export default (o, c, d) => {
y: 'a year',
yy: '%d years'
}
d.en.relativeTime = relObj
const fromTo = (input, withoutSuffix, instance, isFrom) => {
const loc = instance.$locale().relativeTime
const loc = instance.$locale().relativeTime || relObj
const T = o.thresholds || [
{ l: 's', r: 44, d: C.S },
{ l: 'm', r: 89 },
Expand Down
4 changes: 2 additions & 2 deletions src/plugin/utc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { MILLISECONDS_A_MINUTE, MIN } from '../../constant'
export default (option, Dayjs, dayjs) => {
const localOffset = (new Date()).getTimezoneOffset()
const proto = Dayjs.prototype
dayjs.utc = function (date, format) {
const cfg = { date, utc: true, format }
dayjs.utc = function (date) {
const cfg = { date, utc: true, args: arguments } // eslint-disable-line prefer-rest-params
return new Dayjs(cfg) // eslint-disable-line no-use-before-define
}

Expand Down
11 changes: 6 additions & 5 deletions src/plugin/weekOfYear/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import { MS, Y, D, W } from '../../constant'

export default (o, c) => {
export default (o, c, d) => {
const proto = c.prototype
proto.week = function (week = null) {
if (week !== null) {
return this.add((week - this.week()) * 7, D)
}
const yearStart = this.$locale().yearStart || 1
if (this.month() === 11 && this.date() > 25) {
const nextYearStartDay = this.startOf(Y).add(1, Y).date(yearStart)
const thisEndOfWeek = this.endOf(W)
// d(this) is for badMutable
const nextYearStartDay = d(this).startOf(Y).add(1, Y).date(yearStart)
const thisEndOfWeek = d(this).endOf(W)
if (nextYearStartDay.isBefore(thisEndOfWeek)) {
return 1
}
}
const yearStartDay = this.startOf(Y).date(yearStart)
const yearStartDay = d(this).startOf(Y).date(yearStart)
const yearStartWeek = yearStartDay.startOf(W).subtract(1, MS)
const diffInWeek = this.diff(yearStartWeek, W, true)
if (diffInWeek < 0) {
return this.startOf('week').week()
return d(this).startOf('week').week()
}
return Math.ceil(diffInWeek)
}
Expand Down
9 changes: 9 additions & 0 deletions test/plugin/badMutable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import badMutable from '../../src/plugin/badMutable'
import weekOfYear from '../../src/plugin/weekOfYear'
import '../../src/locale/zh-cn'

dayjs.extend(badMutable)
dayjs.extend(weekOfYear)

beforeEach(() => {
MockDate.set(new Date())
Expand Down Expand Up @@ -175,3 +177,10 @@ it('isAfter isBefore isSame', () => {
expect(d.format()).toBe(format)
expect(d.isAfter()).toBe(false)
})

it('WeekOfYear get week won"t change instance', () => {
const d = dayjs()
const format = d.format()
d.week()
expect(d.format()).toBe(format)
})
20 changes: 18 additions & 2 deletions test/plugin/customParseFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ it('parse month from string with locale in config', () => {
const input = '2018 лютий 03'
const format = 'YYYY MMMM DD'

expect(dayjs(input, { format, locale: uk }).valueOf()).toBe(moment(input, format, 'uk').valueOf())
expect(dayjs(input, format, 'uk').valueOf()).toBe(moment(input, format, 'uk').valueOf())
})

it('parse month from short string with locale in config', () => {
const input = '2018 трав 03'
const format = 'YYYY MMM DD'
expect(dayjs(input, { format, locale: uk }).valueOf()).toBe(moment(input, format, 'uk').valueOf())
expect(dayjs(input, format, 'uk').valueOf()).toBe(moment(input, format, 'uk').valueOf())
})

it('parse month from short string with locale in argument', () => {
Expand Down Expand Up @@ -232,3 +232,19 @@ it('correctly parse ordinal', () => {
expect(dayjsCN.locale())
.toBe(momentCN.locale())
})


describe('Strict mode', () => {
it('without locale', () => {
const input = '1970-00-00'
const format = 'YYYY-MM-DD'
expect(dayjs(input, format).isValid()).toBe(true)
expect(dayjs(input, format, true).isValid()).toBe(false)
})
it('with locale', () => {
const input = '2018 三月 99'
const format = 'YYYY MMMM DD'
expect(dayjs(input, format, 'zh-cn').isValid()).toBe(true)
expect(dayjs(input, format, 'zh-cn', true).isValid()).toBe(false)
})
})
4 changes: 2 additions & 2 deletions test/plugin/duration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('Creating', () => {
})
it('two argument will bubble up to the next', () => {
expect(dayjs.duration(59, 'seconds').toISOString()).toBe('PT59S')
expect(dayjs.duration(60, 'seconds').toISOString()).toBe('P1M')
expect(dayjs.duration(60, 'seconds').toISOString()).toBe('PT1M')
expect(dayjs.duration(13213, 'seconds').toISOString()).toBe('PT3H40M13S')
})
it('object with float', () => {
Expand Down Expand Up @@ -78,7 +78,7 @@ it('Is duration', () => {
it('toJSON', () => {
expect(JSON.stringify({
postDuration: dayjs.duration(5, 'minutes')
})).toBe('{"postDuration":"P5M"}')
})).toBe('{"postDuration":"PT5M"}')
})

describe('Humanize', () => {
Expand Down
1 change: 1 addition & 0 deletions test/plugin/isoWeek.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ it('isoWeekday', () => {
for (let i = 0; i < 7; i += 1) {
expect(dayjs().add(i, 'day').isoWeekday()).toBe(moment().add(i, 'day').isoWeekday())
expect(dayjs().isoWeekday(i).valueOf()).toBe(moment().isoWeekday(i).valueOf())
expect(dayjs().add(1, 'day').isoWeekday(i).valueOf()).toBe(moment().add(1, 'day').isoWeekday(i).valueOf())
}
})

Expand Down
6 changes: 6 additions & 0 deletions test/plugin/relativeTime.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,9 @@ it('Custom thresholds and rounding support', () => {
})
expect(dayjs().subtract(45, 'm').fromNow()).toBe('45 minutes ago')
})

it('Locale without relativeTime config fallback', () => {
expect(dayjs().locale({
name: 'test-locale'
}).fromNow()).toEqual(expect.any(String))
})

0 comments on commit 9fbb62b

Please sign in to comment.