Skip to content

Commit 168aa14

Browse files
authored
Merge pull request #2088 from iamkun/dev
D2M
2 parents e3777ed + d3a4b5e commit 168aa14

File tree

8 files changed

+257
-27
lines changed

8 files changed

+257
-27
lines changed

src/locale/en.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,10 @@
33
export default {
44
name: 'en',
55
weekdays: 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_'),
6-
months: 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_')
6+
months: 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_'),
7+
ordinal: (n) => {
8+
const s = ['th', 'st', 'nd', 'rd']
9+
const v = n % 100
10+
return `[${n}${(s[(v - 20) % 10] || s[v] || s[0])}]`
11+
}
712
}

src/plugin/advancedFormat/index.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
import { FORMAT_DEFAULT } from '../../constant'
22

3-
export default (o, c, d) => { // locale needed later
3+
export default (o, c) => { // locale needed later
44
const proto = c.prototype
55
const oldFormat = proto.format
6-
d.en.ordinal = (number) => {
7-
const s = ['th', 'st', 'nd', 'rd']
8-
const v = number % 100
9-
return `[${number}${(s[(v - 20) % 10] || s[v] || s[0])}]`
10-
}
11-
// extend en locale here
126
proto.format = function (formatStr) {
137
const locale = this.$locale()
148

src/plugin/bigIntSupport/index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// eslint-disable-next-line valid-typeof
2+
const isBigInt = num => typeof num === 'bigint'
3+
export default (o, c, dayjs) => {
4+
const proto = c.prototype
5+
const parseDate = (cfg) => {
6+
const { date } = cfg
7+
if (isBigInt(date)) {
8+
return Number(date)
9+
}
10+
return date
11+
}
12+
13+
const oldParse = proto.parse
14+
proto.parse = function (cfg) {
15+
cfg.date = parseDate.bind(this)(cfg)
16+
oldParse.bind(this)(cfg)
17+
}
18+
19+
20+
const oldUnix = dayjs.unix
21+
dayjs.unix = function (timestamp) {
22+
const ts = isBigInt(timestamp) ? Number(timestamp) : timestamp
23+
return oldUnix(ts)
24+
}
25+
}

src/plugin/objectSupport/index.js

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export default (o, c, dayjs) => {
22
const proto = c.prototype
3-
const isObject = obj => !(obj instanceof Date) && !(obj instanceof Array) && obj instanceof Object
3+
const isObject = obj => !(obj instanceof Date) && !(obj instanceof Array)
4+
&& !proto.$utils().u(obj) && (obj.constructor.name === 'Object')
45
const prettyUnit = (u) => {
56
const unit = proto.$utils().p(u)
67
return unit === 'date' ? 'day' : unit
@@ -39,29 +40,36 @@ export default (o, c, dayjs) => {
3940

4041
const oldSet = proto.set
4142
const oldAdd = proto.add
43+
const oldSubtract = proto.subtract
4244

4345
const callObject = function (call, argument, string, offset = 1) {
44-
if (argument instanceof Object) {
45-
const keys = Object.keys(argument)
46-
let chain = this
47-
keys.forEach((key) => {
48-
chain = call.bind(chain)(argument[key] * offset, key)
49-
})
50-
return chain
51-
}
52-
return call.bind(this)(argument * offset, string)
46+
const keys = Object.keys(argument)
47+
let chain = this
48+
keys.forEach((key) => {
49+
chain = call.bind(chain)(argument[key] * offset, key)
50+
})
51+
return chain
5352
}
5453

55-
proto.set = function (string, int) {
56-
int = int === undefined ? string : int
57-
return callObject.bind(this)(function (i, s) {
58-
return oldSet.bind(this)(s, i)
59-
}, int, string)
54+
proto.set = function (unit, value) {
55+
value = value === undefined ? unit : value
56+
if (unit.constructor.name === 'Object') {
57+
return callObject.bind(this)(function (i, s) {
58+
return oldSet.bind(this)(s, i)
59+
}, value, unit)
60+
}
61+
return oldSet.bind(this)(unit, value)
6062
}
61-
proto.add = function (number, string) {
62-
return callObject.bind(this)(oldAdd, number, string)
63+
proto.add = function (value, unit) {
64+
if (value.constructor.name === 'Object') {
65+
return callObject.bind(this)(oldAdd, value, unit)
66+
}
67+
return oldAdd.bind(this)(value, unit)
6368
}
64-
proto.subtract = function (number, string) {
65-
return callObject.bind(this)(oldAdd, number, string, -1)
69+
proto.subtract = function (value, unit) {
70+
if (value.constructor.name === 'Object') {
71+
return callObject.bind(this)(oldAdd, value, unit, -1)
72+
}
73+
return oldSubtract.bind(this)(value, unit)
6674
}
6775
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import MockDate from 'mockdate'
2+
import dayjs from '../../src'
3+
import duration from '../../src/plugin/duration'
4+
import objectSupport from '../../src/plugin/objectSupport'
5+
6+
dayjs.extend(objectSupport)
7+
dayjs.extend(duration)
8+
9+
beforeEach(() => {
10+
MockDate.set(new Date())
11+
})
12+
13+
afterEach(() => {
14+
MockDate.reset()
15+
})
16+
17+
// issue 2027
18+
describe('issue 2027 - order objectSupport > Duration', () => {
19+
it('add Duration object returns correct date', () => {
20+
const baseDate = dayjs('2022-06-26T14:01:02.003')
21+
const durationToAdd = dayjs.duration(6, 'hours')
22+
const testDate = baseDate.add(durationToAdd)
23+
24+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 20:01:02.003')
25+
})
26+
it('subtract Duration object returns correct date', () => {
27+
const baseDate = dayjs('2022-06-26T14:01:02.003')
28+
const durationToAdd = dayjs.duration(6, 'hours')
29+
const testDate = baseDate.subtract(durationToAdd)
30+
31+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 08:01:02.003')
32+
})
33+
34+
it('add number with unit returns correct date', () => {
35+
const baseDate = dayjs('2022-06-26T14:01:02.003')
36+
const testDate = baseDate.add(6, 'hours')
37+
38+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 20:01:02.003')
39+
})
40+
it('subtract number with unit returns correct date', () => {
41+
const baseDate = dayjs('2022-06-26T14:01:02.003')
42+
const testDate = baseDate.subtract(6, 'hours')
43+
44+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 08:01:02.003')
45+
})
46+
47+
it('parse string returns correct date', () => {
48+
const testDate = dayjs('2022-06-26T14:01:02.003')
49+
50+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 14:01:02.003')
51+
})
52+
it('parse object returns correct date', () => {
53+
const testDate = dayjs({
54+
year: '2022',
55+
month: '05',
56+
day: '26',
57+
hour: '14',
58+
minute: '01',
59+
second: '02',
60+
millisecond: '003'
61+
})
62+
63+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 14:01:02.003')
64+
})
65+
66+
it('set hour with number returns correct date', () => {
67+
const baseDate = dayjs('2022-06-26T14:01:02.003')
68+
const testDate = baseDate.hour(10)
69+
70+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 10:01:02.003')
71+
})
72+
it('set hour with object returns correct date', () => {
73+
const baseDate = dayjs('2022-06-26T14:01:02.003')
74+
const testDate = baseDate.set({ hour: '10' })
75+
76+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 10:01:02.003')
77+
})
78+
})
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import MockDate from 'mockdate'
2+
import dayjs from '../../src'
3+
import duration from '../../src/plugin/duration'
4+
import objectSupport from '../../src/plugin/objectSupport'
5+
6+
dayjs.extend(duration)
7+
dayjs.extend(objectSupport)
8+
9+
beforeEach(() => {
10+
MockDate.set(new Date())
11+
})
12+
13+
afterEach(() => {
14+
MockDate.reset()
15+
})
16+
17+
// issue 2027
18+
describe('issue 2027 - order objectSupport > Duration', () => {
19+
it('add Duration object returns correct date', () => {
20+
const baseDate = dayjs('2022-06-26T14:01:02.003')
21+
const durationToAdd = dayjs.duration(6, 'hours')
22+
const testDate = baseDate.add(durationToAdd)
23+
24+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 20:01:02.003')
25+
})
26+
27+
it('subtract Duration object returns correct date', () => {
28+
const baseDate = dayjs('2022-06-26T14:01:02.003')
29+
const durationToAdd = dayjs.duration(6, 'hours')
30+
const testDate = baseDate.subtract(durationToAdd)
31+
32+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 08:01:02.003')
33+
})
34+
35+
it('add number with unit returns correct date', () => {
36+
const baseDate = dayjs('2022-06-26T14:01:02.003')
37+
const testDate = baseDate.add(6, 'hours')
38+
39+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 20:01:02.003')
40+
})
41+
it('subtract number with unit returns correct date', () => {
42+
const baseDate = dayjs('2022-06-26T14:01:02.003')
43+
const testDate = baseDate.subtract(6, 'hours')
44+
45+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 08:01:02.003')
46+
})
47+
48+
it('parse string returns correct date', () => {
49+
const testDate = dayjs('2022-06-26T14:01:02.003')
50+
51+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 14:01:02.003')
52+
})
53+
it('parse object returns correct date', () => {
54+
const testDate = dayjs({
55+
year: '2022',
56+
month: '05',
57+
day: '26',
58+
hour: '14',
59+
minute: '01',
60+
second: '02',
61+
millisecond: '003'
62+
})
63+
64+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 14:01:02.003')
65+
})
66+
67+
it('set hour with number returns correct date', () => {
68+
const baseDate = dayjs('2022-06-26T14:01:02.003')
69+
const testDate = baseDate.hour(10)
70+
71+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 10:01:02.003')
72+
})
73+
it('set hour with object returns correct date', () => {
74+
const baseDate = dayjs('2022-06-26T14:01:02.003')
75+
const testDate = baseDate.set({ hour: '10' })
76+
77+
expect(testDate.format('YYYY-MM-DD HH:mm:ss.SSS')).toBe('2022-06-26 10:01:02.003')
78+
})
79+
})

test/plugin/bigIntSupport.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import MockDate from 'mockdate'
2+
import moment from 'moment'
3+
import dayjs from '../../src'
4+
import bigIntSupport from '../../src/plugin/bigIntSupport'
5+
6+
dayjs.extend(bigIntSupport)
7+
8+
beforeEach(() => {
9+
MockDate.set(new Date())
10+
})
11+
12+
afterEach(() => {
13+
MockDate.reset()
14+
})
15+
16+
/* global BigInt */
17+
18+
it('Parse BigInt ts and tsms', () => {
19+
const tsms = 1666310421101
20+
const tsmsBig = BigInt(tsms)
21+
const ts = 1666311003
22+
const tsBig = BigInt(ts)
23+
const momentTsms = moment(tsms)
24+
const momentTs = moment.unix(ts)
25+
expect(dayjs(tsms).valueOf()).toBe(momentTsms.valueOf())
26+
expect(dayjs(tsms).valueOf()).toBe(dayjs(tsmsBig).valueOf())
27+
expect(dayjs.unix(ts).valueOf()).toBe(momentTs.valueOf())
28+
expect(dayjs.unix(tsBig).valueOf()).toBe(dayjs.unix(tsBig).valueOf())
29+
})
30+

types/plugin/bigIntSupport.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { PluginFunc } from 'dayjs'
2+
3+
declare module 'dayjs' {
4+
interface ConfigTypeMap {
5+
bigIntSupport: BigInt
6+
}
7+
export function unix(t: BigInt): Dayjs
8+
}
9+
10+
declare const plugin: PluginFunc
11+
export = plugin

0 commit comments

Comments
 (0)