Skip to content

Commit cfddead

Browse files
author
sttk
committed
Change the API of converter
1 parent 37151b0 commit cfddead

8 files changed

+514
-271
lines changed

README.md

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ Usage
5050
var src = { a: 1, b: { b1: 'bbb' } };
5151
var dst = { a: 0 };
5252

53-
copyProps(src, dst, function(value, keychain) {
54-
if (keychain === 'a') {
55-
return value * 2;
53+
copyProps(src, dst, function(srcInfo) {
54+
if (srcInfo.keyChain === 'a') {
55+
return srcInfo.value * 2;
5656
}
57-
if (keychain === 'b.b1') {
58-
return value.toUpperCase();
57+
if (srcInfo.keyChain === 'b.b1') {
58+
return srcInfo.value.toUpperCase();
5959
}
6060
});
6161
// => { a: 2, b: { b1: 'BBB' } }
@@ -99,11 +99,11 @@ Usage
9999
var src = { a: 'A', b: undefined, c: null, d: 1 };
100100
var dst = { a: 'a', b: 'b', c: 'c' };
101101
102-
copyProps(src, dst, function(value, key) {
103-
if (key === 'd') {
102+
copyProps(src, dst, function(srcInfo) {
103+
if (srcInfo.keyChain === 'd') {
104104
return undefined;
105105
} else {
106-
return value;
106+
return srcInfo.value;
107107
}
108108
});
109109
// => { a: 'A', b: 'b', c: null }
@@ -115,13 +115,12 @@ Usage
115115
var src = { a: 1, b: 2 };
116116
var dst = {};
117117
118-
copyProps(src, dst, function(srcval, srckey, dstkey, dstval, dstParent) {
119-
var key = dstkey.split('.').pop();
120-
Object.defineProperty(dstParent, key, {
118+
copyProps(src, dst, function(srcInfo, dstInfo) {
119+
Object.defineProperty(dstInfo.parent, dstInfo.key, {
121120
writable: false,
122121
enumerable: true,
123122
configurable: false,
124-
value: srcval * 2
123+
value: srcInfo.value * 2
125124
})
126125
}); // => { a: 2, b: 4 }
127126
@@ -139,15 +138,15 @@ Copy properties of *src* to *dst* deeply.
139138
If *fromto* is given, it is able to copy between different properties.
140139
If *converter* is given, it is able to convert the terminal values.
141140

142-
* **Arguments:**
141+
**Arguments:**
143142

144-
* **src** [object] : a source object of copy.
145-
* **dst** [object] : a destinate object of copy.
146-
* **fromto** [object | array] : an object mapping properties between *src* and *dst*. (optional)
147-
* **converter** [function] : a function to convert terminal values in *src*. (optional)
148-
* **reverse** [boolean] : copys reversively from dst to src and returns src object. `fromto` is also reversively used from value to key. This default value is `false`. (optional)
143+
* **src** [object] : a source object of copy.
144+
* **dst** [object] : a destinate object of copy.
145+
* **fromto** [object | array] : an object mapping properties between *src* and *dst*. (optional)
146+
* **converter** [function] : a function to convert terminal values in *src*. (optional)
147+
* **reverse** [boolean] : copys reversively from dst to src and returns src object. `fromto` is also reversively used from value to key. This default value is `false`. (optional)
149148

150-
* **Return** [object] : *dst* object after copying.
149+
**Return** [object] : *dst* object after copying.
151150

152151
#### *Format of fromto*
153152

@@ -166,19 +165,30 @@ copyProps(src, dst, {
166165

167166
#### *API of converter*
168167

169-
**<u>converter(srcValue, srcKeychain, dstKeyChain, dstValue, dstParent) => any</u>**
168+
**<u>converter(srcInfo, dstInfo) => any</u>**
170169

171170
*converter* is a function to convert terminal values of propeerties of *src*.
172171

173-
* **Arguments:**
172+
**Arguments:**
174173

175-
* **srcValue** [any] : a source property value to be converted.
176-
* **srcKeychain** [string] : a source property key string concatenated with dots.
177-
* **dstKeychain** [string] : a destination property key string concatenated with dots.
178-
* **dstValue** [any] : a destination property value before copying.
179-
* **dstParent** [object] : the destination node object which has the copied property.
174+
* **srcInfo** [object] : an object which has informations about the current node of *src*. This object has following properties:
180175

181-
* **Return:** [any] : converted value to be set as a destination property value. If this value is undefined, the destination property is not set to the destination node object.
176+
* **value** : The value of the current node.
177+
* **key** : The key name of the current node.
178+
* **keyChain** : The full key of the current node concatenated with dot.
179+
* **depth** : The depth of the current node.
180+
* **parent** : The parent node of the current node.
181+
182+
* **dstInfo** [object] : an object which has informations about the current node of *dst*. This object has following properties:
183+
184+
* **value** : The value of the current node.
185+
* **key** : The key name of the current node.
186+
* **keyChain** : The full key of the current node concatenated with dot.
187+
* **depth** : The depth of the current node.
188+
* **parent** : The parent node of the current node.
189+
190+
191+
**Return:** [any] : converted value to be set as a destination property value. If this value is undefined, the destination property is not set to the destination node object.
182192

183193
License
184194
-------
@@ -189,7 +199,7 @@ This program is free software under [MIT][mit-url] License.
189199
See the file LICENSE in this distribution for more details.
190200

191201
[repo-url]: https://github.com/sttk/copy-props/
192-
[npm-img]: https://img.shields.io/badge/npm-v1.6.0-blue.svg
202+
[npm-img]: https://img.shields.io/badge/npm-v2.0.0-blue.svg
193203
[npm-url]: https://www.npmjs.org/package/copy-props/
194204
[mit-img]: https://img.shields.io/badge/license-MIT-green.svg
195205
[mit-url]: https://opensource.org/licenses.MIT

index.js

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,25 @@ function copyWithFromto(value, keyChain, nodeInfo) {
8383
dstKeyChains = [dstKeyChains];
8484
}
8585

86+
var srcInfo = {
87+
keyChain: keyChain,
88+
value: value,
89+
key: nodeInfo.name,
90+
depth: nodeInfo.depth,
91+
parent: nodeInfo.parent,
92+
};
93+
8694
for (var i = 0, n = dstKeyChains.length; i < n; i++) {
87-
setDeep(nodeInfo.dest, dstKeyChains[i], function(dstValue, dstParent) {
88-
return nodeInfo.convert(value, keyChain, dstKeyChains[i], dstValue,
89-
dstParent);
95+
setDeep(nodeInfo.dest, dstKeyChains[i], function(parent, key, depth) {
96+
var dstInfo = {
97+
keyChain: dstKeyChains[i],
98+
value: parent[key],
99+
key: key,
100+
depth: depth,
101+
parent: parent,
102+
};
103+
104+
return nodeInfo.convert(srcInfo, dstInfo);
90105
});
91106
}
92107
}
@@ -100,17 +115,33 @@ function copyWithoutFromto(value, keyChain, nodeInfo) {
100115
return;
101116
}
102117

103-
setDeep(nodeInfo.dest, keyChain, function(dstValue, dstParent) {
104-
return nodeInfo.convert(value, keyChain, keyChain, dstValue, dstParent);
118+
var srcInfo = {
119+
keyChain: keyChain,
120+
value: value,
121+
key: nodeInfo.name,
122+
depth: nodeInfo.depth,
123+
parent: nodeInfo.parent,
124+
};
125+
126+
setDeep(nodeInfo.dest, keyChain, function(parent, key, depth) {
127+
var dstInfo = {
128+
keyChain: keyChain,
129+
value: parent[key],
130+
key: key,
131+
depth: depth,
132+
parent: parent,
133+
};
134+
135+
return nodeInfo.convert(srcInfo, dstInfo);
105136
});
106137
}
107138

108139
function newObject() {
109140
return {};
110141
}
111142

112-
function noop(v) {
113-
return v;
143+
function noop(srcInfo) {
144+
return srcInfo.value;
114145
}
115146

116147
function onlyValueIsString(obj) {
@@ -148,13 +179,13 @@ function invert(fromto) {
148179
}
149180

150181
function setDeep(obj, keyChain, valueCreator) {
151-
_setDeep(obj, keyChain.split('.'), valueCreator);
182+
_setDeep(obj, keyChain.split('.'), 1, valueCreator);
152183
}
153184

154-
function _setDeep(obj, keyElems, valueCreator) {
185+
function _setDeep(obj, keyElems, depth, valueCreator) {
155186
var key = keyElems.shift();
156187
if (!keyElems.length) {
157-
var value = valueCreator(obj[key], obj);
188+
var value = valueCreator(obj, key, depth);
158189
if (value !== undefined) {
159190
obj[key] = value;
160191
}
@@ -164,7 +195,7 @@ function _setDeep(obj, keyElems, valueCreator) {
164195
if (!isPlainObject(obj[key])) {
165196
obj[key] = {};
166197
}
167-
_setDeep(obj[key], keyElems, valueCreator);
198+
_setDeep(obj[key], keyElems, depth + 1, valueCreator);
168199
}
169200

170201
function setParentEmptyObject(obj, fromto) {

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.6.0",
3+
"version": "2.0.0",
44
"description": "Copy properties deeply between two objects.",
55
"main": "index.js",
66
"files": [

test/copy-props-args.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,7 @@ describe('Arguments', function() {
9797
it('Should succeed when 3rd arg is a function', function(done) {
9898
var src = { a: 1, b: { c: 2 } };
9999
var dst = { a: 10, b: { d: 20 } };
100-
var converter = function(value) {
101-
return value * 2;
102-
};
100+
var converter = function(srcInfo) { return srcInfo.value * 2; };
103101
var expected = { a: 2, b: { c: 4, d: 20 } };
104102
expect(copyProps(src, dst, converter)).to.deep.equal(expected);
105103
done();
@@ -137,8 +135,8 @@ describe('Arguments', function() {
137135
var dst = { A: 10, B: { D: 20 } };
138136
var fromto = { a: 'A', 'b.c': 'B.C' };
139137
var expected = { A: 2, B: { C: 4, D: 20 } };
140-
expect(copyProps(src, dst, fromto, function(value) {
141-
return value * 2;
138+
expect(copyProps(src, dst, fromto, function(srcInfo) {
139+
return srcInfo.value * 2;
142140
})).to.deep.equal(expected);
143141
done();
144142
});
@@ -178,7 +176,7 @@ describe('Arguments', function() {
178176
var src = { a: 1, b: { c: 2 } };
179177
var dst = { A: 10, B: { D: 20 } };
180178
var fromto = { a: 'A', 'b.c': 'B.C', 'b.d': 'B.D' };
181-
var converter = function(value) { return value * 2; };
179+
var converter = function(srcInfo) { return srcInfo.value * 2; };
182180
var expected = { a: 20, b: { c: 2, d: 40 } };
183181
expect(copyProps(src, dst, fromto, converter, true))
184182
.to.deep.equal(expected);
@@ -189,7 +187,7 @@ describe('Arguments', function() {
189187
var src = { a: 1, b: { c: 2 } };
190188
var dst = { A: 10, B: { D: 20 } };
191189
var fromto = { a: 'A', 'b.c': 'B.C', 'b.d': 'B.D' };
192-
var converter = function(value) { return value * 2; };
190+
var converter = function(srcInfo) { return srcInfo.value * 2; };
193191
var expected = { A: 2, B: { C: 4, D: 20 } };
194192
expect(copyProps(src, dst, fromto, converter, undefined))
195193
.to.deep.equal(expected);

0 commit comments

Comments
 (0)