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/max | |
parent | f26125e039143b92dc0d84e7775f508ab0cdcaa8 (diff) | |
download | node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.tar.gz node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.tar.bz2 node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.zip |
Diffstat (limited to 'date-fns/src/max')
-rw-r--r-- | date-fns/src/max/benchmark.js | 27 | ||||
-rw-r--r-- | date-fns/src/max/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/max/index.js.flow | 52 | ||||
-rw-r--r-- | date-fns/src/max/index.ts | 69 | ||||
-rw-r--r-- | date-fns/src/max/test.ts | 92 |
5 files changed, 244 insertions, 0 deletions
diff --git a/date-fns/src/max/benchmark.js b/date-fns/src/max/benchmark.js new file mode 100644 index 0000000..6726268 --- /dev/null +++ b/date-fns/src/max/benchmark.js @@ -0,0 +1,27 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import max from '.' +import moment from 'moment' + +suite( + 'max', + function() { + benchmark('date-fns', function() { + return max([this.dateA, this.dateB]) + }) + + benchmark('Moment.js', function() { + return moment.max(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/max/index.d.ts b/date-fns/src/max/index.d.ts new file mode 100644 index 0000000..733a96e --- /dev/null +++ b/date-fns/src/max/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { max } from 'date-fns' +export default max diff --git a/date-fns/src/max/index.js.flow b/date-fns/src/max/index.js.flow new file mode 100644 index 0000000..8543909 --- /dev/null +++ b/date-fns/src/max/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/max/index.ts b/date-fns/src/max/index.ts new file mode 100644 index 0000000..1c74453 --- /dev/null +++ b/date-fns/src/max/index.ts @@ -0,0 +1,69 @@ +import toDate from '../toDate/index' +import requiredArgs from '../_lib/requiredArgs/index' + +/** + * @name max + * @category Common Helpers + * @summary Return the latest of the given dates. + * + * @description + * Return the latest 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). + * + * - `max` function now accepts an array of dates rather than spread arguments. + * + * ```javascript + * // Before v2.0.0 + * var date1 = new Date(1989, 6, 10) + * var date2 = new Date(1987, 1, 11) + * var maxDate = max(date1, date2) + * + * // v2.0.0 onward: + * var dates = [new Date(1989, 6, 10), new Date(1987, 1, 11)] + * var maxDate = max(dates) + * ``` + * + * @param {Date[]|Number[]} datesArray - the dates to compare + * @returns {Date} the latest of the dates + * @throws {TypeError} 1 argument required + * + * @example + * // Which of these dates is the latest? + * var result = max([ + * new Date(1989, 6, 10), + * new Date(1987, 1, 11), + * new Date(1995, 6, 2), + * new Date(1990, 0, 1) + * ]) + * //=> Sun Jul 02 1995 00:00:00 + */ +export default function max(dirtyDatesArray: Date[] | string[] | number[]): Date { + requiredArgs(1, arguments) + + let datesArray + // `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) { + const currentDate = toDate(dirtyDate) + + if (result === undefined || result < currentDate || isNaN(Number(currentDate))) { + result = currentDate + } + }) + + return result || new Date(NaN) +} diff --git a/date-fns/src/max/test.ts b/date-fns/src/max/test.ts new file mode 100644 index 0000000..a394457 --- /dev/null +++ b/date-fns/src/max/test.ts @@ -0,0 +1,92 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'power-assert' +import max from '.' + +describe('max', function() { + function isInvalidDate(dirtyDate: Date | number): boolean { + return dirtyDate instanceof Date && isNaN(Number(dirtyDate)) + } + + it('returns the latest date', function() { + const result = max([ + new Date(1989, 6 /* Jul */, 10), + new Date(1987, 1 /* Feb */, 11) + ]) + assert.deepEqual(result, new Date(1989, 6 /* Jul */, 10)) + }) + + it('accepts array with more than 2 entries', function() { + const result = max([ + new Date(1987, 1 /* Feb */, 11), + new Date(1989, 6 /* Jul */, 10), + new Date(1995, 6 /* Jul */, 2), + new Date(1990, 0 /* Jan */, 1) + ]) + assert.deepEqual(result, new Date(1995, 6 /* Jul */, 2)) + }) + + it('accepts timestamps', function() { + const result = max([ + new Date(1989, 6 /* Jul */, 10).getTime(), + new Date(1987, 1 /* Feb */, 11).getTime() + ]) + assert.deepEqual(result, new Date(1989, 6 /* Jul */, 10)) + }) + + it('returns `Invalid Date` if any given date is invalid', function() { + const result = max([ + 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', function() { + const result = max([ + 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', function() { + const result = max([]) + assert(isInvalidDate(result)) + }) + + it('converts Array-like objects into Array', function() { + // @ts-expect-error + const result = max({ + '0': new Date(1989, 6 /* Jul */, 10), + '1': new Date(1987, 1 /* Feb */, 11), + length: 2 + }) + assert.deepEqual(result, new Date(1989, 6 /* Jul */, 10)) + }) + + it('converts iterable objects into Array', function() { + const result = max( + // @ts-expect-error + new Set([ + new Date(1989, 6 /* Jul */, 10), + new Date(1987, 1 /* Feb */, 11) + ]) + ) + assert.deepEqual(result, new Date(1989, 6 /* Jul */, 10)) + }) + + it('returns `Invalid Date` if given a non-iterable value', function() { + // @ts-expect-error + const result = max(undefined) + assert(isInvalidDate(result)) + }) + + it('throws TypeError exception if passed less than 1 argument', function() { + assert.throws(max.bind(null), TypeError) + }) +}) |