Home Reference Source

ts_temp/object/propSetBy.js

import curryN from '../function/curryN';
import has from './has';
import propApply from './propApply';
/**
 * Makes a shallow clone of an object, setting or overriding the specified
 * property with the result of `fn` call.
 *
 * **Note:** If property in the object is equal to value by reference then function
 * just returns object without changes
 *
 * @param {String} prop The property name to set
 * @param {Function} fn The function to execute
 * @param {Object} obj The object to clone
 * @return {Object} A new object equivalent to the original except for the changed property.
 * @example
 *
 *      propSetBy('a', x => x+1, {a: 1}); //=> {a: 2}
 */
export default curryN(3, (prop, fn, obj = {}) => {
    const res = propApply(prop, fn, obj);
    if (has(prop, obj) && obj[prop] === res) {
        return obj;
    }
    return {
        ...obj,
        [prop]: res,
    };
});