Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add new function roundToNearestMinutes
- Loading branch information
Showing
13 changed files
with
237 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import toDate from '../toDate' | ||
|
||
/** | ||
* @name roundToNearestMinutes | ||
* @category Minute Helpers | ||
* @summary Rounds the given date to the nearest minute | ||
* | ||
* @description | ||
* Rounds the given date to the nearest minute | ||
* | ||
* @param {Date|String|Number} date - the date to round | ||
* @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 | ||
* | ||
* @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) { | ||
if (arguments.length < 1) { | ||
throw new TypeError('1 argument required, but only none provided present') | ||
} | ||
|
||
var date = toDate(dirtyDate, dirtyOptions) | ||
var addedMinute = date.getSeconds() >= 30 ? 1 : 0 | ||
|
||
return new Date(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes() + addedMinute) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// @flow | ||
/* eslint-env mocha */ | ||
|
||
import assert from 'power-assert' | ||
import roundToNearestMinutes from '.' | ||
|
||
describe('roundToNearestMinutes', function () { | ||
it('rounds given date to the nearest minute', 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)) | ||
}) | ||
|
||
it('accepts a string', function () { | ||
var result = roundToNearestMinutes( | ||
new Date(2014, 6 /* Jul */, 10, 12, 13, 16).toISOString() | ||
) | ||
assert.deepEqual(result, new Date(2014, 6 /* Jul */, 10, 12, 13, 0)) | ||
}) | ||
|
||
it('accepts a timestamp', function () { | ||
var result = roundToNearestMinutes( | ||
new Date(2014, 6 /* Jul */, 10, 12, 13, 16).getTime() | ||
) | ||
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 down <30 seconds', 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)) | ||
}) | ||
|
||
it('does not mutate the original date', function () { | ||
var date = new Date(2014, 6 /* Jul */, 10, 12, 10, 10, 99) | ||
roundToNearestMinutes(date) | ||
assert.deepEqual(date, new Date(2014, 6 /* Jul */, 10, 12, 10, 10, 99)) | ||
}) | ||
|
||
it('returns `Invalid Date` if the given date is invalid', function () { | ||
var result = roundToNearestMinutes(new Date(NaN)) | ||
assert(result instanceof Date && isNaN(result)) | ||
}) | ||
|
||
it('throws `RangeError` if `options.additionalDigits` is not convertable to 0, 1, 2 or undefined`', function () { | ||
// $ExpectedMistake | ||
var block = roundToNearestMinutes.bind(null, new Date(2014, 6 /* Jul */, 10, 12, 10, 30), {additionalDigits: NaN}) | ||
assert.throws(block, RangeError) | ||
}) | ||
|
||
it('throws TypeError exception if passed less than 1 argument', function () { | ||
assert.throws(roundToNearestMinutes.bind(null), TypeError) | ||
}) | ||
}) |
Oops, something went wrong.