|
1 | 1 | /** |
2 | 2 | * @license |
3 | 3 | * lodash (Custom Build) <https://lodash.com/> |
4 | | - * Build: `lodash include="isUndefined,isFunction,toArray,includes,union,each,isString,merge,isObject" exports="node"` |
| 4 | + * Build: `lodash include="isUndefined,isFunction,toArray,includes,union,each,isString,merge,isObject,set" exports="node"` |
5 | 5 | * Copyright jQuery Foundation and other contributors <https://jquery.org/> |
6 | 6 | * Released under MIT license <https://lodash.com/license> |
7 | 7 | * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE> |
|
1896 | 1896 | }; |
1897 | 1897 | } |
1898 | 1898 |
|
| 1899 | + /** |
| 1900 | + * The base implementation of `_.set`. |
| 1901 | + * |
| 1902 | + * @private |
| 1903 | + * @param {Object} object The object to query. |
| 1904 | + * @param {Array|string} path The path of the property to set. |
| 1905 | + * @param {*} value The value to set. |
| 1906 | + * @param {Function} [customizer] The function to customize path creation. |
| 1907 | + * @returns {Object} Returns `object`. |
| 1908 | + */ |
| 1909 | + function baseSet(object, path, value, customizer) { |
| 1910 | + path = isKey(path, object) ? [path] : castPath(path); |
| 1911 | + |
| 1912 | + var index = -1, |
| 1913 | + length = path.length, |
| 1914 | + lastIndex = length - 1, |
| 1915 | + nested = object; |
| 1916 | + |
| 1917 | + while (nested != null && ++index < length) { |
| 1918 | + var key = toKey(path[index]); |
| 1919 | + if (isObject(nested)) { |
| 1920 | + var newValue = value; |
| 1921 | + if (index != lastIndex) { |
| 1922 | + var objValue = nested[key]; |
| 1923 | + newValue = customizer ? customizer(objValue, key, nested) : undefined; |
| 1924 | + if (newValue === undefined) { |
| 1925 | + newValue = objValue == null |
| 1926 | + ? (isIndex(path[index + 1]) ? [] : {}) |
| 1927 | + : objValue; |
| 1928 | + } |
| 1929 | + } |
| 1930 | + assignValue(nested, key, newValue); |
| 1931 | + } |
| 1932 | + nested = nested[key]; |
| 1933 | + } |
| 1934 | + return object; |
| 1935 | + } |
| 1936 | + |
1899 | 1937 | /** |
1900 | 1938 | * The base implementation of `_.toString` which doesn't convert nullish |
1901 | 1939 | * values to empty strings. |
|
3993 | 4031 | baseMerge(object, source, srcIndex); |
3994 | 4032 | }); |
3995 | 4033 |
|
| 4034 | + /** |
| 4035 | + * Sets the value at `path` of `object`. If a portion of `path` doesn't exist, |
| 4036 | + * it's created. Arrays are created for missing index properties while objects |
| 4037 | + * are created for all other missing properties. Use `_.setWith` to customize |
| 4038 | + * `path` creation. |
| 4039 | + * |
| 4040 | + * **Note:** This method mutates `object`. |
| 4041 | + * |
| 4042 | + * @static |
| 4043 | + * @memberOf _ |
| 4044 | + * @since 3.7.0 |
| 4045 | + * @category Object |
| 4046 | + * @param {Object} object The object to modify. |
| 4047 | + * @param {Array|string} path The path of the property to set. |
| 4048 | + * @param {*} value The value to set. |
| 4049 | + * @returns {Object} Returns `object`. |
| 4050 | + * @example |
| 4051 | + * |
| 4052 | + * var object = { 'a': [{ 'b': { 'c': 3 } }] }; |
| 4053 | + * |
| 4054 | + * _.set(object, 'a[0].b.c', 4); |
| 4055 | + * console.log(object.a[0].b.c); |
| 4056 | + * // => 4 |
| 4057 | + * |
| 4058 | + * _.set(object, ['x', '0', 'y', 'z'], 5); |
| 4059 | + * console.log(object.x[0].y.z); |
| 4060 | + * // => 5 |
| 4061 | + */ |
| 4062 | + function set(object, path, value) { |
| 4063 | + return object == null ? object : baseSet(object, path, value); |
| 4064 | + } |
| 4065 | + |
3996 | 4066 | /** |
3997 | 4067 | * Creates an array of the own enumerable string keyed property values of `object`. |
3998 | 4068 | * |
|
4182 | 4252 | lodash.merge = merge; |
4183 | 4253 | lodash.property = property; |
4184 | 4254 | lodash.rest = rest; |
| 4255 | + lodash.set = set; |
4185 | 4256 | lodash.toArray = toArray; |
4186 | 4257 | lodash.toPlainObject = toPlainObject; |
4187 | 4258 | lodash.union = union; |
|
0 commit comments