baseSet.cjs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. "use strict";
  2. // @ts-nocheck
  3. var __importDefault = (this && this.__importDefault) || function (mod) {
  4. return (mod && mod.__esModule) ? mod : { "default": mod };
  5. };
  6. Object.defineProperty(exports, "__esModule", { value: true });
  7. const assignValue_js_1 = __importDefault(require("./assignValue.cjs"));
  8. const castPath_js_1 = __importDefault(require("./castPath.cjs"));
  9. const isIndex_js_1 = __importDefault(require("./isIndex.cjs"));
  10. const isObject_js_1 = __importDefault(require("./isObject.cjs"));
  11. const toKey_js_1 = __importDefault(require("./toKey.cjs"));
  12. /**
  13. * The base implementation of `set`.
  14. *
  15. * @private
  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. * @param {Function} [customizer] The function to customize path creation.
  20. * @returns {Object} Returns `object`.
  21. */
  22. function baseSet(object, path, value, customizer) {
  23. if (!(0, isObject_js_1.default)(object)) {
  24. return object;
  25. }
  26. path = (0, castPath_js_1.default)(path, object);
  27. const length = path.length;
  28. const lastIndex = length - 1;
  29. let index = -1;
  30. let nested = object;
  31. while (nested != null && ++index < length) {
  32. const key = (0, toKey_js_1.default)(path[index]);
  33. let newValue = value;
  34. if (index !== lastIndex) {
  35. const objValue = nested[key];
  36. newValue = customizer ? customizer(objValue, key, nested) : undefined;
  37. if (newValue === undefined) {
  38. newValue = (0, isObject_js_1.default)(objValue)
  39. ? objValue
  40. : (0, isIndex_js_1.default)(path[index + 1])
  41. ? []
  42. : {};
  43. }
  44. }
  45. (0, assignValue_js_1.default)(nested, key, newValue);
  46. nested = nested[key];
  47. }
  48. return object;
  49. }
  50. exports.default = baseSet;