/
setIn.js
42 lines (39 loc) · 1.56 KB
/
setIn.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import isNil from "../core/isNil";
import _makeTypeErrorFor from "../privates/_makeTypeErrorFor";
import _setIn from "../privates/_setIn";
/**
* Sets the specified key to the given value in a copy of the provided object.<br/>
* All the remaining enumerable keys of the source object will be simply copied in the
* result object without breaking references.<br/>
* If the specified key is not part of the source object, it will be added to the
* result.<br/>
* The main purpose of the function is to work on simple plain objects used as
* data structures, such as JSON objects, and makes no effort to play nice with
* objects created from an OOP perspective (it's not worth it).<br/>
* For example the prototype of the result will be <code>Object</code>'s regardless
* of the <code>source</code>'s one.
* @example
* var user = {name: "John", surname: "Doe", age: 30};
*
* _.setIn(user, "name", "Jane") // => {name: "Jane", surname: "Doe", age: 30}
* _.setIn(user, "gender", "male") // => {name: "John", surname: "Doe", age: 30, gender: "male"}
*
* // `user` still is {name: "John", surname: "Doe", age: 30}
*
* @memberof module:lamb
* @category Object
* @see {@link module:lamb.setKey|setKey}
* @see {@link module:lamb.setPath|setPath}, {@link module:lamb.setPathIn|setPathIn}
* @since 0.18.0
* @param {Object} source
* @param {String} key
* @param {*} value
* @returns {Object}
*/
function setIn (source, key, value) {
if (isNil(source)) {
throw _makeTypeErrorFor(source, "object");
}
return _setIn(source, key, value);
}
export default setIn;