-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New add function that allows adding a duration to a date (#1581) (closes
- Loading branch information
1 parent
16a561d
commit 6266fa2
Showing
14 changed files
with
353 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// @flow | ||
/* eslint-env mocha */ | ||
/* global suite, benchmark */ | ||
|
||
import add from '.' | ||
import moment from 'moment' | ||
|
||
suite( | ||
'add', | ||
function() { | ||
benchmark('date-fns', function() { | ||
return add(this.date, { hours: 5, minutes: 10 }) | ||
}) | ||
|
||
benchmark('Moment.js', function() { | ||
return this.moment.add({ hours: 5, minutes: 10 }, 'seconds') | ||
}) | ||
}, | ||
{ | ||
setup: function() { | ||
this.date = new Date() | ||
this.moment = moment() | ||
} | ||
} | ||
) |
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,4 @@ | ||
// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. | ||
|
||
import { add } from 'date-fns' | ||
export default add |
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,66 @@ | ||
import addMonths from '../addMonths/index.js' | ||
import toDate from '../toDate/index.js' | ||
import requiredArgs from '../_lib/requiredArgs/index.js' | ||
import toInteger from '../_lib/toInteger/index.js' | ||
|
||
/** | ||
* @name add | ||
* @category Common Helpers | ||
* @summary Add the specified years, months, days, hours, minutes and seconds to the given date. | ||
* | ||
* @description | ||
* Add the specified years, months, days, hours, minutes and seconds to the given date. | ||
* | ||
* @param {Date|Number} date - the date to be changed | ||
* @param {Object} duration - the object with years, months, days, hours, minutes and seconds to be added | ||
* | ||
* | Key | Description | | ||
* |----------------|------------------------------------| | ||
* | years | Amount of years to be added | | ||
* | months | Amount of months to be added | | ||
* | days | Amount of days to be added | | ||
* | hours | Amount of hours to be added | | ||
* | minutes | Amount of minutes to be added | | ||
* | seconds | Amount of seconds to be added | | ||
* | ||
* All values default to 0 | ||
* | ||
* @returns {Date} the new date with the seconds added | ||
* @throws {TypeError} 2 arguments required | ||
* | ||
* @example | ||
* // Add the following duration to 1 September 2014, 10:19:50 | ||
* var result = add(new Date(2014, 8, 1, 10, 19, 50), { | ||
* years: 2, | ||
* months: 24, | ||
* days: 7, | ||
* hours: 5, | ||
* minutes: 9, | ||
* seconds: 30, | ||
* }) | ||
* //=> Sat Sep 8 2018 15:29:20 | ||
*/ | ||
export default function add(dirtyDate, duration) { | ||
requiredArgs(2, arguments) | ||
|
||
if (!duration || typeof duration !== 'object') return new Date(NaN) | ||
|
||
const years = 'years' in duration ? toInteger(duration.years) : 0 | ||
const months = 'months' in duration ? toInteger(duration.months) : 0 | ||
const days = 'days' in duration ? toInteger(duration.days) : 0 | ||
const hours = 'hours' in duration ? toInteger(duration.hours) : 0 | ||
const minutes = 'minutes' in duration ? toInteger(duration.minutes) : 0 | ||
const seconds = 'seconds' in duration ? toInteger(duration.seconds) : 0 | ||
|
||
// Add years and months | ||
const dateWithMonths = addMonths(toDate(dirtyDate), months + years * 12) | ||
|
||
// Add days, hours, minutes and seconds | ||
const hoursToAdd = hours + days * 24 | ||
const minutesToAdd = minutes + hoursToAdd * 60 | ||
const secondsToAdd = seconds + minutesToAdd * 60 | ||
const msToAdd = secondsToAdd * 1000 | ||
const finalDate = new Date(dateWithMonths.getTime() + msToAdd) | ||
|
||
return finalDate | ||
} |
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,38 @@ | ||
// @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 = { | ||
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: Object, | ||
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 | ||
} | ||
} | ||
|
||
declare module.exports: (date: Date | number, duration: Object) => Date |
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,78 @@ | ||
// @flow | ||
/* eslint-env mocha */ | ||
|
||
import assert from 'power-assert' | ||
import add from '.' | ||
|
||
describe('add', function() { | ||
it('adds the values from the given object', function() { | ||
var result = add(new Date(2014, 8 /* Sep */, 1, 10, 19, 50), { | ||
seconds: 30, | ||
minutes: 9, | ||
hours: 5, | ||
days: 7, | ||
months: 9, | ||
years: 2 | ||
}) | ||
assert.deepEqual(result, new Date(2017, 5 /* June */, 8, 15, 29, 20)) | ||
}) | ||
|
||
it('returns same date object when passed empty duration values', function() { | ||
var result = add(new Date(2014, 8 /* Sep */, 1, 10).getTime(), {}) | ||
assert.deepEqual(result, new Date(2014, 8 /* Sep */, 1, 10)) | ||
}) | ||
|
||
it('accepts a timestamp', function() { | ||
var result = add(new Date(2014, 8 /* Sep */, 1, 10).getTime(), { hours: 4 }) | ||
assert.deepEqual(result, new Date(2014, 8 /* Sep */, 1, 14)) | ||
}) | ||
|
||
it('converts a fractional number to an integer', function() { | ||
var result = add(new Date(2014, 8 /* Sep */, 1, 10), { hours: 4.2 }) | ||
assert.deepEqual(result, new Date(2014, 8 /* Sep */, 1, 14)) | ||
}) | ||
|
||
it('implicitly converts number arguments', function() { | ||
var result = add(new Date(2014, 8 /* Sep */, 1, 10), { hours: '4.2' }) | ||
assert.deepEqual(result, new Date(2014, 8 /* Sep */, 1, 14)) | ||
}) | ||
|
||
it('does not mutate the original date', function() { | ||
var date = new Date(2014, 8 /* Sep */, 1, 10) | ||
add(date, { hours: 4 }) | ||
assert.deepEqual(date, new Date(2014, 8 /* Sep */, 1, 10)) | ||
}) | ||
|
||
it('works well if the desired month has fewer days and the provided date is in the last day of a month', function() { | ||
var date = new Date(2014, 11 /* Dec */, 31) | ||
var result = add(date, { months: 9 }) | ||
assert.deepEqual(result, new Date(2015, 8 /* Sep */, 30)) | ||
}) | ||
|
||
it('handles dates before 100 AD', function() { | ||
var initialDate = new Date(0) | ||
initialDate.setFullYear(-1, 10 /* Nov */, 30) | ||
initialDate.setHours(0, 0, 0, 0) | ||
var expectedResult = new Date(0) | ||
expectedResult.setFullYear(0, 1 /* Feb */, 29) | ||
expectedResult.setHours(0, 0, 0, 0) | ||
var result = add(initialDate, { months: 3 }) | ||
assert.deepEqual(result, expectedResult) | ||
}) | ||
|
||
it('returns `Invalid Date` if the given date is invalid', function() { | ||
var result = add(new Date(NaN), { hours: 5 }) | ||
assert(result instanceof Date && isNaN(result)) | ||
}) | ||
|
||
it('throws RangeError exception if passed Number as duration', function() { | ||
// $ExpectedMistake | ||
const result = add(new Date(2014, 8, 1), 'wut') | ||
assert(result instanceof Date && isNaN(result)) | ||
}) | ||
|
||
it('throws TypeError exception if passed less than 2 arguments', function() { | ||
assert.throws(add.bind(null), TypeError) | ||
assert.throws(add.bind(null, 1), TypeError) | ||
}) | ||
}) |
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,4 @@ | ||
// This file is generated automatically by `scripts/build/typings.js`. Please, don't change it. | ||
|
||
import { add } from 'date-fns/fp' | ||
export default add |
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,8 @@ | ||
// This file is generated automatically by `scripts/build/fp.js`. Please, don't change it. | ||
|
||
import fn from '../../add/index.js' | ||
import convertToFP from '../_lib/convertToFP/index.js' | ||
|
||
var add = convertToFP(fn, 2) | ||
|
||
export default add |
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,44 @@ | ||
// @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 = { | ||
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: Object, | ||
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 | ||
} | ||
} | ||
|
||
type CurriedFn1<A, R> = <A>(a: A) => R | ||
|
||
type CurriedFn2<A, B, R> = <A>( | ||
a: A | ||
) => CurriedFn1<B, R> | (<A, B>(a: A, b: B) => R) | ||
|
||
declare module.exports: CurriedFn2<Object, Date | number, Date> |
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
Oops, something went wrong.