react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / js-yaml / node_modules / argparse / node_modules / lodash / object / set.js
80742 viewsvar isIndex = require('../internal/isIndex'),1isKey = require('../internal/isKey'),2isObject = require('../lang/isObject'),3toPath = require('../internal/toPath');45/**6* Sets the property value of `path` on `object`. If a portion of `path`7* does not exist it is created.8*9* @static10* @memberOf _11* @category Object12* @param {Object} object The object to augment.13* @param {Array|string} path The path of the property to set.14* @param {*} value The value to set.15* @returns {Object} Returns `object`.16* @example17*18* var object = { 'a': [{ 'b': { 'c': 3 } }] };19*20* _.set(object, 'a[0].b.c', 4);21* console.log(object.a[0].b.c);22* // => 423*24* _.set(object, 'x[0].y.z', 5);25* console.log(object.x[0].y.z);26* // => 527*/28function set(object, path, value) {29if (object == null) {30return object;31}32var pathKey = (path + '');33path = (object[pathKey] != null || isKey(path, object)) ? [pathKey] : toPath(path);3435var index = -1,36length = path.length,37lastIndex = length - 1,38nested = object;3940while (nested != null && ++index < length) {41var key = path[index];42if (isObject(nested)) {43if (index == lastIndex) {44nested[key] = value;45} else if (nested[key] == null) {46nested[key] = isIndex(path[index + 1]) ? [] : {};47}48}49nested = nested[key];50}51return object;52}5354module.exports = set;555657