From 9d54684ff5bf1c371f6631e5c7445510d9fa2727 Mon Sep 17 00:00:00 2001 From: Kizer Date: Sat, 13 Oct 2018 18:32:10 +0200 Subject: [PATCH] add option nearestTo --- src/fp/index.js.flow | 4 +- src/fp/roundToNearestMinutes/index.d.ts | 4 ++ src/fp/roundToNearestMinutes/index.js | 8 +++ src/fp/roundToNearestMinutes/index.js.flow | 55 +++++++++++++++++ .../index.d.ts | 4 ++ .../roundToNearestMinutesWithOptions/index.js | 8 +++ .../index.js.flow | 59 +++++++++++++++++++ src/fp/test.js | 10 +++- src/index.js.flow | 1 + src/roundToNearestMinutes/index.d.ts | 4 ++ src/roundToNearestMinutes/index.js | 27 +++++++-- src/roundToNearestMinutes/index.js.flow | 54 +++++++++++++++++ src/roundToNearestMinutes/test.js | 29 +++++++-- typings.d.ts | 11 ++-- 14 files changed, 259 insertions(+), 19 deletions(-) create mode 100644 src/fp/roundToNearestMinutes/index.d.ts create mode 100644 src/fp/roundToNearestMinutes/index.js create mode 100644 src/fp/roundToNearestMinutes/index.js.flow create mode 100644 src/fp/roundToNearestMinutesWithOptions/index.d.ts create mode 100644 src/fp/roundToNearestMinutesWithOptions/index.js create mode 100644 src/fp/roundToNearestMinutesWithOptions/index.js.flow create mode 100644 src/roundToNearestMinutes/index.d.ts create mode 100644 src/roundToNearestMinutes/index.js.flow diff --git a/src/fp/index.js.flow b/src/fp/index.js.flow index 575c909c91..2437aab5af 100644 --- a/src/fp/index.js.flow +++ b/src/fp/index.js.flow @@ -282,8 +282,8 @@ declare module.exports: { minWithOptions: CurriedFn2, parse: CurriedFn3, parseWithOptions: CurriedFn4, - roundToNearestMinutes: CurriedFn1, - roundToNearestMinutesWithOptions: CurriedFn2, + roundToNearestMinutes: CurriedFn2, + roundToNearestMinutesWithOptions: CurriedFn3, setDate: CurriedFn2, setDateWithOptions: CurriedFn3, setDay: CurriedFn2, diff --git a/src/fp/roundToNearestMinutes/index.d.ts b/src/fp/roundToNearestMinutes/index.d.ts new file mode 100644 index 0000000000..d2aaff5272 --- /dev/null +++ b/src/fp/roundToNearestMinutes/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import {roundToNearestMinutes} from 'date-fns/fp' +export = roundToNearestMinutes diff --git a/src/fp/roundToNearestMinutes/index.js b/src/fp/roundToNearestMinutes/index.js new file mode 100644 index 0000000000..a56ffdca76 --- /dev/null +++ b/src/fp/roundToNearestMinutes/index.js @@ -0,0 +1,8 @@ +// This file is generated automatically by `scripts/build/fp.js`. Please, don't change it. + +import fn from '../../roundToNearestMinutes/index.js' +import convertToFP from '../_lib/convertToFP/index.js' + +var roundToNearestMinutes = convertToFP(fn, 2) + +export default roundToNearestMinutes diff --git a/src/fp/roundToNearestMinutes/index.js.flow b/src/fp/roundToNearestMinutes/index.js.flow new file mode 100644 index 0000000000..148996fc2f --- /dev/null +++ b/src/fp/roundToNearestMinutes/index.js.flow @@ -0,0 +1,55 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +type Interval = { + start: Date | string | number, + end: Date | string | number +} + +type Options = { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7, + additionalDigits?: 0 | 1 | 2, + locale?: Locale, + includeSeconds?: boolean, + addSuffix?: boolean, + unit?: 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year', + roundingMethod?: 'floor' | 'ceil' | 'round', + awareOfUnicodeTokens?: boolean +} + +type Locale = { + formatDistance: Function, + formatRelative: Function, + localize: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + formatLong: Object, + date: Function, + time: Function, + dateTime: Function, + match: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 + } +} + +type CurriedFn1 = (a: A) => R + +type CurriedFn2 = (a: A) => CurriedFn1 + | (a: A, b: B) => R + +declare module.exports: CurriedFn2 diff --git a/src/fp/roundToNearestMinutesWithOptions/index.d.ts b/src/fp/roundToNearestMinutesWithOptions/index.d.ts new file mode 100644 index 0000000000..5c11131345 --- /dev/null +++ b/src/fp/roundToNearestMinutesWithOptions/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import {roundToNearestMinutesWithOptions} from 'date-fns/fp' +export = roundToNearestMinutesWithOptions diff --git a/src/fp/roundToNearestMinutesWithOptions/index.js b/src/fp/roundToNearestMinutesWithOptions/index.js new file mode 100644 index 0000000000..0ea1077c8c --- /dev/null +++ b/src/fp/roundToNearestMinutesWithOptions/index.js @@ -0,0 +1,8 @@ +// This file is generated automatically by `scripts/build/fp.js`. Please, don't change it. + +import fn from '../../roundToNearestMinutes/index.js' +import convertToFP from '../_lib/convertToFP/index.js' + +var roundToNearestMinutesWithOptions = convertToFP(fn, 3) + +export default roundToNearestMinutesWithOptions diff --git a/src/fp/roundToNearestMinutesWithOptions/index.js.flow b/src/fp/roundToNearestMinutesWithOptions/index.js.flow new file mode 100644 index 0000000000..0934082a08 --- /dev/null +++ b/src/fp/roundToNearestMinutesWithOptions/index.js.flow @@ -0,0 +1,59 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +type Interval = { + start: Date | string | number, + end: Date | string | number +} + +type Options = { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7, + additionalDigits?: 0 | 1 | 2, + locale?: Locale, + includeSeconds?: boolean, + addSuffix?: boolean, + unit?: 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year', + roundingMethod?: 'floor' | 'ceil' | 'round', + awareOfUnicodeTokens?: boolean +} + +type Locale = { + formatDistance: Function, + formatRelative: Function, + localize: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + formatLong: Object, + date: Function, + time: Function, + dateTime: Function, + match: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 + } +} + +type CurriedFn1 = (a: A) => R + +type CurriedFn2 = (a: A) => CurriedFn1 + | (a: A, b: B) => R + +type CurriedFn3 = (a: A) => CurriedFn2 + | (a: A, b: B) => CurriedFn1 + | (a: A, b: B, c: C) => R + +declare module.exports: CurriedFn3 diff --git a/src/fp/test.js b/src/fp/test.js index d2154b80de..7bec0f5b91 100644 --- a/src/fp/test.js +++ b/src/fp/test.js @@ -1308,12 +1308,18 @@ describe('FP functions', function () { }) it('roundToNearestMinutes', function () { - var result = fp.roundToNearestMinutes()(new Date(2014, 6 /* Jul */, 10, 12, 10, 34, 99)) + var result = fp.roundToNearestMinutes(5)(new Date(2014, 6 /* Jul */, 10, 12, 11, 34, 99)) + assert.deepEqual(result, new Date(2014, 6 /* Jul */, 10, 12, 10)) + + var result = fp.roundToNearestMinutes()(new Date(2014, 6 /* Jul */, 10, 12, 11, 34, 99)) assert.deepEqual(result, new Date(2014, 6 /* Jul */, 10, 12, 11)) }) it('roundToNearestMinutesWithOptions', function () { - var result = fp.addMinutesWithOptions({})()(new Date(2014, 6 /* Jul */, 10, 12, 10, 34, 99)) + var result = fp.roundToNearestMinutesWithOptions({})(5)(new Date(2014, 6 /* Jul */, 10, 12, 11, 34, 99)) + assert.deepEqual(result, new Date(2014, 6 /* Jul */, 10, 12, 10)) + + var result = fp.roundToNearestMinutesWithOptions({})()(new Date(2014, 6 /* Jul */, 10, 12, 11, 34, 99)) assert.deepEqual(result, new Date(2014, 6 /* Jul */, 10, 12, 11)) }) diff --git a/src/index.js.flow b/src/index.js.flow index 841ecd58ae..95606b07f5 100644 --- a/src/index.js.flow +++ b/src/index.js.flow @@ -653,6 +653,7 @@ declare module.exports: { roundToNearestMinutes: ( date: Date | string | number, + nearestTo?: number, options?: Options ) => Date, diff --git a/src/roundToNearestMinutes/index.d.ts b/src/roundToNearestMinutes/index.d.ts new file mode 100644 index 0000000000..c7574e655b --- /dev/null +++ b/src/roundToNearestMinutes/index.d.ts @@ -0,0 +1,4 @@ +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +import {roundToNearestMinutes} from 'date-fns' +export = roundToNearestMinutes diff --git a/src/roundToNearestMinutes/index.js b/src/roundToNearestMinutes/index.js index 7cee17cd79..907e482fba 100644 --- a/src/roundToNearestMinutes/index.js +++ b/src/roundToNearestMinutes/index.js @@ -1,4 +1,5 @@ -import toDate from '../toDate' +import toDate from '../toDate/index.js' +import toInteger from '../_lib/toInteger/index.js' /** * @name roundToNearestMinutes @@ -9,24 +10,40 @@ import toDate from '../toDate' * Rounds the given date to the nearest minute * * @param {Date|String|Number} date - the date to round + * @param {Number} [nearestTo=1] - the closest minute to round to, must be between 1 and 30 inclusive * @param {Options} [options] - the object with options. See [Options]{@link https://date-fns.org/docs/Options} * @param {0|1|2} [options.additionalDigits=2] - passed to `toDate`. See [toDate]{@link https://date-fns.org/docs/toDate} * @returns {Date} the new date rounded to the closest minute * @throws {TypeError} 1 argument required - * @throws {RangeError} `options.additionalDigits` must be 0, 1 or 2 + * @throws {RangeError} `nearestTo` must be between 1 and 30 * * @example * // Round 10 July 2014 12:12:34 to nearest minute: * var result = roundToNearestMinutes(new Date(2014, 6, 10, 12, 12, 34)) * //=> Thu Jul 10 2014 12:13:00 */ -export default function roundToNearestMinutes (dirtyDate, dirtyOptions) { +export default function roundToNearestMinutes (dirtyDate, dirtyNearestTo, dirtyOptions) { if (arguments.length < 1) { throw new TypeError('1 argument required, but only none provided present') } + var nearestTo = arguments.length === 1 ? 1 : toInteger(dirtyNearestTo) + + if (arguments.length === 2 && typeof dirtyNearestTo !== 'number' && typeof dirtyNearestTo !== 'string') { + dirtyOptions = dirtyNearestTo + nearestTo = 1 + } + + if (!nearestTo || nearestTo > 30 || nearestTo < 1) { + throw new RangeError('nearestTo must be between 1 and 30') + } + var date = toDate(dirtyDate, dirtyOptions) - var addedMinute = date.getSeconds() >= 30 ? 1 : 0 + var seconds = date.getSeconds() // relevant if nearestTo is 1, which is the default case + var minutes = date.getMinutes() + seconds / 60 + var roundedMinutes = Math.floor(minutes / nearestTo) * nearestTo + var remainderMinutes = minutes % nearestTo + var addedMinutes = Math.round(remainderMinutes / nearestTo) * nearestTo - return new Date(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes() + addedMinute) + return new Date(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), roundedMinutes + addedMinutes) } diff --git a/src/roundToNearestMinutes/index.js.flow b/src/roundToNearestMinutes/index.js.flow new file mode 100644 index 0000000000..b7b0da25b2 --- /dev/null +++ b/src/roundToNearestMinutes/index.js.flow @@ -0,0 +1,54 @@ +// @flow +// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. + +type Interval = { + start: Date | string | number, + end: Date | string | number +} + +type Options = { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7, + additionalDigits?: 0 | 1 | 2, + locale?: Locale, + includeSeconds?: boolean, + addSuffix?: boolean, + unit?: 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year', + roundingMethod?: 'floor' | 'ceil' | 'round', + awareOfUnicodeTokens?: boolean +} + +type Locale = { + formatDistance: Function, + formatRelative: Function, + localize: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + formatLong: Object, + date: Function, + time: Function, + dateTime: Function, + match: { + ordinalNumber: Function, + era: Function, + quarter: Function, + month: Function, + day: Function, + dayPeriod: Function + }, + options?: { + weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6, + firstWeekContainsDate?: 1 | 2 | 3 | 4 | 5 | 6 | 7 + } +} + +declare module.exports: ( + date: Date | string | number, + nearestTo?: number, + options?: Options +) => Date diff --git a/src/roundToNearestMinutes/test.js b/src/roundToNearestMinutes/test.js index 40cac1aeaf..e775b1d56e 100644 --- a/src/roundToNearestMinutes/test.js +++ b/src/roundToNearestMinutes/test.js @@ -5,7 +5,7 @@ import assert from 'power-assert' import roundToNearestMinutes from '.' describe('roundToNearestMinutes', function () { - it('rounds given date to the nearest minute', function () { + it('rounds given date to the nearest minute by default', function () { var result = roundToNearestMinutes(new Date(2014, 6 /* Jul */, 10, 12, 16, 16)) assert.deepEqual(result, new Date(2014, 6 /* Jul */, 10, 12, 16, 0)) }) @@ -24,12 +24,17 @@ describe('roundToNearestMinutes', function () { assert.deepEqual(result, new Date(2014, 6 /* Jul */, 10, 12, 13, 0)) }) - it('rounds up 30 seconds and above', function () { - var result = roundToNearestMinutes(new Date(2014, 6 /* Jul */, 10, 12, 10, 30)) - assert.deepEqual(result, new Date(2014, 6 /* Jul */, 10, 12, 11, 0)) + it('rounds to the closest x minutes if nearestTo is provided', function () { + var result = roundToNearestMinutes(new Date(2014, 6 /* Jul */, 10, 12, 10, 30), 4) + assert.deepEqual(result, new Date(2014, 6 /* Jul */, 10, 12, 12, 0)) }) - it('rounds down <30 seconds', function () { + it('rounds up >=30 seconds for nearestTo=1', function () { + var result = roundToNearestMinutes(new Date(2014, 6 /* Jul */, 10, 12, 13, 30)) + assert.deepEqual(result, new Date(2014, 6 /* Jul */, 10, 12, 14, 0)) + }) + + it('rounds down <30 seconds for nearestTo=1', function () { var result = roundToNearestMinutes(new Date(2014, 6 /* Jul */, 10, 12, 13, 29, 999)) assert.deepEqual(result, new Date(2014, 6 /* Jul */, 10, 12, 13, 0)) }) @@ -40,6 +45,12 @@ describe('roundToNearestMinutes', function () { assert.deepEqual(date, new Date(2014, 6 /* Jul */, 10, 12, 10, 10, 99)) }) + it('treats second arg as options if not string or number', function () { + // $ExpectedMistake + var result = roundToNearestMinutes(new Date(2014, 6 /* Jul */, 10, 12, 13, 29, 999), {}) + assert.deepEqual(result, new Date(2014, 6 /* Jul */, 10, 12, 13, 0)) + }) + it('returns `Invalid Date` if the given date is invalid', function () { var result = roundToNearestMinutes(new Date(NaN)) assert(result instanceof Date && isNaN(result)) @@ -51,7 +62,13 @@ describe('roundToNearestMinutes', function () { assert.throws(block, RangeError) }) - it('throws TypeError exception if passed less than 1 argument', function () { + it('throws `TypeError` exception if passed less than 1 argument', function () { assert.throws(roundToNearestMinutes.bind(null), TypeError) }) + + it('throws `RangeError` if nearestTo is not between 1 and 30', function () { + var date = new Date(2014, 6 /* Jul */, 10, 12, 10, 30) + assert.throws(roundToNearestMinutes.bind(null, date, 31), RangeError) + assert.throws(roundToNearestMinutes.bind(null, date, 0), RangeError) + }) }) diff --git a/typings.d.ts b/typings.d.ts index d51927c2a9..be4eb761b0 100644 --- a/typings.d.ts +++ b/typings.d.ts @@ -790,6 +790,7 @@ declare module 'date-fns' { function roundToNearestMinutes ( date: Date | string | number, + nearestTo?: number, options?: Options ): Date namespace roundToNearestMinutes {} @@ -3946,10 +3947,10 @@ declare module 'date-fns/fp' { const parseWithOptions: CurriedFn4 namespace parseWithOptions {} - const roundToNearestMinutes: CurriedFn1 + const roundToNearestMinutes: CurriedFn2 namespace roundToNearestMinutes {} - const roundToNearestMinutesWithOptions: CurriedFn2 + const roundToNearestMinutesWithOptions: CurriedFn3 namespace roundToNearestMinutesWithOptions {} const setDate: CurriedFn2 @@ -9369,6 +9370,7 @@ declare module 'date-fns/esm' { function roundToNearestMinutes ( date: Date | string | number, + nearestTo?: number, options?: Options ): Date namespace roundToNearestMinutes {} @@ -12525,10 +12527,10 @@ declare module 'date-fns/esm/fp' { const parseWithOptions: CurriedFn4 namespace parseWithOptions {} - const roundToNearestMinutes: CurriedFn1 + const roundToNearestMinutes: CurriedFn2 namespace roundToNearestMinutes {} - const roundToNearestMinutesWithOptions: CurriedFn2 + const roundToNearestMinutesWithOptions: CurriedFn3 namespace roundToNearestMinutesWithOptions {} const setDate: CurriedFn2 @@ -19646,6 +19648,7 @@ interface dateFns { roundToNearestMinutes( date: Date | string | number, + nearestTo?: number, options?: Options ): Date