From 815577e3528e0597988ec03d837b018e8b89d741 Mon Sep 17 00:00:00 2001 From: Spencer Snyder Date: Sat, 8 Aug 2020 12:40:06 -0400 Subject: [PATCH 1/3] feat: adds plugin for plural getters/setters --- src/plugin/pluralGetSet/index.js | 21 ++++++ test/plugin/pluralGetSet.test.js | 125 +++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 src/plugin/pluralGetSet/index.js create mode 100644 test/plugin/pluralGetSet.test.js diff --git a/src/plugin/pluralGetSet/index.js b/src/plugin/pluralGetSet/index.js new file mode 100644 index 000000000..3ac3bbdb0 --- /dev/null +++ b/src/plugin/pluralGetSet/index.js @@ -0,0 +1,21 @@ +export default (o, c) => { + const proto = c.prototype + + const pluralAliases = [ + 'milliseconds', + 'seconds', + 'minutes', + 'hours', + 'days', + 'weeks', + 'isoWeeks', + 'months', + 'quarters', + 'years', + 'dates' + ] + + pluralAliases.forEach((alias) => { + proto[alias] = proto[alias.replace(/s$/, '')] + }) +} diff --git a/test/plugin/pluralGetSet.test.js b/test/plugin/pluralGetSet.test.js new file mode 100644 index 000000000..5d46e57c7 --- /dev/null +++ b/test/plugin/pluralGetSet.test.js @@ -0,0 +1,125 @@ +import MockDate from 'mockdate' +import moment from 'moment' +import dayjs from '../../src' +import pluralGetSet from '../../src/plugin/pluralGetSet' + +dayjs.extend(pluralGetSet) + +beforeEach(() => { + MockDate.set(new Date()) +}) + +afterEach(() => { + MockDate.reset() +}) + +it('Years', () => { + expect(dayjs().get('years')).toBe(moment().get('years')) + expect(dayjs().years()).toBe(moment().years()) + expect(dayjs().years(0).valueOf()).toBe(moment().years(0).valueOf()) + expect(dayjs().years(2000).valueOf()).toBe(moment().years(2000).valueOf()) +}) + +it('Months', () => { + expect(dayjs().get('months')).toBe(moment().get('months')) + expect(dayjs().months()).toBe(moment().months()) + expect(dayjs().months(0).valueOf()).toBe(moment().months(0).valueOf()) + expect(dayjs().months(1).valueOf()).toBe(moment().months(1).valueOf()) +}) + +it('Days of Week', () => { + expect(dayjs().get('days')).toBe(moment().get('days')) + expect(dayjs().days()).toBe(moment().days()) + expect(dayjs().days(0).format()).toBe(moment().days(0).format()) + expect(dayjs().days(1).format()).toBe(moment().days(1).format()) +}) + +it('Dates', () => { + expect(dayjs().get('dates')).toBe(moment().get('dates')) + expect(dayjs().dates()).toBe(moment().dates()) + expect(dayjs().dates(0).valueOf()).toBe(moment().dates(0).valueOf()) + expect(dayjs().dates(1).valueOf()).toBe(moment().dates(1).valueOf()) +}) + +it('Hours', () => { + expect(dayjs().get('hours')).toBe(moment().get('hours')) + expect(dayjs().hours()).toBe(moment().hours()) + expect(dayjs().hours(0).valueOf()).toBe(moment().hours(0).valueOf()) + expect(dayjs().hours(1).valueOf()).toBe(moment().hours(1).valueOf()) +}) + +it('Minutes', () => { + expect(dayjs().get('minutes')).toBe(moment().get('minutes')) + expect(dayjs().minutes()).toBe(moment().minutes()) + expect(dayjs().minutes(0).valueOf()).toBe(moment().minutes(0).valueOf()) + expect(dayjs().minutes(1).valueOf()).toBe(moment().minutes(1).valueOf()) +}) + +it('Seconds', () => { + expect(dayjs().get('seconds')).toBe(moment().get('seconds')) + expect(dayjs().seconds()).toBe(moment().seconds()) + expect(dayjs().seconds(0).valueOf()).toBe(moment().seconds(0).valueOf()) + expect(dayjs().seconds(1).valueOf()).toBe(moment().seconds(1).valueOf()) +}) + +it('Milliseconds', () => { + expect(dayjs().get('milliseconds')).toBe(moment().get('milliseconds')) + expect(dayjs().milliseconds()).toBe(moment().milliseconds()) + expect(dayjs().milliseconds(0).valueOf()).toBe(moment().milliseconds(0).valueOf()) + expect(dayjs().milliseconds(1).valueOf()).toBe(moment().milliseconds(1).valueOf()) +}) + +it('Set Dates', () => { + expect(dayjs().date(30).valueOf()).toBe(moment().dates(30).valueOf()) + expect(dayjs().set('dates', 30).valueOf()).toBe(moment().set('dates', 30).valueOf()) +}) + +it('Set Days of Week', () => { + expect(dayjs().days(0).valueOf()).toBe(moment().days(0).valueOf()) + expect(dayjs().set('days', 0).valueOf()).toBe(moment().set('days', 0).valueOf()) +}) + +it('Set Months', () => { + expect(dayjs().months(11).valueOf()).toBe(moment().months(11).valueOf()) + expect(dayjs().set('months', 11).valueOf()).toBe(moment().set('months', 11).valueOf()) +}) + +it('Set Years', () => { + expect(dayjs().years(2008).valueOf()).toBe(moment().year(2008).valueOf()) + expect(dayjs().set('years', 2008).valueOf()).toBe(moment().set('years', 2008).valueOf()) +}) + +it('Set Hours', () => { + expect(dayjs().set('hours', 6).valueOf()).toBe(moment().set('hours', 6).valueOf()) + expect(dayjs().hours(6).valueOf()).toBe(moment().hours(6).valueOf()) +}) + +it('Set Minutes', () => { + expect(dayjs().minutes(59).valueOf()).toBe(moment().minutes(59).valueOf()) + expect(dayjs().set('minutes', 59).valueOf()).toBe(moment().set('minutes', 59).valueOf()) +}) + +it('Set Seconds', () => { + expect(dayjs().seconds(59).valueOf()).toBe(moment().seconds(59).valueOf()) + expect(dayjs().set('second', 59).valueOf()).toBe(moment().set('second', 59).valueOf()) +}) + +it('Set Milliseconds', () => { + expect(dayjs().milliseconds(999).valueOf()).toBe(moment().milliseconds(999).valueOf()) + expect(dayjs().set('millisecond', 999).valueOf()).toBe(moment().set('millisecond', 999).valueOf()) +}) + +it('Set Month and Year in last day of month', () => { + // 2011-07-31 -> 2011-02-28 + const origin = dayjs('2011-07-31T14:48:00.000Z') + const setMonth = origin.set('month', 1) + expect(setMonth.months()).toBe(1) + expect(origin.dates()).toBe(31) + expect(setMonth.dates()).toBe(28) + // 2000-02-29 -> 2001-02-28 + const origin2 = dayjs('2000-02-29T14:48:00.000Z') + const setYear = origin2.set('years', 2001) + expect(setYear.months()).toBe(1) + expect(origin2.dates()).toBe(29) + expect(setYear.dates()).toBe(28) +}) From 64a46c72be3246dfe51b6cabaa99b25f4195bbc1 Mon Sep 17 00:00:00 2001 From: Spencer Snyder Date: Sun, 9 Aug 2020 17:08:42 -0400 Subject: [PATCH 2/3] chore: adds plural get/set type def --- types/plugin/pluralGetSet.d.ts | 40 ++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 types/plugin/pluralGetSet.d.ts diff --git a/types/plugin/pluralGetSet.d.ts b/types/plugin/pluralGetSet.d.ts new file mode 100644 index 000000000..638ee9c4b --- /dev/null +++ b/types/plugin/pluralGetSet.d.ts @@ -0,0 +1,40 @@ +import { PluginFunc, UnitType, ConfigType } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin + +declare module 'dayjs' { + interface Dayjs { + years(): number + + years(value: number): Dayjs + + months(): number + + months(value: number): Dayjs + + dates(): number + + dates(value: number): Dayjs + + days(): number + + days(value: number): Dayjs + + hours(): number + + hours(value: number): Dayjs + + minutes(): number + + minutes(value: number): Dayjs + + seconds(): number + + seconds(value: number): Dayjs + + milliseconds(): number + + milliseconds(value: number): Dayjs + } +} From f2496600865fa47315393c802275a09e415169b2 Mon Sep 17 00:00:00 2001 From: Spencer Snyder Date: Sun, 9 Aug 2020 17:16:00 -0400 Subject: [PATCH 3/3] fix: add weeks to type defs --- types/plugin/pluralGetSet.d.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/types/plugin/pluralGetSet.d.ts b/types/plugin/pluralGetSet.d.ts index 638ee9c4b..ab2d89aaf 100644 --- a/types/plugin/pluralGetSet.d.ts +++ b/types/plugin/pluralGetSet.d.ts @@ -17,6 +17,10 @@ declare module 'dayjs' { dates(value: number): Dayjs + weeks(): number + + weeks(value: number): Dayjs + days(): number days(value: number): Dayjs