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/compareDesc | |
parent | f26125e039143b92dc0d84e7775f508ab0cdcaa8 (diff) | |
download | node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.tar.gz node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.tar.bz2 node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.zip |
Diffstat (limited to 'date-fns/src/compareDesc')
-rw-r--r-- | date-fns/src/compareDesc/benchmark.js | 16 | ||||
-rw-r--r-- | date-fns/src/compareDesc/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/compareDesc/index.js.flow | 55 | ||||
-rw-r--r-- | date-fns/src/compareDesc/index.ts | 59 | ||||
-rw-r--r-- | date-fns/src/compareDesc/test.ts | 80 |
5 files changed, 214 insertions, 0 deletions
diff --git a/date-fns/src/compareDesc/benchmark.js b/date-fns/src/compareDesc/benchmark.js new file mode 100644 index 0000000..c67d4bc --- /dev/null +++ b/date-fns/src/compareDesc/benchmark.js @@ -0,0 +1,16 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import compareDesc from '.' + +suite('compareDesc', function () { + benchmark('date-fns', function () { + return compareDesc(this.dateA, this.dateB) + }) +}, { + setup: function () { + this.dateA = new Date() + this.dateB = new Date(this.dateA.getTime() + 604800000) + } +}) diff --git a/date-fns/src/compareDesc/index.d.ts b/date-fns/src/compareDesc/index.d.ts new file mode 100644 index 0000000..673fab5 --- /dev/null +++ b/date-fns/src/compareDesc/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { compareDesc } from 'date-fns' +export default compareDesc diff --git a/date-fns/src/compareDesc/index.js.flow b/date-fns/src/compareDesc/index.js.flow new file mode 100644 index 0000000..c799819 --- /dev/null +++ b/date-fns/src/compareDesc/index.js.flow @@ -0,0 +1,55 @@ +// @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: ( + dateLeft: Date | number, + dateRight: Date | number +) => number diff --git a/date-fns/src/compareDesc/index.ts b/date-fns/src/compareDesc/index.ts new file mode 100644 index 0000000..bdedfa9 --- /dev/null +++ b/date-fns/src/compareDesc/index.ts @@ -0,0 +1,59 @@ +import toDate from '../toDate/index' +import requiredArgs from '../_lib/requiredArgs/index' + +/** + * @name compareDesc + * @category Common Helpers + * @summary Compare the two dates reverse chronologically and return -1, 0 or 1. + * + * @description + * Compare the two dates and return -1 if the first date is after the second, + * 1 if the first date is before the second or 0 if dates are equal. + * + * ### 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). + * + * @param {Date|Number} dateLeft - the first date to compare + * @param {Date|Number} dateRight - the second date to compare + * @returns {Number} the result of the comparison + * @throws {TypeError} 2 arguments required + * + * @example + * // Compare 11 February 1987 and 10 July 1989 reverse chronologically: + * const result = compareDesc(new Date(1987, 1, 11), new Date(1989, 6, 10)) + * //=> 1 + * + * @example + * // Sort the array of dates in reverse chronological order: + * const result = [ + * new Date(1995, 6, 2), + * new Date(1987, 1, 11), + * new Date(1989, 6, 10) + * ].sort(compareDesc) + * //=> [ + * // Sun Jul 02 1995 00:00:00, + * // Mon Jul 10 1989 00:00:00, + * // Wed Feb 11 1987 00:00:00 + * // ] + */ +export default function compareDesc( + dirtyDateLeft: Date | number, + dirtyDateRight: Date | number +): number { + requiredArgs(2, arguments) + + const dateLeft = toDate(dirtyDateLeft) + const dateRight = toDate(dirtyDateRight) + + const diff = dateLeft.getTime() - dateRight.getTime() + + if (diff > 0) { + return -1 + } else if (diff < 0) { + return 1 + // Return 0 if diff is 0; return NaN if diff is NaN + } else { + return diff + } +} diff --git a/date-fns/src/compareDesc/test.ts b/date-fns/src/compareDesc/test.ts new file mode 100644 index 0000000..1ecb557 --- /dev/null +++ b/date-fns/src/compareDesc/test.ts @@ -0,0 +1,80 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import compareDesc from '.' + +describe('compareDesc', function() { + it('returns 0 if the given dates are equal', function() { + const result = compareDesc( + new Date(1989, 6 /* Jul */, 10), + new Date(1989, 6 /* Jul */, 10) + ) + assert(result === 0) + }) + + it('returns 1 if the first date is before the second one', function() { + const result = compareDesc( + new Date(1987, 1 /* Feb */, 11), + new Date(1989, 6 /* Jul */, 10) + ) + assert(result === 1) + }) + + it('returns -1 if the first date is after the second one', function() { + const result = compareDesc( + new Date(1989, 6 /* Jul */, 10), + new Date(1987, 1 /* Feb */, 11) + ) + assert(result === -1) + }) + + it('sorts the dates array in the reverse chronological order when function is passed as the argument to Array.prototype.sort()', function() { + const unsortedArray = [ + new Date(1995, 6 /* Jul */, 2), + new Date(1987, 1 /* Feb */, 11), + new Date(1989, 6 /* Jul */, 10) + ] + + const sortedArray = [ + new Date(1995, 6 /* Jul */, 2), + new Date(1989, 6 /* Jul */, 10), + new Date(1987, 1 /* Feb */, 11) + ] + + unsortedArray.sort(compareDesc) + const result = unsortedArray + + assert.deepStrictEqual(result, sortedArray) + }) + + it('accepts timestamps', function() { + const result = compareDesc( + new Date(1987, 1 /* Feb */, 11).getTime(), + new Date(1989, 6 /* Jul */, 10).getTime() + ) + assert(result === 1) + }) + + it('returns NaN if the first date is `Invalid Date`', function() { + const result = compareDesc(new Date(NaN), new Date(1989, 6 /* Jul */, 10)) + assert(isNaN(result)) + }) + + it('returns NaN if the second date is `Invalid Date`', function() { + const result = compareDesc(new Date(1989, 6 /* Jul */, 10), new Date(NaN)) + assert(isNaN(result)) + }) + + it('returns NaN if the both dates are `Invalid Date`', function() { + const result = compareDesc(new Date(1989, 6 /* Jul */, 10), new Date(NaN)) + assert(isNaN(result)) + }) + + it('throws TypeError exception if passed less than 2 arguments', function() { + //@ts-expect-error + assert.throws(compareDesc.bind(null), TypeError) + //@ts-expect-error + assert.throws(compareDesc.bind(null, 1), TypeError) + }) +}) |