1 | ;
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = set;
|
7 |
|
8 | var _index = _interopRequireDefault(require("../toDate/index.js"));
|
9 |
|
10 | var _index2 = _interopRequireDefault(require("../setMonth/index.js"));
|
11 |
|
12 | var _index3 = _interopRequireDefault(require("../_lib/toInteger/index.js"));
|
13 |
|
14 | var _index4 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
|
15 |
|
16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
17 |
|
18 | /**
|
19 | * @name set
|
20 | * @category Common Helpers
|
21 | * @summary Set date values to a given date.
|
22 | *
|
23 | * @description
|
24 | * Set date values to a given date.
|
25 | *
|
26 | * Sets time values to date from object `values`.
|
27 | * A value is not set if it is undefined or null or doesn't exist in `values`.
|
28 | *
|
29 | * Note about bundle size: `set` does not internally use `setX` functions from date-fns but instead opts
|
30 | * to use native `Date#setX` methods. If you use this function, you may not want to include the
|
31 | * other `setX` functions that date-fns provides if you are concerned about the bundle size.
|
32 | *
|
33 | * @param {Date|Number} date - the date to be changed
|
34 | * @param {Object} values - an object with options
|
35 | * @param {Number} [values.year] - the number of years to be set
|
36 | * @param {Number} [values.month] - the number of months to be set
|
37 | * @param {Number} [values.date] - the number of days to be set
|
38 | * @param {Number} [values.hours] - the number of hours to be set
|
39 | * @param {Number} [values.minutes] - the number of minutes to be set
|
40 | * @param {Number} [values.seconds] - the number of seconds to be set
|
41 | * @param {Number} [values.milliseconds] - the number of milliseconds to be set
|
42 | * @returns {Date} the new date with options set
|
43 | * @throws {TypeError} 2 arguments required
|
44 | * @throws {RangeError} `values` must be an object
|
45 | *
|
46 | * @example
|
47 | * // Transform 1 September 2014 into 20 October 2015 in a single line:
|
48 | * var result = set(new Date(2014, 8, 20), { year: 2015, month: 9, date: 20 })
|
49 | * //=> Tue Oct 20 2015 00:00:00
|
50 | *
|
51 | * @example
|
52 | * // Set 12 PM to 1 September 2014 01:23:45 to 1 September 2014 12:00:00:
|
53 | * var result = set(new Date(2014, 8, 1, 1, 23, 45), { hours: 12 })
|
54 | * //=> Mon Sep 01 2014 12:23:45
|
55 | */
|
56 | function set(dirtyDate, values) {
|
57 | (0, _index4.default)(2, arguments);
|
58 |
|
59 | if (typeof values !== 'object' || values === null) {
|
60 | throw new RangeError('values parameter must be an object');
|
61 | }
|
62 |
|
63 | var date = (0, _index.default)(dirtyDate); // Check if date is Invalid Date because Date.prototype.setFullYear ignores the value of Invalid Date
|
64 |
|
65 | if (isNaN(date)) {
|
66 | return new Date(NaN);
|
67 | }
|
68 |
|
69 | if (values.year != null) {
|
70 | date.setFullYear(values.year);
|
71 | }
|
72 |
|
73 | if (values.month != null) {
|
74 | date = (0, _index2.default)(date, values.month);
|
75 | }
|
76 |
|
77 | if (values.date != null) {
|
78 | date.setDate((0, _index3.default)(values.date));
|
79 | }
|
80 |
|
81 | if (values.hours != null) {
|
82 | date.setHours((0, _index3.default)(values.hours));
|
83 | }
|
84 |
|
85 | if (values.minutes != null) {
|
86 | date.setMinutes((0, _index3.default)(values.minutes));
|
87 | }
|
88 |
|
89 | if (values.seconds != null) {
|
90 | date.setSeconds((0, _index3.default)(values.seconds));
|
91 | }
|
92 |
|
93 | if (values.milliseconds != null) {
|
94 | date.setMilliseconds((0, _index3.default)(values.milliseconds));
|
95 | }
|
96 |
|
97 | return date;
|
98 | }
|
99 |
|
100 | module.exports = exports.default; |
\ | No newline at end of file |