| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|---|
| 4 |
|
|---|
| 5 | const get = require('./get.js');
|
|---|
| 6 | const isUnsafeProperty = require('../../_internal/isUnsafeProperty.js');
|
|---|
| 7 | const assignValue = require('../_internal/assignValue.js');
|
|---|
| 8 | const isIndex = require('../_internal/isIndex.js');
|
|---|
| 9 | const isKey = require('../_internal/isKey.js');
|
|---|
| 10 | const toKey = require('../_internal/toKey.js');
|
|---|
| 11 | const isObject = require('../predicate/isObject.js');
|
|---|
| 12 | const toPath = require('../util/toPath.js');
|
|---|
| 13 |
|
|---|
| 14 | function updateWith(obj, path, updater, customizer) {
|
|---|
| 15 | if (obj == null && !isObject.isObject(obj)) {
|
|---|
| 16 | return obj;
|
|---|
| 17 | }
|
|---|
| 18 | let resolvedPath;
|
|---|
| 19 | if (isKey.isKey(path, obj)) {
|
|---|
| 20 | resolvedPath = [path];
|
|---|
| 21 | }
|
|---|
| 22 | else if (Array.isArray(path)) {
|
|---|
| 23 | resolvedPath = path;
|
|---|
| 24 | }
|
|---|
| 25 | else {
|
|---|
| 26 | resolvedPath = toPath.toPath(path);
|
|---|
| 27 | }
|
|---|
| 28 | const updateValue = updater(get.get(obj, resolvedPath));
|
|---|
| 29 | let current = obj;
|
|---|
| 30 | for (let i = 0; i < resolvedPath.length && current != null; i++) {
|
|---|
| 31 | const key = toKey.toKey(resolvedPath[i]);
|
|---|
| 32 | if (isUnsafeProperty.isUnsafeProperty(key)) {
|
|---|
| 33 | continue;
|
|---|
| 34 | }
|
|---|
| 35 | let newValue;
|
|---|
| 36 | if (i === resolvedPath.length - 1) {
|
|---|
| 37 | newValue = updateValue;
|
|---|
| 38 | }
|
|---|
| 39 | else {
|
|---|
| 40 | const objValue = current[key];
|
|---|
| 41 | const customizerResult = customizer?.(objValue, key, obj);
|
|---|
| 42 | newValue =
|
|---|
| 43 | customizerResult !== undefined
|
|---|
| 44 | ? customizerResult
|
|---|
| 45 | : isObject.isObject(objValue)
|
|---|
| 46 | ? objValue
|
|---|
| 47 | : isIndex.isIndex(resolvedPath[i + 1])
|
|---|
| 48 | ? []
|
|---|
| 49 | : {};
|
|---|
| 50 | }
|
|---|
| 51 | assignValue.assignValue(current, key, newValue);
|
|---|
| 52 | current = current[key];
|
|---|
| 53 | }
|
|---|
| 54 | return obj;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | exports.updateWith = updateWith;
|
|---|