diff options
Diffstat (limited to 'date-fns/src/setMilliseconds')
-rw-r--r-- | date-fns/src/setMilliseconds/benchmark.js | 21 | ||||
-rw-r--r-- | date-fns/src/setMilliseconds/index.d.ts | 4 | ||||
-rw-r--r-- | date-fns/src/setMilliseconds/index.js.flow | 52 | ||||
-rw-r--r-- | date-fns/src/setMilliseconds/index.ts | 34 | ||||
-rw-r--r-- | date-fns/src/setMilliseconds/test.ts | 64 |
5 files changed, 175 insertions, 0 deletions
diff --git a/date-fns/src/setMilliseconds/benchmark.js b/date-fns/src/setMilliseconds/benchmark.js new file mode 100644 index 0000000..e292c6c --- /dev/null +++ b/date-fns/src/setMilliseconds/benchmark.js @@ -0,0 +1,21 @@ +// @flow +/* eslint-env mocha */ +/* global suite, benchmark */ + +import setMilliseconds from '.' +import moment from 'moment' + +suite('setMilliseconds', function () { + benchmark('date-fns', function () { + return setMilliseconds(this.date, 400) + }) + + benchmark('Moment.js', function () { + return this.moment.milliseconds(400) + }) +}, { + setup: function () { + this.date = new Date() + this.moment = moment() + } +}) diff --git a/date-fns/src/setMilliseconds/index.d.ts b/date-fns/src/setMilliseconds/index.d.ts new file mode 100644 index 0000000..04743a0 --- /dev/null +++ b/date-fns/src/setMilliseconds/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import { setMilliseconds } from 'date-fns' +export default setMilliseconds diff --git a/date-fns/src/setMilliseconds/index.js.flow b/date-fns/src/setMilliseconds/index.js.flow new file mode 100644 index 0000000..d9bc57f --- /dev/null +++ b/date-fns/src/setMilliseconds/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, milliseconds: number) => Date diff --git a/date-fns/src/setMilliseconds/index.ts b/date-fns/src/setMilliseconds/index.ts new file mode 100644 index 0000000..2c55c65 --- /dev/null +++ b/date-fns/src/setMilliseconds/index.ts @@ -0,0 +1,34 @@ +import toInteger from '../_lib/toInteger/index' +import toDate from '../toDate/index' +import requiredArgs from '../_lib/requiredArgs/index' + +/** + * @name setMilliseconds + * @category Millisecond Helpers + * @summary Set the milliseconds to the given date. + * + * @description + * Set the milliseconds 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} milliseconds - the milliseconds of the new date + * @returns {Date} the new date with the milliseconds set + * @throws {TypeError} 2 arguments required + * + * @example + * // Set 300 milliseconds to 1 September 2014 11:30:40.500: + * const result = setMilliseconds(new Date(2014, 8, 1, 11, 30, 40, 500), 300) + * //=> Mon Sep 01 2014 11:30:40.300 + */ +export default function setMilliseconds(dirtyDate: Date | number, dirtyMilliseconds: number): Date { + requiredArgs(2, arguments) + + const date = toDate(dirtyDate) + const milliseconds = toInteger(dirtyMilliseconds) + date.setMilliseconds(milliseconds) + return date +} diff --git a/date-fns/src/setMilliseconds/test.ts b/date-fns/src/setMilliseconds/test.ts new file mode 100644 index 0000000..6becea2 --- /dev/null +++ b/date-fns/src/setMilliseconds/test.ts @@ -0,0 +1,64 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'power-assert' +import setMilliseconds from '.' + +describe('setMilliseconds', function() { + it('sets the milliseconds', function() { + const result = setMilliseconds( + new Date(2014, 8 /* Sep */, 1, 11, 30, 40, 500), + 300 + ) + assert.deepEqual(result, new Date(2014, 8 /* Sep */, 1, 11, 30, 40, 300)) + }) + + it('accepts a timestamp', function() { + const result = setMilliseconds( + new Date(2014, 8 /* Sep */, 1, 11, 30, 15, 750).getTime(), + 755 + ) + assert.deepEqual(result, new Date(2014, 8 /* Sep */, 1, 11, 30, 15, 755)) + }) + + it('converts a fractional number to an integer', function() { + const result = setMilliseconds( + new Date(2014, 8 /* Sep */, 1, 11, 30, 40, 500), + 300.999 + ) + assert.deepEqual(result, new Date(2014, 8 /* Sep */, 1, 11, 30, 40, 300)) + }) + + it('implicitly converts number arguments', function() { + const result = setMilliseconds( + new Date(2014, 8 /* Sep */, 1, 11, 30, 40, 500), + // @ts-expect-error + '300' + ) + assert.deepEqual(result, new Date(2014, 8 /* Sep */, 1, 11, 30, 40, 300)) + }) + + it('does not mutate the original date', function() { + var date = new Date(2014, 8 /* Sep */, 1, 11, 30, 40, 500) + setMilliseconds(date, 137) + assert.deepEqual(date, new Date(2014, 8 /* Sep */, 1, 11, 30, 40, 500)) + }) + + it('returns `Invalid Date` if the given date is invalid', function() { + const result = setMilliseconds(new Date(NaN), 300) + assert(result instanceof Date && isNaN(result.getTime())) + }) + + it('returns `Invalid Date` if the given amount is NaN', function() { + const result = setMilliseconds( + 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(setMilliseconds.bind(null), TypeError) + assert.throws(setMilliseconds.bind(null, 1), TypeError) + }) +}) |