1 | var assignValue = require('./_assignValue'),
|
2 | castPath = require('./_castPath'),
|
3 | isIndex = require('./_isIndex'),
|
4 | isObject = require('./isObject'),
|
5 | toKey = require('./_toKey');
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function baseSet(object, path, value, customizer) {
|
18 | if (!isObject(object)) {
|
19 | return object;
|
20 | }
|
21 | path = castPath(path, object);
|
22 |
|
23 | var index = -1,
|
24 | length = path.length,
|
25 | lastIndex = length - 1,
|
26 | nested = object;
|
27 |
|
28 | while (nested != null && ++index < length) {
|
29 | var key = toKey(path[index]),
|
30 | newValue = value;
|
31 |
|
32 | if (index != lastIndex) {
|
33 | var objValue = nested[key];
|
34 | newValue = customizer ? customizer(objValue, key, nested) : undefined;
|
35 | if (newValue === undefined) {
|
36 | newValue = isObject(objValue)
|
37 | ? objValue
|
38 | : (isIndex(path[index + 1]) ? [] : {});
|
39 | }
|
40 | }
|
41 | assignValue(nested, key, newValue);
|
42 | nested = nested[key];
|
43 | }
|
44 | return object;
|
45 | }
|
46 |
|
47 | module.exports = baseSet;
|