Skip to content

Commit 6a42e0d

Browse files
authored
fix: Add NegativeYear Plugin support (#2640)
1 parent 3737c8c commit 6a42e0d

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

src/plugin/negativeYear/index.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export default (_, c, dayjs) => {
2+
const proto = c.prototype
3+
4+
const parseDate = (cfg) => {
5+
const { date } = cfg
6+
if (typeof date === 'string' && date.charAt(0) === '-') {
7+
const newDate = new Date(date.slice(1))
8+
const fullYear = newDate.getFullYear()
9+
if (date.indexOf(`-${fullYear}`) !== -1) {
10+
return dayjs(newDate).subtract(fullYear * 2, 'year').toDate()
11+
}
12+
return date
13+
}
14+
return date
15+
}
16+
17+
const oldParse = proto.parse
18+
proto.parse = function (cfg) {
19+
cfg.date = parseDate.bind(this)(cfg)
20+
oldParse.bind(this)(cfg)
21+
}
22+
}
23+

test/plugin/negativeYear.test.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import MockDate from 'mockdate'
2+
import dayjs from 'dayjs'
3+
import negativeYear from '../../src/plugin/negativeYear'
4+
import utc from '../../src/plugin/utc'
5+
import { REGEX_PARSE } from '../../src/constant'
6+
7+
8+
dayjs.extend(negativeYear)
9+
dayjs.extend(utc)
10+
11+
beforeEach(() => {
12+
MockDate.set(new Date())
13+
})
14+
15+
afterEach(() => {
16+
MockDate.reset()
17+
})
18+
19+
describe('negativeYear', () => {
20+
it('parses negative years', () => {
21+
expect(dayjs('-2020-01-01').year()).toBe(-2020)
22+
const date = '-2021/01/03'
23+
const date2 = '01/03/-2021'
24+
const date3 = '01-03--2021'
25+
const d = date.match(REGEX_PARSE)
26+
expect(dayjs(date).format('YYYY-MM-DD')).toBe('-2021-01-03')
27+
expect(dayjs(date2).format('YYYY-MM-DD')).toBe('Invalid Date')
28+
expect(dayjs(date3).format()).toBe('Invalid Date')
29+
expect(d).toBe(null)
30+
})
31+
32+
it('does not parse non-negative years', () => {
33+
expect(dayjs('2020-01-01').year()).toBe(2020)
34+
})
35+
36+
it('works with other plugins', () => {
37+
expect(dayjs.utc('-2020-01-01').year()).toBe(-2020)
38+
})
39+
40+
it('Add and subtract with negative years', () => {
41+
expect(dayjs('-2006').add(1, 'y')).toEqual(dayjs('-2005'))
42+
expect(dayjs('-2006').subtract(1, 'y')).toEqual(dayjs('-2007'))
43+
expect(dayjs('-2006').add(1, 'y').format('YYYY')).toBe(dayjs('-2005').format('YYYY'))
44+
expect(dayjs('-2006').subtract(1, 'y').format('YYYY')).toBe(dayjs('-2007').format('YYYY'))
45+
})
46+
47+
it('Compare date with negative years', () => {
48+
expect(dayjs('-2006').isAfter(dayjs('-2007'))).toBeTruthy()
49+
expect(dayjs('-2006').isBefore(dayjs('-2005'))).toBeTruthy()
50+
expect(dayjs('-2006').isSame('-2006')).toBeTruthy()
51+
})
52+
})

types/plugin/negativeYear.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {PluginFunc} from 'dayjs'
2+
3+
declare const plugin: PluginFunc
4+
export = plugin

0 commit comments

Comments
 (0)