source: node_modules/es-toolkit/dist/compat/object/updateWith.js

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 1.8 KB
RevLine 
[a762898]1'use strict';
2
3Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
5const get = require('./get.js');
6const isUnsafeProperty = require('../../_internal/isUnsafeProperty.js');
7const assignValue = require('../_internal/assignValue.js');
8const isIndex = require('../_internal/isIndex.js');
9const isKey = require('../_internal/isKey.js');
10const toKey = require('../_internal/toKey.js');
11const isObject = require('../predicate/isObject.js');
12const toPath = require('../util/toPath.js');
13
14function 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
57exports.updateWith = updateWith;
Note: See TracBrowser for help on using the repository browser.