0
# lodash._basecopy
1
2
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.
3
4
## Package Information
5
6
- **Package Name**: lodash._basecopy
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install lodash._basecopy`
10
11
## Core Imports
12
13
```javascript
14
var baseCopy = require('lodash._basecopy');
15
```
16
17
For AMD:
18
19
```javascript
20
define(['lodash._basecopy'], function(baseCopy) {
21
// use baseCopy
22
});
23
```
24
25
## Basic Usage
26
27
```javascript
28
var baseCopy = require('lodash._basecopy');
29
30
// Copy specific properties from source to a new object
31
var source = { name: 'Alice', age: 30, city: 'New York', country: 'USA' };
32
var result = baseCopy(source, ['name', 'age']);
33
console.log(result); // { name: 'Alice', age: 30 }
34
35
// Copy properties to an existing target object
36
var target = { role: 'developer' };
37
baseCopy(source, target, ['name', 'city']);
38
console.log(target); // { role: 'developer', name: 'Alice', city: 'New York' }
39
```
40
41
## Capabilities
42
43
### Property Copying
44
45
Copies the properties of `source` to `object` based on the specified property names.
46
47
```javascript { .api }
48
/**
49
* Copies the properties of `source` to `object`.
50
*
51
* @param {Object} source The object to copy properties from.
52
* @param {Object} [object={}] The object to copy properties to.
53
* @param {Array} props The property names to copy.
54
* @returns {Object} Returns `object`.
55
*/
56
function baseCopy(source, object, props);
57
```
58
59
**Parameters:**
60
61
- `source` (Object): The object to copy properties from
62
- `object` (Object, optional): The object to copy properties to. If not provided, a new empty object is created
63
- `props` (Array): The property names to copy as an array of strings
64
65
**Returns:**
66
67
- (Object): Returns the `object` parameter (or newly created object if `object` was not provided)
68
69
**Usage Patterns:**
70
71
Two-parameter usage (creates new target object):
72
```javascript
73
var result = baseCopy(sourceObj, ['prop1', 'prop2']);
74
```
75
76
Three-parameter usage (copies to existing target object):
77
```javascript
78
var result = baseCopy(sourceObj, targetObj, ['prop1', 'prop2']);
79
```
80
81
**Implementation Details:**
82
83
- If the `object` parameter is not provided, the function treats the second parameter as `props` and creates a new empty object as the target
84
- Properties are copied using direct assignment (`object[key] = source[key]`)
85
- Only copies properties that exist in the `props` array
86
- Does not perform deep copying - only copies the property values directly
87
- If a property doesn't exist on the source object, `undefined` will be assigned to the target object
88
89
**Examples:**
90
91
```javascript
92
var baseCopy = require('lodash._basecopy');
93
94
// Example 1: Basic property copying
95
var user = { id: 1, name: 'John', email: 'john@example.com', password: 'secret' };
96
var publicUser = baseCopy(user, ['id', 'name', 'email']);
97
// Result: { id: 1, name: 'John', email: 'john@example.com' }
98
99
// Example 2: Copying to existing object
100
var config = { theme: 'dark' };
101
var defaults = { timeout: 5000, retries: 3, theme: 'light' };
102
baseCopy(defaults, config, ['timeout', 'retries']);
103
// config is now: { theme: 'dark', timeout: 5000, retries: 3 }
104
105
// Example 3: Handling non-existent properties
106
var source = { a: 1, b: 2 };
107
var result = baseCopy(source, ['a', 'b', 'c']);
108
// Result: { a: 1, b: 2, c: undefined }
109
```