From 2870a23d973e1b05c178393eb77682d377549f0e Mon Sep 17 00:00:00 2001 From: iamkun Date: Thu, 28 Mar 2019 17:10:24 +0800 Subject: [PATCH] fix: Add plugin minMax to sopport .max .min --- src/plugin/minMax/index.js | 28 +++++++++++++++++++++++ test/plugin/minMax.test.js | 46 ++++++++++++++++++++++++++++++++++++++ types/plugin/minMax.d.ts | 11 +++++++++ 3 files changed, 85 insertions(+) create mode 100644 src/plugin/minMax/index.js create mode 100644 test/plugin/minMax.test.js create mode 100644 types/plugin/minMax.d.ts diff --git a/src/plugin/minMax/index.js b/src/plugin/minMax/index.js new file mode 100644 index 000000000..d96d0568f --- /dev/null +++ b/src/plugin/minMax/index.js @@ -0,0 +1,28 @@ +export default (o, c, d) => { + const sortBy = (method, dates) => { + if (!dates.length) { + return d() + } + if (dates.length === 1 && dates[0].length > 0) { + [dates] = dates + } + let result + [result] = dates + for (let i = 1; i < dates.length; i += 1) { + if (!dates[i].isValid() || dates[i][method](result)) { + result = dates[i] + } + } + return result + } + + d.max = function () { + const args = [].slice.call(arguments, 0) // eslint-disable-line prefer-rest-params + return sortBy('isAfter', args) + } + d.min = function () { + const args = [].slice.call(arguments, 0) // eslint-disable-line prefer-rest-params + return sortBy('isBefore', args) + } +} + diff --git a/test/plugin/minMax.test.js b/test/plugin/minMax.test.js new file mode 100644 index 000000000..7cc9db92d --- /dev/null +++ b/test/plugin/minMax.test.js @@ -0,0 +1,46 @@ +import MockDate from 'mockdate' +import dayjs from '../../src' +import minMax from '../../src/plugin/minMax' + +dayjs.extend(minMax) + +beforeEach(() => { + MockDate.set(new Date()) +}) + +afterEach(() => { + MockDate.reset() +}) + +const arg1 = dayjs('2019-01-01') +const arg2 = dayjs('2018-01-01') +const arg3 = dayjs('2017-01-01') +const arg4 = dayjs('Invalid Date') + +it('Return current time if no argument', () => { + expect(dayjs.max().format()) + .toBe(dayjs().format()) + expect(dayjs.min().format()) + .toBe(dayjs().format()) +}) + +it('Compare between arguments', () => { + expect(dayjs.max(arg1, arg2, arg3).format()) + .toBe(arg1.format()) + expect(dayjs.min(arg1, arg2, arg3).format()) + .toBe(arg3.format()) +}) + +it('Compare in array', () => { + expect(dayjs.max([arg1, arg2, arg3]).format()) + .toBe(arg1.format()) + expect(dayjs.min([arg1, arg2, arg3]).format()) + .toBe(arg3.format()) +}) + +it('If Invalid Date return Invalid Date', () => { + expect(dayjs.max(arg1, arg2, arg3, arg4).format()) + .toBe(arg4.format()) + expect(dayjs.min([arg1, arg2, arg3, arg4]).format()) + .toBe(arg4.format()) +}) diff --git a/types/plugin/minMax.d.ts b/types/plugin/minMax.d.ts new file mode 100644 index 000000000..1cca06ef2 --- /dev/null +++ b/types/plugin/minMax.d.ts @@ -0,0 +1,11 @@ +import { PluginFunc, ConfigType } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin + +declare module 'dayjs' { + export function max(dayjs: Dayjs[]): Dayjs + export function max(...dayjs: Dayjs[]): Dayjs + export function min(dayjs: Dayjs[]): Dayjs + export function min(...dayjs: Dayjs[]): Dayjs +}