Repository: date-fns/date-fns
File: src/set/index.js (JavaScript)

Code before:
 * @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 options object must only contain number properties
 * @throws {RangeError} The options object must not contain a NaN
 *
 * @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 || {}

  var date = toDate(dirtyDate)

  var year = values.year == null ? 0 : toInteger(values.year)
  var month = values.month
  var days = values.date == null ? date.getDate() : toInteger(values.date)
  var hours = values.hours == null ? 0 : toInteger(values.hours)
  var minutes = values.minutes == null ? 0 : toInteger(values.minutes)
  var seconds = values.seconds == null ? 0 : toInteger(values.seconds)
  var milliseconds =
    values.milliseconds == null ? 0 : toInteger(values.milliseconds)

  // Check if date is Invalid Date because Date.prototype.setFullYear ignores the value of Invalid Date
  if (isNaN(date)) {
    return new Date(NaN)
  }

  if (year > 0) date.setFullYear(year)
  if (month != null) date.setMonth(toInteger(month), days)
  if (days > 0) date.setDate(days)
  if (hours > 0) date.setHours(hours)
  if (minutes > 0) date.setMinutes(minutes)
  if (seconds > 0) date.setSeconds(seconds)
  if (milliseconds > 0) date.setMilliseconds(milliseconds)


Review comment (suggestion):
Good, but I think we need to add a new exception. Please throw `RangeError` if `values` is not an object (and don't forget to add this to the docs 🙏 )

Code after:
 * @throws {RangeError} `values` must be an object
 *
 * @example
 * // Transform 1 September 2014 into 20 October 2015 in a single line:
 * var result = set(new Date(2014, 8, 20), { year: 2015, month: 9, date: 20 })
 * //=> Tue Oct 20 2015 00:00:00
 *
 * @example
 * // Set 12 PM to 1 September 2014 01:23:45 to 1 September 2014 12:00:00:
 * var result = set(new Date(2014, 8, 1, 1, 23, 45), { hours: 12 })
 * //=> Mon Sep 01 2014 12:23:45
 */

export default function set(dirtyDate, values) {
  if (arguments.length < 2) {
    throw new TypeError(
      '2 arguments required, but only ' + arguments.length + ' present'
    )
  }

  if (typeof values !== 'object' || values === null) {
    throw new RangeError('values parameter must be an object')
  }

  var date = toDate(dirtyDate)

  // 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(date, values.month)
  }

  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))
  }