From 6a42e0d7398639238f575d51287daaf4d495a2a3 Mon Sep 17 00:00:00 2001 From: klm Date: Sun, 12 May 2024 13:01:58 +0000 Subject: [PATCH] fix: Add NegativeYear Plugin support (#2640) --- src/plugin/negativeYear/index.js | 23 ++++++++++++++ test/plugin/negativeYear.test.js | 52 ++++++++++++++++++++++++++++++++ types/plugin/negativeYear.d.ts | 4 +++ 3 files changed, 79 insertions(+) create mode 100644 src/plugin/negativeYear/index.js create mode 100644 test/plugin/negativeYear.test.js create mode 100644 types/plugin/negativeYear.d.ts diff --git a/src/plugin/negativeYear/index.js b/src/plugin/negativeYear/index.js new file mode 100644 index 000000000..6a3fbea7a --- /dev/null +++ b/src/plugin/negativeYear/index.js @@ -0,0 +1,23 @@ +export default (_, c, dayjs) => { + const proto = c.prototype + + const parseDate = (cfg) => { + const { date } = cfg + if (typeof date === 'string' && date.charAt(0) === '-') { + const newDate = new Date(date.slice(1)) + const fullYear = newDate.getFullYear() + if (date.indexOf(`-${fullYear}`) !== -1) { + return dayjs(newDate).subtract(fullYear * 2, 'year').toDate() + } + return date + } + return date + } + + const oldParse = proto.parse + proto.parse = function (cfg) { + cfg.date = parseDate.bind(this)(cfg) + oldParse.bind(this)(cfg) + } +} + diff --git a/test/plugin/negativeYear.test.js b/test/plugin/negativeYear.test.js new file mode 100644 index 000000000..a9713fa26 --- /dev/null +++ b/test/plugin/negativeYear.test.js @@ -0,0 +1,52 @@ +import MockDate from 'mockdate' +import dayjs from 'dayjs' +import negativeYear from '../../src/plugin/negativeYear' +import utc from '../../src/plugin/utc' +import { REGEX_PARSE } from '../../src/constant' + + +dayjs.extend(negativeYear) +dayjs.extend(utc) + +beforeEach(() => { + MockDate.set(new Date()) +}) + +afterEach(() => { + MockDate.reset() +}) + +describe('negativeYear', () => { + it('parses negative years', () => { + expect(dayjs('-2020-01-01').year()).toBe(-2020) + const date = '-2021/01/03' + const date2 = '01/03/-2021' + const date3 = '01-03--2021' + const d = date.match(REGEX_PARSE) + expect(dayjs(date).format('YYYY-MM-DD')).toBe('-2021-01-03') + expect(dayjs(date2).format('YYYY-MM-DD')).toBe('Invalid Date') + expect(dayjs(date3).format()).toBe('Invalid Date') + expect(d).toBe(null) + }) + + it('does not parse non-negative years', () => { + expect(dayjs('2020-01-01').year()).toBe(2020) + }) + + it('works with other plugins', () => { + expect(dayjs.utc('-2020-01-01').year()).toBe(-2020) + }) + + it('Add and subtract with negative years', () => { + expect(dayjs('-2006').add(1, 'y')).toEqual(dayjs('-2005')) + expect(dayjs('-2006').subtract(1, 'y')).toEqual(dayjs('-2007')) + expect(dayjs('-2006').add(1, 'y').format('YYYY')).toBe(dayjs('-2005').format('YYYY')) + expect(dayjs('-2006').subtract(1, 'y').format('YYYY')).toBe(dayjs('-2007').format('YYYY')) + }) + + it('Compare date with negative years', () => { + expect(dayjs('-2006').isAfter(dayjs('-2007'))).toBeTruthy() + expect(dayjs('-2006').isBefore(dayjs('-2005'))).toBeTruthy() + expect(dayjs('-2006').isSame('-2006')).toBeTruthy() + }) +}) diff --git a/types/plugin/negativeYear.d.ts b/types/plugin/negativeYear.d.ts new file mode 100644 index 000000000..43bb86878 --- /dev/null +++ b/types/plugin/negativeYear.d.ts @@ -0,0 +1,4 @@ +import {PluginFunc} from 'dayjs' + +declare const plugin: PluginFunc +export = plugin \ No newline at end of file