CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-lodash-basecopy

The modern build of lodash's internal baseCopy function as a standalone module for copying object properties.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

lodash._basecopy

lodash._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.

Package Information

  • Package Name: lodash._basecopy
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash._basecopy

Core Imports

var baseCopy = require('lodash._basecopy');

For AMD:

define(['lodash._basecopy'], function(baseCopy) {
  // use baseCopy
});

Basic Usage

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' }

Capabilities

Property Copying

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 from
  • object (Object, optional): The object to copy properties to. If not provided, a new empty object is created
  • props (Array): The property names to copy as an array of strings

Returns:

  • (Object): Returns the 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:

  • If the object parameter is not provided, the function treats the second parameter as props and creates a new empty object as the target
  • Properties are copied using direct assignment (object[key] = source[key])
  • Only copies properties that exist in the props array
  • Does not perform deep copying - only copies the property values directly
  • If a property doesn't exist on the source object, undefined will be assigned to the target object

Examples:

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 }
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash._basecopy@3.0.x
Publish Source
CLI
Badge
tessl/npm-lodash-basecopy badge