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/setSeconds | |
parent | f26125e039143b92dc0d84e7775f508ab0cdcaa8 (diff) | |
download | node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.tar.gz node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.tar.bz2 node-vendor-38acabfa6089ab8ac469c12b5f55022fb96935e5.zip |
Diffstat (limited to 'date-fns/src/setSeconds')
-rw-r--r-- | date-fns/src/setSeconds/benchmark.js | 21 | ||||
-rw-r--r-- | date-fns/src/setSeconds/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/setSeconds/index.js.flow | 52 | ||||
-rw-r--r-- | date-fns/src/setSeconds/index.ts | 34 | ||||
-rw-r--r-- | date-fns/src/setSeconds/test.ts | 61 |
5 files changed, 172 insertions, 0 deletions
diff --git a/date-fns/src/setSeconds/benchmark.js b/date-fns/src/setSeconds/benchmark.js new file mode 100644 index 0000000..41733f2 --- /dev/null +++ b/date-fns/src/setSeconds/benchmark.js @@ -0,0 +1,21 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import setSeconds from '.' +import moment from 'moment' + +suite('setSeconds', function () { + benchmark('date-fns', function () { + return setSeconds(this.date, 15) + }) + + benchmark('Moment.js', function () { + return this.moment.seconds(15) + }) +}, { + setup: function () { + this.date = new Date() + this.moment = moment() + } +}) diff --git a/date-fns/src/setSeconds/index.d.ts b/date-fns/src/setSeconds/index.d.ts new file mode 100644 index 0000000..a67a07f --- /dev/null +++ b/date-fns/src/setSeconds/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { setSeconds } from 'date-fns' +export default setSeconds diff --git a/date-fns/src/setSeconds/index.js.flow b/date-fns/src/setSeconds/index.js.flow new file mode 100644 index 0000000..056c068 --- /dev/null +++ b/date-fns/src/setSeconds/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, seconds: number) => Date diff --git a/date-fns/src/setSeconds/index.ts b/date-fns/src/setSeconds/index.ts new file mode 100644 index 0000000..5ed16a0 --- /dev/null +++ b/date-fns/src/setSeconds/index.ts @@ -0,0 +1,34 @@ +import toInteger from '../_lib/toInteger/index' +import toDate from '../toDate/index' +import requiredArgs from '../_lib/requiredArgs/index' + +/** + * @name setSeconds + * @category Second Helpers + * @summary Set the seconds to the given date. + * + * @description + * Set the seconds to the given date. + * + * ### 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 be changed + * @param {Number} seconds - the seconds of the new date + * @returns {Date} the new date with the seconds set + * @throws {TypeError} 2 arguments required + * + * @example + * // Set 45 seconds to 1 September 2014 11:30:40: + * const result = setSeconds(new Date(2014, 8, 1, 11, 30, 40), 45) + * //=> Mon Sep 01 2014 11:30:45 + */ +export default function setSeconds(dirtyDate: Date | number, dirtySeconds: number): Date { + requiredArgs(2, arguments) + + const date = toDate(dirtyDate) + const seconds = toInteger(dirtySeconds) + date.setSeconds(seconds) + return date +} diff --git a/date-fns/src/setSeconds/test.ts b/date-fns/src/setSeconds/test.ts new file mode 100644 index 0000000..855398d --- /dev/null +++ b/date-fns/src/setSeconds/test.ts @@ -0,0 +1,61 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'power-assert' +import setSeconds from '.' + +describe('setSeconds', function() { + it('sets the seconds', function() { + const result = setSeconds(new Date(2014, 8 /* Sep */, 1, 11, 30, 40, 500), 45) + assert.deepEqual(result, new Date(2014, 8 /* Sep */, 1, 11, 30, 45, 500)) + }) + + it('accepts a timestamp', function() { + const result = setSeconds( + new Date(2014, 8 /* Sep */, 1, 11, 30, 15).getTime(), + 45 + ) + assert.deepEqual(result, new Date(2014, 8 /* Sep */, 1, 11, 30, 45)) + }) + + it('converts a fractional number to an integer', function() { + const result = setSeconds( + new Date(2014, 8 /* Sep */, 1, 11, 30, 40, 500), + 45.54 + ) + assert.deepEqual(result, new Date(2014, 8 /* Sep */, 1, 11, 30, 45, 500)) + }) + + it('implicitly converts number arguments', function() { + const result = setSeconds( + new Date(2014, 8 /* Sep */, 1, 11, 30, 40, 500), + // @ts-expect-error + '45' + ) + assert.deepEqual(result, new Date(2014, 8 /* Sep */, 1, 11, 30, 45, 500)) + }) + + it('does not mutate the original date', function() { + const date = new Date(2014, 8 /* Sep */, 1, 11, 30, 40) + setSeconds(date, 15) + assert.deepEqual(date, new Date(2014, 8 /* Sep */, 1, 11, 30, 40)) + }) + + it('returns `Invalid Date` if the given date is invalid', function() { + const result = setSeconds(new Date(NaN), 45) + assert(result instanceof Date && isNaN(result.getTime())) + }) + + it('returns `Invalid Date` if the given amount is NaN', function() { + const result = setSeconds( + new Date(2014, 8 /* Sep */, 1, 11, 30, 40, 500), + NaN + ) + assert(result instanceof Date && isNaN(result.getTime())) + }) + + it('throws TypeError exception if passed less than 2 arguments', function() { + assert.throws(setSeconds.bind(null), TypeError) + assert.throws(setSeconds.bind(null, 1), TypeError) + }) +}) |