ts_temp/object/pathSet.js
import curryN from '../function/curryN';
import isObject from '../is/object';
import isNumber from '../is/number';
import has from './has';
import assign from '../assign';
/**
* Returns the result of "setting" the portion of the given data structure
* focused by the given paths to the given value.
*
* **Note:** If property in the object is equal to value by reference then function
* just returns object without changes
*
* @param {[String]} paths
* @param {*} value
* @param {Object} obj
* @return {Object}
* @example
*
* pathSet(['a', 'b'], 3, {}) // => { a: { b: 3 } }
*/
export default curryN(3, (paths = [], value, obj = {}) => {
const n = paths.length - 1;
const result = assign(obj);
const key = paths[n];
let val = result;
let v;
for (let i = 0; i < n; i++) {
v = val[paths[i]];
if (isObject(v)) {
v = assign(v);
}
else {
v = isNumber(paths[i + 1]) ? [] : {};
}
val[paths[i]] = v;
val = v;
}
if (has(key, val) && val[key] === value) {
return obj;
}
val[key] = value;
return result;
});