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/isThisSecond | |
parent | f26125e039143b92dc0d84e7775f508ab0cdcaa8 (diff) | |
download | node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.tar.gz node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.tar.bz2 node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.zip |
Diffstat (limited to 'date-fns/src/isThisSecond')
-rw-r--r-- | date-fns/src/isThisSecond/benchmark.js | 20 | ||||
-rw-r--r-- | date-fns/src/isThisSecond/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/isThisSecond/index.js.flow | 52 | ||||
-rw-r--r-- | date-fns/src/isThisSecond/index.ts | 34 | ||||
-rw-r--r-- | date-fns/src/isThisSecond/test.ts | 39 |
5 files changed, 149 insertions, 0 deletions
diff --git a/date-fns/src/isThisSecond/benchmark.js b/date-fns/src/isThisSecond/benchmark.js new file mode 100644 index 0000000..4fd274b --- /dev/null +++ b/date-fns/src/isThisSecond/benchmark.js @@ -0,0 +1,20 @@ +// @flow +/* eslint-env mocha */ + +/* global suite, benchmark */ + +import isThisSecond from '.' + +suite( + 'isThisSecond', + () => { + benchmark('date-fns', function() { + return isThisSecond(this.date) + }) + }, + { + setup: function() { + this.date = new Date() + } + } +) diff --git a/date-fns/src/isThisSecond/index.d.ts b/date-fns/src/isThisSecond/index.d.ts new file mode 100644 index 0000000..9d45da1 --- /dev/null +++ b/date-fns/src/isThisSecond/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { isThisSecond } from 'date-fns' +export default isThisSecond diff --git a/date-fns/src/isThisSecond/index.js.flow b/date-fns/src/isThisSecond/index.js.flow new file mode 100644 index 0000000..d3f01ee --- /dev/null +++ b/date-fns/src/isThisSecond/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: (date: Date | number) => boolean diff --git a/date-fns/src/isThisSecond/index.ts b/date-fns/src/isThisSecond/index.ts new file mode 100644 index 0000000..61488c6 --- /dev/null +++ b/date-fns/src/isThisSecond/index.ts @@ -0,0 +1,34 @@ +import isSameSecond from '../isSameSecond/index' +import requiredArgs from '../_lib/requiredArgs/index' + +/** + * @name isThisSecond + * @category Second Helpers + * @summary Is the given date in the same second as the current date? + * @pure false + * + * @description + * Is the given date in the same second as the current date? + * + * > ⚠️ Please note that this function is not present in the FP submodule as + * > it uses `Date.now()` internally hence impure and can't be safely curried. + * + * ### 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} date - the date to check + * @returns {Boolean} the date is in this second + * @throws {TypeError} 1 argument required + * + * @example + * // If now is 25 September 2014 18:30:15.500, + * // is 25 September 2014 18:30:15.000 in this second? + * var result = isThisSecond(new Date(2014, 8, 25, 18, 30, 15)) + * //=> true + */ +export default function isThisSecond(dirtyDate: Date | number): boolean { + requiredArgs(1, arguments) + + return isSameSecond(Date.now(), dirtyDate) +} diff --git a/date-fns/src/isThisSecond/test.ts b/date-fns/src/isThisSecond/test.ts new file mode 100644 index 0000000..125542a --- /dev/null +++ b/date-fns/src/isThisSecond/test.ts @@ -0,0 +1,39 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'assert' +import sinon from 'sinon' +import isThisSecond from '.' + +describe('isThisSecond', () => { + let clock: sinon.SinonFakeTimers + beforeEach(() => { + clock = sinon.useFakeTimers( + new Date(2014, 8 /* Sep */, 25, 18, 30, 15, 500).getTime() + ) + }) + + afterEach(() => { + clock.restore() + }) + + it('returns true if the given date and the current date have the same second', () => { + const date = new Date(2014, 8 /* Sep */, 25, 18, 30, 15) + assert(isThisSecond(date) === true) + }) + + it('returns false if the given date and the current date have different seconds', () => { + const date = new Date(2014, 8 /* Sep */, 25, 18, 30, 16) + assert(isThisSecond(date) === false) + }) + + it('accepts a timestamp', () => { + const date = new Date(2014, 8 /* Sep */, 25, 18, 30, 15, 250).getTime() + assert(isThisSecond(date) === true) + }) + + it('throws TypeError exception if passed less than 1 argument', function() { + // @ts-expect-error + assert.throws(isThisSecond.bind(null), TypeError) + }) +}) |