_assignValue.js 830 B

123456789101112131415161718192021222324252627
  1. var eq = require('./eq');
  2. /** Used for built-in method references. */
  3. var objectProto = Object.prototype;
  4. /** Used to check objects for own properties. */
  5. var hasOwnProperty = objectProto.hasOwnProperty;
  6. /**
  7. * Assigns `value` to `key` of `object` if the existing value is not equivalent
  8. * using [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
  9. * for equality comparisons.
  10. *
  11. * @private
  12. * @param {Object} object The object to modify.
  13. * @param {string} key The key of the property to assign.
  14. * @param {*} value The value to assign.
  15. */
  16. function assignValue(object, key, value) {
  17. var objValue = object[key];
  18. if (!(hasOwnProperty.call(object, key) && eq(objValue, value)) ||
  19. (value === undefined && !(key in object))) {
  20. object[key] = value;
  21. }
  22. }
  23. module.exports = assignValue;