Skip to content

Commit 25b2ddc

Browse files
author
sttk
committed
Add a destination node object as an argument of a converter
1 parent e68e793 commit 25b2ddc

File tree

5 files changed

+89
-20
lines changed

5 files changed

+89
-20
lines changed

README.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,27 @@ Usage
109109
// => { a: 'A', b: 'b', c: null }
110110
```
111111

112+
* You can operate the parent node object directly in converter.
113+
114+
```js
115+
var src = { a: 1, b: 2 };
116+
var dst = {};
117+
118+
copyProps(src, dst, function(srcval, srckey, dstkey, dstval, dstParent) {
119+
var key = dstkey.split('.').pop();
120+
Object.defineProperty(dstParent, key, {
121+
writable: false,
122+
enumerable: true,
123+
configurable: false,
124+
value: srcval * 2
125+
})
126+
}); // => { a: 2, b: 4 }
127+
128+
dst // => { a: 2, b: 4 }
129+
dst.a = 9
130+
dst // -> { a: 2, b: 4 }
131+
```
132+
112133
API
113134
---
114135

@@ -145,7 +166,7 @@ copyProps(src, dst, {
145166

146167
#### *API of converter*
147168

148-
**<u>converter(srcValue, srcKeychain, dstKeyChain, dstValue) => any</u>**
169+
**<u>converter(srcValue, srcKeychain, dstKeyChain, dstValue, dstParent) => any</u>**
149170

150171
*converter* is a function to convert terminal values of propeerties of *src*.
151172

@@ -155,8 +176,9 @@ copyProps(src, dst, {
155176
* **srcKeychain** [string] : a source property key string concatenated with dots.
156177
* **dstKeychain** [string] : a destination property key string concatenated with dots.
157178
* **dstValue** [any] : a destination property value before copying.
179+
* **dstParent** [object] : the destination node object which has the copied property.
158180

159-
* **Return:** [any] : converted value to be set as a destination property value.
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.
160182

161183
License
162184
-------

index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ function copyWithFromto(value, keyChain, nodeInfo) {
8484
}
8585

8686
for (var i = 0, n = dstKeyChains.length; i < n; i++) {
87-
setDeep(nodeInfo.dest, dstKeyChains[i], function(dstValue) {
88-
return nodeInfo.convert(value, keyChain, dstKeyChains[i], dstValue);
87+
setDeep(nodeInfo.dest, dstKeyChains[i], function(dstValue, dstParent) {
88+
return nodeInfo.convert(value, keyChain, dstKeyChains[i], dstValue,
89+
dstParent);
8990
});
9091
}
9192
}
@@ -99,8 +100,8 @@ function copyWithoutFromto(value, keyChain, nodeInfo) {
99100
return;
100101
}
101102

102-
setDeep(nodeInfo.dest, keyChain, function(dstValue) {
103-
return nodeInfo.convert(value, keyChain, keyChain, dstValue);
103+
setDeep(nodeInfo.dest, keyChain, function(dstValue, dstParent) {
104+
return nodeInfo.convert(value, keyChain, keyChain, dstValue, dstParent);
104105
});
105106
}
106107

@@ -153,7 +154,7 @@ function setDeep(obj, keyChain, valueCreator) {
153154
function _setDeep(obj, keyElems, valueCreator) {
154155
var key = keyElems.shift();
155156
if (!keyElems.length) {
156-
var value = valueCreator(obj[key]);
157+
var value = valueCreator(obj[key], obj);
157158
if (value !== undefined) {
158159
obj[key] = value;
159160
}

test/copy-props-proc.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,30 +256,34 @@ describe('Processing', function() {
256256
it('When converter returns undefined', function(done) {
257257
var src = { a: 1, b: { c: 2, d: 3, e: 4 } };
258258
var dst = { a: 'A', b: { e: 'E' } };
259-
function fn(value, keyChain, dstKeyChain, dstValue) {
259+
function fn(value, keyChain, dstKeyChain, dstValue, dstParent) {
260260
switch (keyChain) {
261261
case 'a': {
262262
expect(value).to.equal(1);
263263
expect(dstKeyChain).to.equal(keyChain);
264264
expect(dstValue).to.equal('A');
265+
expect(dstParent).to.equal(dst);
265266
break;
266267
}
267268
case 'b.c': {
268269
expect(value).to.equal(2);
269270
expect(dstKeyChain).to.equal(keyChain);
270271
expect(dstValue).to.be.undefined;
272+
expect(dstParent).to.equal(dst.b);
271273
break;
272274
}
273275
case 'b.d': {
274276
expect(value).to.equal(3);
275277
expect(dstKeyChain).to.equal(keyChain);
276278
expect(dstValue).to.be.undefined;
279+
expect(dstParent).to.equal(dst.b);
277280
break;
278281
}
279282
case 'b.e': {
280283
expect(value).to.equal(4);
281284
expect(dstKeyChain).to.equal(keyChain);
282285
expect(dstValue).to.equal('E');
286+
expect(dstParent).to.equal(dst.b);
283287
break;
284288
}
285289
default: {
@@ -298,24 +302,27 @@ describe('Processing', function() {
298302
it('When converter returns null', function(done) {
299303
var src = { a: 1, b: { c: 2, d: 3 } };
300304
var dst = {};
301-
function fn(value, keyChain, dstKeyChain, dstValue) {
305+
function fn(value, keyChain, dstKeyChain, dstValue, dstParent) {
302306
switch (keyChain) {
303307
case 'a': {
304308
expect(value).to.equal(1);
305309
expect(dstKeyChain).to.equal(keyChain);
306310
expect(dstValue).to.be.undefined;
311+
expect(dstParent).to.equal(dst);
307312
break;
308313
}
309314
case 'b.c': {
310315
expect(value).to.equal(2);
311316
expect(dstKeyChain).to.equal(keyChain);
312317
expect(dstValue).to.be.undefined;
318+
expect(dstParent).to.equal(dst.b);
313319
break;
314320
}
315321
case 'b.d': {
316322
expect(value).to.equal(3);
317323
expect(dstKeyChain).to.equal(keyChain);
318324
expect(dstValue).to.be.undefined;
325+
expect(dstParent).to.equal(dst.b);
319326
break;
320327
}
321328
default: {
@@ -351,24 +358,28 @@ describe('Processing', function() {
351358

352359
src = { a: 1, b: { c: 2, d: 3 } };
353360
dst = { a: 'A', b: { c: 'C', d: 'D' } };
354-
var converter = function(value, keyChain, dstKeyChain, dstValue) {
361+
var converter = function(value, keyChain, dstKeyChain, dstValue,
362+
dstParent) {
355363
switch (keyChain) {
356364
case 'a': {
357365
expect(value).to.equal('A');
358366
expect(dstKeyChain).to.equal(keyChain);
359367
expect(dstValue).to.equal(1);
368+
expect(dstParent).to.equal(src);
360369
break;
361370
}
362371
case 'b.c': {
363372
expect(value).to.equal('C');
364373
expect(dstKeyChain).to.equal(keyChain);
365374
expect(dstValue).to.equal(2);
375+
expect(dstParent).to.equal(src.b);
366376
break;
367377
}
368378
case 'b.d': {
369379
expect(value).to.equal('D');
370380
expect(dstKeyChain).to.equal(keyChain);
371381
expect(dstValue).to.equal(3);
382+
expect(dstParent).to.equal(src.b);
372383
break;
373384
}
374385
default: {
@@ -389,30 +400,34 @@ describe('Processing', function() {
389400
src = { a: 1, b: { c: 2, d: 3 } };
390401
dst = { a: 'A', b: { c: 'C', d: 'D', e: 'E', f: 'F' } };
391402
fromto = ['a', 'b.c', 'b.d', 'b.e'];
392-
converter = function(value, keyChain, dstKeyChain, dstValue) {
403+
converter = function(value, keyChain, dstKeyChain, dstValue, dstParent) {
393404
switch (keyChain) {
394405
case 'a': {
395406
expect(value).to.equal('A');
396407
expect(dstKeyChain).to.equal(keyChain);
397408
expect(dstValue).to.equal(1);
409+
expect(dstParent).to.equal(src);
398410
break;
399411
}
400412
case 'b.c': {
401413
expect(value).to.equal('C');
402414
expect(dstKeyChain).to.equal(keyChain);
403415
expect(dstValue).to.equal(2);
416+
expect(dstParent).to.equal(src.b);
404417
break;
405418
}
406419
case 'b.d': {
407420
expect(value).to.equal('D');
408421
expect(dstKeyChain).to.equal(keyChain);
409422
expect(dstValue).to.equal(3);
423+
expect(dstParent).to.equal(src.b);
410424
break;
411425
}
412426
case 'b.e': {
413427
expect(value).to.equal('E');
414428
expect(dstKeyChain).to.equal(keyChain);
415429
expect(dstValue).to.be.undefined;
430+
expect(dstParent).to.equal(src.b);
416431
break;
417432
}
418433
default: {
@@ -448,24 +463,28 @@ describe('Processing', function() {
448463

449464
src = { a: 1, b: { c: 2, d: 3 } };
450465
dst = { a: 'A', b: { c: 'C', d: 'D' } };
451-
var converter = function(value, keyChain, dstKeyChain, dstValue) {
466+
var converter = function(value, keyChain, dstKeyChain, dstValue,
467+
dstParent) {
452468
switch (keyChain) {
453469
case 'a': {
454470
expect(value).to.equal(1);
455471
expect(dstKeyChain).to.equal(keyChain);
456472
expect(dstValue).to.equal('A');
473+
expect(dstParent).to.equal(dst);
457474
break;
458475
}
459476
case 'b.c': {
460477
expect(value).to.equal(2);
461478
expect(dstKeyChain).to.equal(keyChain);
462479
expect(dstValue).to.equal('C');
480+
expect(dstParent).to.equal(dst.b);
463481
break;
464482
}
465483
case 'b.d': {
466484
expect(value).to.equal(3);
467485
expect(dstKeyChain).to.equal(keyChain);
468486
expect(dstValue).to.equal('D');
487+
expect(dstParent).to.equal(dst.b);
469488
break;
470489
}
471490
default: {
@@ -486,30 +505,34 @@ describe('Processing', function() {
486505
src = { a: 1, b: { c: 2, d: 3 } };
487506
dst = { a: 'A', b: { c: 'C', d: 'D', e: 'E', f: 'F' } };
488507
fromto = ['a', 'b.c', 'b.d', 'b.e'];
489-
converter = function(value, keyChain, dstKeyChain, dstValue) {
508+
converter = function(value, keyChain, dstKeyChain, dstValue, dstParent) {
490509
switch (keyChain) {
491510
case 'a': {
492511
expect(value).to.equal(1);
493512
expect(dstKeyChain).to.equal(keyChain);
494513
expect(dstValue).to.equal('A');
514+
expect(dstParent).to.equal(dst);
495515
break;
496516
}
497517
case 'b.c': {
498518
expect(value).to.equal(2);
499519
expect(dstKeyChain).to.equal(keyChain);
500520
expect(dstValue).to.equal('C');
521+
expect(dstParent).to.equal(dst.b);
501522
break;
502523
}
503524
case 'b.d': {
504525
expect(value).to.equal(3);
505526
expect(dstKeyChain).to.equal(keyChain);
506527
expect(dstValue).to.equal('D');
528+
expect(dstParent).to.equal(dst.b);
507529
break;
508530
}
509531
case 'b.e': {
510532
expect(value).to.be.undefined;
511533
expect(dstKeyChain).to.equal(keyChain);
512534
expect(dstValue).to.equal('E');
535+
expect(dstParent).to.equal(dst.b);
513536
break;
514537
}
515538
default: {

0 commit comments

Comments
 (0)