UNPKG

super-utils-plus

Version:

A superior alternative to Lodash with improved performance, TypeScript support, and developer experience

153 lines (152 loc) 4.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.extendWith = exports.extend = void 0; exports.assign = assign; exports.assignIn = assignIn; exports.assignWith = assignWith; exports.assignInWith = assignInWith; const is_1 = require("../utils/is"); /** * Assigns own enumerable string keyed properties of source objects to the * destination object. Source objects are applied from left to right. * Subsequent sources overwrite property assignments of previous sources. * * @param object - The destination object * @param sources - The source objects * @returns The destination object * * @example * ```ts * const object = { 'a': 1 }; * * assign(object, { 'b': 2 }, { 'c': 3 }); * // => { 'a': 1, 'b': 2, 'c': 3 } * ``` */ function assign(object, ...sources) { if (!sources.length) { return object; } // Use native Object.assign return Object.assign(object, ...sources); } /** * This method is like assign except that it iterates over own and * inherited source properties. * * @param object - The destination object * @param sources - The source objects * @returns The destination object * * @example * ```ts * function Foo() { * this.a = 1; * } * * Foo.prototype.b = 2; * * assignIn({ 'c': 3 }, new Foo); * // => { 'a': 1, 'b': 2, 'c': 3 } * ``` */ function assignIn(object, ...sources) { if (!sources.length) { return object; } const result = { ...object }; for (const source of sources) { // Include inherited properties for (const key in source) { result[key] = source[key]; } } return result; } /** * This method is like assign except that it accepts customizer which * is invoked to produce the assigned values. If customizer returns undefined, * assignment is handled by the method instead. The customizer is invoked * with five arguments: (objValue, srcValue, key, object, source). * * @param object - The destination object * @param sources - The source objects * @param customizer - The function to customize assigned values * @returns The destination object * * @example * ```ts * function customizer(objValue, srcValue) { * return _.isUndefined(objValue) ? srcValue : objValue; * } * * const defaults = _.partialRight(_.assignWith, customizer); * * defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 }); * // => { 'a': 1, 'b': 2 } * ``` */ function assignWith(object, source, customizer) { if (!(0, is_1.isObject)(object) || !(0, is_1.isObject)(source)) { return object; } const result = { ...object }; for (const key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { const objValue = object[key]; const srcValue = source[key]; const newValue = customizer(objValue, srcValue, key, object, source); if (newValue !== undefined) { result[key] = newValue; } else { result[key] = srcValue; } } } return result; } /** * This method is like assignIn except that it accepts customizer which * is invoked to produce the assigned values. If customizer returns undefined, * assignment is handled by the method instead. The customizer is invoked * with five arguments: (objValue, srcValue, key, object, source). * * @param object - The destination object * @param sources - The source objects * @param customizer - The function to customize assigned values * @returns The destination object * * @example * ```ts * function customizer(objValue, srcValue) { * return _.isUndefined(objValue) ? srcValue : objValue; * } * * const defaults = _.partialRight(_.assignInWith, customizer); * * defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 }); * // => { 'a': 1, 'b': 2 } * ``` */ function assignInWith(object, source, customizer) { if (!(0, is_1.isObject)(object) || !(0, is_1.isObject)(source)) { return object; } const result = { ...object }; for (const key in source) { const objValue = object[key]; const srcValue = source[key]; const newValue = customizer(objValue, srcValue, key, object, source); if (newValue !== undefined) { result[key] = newValue; } else { result[key] = srcValue; } } return result; } // Aliases exports.extend = assignIn; exports.extendWith = assignInWith;