The modern build of lodash's internal baseCopy function as a standalone module for copying object properties.
npx @tessl/cli install tessl/npm-lodash---basecopy@3.0.0lodash._basecopy is a standalone modular package that provides lodash's internal baseCopy utility function. This low-level utility copies properties from a source object to a target object based on an array of property names, serving as a foundational building block for other lodash functions like _.assign and _.defaults.
npm install lodash._basecopyvar baseCopy = require('lodash._basecopy');For AMD:
define(['lodash._basecopy'], function(baseCopy) {
// use baseCopy
});var baseCopy = require('lodash._basecopy');
// Copy specific properties from source to a new object
var source = { name: 'Alice', age: 30, city: 'New York', country: 'USA' };
var result = baseCopy(source, ['name', 'age']);
console.log(result); // { name: 'Alice', age: 30 }
// Copy properties to an existing target object
var target = { role: 'developer' };
baseCopy(source, target, ['name', 'city']);
console.log(target); // { role: 'developer', name: 'Alice', city: 'New York' }Copies the properties of source to object based on the specified property names.
/**
* Copies the properties of `source` to `object`.
*
* @param {Object} source The object to copy properties from.
* @param {Object} [object={}] The object to copy properties to.
* @param {Array} props The property names to copy.
* @returns {Object} Returns `object`.
*/
function baseCopy(source, object, props);Parameters:
source (Object): The object to copy properties fromobject (Object, optional): The object to copy properties to. If not provided, a new empty object is createdprops (Array): The property names to copy as an array of stringsReturns:
object parameter (or newly created object if object was not provided)Usage Patterns:
Two-parameter usage (creates new target object):
var result = baseCopy(sourceObj, ['prop1', 'prop2']);Three-parameter usage (copies to existing target object):
var result = baseCopy(sourceObj, targetObj, ['prop1', 'prop2']);Implementation Details:
object parameter is not provided, the function treats the second parameter as props and creates a new empty object as the targetobject[key] = source[key])props arrayundefined will be assigned to the target objectExamples:
var baseCopy = require('lodash._basecopy');
// Example 1: Basic property copying
var user = { id: 1, name: 'John', email: 'john@example.com', password: 'secret' };
var publicUser = baseCopy(user, ['id', 'name', 'email']);
// Result: { id: 1, name: 'John', email: 'john@example.com' }
// Example 2: Copying to existing object
var config = { theme: 'dark' };
var defaults = { timeout: 5000, retries: 3, theme: 'light' };
baseCopy(defaults, config, ['timeout', 'retries']);
// config is now: { theme: 'dark', timeout: 5000, retries: 3 }
// Example 3: Handling non-existent properties
var source = { a: 1, b: 2 };
var result = baseCopy(source, ['a', 'b', 'c']);
// Result: { a: 1, b: 2, c: undefined }