Skip to content

Commit 3ffcbd3

Browse files
author
sttk
committed
1.1.0
1 parent 8a4a497 commit 3ffcbd3

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

README.md

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,55 @@ Usage
6969
// => { a: 2, b: { b1: 'BBB' } }
7070
```
7171

72+
* Can use an array instead of a map as property mapping :
73+
74+
```js
75+
var src = { a: 1, b: { c: 'CCC' }, d: { e: 'EEE' } };
76+
var dst = { a: 9, b: { c: 'xxx' }, d: { e: 'yyy' } };
77+
var fromToProps = [ 'a.b.c', 'd.e' ];
78+
copyProps(src, dst, fromToProps);
79+
// => { a: 1, b: { c: 'CCC' }, d: { e: 'EEE' } }
80+
```
81+
82+
* Can copy reversively (from *dst* to *src*) by boolean flag :
83+
84+
```js
85+
var src = { a: 1, b: { b1: 'bbb' }, c: 'ccc' };
86+
var dst = { a: 2, b: { b1: 'xxx', b2: 'yyy' } };
87+
88+
copyProps(src, dst, true);
89+
// => { a: 2, b: { b1: 'xxx', b2: 'yyy' }, c: 'ccc' }
90+
```
91+
92+
```js
93+
var src = { a: 1, b: { b1: 'bbb' }, c: 'ccc', d: 'ddd' };
94+
var dst = { f: { a: 2, b1: 'xxx', b2: 'yyy' }, e: 'zzz' };
95+
96+
copyProps(src, dst, {
97+
a: 'f.a',
98+
'b.b1': 'f.b1',
99+
'b.b2': 'f.b2',
100+
'c': 'f.c',
101+
}, true);
102+
// => { a: 2, b: { b1: 'bbb', b2: 'yyy' }, c: 'ccc', d: 'ddd' }
103+
```
104+
72105
API
73106
---
74107

75-
### <u>copyProps(src, dst [, fromToProps] [, converter]) => object</u>
108+
### <u>copyProps(src, dst [, fromToProps] [, converter] [, isReversed]) => object</u>
76109

77110
Copy properties of *src* to *dst* deeply.
78-
If *map* is given, it is able to copy between different properties.
111+
If *fromToProps* is given, it is able to copy between different properties.
79112
If *converter* is given, it is able to convert the terminal values.
80113

81114
* **Arguments:**
82115

83116
* **src** [object] : a source object of copy.
84117
* **dst** [object] : a destinate object of copy.
85-
* **fromToProps** [object] : an object mapping properties between *src* and *dst*.
86-
* **converter** [function] : a function to convert terminal values in *src*.
118+
* **fromToProps** [object | array] : an object mapping properties between *src* and *dst*. (optional)
119+
* **converter** [function] : a function to convert terminal values in *src*. (optional)
120+
* **isReversed** [boolean] : a flag to copy reversively. (optional)
87121

88122
* **Return** [object] : *dst* object after copying.
89123

@@ -100,6 +134,8 @@ copyProps(src, dst, {
100134
})
101135
```
102136

137+
*fromToProps* can be an array. In that case, the array works as a map which has pairs of same key and value.
138+
103139
#### API of *converter*
104140

105141
**<u>converter(value, keyChain) => any</u>**

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "copy-props",
3-
"version": "1.0.2",
3+
"version": "1.1.0",
44
"description": "Copy properties deeply between two objects.",
55
"main": "index.js",
66
"files": [

0 commit comments

Comments
 (0)