/
index.js
100 lines (85 loc) · 3.22 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import toDate from '../toDate/index.js'
import toInteger from '../_lib/toInteger/index.js'
/**
* @name set
* @category Common Helpers
* @summary Set date values to a given date
*
* @description
* Set date values to a given date
*
* The set function takes in an inital Date() paramater, and allows you to set any type of date
* method such as years or months in one object. This way you only need to import one function
* and allows for greater readability.
*
* It is not required to include all values. For example, if you only wanted to set the hours, you
* would not also need to include year, month, etc. All that is required is at least one date method
* you wish to set.
*
* Note about bundle size: set does not internally use setX functions from date-fns but instead opts
* to use native set functions. If you use this function, you may not want to include the
* other setX functions that date-fns provides if you are concerned about the bundle size.
*
* @param {Date|Number} date - the date to be changed
* @param {Object} values - an object with options
* @param {Number} values.year - the number of years to be set
* @param {Number} values.month - the number of months to be set
* @param {Number} values.date - the number of days to be set
* @param {Number} values.hours - the number of hours to be set
* @param {Number} values.minutes - the number of minutes to be set
* @param {Number} values.seconds - the number of seconds to be set
* @param {Number} values.milliseconds - the number of milliseconds to be set
* @returns {Date} the new date with options set
* @throws {TypeError} 2 arguments required
* @throws {RangeError} the value parameter must be an object
*
* @example
* // Set 1 September 2014 to 20 October 2015 in one function
* var result = set(new Date(2014, 8, 20), { year: 2015, month: 9, date: 20 })
* // => 20 October 2015 00:00:00
*
* @example
* // Set 1 September 2014 00:00:00 to 1 September 2014 12:00:00
* var result = set(new Date(2014, 8, 1), { hours: 12 })
* // => 1 September 2014 12:00:00
*/
export default function set(dirtyDate, dirtyOptions) {
if (arguments.length < 2) {
throw new TypeError(
'2 arguments required, but only ' + arguments.length + ' present'
)
}
var values = dirtyOptions
if (typeof values !== 'object' || values === null) {
throw new RangeError('The value parameter must be an object')
}
var date = toDate(dirtyDate)
var daysForMonth =
values.date == null ? date.getDate() : toInteger(values.date)
// Check if date is Invalid Date because Date.prototype.setFullYear ignores the value of Invalid Date
if (isNaN(date)) {
return new Date(NaN)
}
if (values.year != null) {
date.setFullYear(values.year)
}
if (values.month != null) {
date.setMonth(toInteger(values.month), daysForMonth)
}
if (values.date != null) {
date.setDate(toInteger(values.date))
}
if (values.hours != null) {
date.setHours(toInteger(values.hours))
}
if (values.minutes != null) {
date.setMinutes(toInteger(values.minutes))
}
if (values.seconds != null) {
date.setSeconds(toInteger(values.seconds))
}
if (values.milliseconds != null) {
date.setMilliseconds(toInteger(values.milliseconds))
}
return date
}