ac2f78c6e763f2d6cf1078ee6efe404695fe965e
[yaffs-website] / node_modules / uncss / node_modules / lodash / internal / baseSet.js
1 var assignValue = require('./assignValue'),
2     baseToPath = require('./baseToPath'),
3     isIndex = require('./isIndex'),
4     isKey = require('./isKey'),
5     isObject = require('../isObject');
6
7 /**
8  * The base implementation of `_.set`.
9  *
10  * @private
11  * @param {Object} object The object to query.
12  * @param {Array|string} path The path of the property to set.
13  * @param {*} value The value to set.
14  * @param {Function} [customizer] The function to customize path creation.
15  * @returns {Object} Returns `object`.
16  */
17 function baseSet(object, path, value, customizer) {
18   path = isKey(path, object) ? [path + ''] : baseToPath(path);
19
20   var index = -1,
21       length = path.length,
22       lastIndex = length - 1,
23       nested = object;
24
25   while (nested != null && ++index < length) {
26     var key = path[index];
27     if (isObject(nested)) {
28       var newValue = value;
29       if (index != lastIndex) {
30         var objValue = nested[key];
31         newValue = customizer ? customizer(objValue, key, nested) : undefined;
32         if (newValue === undefined) {
33           newValue = objValue == null ? (isIndex(path[index + 1]) ? [] : {}) : objValue;
34         }
35       }
36       assignValue(nested, key, newValue);
37     }
38     nested = nested[key];
39   }
40   return object;
41 }
42
43 module.exports = baseSet;