set.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // @ts-nocheck
  2. import baseSet from "./baseSet.js";
  3. /**
  4. * Sets the value at `path` of `object`. If a portion of `path` doesn't exist,
  5. * it's created. Arrays are created for missing index properties while objects
  6. * are created for all other missing properties. Use `setWith` to customize
  7. * `path` creation.
  8. *
  9. * **Note:** This method mutates `object`.
  10. *
  11. * Inlined to just use set functionality and patch vulnerabilities
  12. * on existing isolated "lodash.set" package.
  13. *
  14. * @since 3.7.0
  15. * @category Object
  16. * @param {Object} object The object to modify.
  17. * @param {Array|string} path The path of the property to set.
  18. * @param {*} value The value to set.
  19. * @returns {Object} Returns `object`.
  20. * @see has, hasIn, get, unset
  21. * @example
  22. *
  23. * const object = { 'a': [{ 'b': { 'c': 3 } }] }
  24. *
  25. * set(object, 'a[0].b.c', 4)
  26. * console.log(object.a[0].b.c)
  27. * // => 4
  28. *
  29. * set(object, ['x', '0', 'y', 'z'], 5)
  30. * console.log(object.x[0].y.z)
  31. * // => 5
  32. */
  33. function set(object, path, value) {
  34. return object == null ? object : baseSet(object, path, value);
  35. }
  36. export default set;