From fd2502ecacd60c8d4ee877aa9908f2199920f12c Mon Sep 17 00:00:00 2001 From: klm-turing Date: Fri, 26 Apr 2024 16:26:56 +0000 Subject: [PATCH 1/8] feat: Add negative year support --- src/index.js | 8 +++++++- test/parse.test.js | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 061ade178..be4f16f76 100644 --- a/src/index.js +++ b/src/index.js @@ -78,8 +78,14 @@ const parseDate = (cfg) => { || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms) } } + const newDate = new Date(date) + const fullYear = newDate.getFullYear() - return new Date(date) // everything else + if (typeof date === 'string' && date.indexOf(`-${fullYear}`) !== -1) { + return dayjs(newDate).subtract(fullYear * 2, 'year').toDate() + } + + return newDate // everything else } class Dayjs { diff --git a/test/parse.test.js b/test/parse.test.js index 9f2dedf2d..e3da2ac57 100644 --- a/test/parse.test.js +++ b/test/parse.test.js @@ -212,4 +212,15 @@ describe('REGEX_PARSE', () => { expect(dayjs(date).valueOf()).toBe(moment(date).valueOf()) expect(d).toBe(null) }) + + it('Parse negative year', () => { + 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('YYYY-MM-DD')).toBe('Invalid Date') + expect(d).toBe(null) + }) }) From 52981d24223b0997163f5aa7e42bea5aed6eccc2 Mon Sep 17 00:00:00 2001 From: klm-turing Date: Fri, 26 Apr 2024 16:39:00 +0000 Subject: [PATCH 2/8] Update parse.test.js Testing format without parameter --- test/parse.test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/parse.test.js b/test/parse.test.js index e3da2ac57..9282d3f57 100644 --- a/test/parse.test.js +++ b/test/parse.test.js @@ -219,8 +219,9 @@ describe('REGEX_PARSE', () => { const date3 = '01-03--2021' const d = date.match(REGEX_PARSE) expect(dayjs(date).format('YYYY-MM-DD')).toBe('-2021-01-03') + expect(dayjs(date).format()).toBe('-2021-01-03T00:00:00-00:15') expect(dayjs(date2).format('YYYY-MM-DD')).toBe('Invalid Date') - expect(dayjs(date3).format('YYYY-MM-DD')).toBe('Invalid Date') + expect(dayjs(date3).format()).toBe('Invalid Date') expect(d).toBe(null) }) }) From a61414b2ab2cf8e4d911e7dcd843a06269f21609 Mon Sep 17 00:00:00 2001 From: klm-turing Date: Fri, 26 Apr 2024 17:38:42 +0000 Subject: [PATCH 3/8] Update manipulate test --- test/manipulate.test.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/manipulate.test.js b/test/manipulate.test.js index 5fe28cb4f..8d1d6d883 100644 --- a/test/manipulate.test.js +++ b/test/manipulate.test.js @@ -74,3 +74,10 @@ it('Add Time with decimal', () => { it('Subtract Time days', () => { expect(dayjs().subtract(1, 'days').valueOf()).toBe(moment().subtract(1, 'days').valueOf()) }) + +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')) +}) From f35ef09dbe20fd8bd04aafd75ba56c85b70f2bdd Mon Sep 17 00:00:00 2001 From: klm-turing Date: Fri, 26 Apr 2024 18:41:55 +0000 Subject: [PATCH 4/8] Add negative years comparison test --- test/manipulate.test.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/manipulate.test.js b/test/manipulate.test.js index 8d1d6d883..d872f7604 100644 --- a/test/manipulate.test.js +++ b/test/manipulate.test.js @@ -81,3 +81,10 @@ it('Add and subtract with negative years', () => { 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() +}) + From ebdb006c0a8697a236bccc2ccab1a32578aa0a96 Mon Sep 17 00:00:00 2001 From: klm-turing Date: Mon, 29 Apr 2024 20:29:33 +0000 Subject: [PATCH 5/8] Create negativeYear Plugin --- src/index.js | 8 +---- src/plugin/negativeYear/index.js | 21 +++++++++++++ test/manipulate.test.js | 14 --------- test/parse.test.js | 12 ------- test/plugin/negativeYear.test.js | 54 ++++++++++++++++++++++++++++++++ types/plugin/negativeYear.d.ts | 4 +++ 6 files changed, 80 insertions(+), 33 deletions(-) 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/index.js b/src/index.js index be4f16f76..061ade178 100644 --- a/src/index.js +++ b/src/index.js @@ -78,14 +78,8 @@ const parseDate = (cfg) => { || 1, d[4] || 0, d[5] || 0, d[6] || 0, ms) } } - const newDate = new Date(date) - const fullYear = newDate.getFullYear() - if (typeof date === 'string' && date.indexOf(`-${fullYear}`) !== -1) { - return dayjs(newDate).subtract(fullYear * 2, 'year').toDate() - } - - return newDate // everything else + return new Date(date) // everything else } class Dayjs { diff --git a/src/plugin/negativeYear/index.js b/src/plugin/negativeYear/index.js new file mode 100644 index 000000000..a6a6161e6 --- /dev/null +++ b/src/plugin/negativeYear/index.js @@ -0,0 +1,21 @@ +export default (_, c, dayjs) => { + const proto = c.prototype + + const parseDate = (cfg) => { + const { date } = cfg + if (!date) return new Date() + const newDate = new Date(date) + const fullYear = newDate.getFullYear() + if (typeof date === 'string' && date.indexOf(`-${fullYear}`) !== -1) { + return dayjs(newDate).subtract(fullYear * 2, 'year').toDate() + } + return newDate + } + + const oldParse = proto.parse + proto.parse = function (cfg) { + cfg.date = parseDate.bind(this)(cfg) + oldParse.bind(this)(cfg) + } +} + diff --git a/test/manipulate.test.js b/test/manipulate.test.js index d872f7604..5fe28cb4f 100644 --- a/test/manipulate.test.js +++ b/test/manipulate.test.js @@ -74,17 +74,3 @@ it('Add Time with decimal', () => { it('Subtract Time days', () => { expect(dayjs().subtract(1, 'days').valueOf()).toBe(moment().subtract(1, 'days').valueOf()) }) - -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/test/parse.test.js b/test/parse.test.js index 9282d3f57..9f2dedf2d 100644 --- a/test/parse.test.js +++ b/test/parse.test.js @@ -212,16 +212,4 @@ describe('REGEX_PARSE', () => { expect(dayjs(date).valueOf()).toBe(moment(date).valueOf()) expect(d).toBe(null) }) - - it('Parse negative year', () => { - 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(date).format()).toBe('-2021-01-03T00:00:00-00:15') - expect(dayjs(date2).format('YYYY-MM-DD')).toBe('Invalid Date') - expect(dayjs(date3).format()).toBe('Invalid Date') - expect(d).toBe(null) - }) }) diff --git a/test/plugin/negativeYear.test.js b/test/plugin/negativeYear.test.js new file mode 100644 index 000000000..6f9edea8a --- /dev/null +++ b/test/plugin/negativeYear.test.js @@ -0,0 +1,54 @@ +import moment from 'moment' +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(`-${moment(date).format('YYYY-MM-DD')}`) + expect(dayjs(date).format()).toBe(`-${moment(date).format()}`) + 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 From ff31f4cf053f5f583f4b1503882057ce30b9a570 Mon Sep 17 00:00:00 2001 From: klm-turing Date: Tue, 30 Apr 2024 18:45:09 +0000 Subject: [PATCH 6/8] Removed cfg.date check and moment from test --- src/plugin/negativeYear/index.js | 1 - test/plugin/negativeYear.test.js | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/plugin/negativeYear/index.js b/src/plugin/negativeYear/index.js index a6a6161e6..fda158cc1 100644 --- a/src/plugin/negativeYear/index.js +++ b/src/plugin/negativeYear/index.js @@ -3,7 +3,6 @@ export default (_, c, dayjs) => { const parseDate = (cfg) => { const { date } = cfg - if (!date) return new Date() const newDate = new Date(date) const fullYear = newDate.getFullYear() if (typeof date === 'string' && date.indexOf(`-${fullYear}`) !== -1) { diff --git a/test/plugin/negativeYear.test.js b/test/plugin/negativeYear.test.js index 6f9edea8a..a9713fa26 100644 --- a/test/plugin/negativeYear.test.js +++ b/test/plugin/negativeYear.test.js @@ -1,4 +1,3 @@ -import moment from 'moment' import MockDate from 'mockdate' import dayjs from 'dayjs' import negativeYear from '../../src/plugin/negativeYear' @@ -24,8 +23,7 @@ describe('negativeYear', () => { const date2 = '01/03/-2021' const date3 = '01-03--2021' const d = date.match(REGEX_PARSE) - expect(dayjs(date).format('YYYY-MM-DD')).toBe(`-${moment(date).format('YYYY-MM-DD')}`) - expect(dayjs(date).format()).toBe(`-${moment(date).format()}`) + 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) From ebeb0db17a55b8e82bf81433844a6aa3f8509699 Mon Sep 17 00:00:00 2001 From: klm-turing Date: Wed, 1 May 2024 14:32:59 +0000 Subject: [PATCH 7/8] make negativeYear return the original untouched date by default --- src/plugin/negativeYear/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugin/negativeYear/index.js b/src/plugin/negativeYear/index.js index fda158cc1..ddd136c45 100644 --- a/src/plugin/negativeYear/index.js +++ b/src/plugin/negativeYear/index.js @@ -8,7 +8,7 @@ export default (_, c, dayjs) => { if (typeof date === 'string' && date.indexOf(`-${fullYear}`) !== -1) { return dayjs(newDate).subtract(fullYear * 2, 'year').toDate() } - return newDate + return date } const oldParse = proto.parse From 982c197f0238541687b18fb72d94e1e4ed8bee93 Mon Sep 17 00:00:00 2001 From: klm-turing Date: Thu, 2 May 2024 18:10:20 +0000 Subject: [PATCH 8/8] Make sure that negativeYear plugin worked in harmony with other plugins --- src/plugin/negativeYear/index.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/plugin/negativeYear/index.js b/src/plugin/negativeYear/index.js index ddd136c45..6a3fbea7a 100644 --- a/src/plugin/negativeYear/index.js +++ b/src/plugin/negativeYear/index.js @@ -3,10 +3,13 @@ export default (_, c, dayjs) => { const parseDate = (cfg) => { const { date } = cfg - const newDate = new Date(date) - const fullYear = newDate.getFullYear() - if (typeof date === 'string' && date.indexOf(`-${fullYear}`) !== -1) { - return dayjs(newDate).subtract(fullYear * 2, 'year').toDate() + 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 }