index.js 3.2 KB

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