diff options
author | Sebastian <sebasjm@gmail.com> | 2021-08-23 16:46:06 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2021-08-23 16:48:30 -0300 |
commit | 38acabfa6089ab8ac469c12b5f55022fb96935e5 (patch) | |
tree | 453dbf70000cc5e338b06201af1eaca8343f8f73 /date-fns/src/min | |
parent | f26125e039143b92dc0d84e7775f508ab0cdcaa8 (diff) | |
download | node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.tar.gz node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.tar.bz2 node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.zip |
Diffstat (limited to 'date-fns/src/min')
-rw-r--r-- | date-fns/src/min/benchmark.js | 27 | ||||
-rw-r--r-- | date-fns/src/min/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/min/index.js.flow | 52 | ||||
-rw-r--r-- | date-fns/src/min/index.ts | 73 | ||||
-rw-r--r-- | date-fns/src/min/test.ts | 93 |
5 files changed, 249 insertions, 0 deletions
diff --git a/date-fns/src/min/benchmark.js b/date-fns/src/min/benchmark.js new file mode 100644 index 0000000..e2a997b --- /dev/null +++ b/date-fns/src/min/benchmark.js @@ -0,0 +1,27 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import min from '.' +import moment from 'moment' + +suite( + 'min', + function() { + benchmark('date-fns', function() { + return min([this.dateA, this.dateB]) + }) + + benchmark('Moment.js', function() { + return moment.min(this.momentA, this.momentB) + }) + }, + { + setup: function() { + this.dateA = new Date() + this.momentA = moment() + this.dateB = new Date(this.dateA.getTime() + 604800000) + this.momentB = this.momentA.clone().add(7, 'days') + } + } +) diff --git a/date-fns/src/min/index.d.ts b/date-fns/src/min/index.d.ts new file mode 100644 index 0000000..f7227e7 --- /dev/null +++ b/date-fns/src/min/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { min } from 'date-fns' +export default min diff --git a/date-fns/src/min/index.js.flow b/date-fns/src/min/index.js.flow new file mode 100644 index 0000000..8543909 --- /dev/null +++ b/date-fns/src/min/index.js.flow @@ -0,0 +1,52 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +export type Interval = { + start: Date | number, + end: Date | number, +} + +export type Locale = { + code?: string, + formatDistance?: (...args: Array<any>) => any, + formatRelative?: (...args: Array<any>) => any, + localize?: { + ordinalNumber: (...args: Array<any>) => any, + era: (...args: Array<any>) => any, + quarter: (...args: Array<any>) => any, + month: (...args: Array<any>) => any, + day: (...args: Array<any>) => any, + dayPeriod: (...args: Array<any>) => any, + }, + formatLong?: { + date: (...args: Array<any>) => any, + time: (...args: Array<any>) => any, + dateTime: (...args: Array<any>) => any, + }, + match?: { + ordinalNumber: (...args: Array<any>) => any, + era: (...args: Array<any>) => any, + quarter: (...args: Array<any>) => any, + month: (...args: Array<any>) => any, + day: (...args: Array<any>) => any, + dayPeriod: (...args: Array<any>) => any, + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7, + }, +} + +export type Duration = { + years?: number, + months?: number, + weeks?: number, + days?: number, + hours?: number, + minutes?: number, + seconds?: number, +} + +export type Day = 0 | 1 | 2 | 3 | 4 | 5 | 6 + +declare module.exports: (datesArray: (Date | number)[]) => Date diff --git a/date-fns/src/min/index.ts b/date-fns/src/min/index.ts new file mode 100644 index 0000000..b29763d --- /dev/null +++ b/date-fns/src/min/index.ts @@ -0,0 +1,73 @@ +import toDate from '../toDate/index' +import requiredArgs from '../_lib/requiredArgs/index' + +/** + * @name min + * @category Common Helpers + * @summary Returns the earliest of the given dates. + * + * @description + * Returns the earliest of the given dates. + * + * ### v2.0.0 breaking changes: + * + * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). + * + * - `min` function now accepts an array of dates rather than spread arguments. + * + * ```javascript + * // Before v2.0.0 + * const date1 = new Date(1989, 6, 10) + * const date2 = new Date(1987, 1, 11) + * const minDate = min(date1, date2) + * + * // v2.0.0 onward: + * const dates = [new Date(1989, 6, 10), new Date(1987, 1, 11)] + * const minDate = min(dates) + * ``` + * + * @param {Date[]|Number[]} datesArray - the dates to compare + * @returns {Date} - the earliest of the dates + * @throws {TypeError} 1 argument required + * + * @example + * // Which of these dates is the earliest? + * const result = min([ + * new Date(1989, 6, 10), + * new Date(1987, 1, 11), + * new Date(1995, 6, 2), + * new Date(1990, 0, 1) + * ]) + * //=> Wed Feb 11 1987 00:00:00 + */ +export default function min(dirtyDatesArray: Date[] | number[]): Date { + requiredArgs(1, arguments) + + let datesArray: Date[] | number[] + // `dirtyDatesArray` is Array, Set or Map, or object with custom `forEach` method + if (dirtyDatesArray && typeof dirtyDatesArray.forEach === 'function') { + datesArray = dirtyDatesArray + // If `dirtyDatesArray` is Array-like Object, convert to Array. + } else if (typeof dirtyDatesArray === 'object' && dirtyDatesArray !== null) { + datesArray = Array.prototype.slice.call(dirtyDatesArray) + } else { + // `dirtyDatesArray` is non-iterable, return Invalid Date + return new Date(NaN) + } + + let result: Date | undefined + + datesArray.forEach(function (dirtyDate: Date | number) { + let currentDate = toDate(dirtyDate) + + if ( + result === undefined || + result > currentDate || + isNaN(currentDate.getDate()) + ) { + result = currentDate + } + }) + + return result || new Date(NaN) +} diff --git a/date-fns/src/min/test.ts b/date-fns/src/min/test.ts new file mode 100644 index 0000000..9bf83ad --- /dev/null +++ b/date-fns/src/min/test.ts @@ -0,0 +1,93 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import min from '.' + +describe('min', () => { + const isInvalidDate = (dirtyDate: Date): boolean => { + return dirtyDate instanceof Date && isNaN(dirtyDate.getDate()) + } + + it('returns the earliest date', () => { + const result = min([ + new Date(1989, 6 /* Jul */, 10), + new Date(1987, 1 /* Feb */, 11), + ]) + assert.deepStrictEqual(result, new Date(1987, 1 /* Feb */, 11)) + }) + + it('accepts array with more than 2 entries', () => { + const result = min([ + new Date(1987, 1 /* Feb */, 11), + new Date(1989, 6 /* Jul */, 10), + new Date(1985, 6 /* Jul */, 2), + new Date(1990, 0 /* Jan */, 1), + ]) + assert.deepStrictEqual(result, new Date(1985, 6 /* Jul */, 2)) + }) + + it('accepts timestamps', () => { + const result = min([ + new Date(1989, 6 /* Jul */, 10).getTime(), + new Date(1987, 1 /* Feb */, 11).getTime(), + ]) + assert.deepStrictEqual(result, new Date(1987, 1 /* Feb */, 11)) + }) + + it('returns `Invalid Date` if any given date is invalid', () => { + const result = min([ + new Date(1989, 6 /* Jul */, 10), + new Date(NaN), + new Date(1987, 1 /* Feb */, 11), + ]) + assert(isInvalidDate(result)) + }) + + it('returns `Invalid Date` if any given value is undefined', () => { + const result = min([ + new Date(1989, 6 /* Jul */, 10), + // @ts-expect-error + undefined, + new Date(1987, 1 /* Feb */, 11), + ]) + assert(isInvalidDate(result)) + }) + + it('returns `Invalid Date` for empty array', () => { + const result = min([]) + assert(isInvalidDate(result)) + }) + + it('converts Array-like objects into Array', () => { + // @ts-expect-error + const result = min({ + '0': new Date(1989, 6 /* Jul */, 10), + '1': new Date(1987, 1 /* Feb */, 11), + length: 2, + }) + assert.deepStrictEqual(result, new Date(1987, 1 /* Feb */, 11)) + }) + + it('converts iterable objects into Array', () => { + const result = min( + // @ts-expect-error + new Set([ + new Date(1989, 6 /* Jul */, 10), + new Date(1987, 1 /* Feb */, 11), + ]) + ) + assert.deepStrictEqual(result, new Date(1987, 1 /* Feb */, 11)) + }) + + it('returns `Invalid Date` if given a non-iterable value', () => { + // @ts-expect-error + const result = min(undefined) + assert(isInvalidDate(result)) + }) + + it('throws TypeError exception if passed less than 1 argument', () => { + // @ts-expect-error + assert.throws(min.bind(null), TypeError) + }) +}) |