Skip to content

Commit

Permalink
Support from-to array instead of from-to map
Browse files Browse the repository at this point in the history
  • Loading branch information
sttk committed Sep 25, 2016
1 parent fd90e5e commit 8a4a497
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 26 deletions.
20 changes: 10 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var inspect = require('util').inspect;
var isPlainObject = require('lodash.isplainobject');
var copyProps = require('./lib/copy-props');

module.exports = function(src, dst, map, converter, isReversed) {
module.exports = function(src, dst, fromTo, converter, isReversed) {
if (!isPlainObject(src)) {
throw new TypeError('The 1st argument need to be a plain object: ' +
inspect(src));
Expand All @@ -17,16 +17,16 @@ module.exports = function(src, dst, map, converter, isReversed) {
inspect(dst));
}

if (typeof map === 'boolean') {
return copyProps(src, dst, undefined, undefined, map);
if (typeof fromTo === 'boolean') {
return copyProps(src, dst, undefined, undefined, fromTo);
}

if (typeof map === 'function') {
if (typeof fromTo === 'function') {
if (typeof converter === 'boolean') {
return copyProps(src, dst, undefined, map, converter);
return copyProps(src, dst, undefined, fromTo, converter);
}
if (isReversed == null && converter == null) {
return copyProps(src, dst, undefined, map);
return copyProps(src, dst, undefined, fromTo);
}
}

Expand All @@ -45,10 +45,10 @@ module.exports = function(src, dst, map, converter, isReversed) {
inspect(converter));
}

if (map != null && !isPlainObject(map)) {
throw new TypeError('The 3th argument need to be a plain object: ' +
inspect(map));
if (fromTo != null && !isPlainObject(fromTo) && !Array.isArray(fromTo)) {
throw new TypeError('The 3th argument need to be a plain object or ' +
'an array: ' + inspect(fromTo));
}

return copyProps(src, dst, map, converter, isReversed);
return copyProps(src, dst, fromTo, converter, isReversed);
};
24 changes: 16 additions & 8 deletions lib/copy-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var setDeep = require('lodash.set');
var foreachProps = require('./foreach-props');

module.exports = function(src, dst, map, converter, isReversed) {
module.exports = function(src, dst, fromTo, converter, isReversed) {
converter = converter || noop;

if (isReversed) {
Expand All @@ -13,26 +13,34 @@ module.exports = function(src, dst, map, converter, isReversed) {
}

var callback;
if (!map) {
if (!fromTo) {

callback = function(value, keychain) {
setDeep(dst, keychain, converter(value, keychain));
};

} else if (Array.isArray(fromTo)) {

callback = function(value, keychain) {
if (fromTo.indexOf(keychain) >= 0) {
setDeep(dst, keychain, converter(value, keychain));
}
};

} else if (!isReversed) {

callback = function(value, keychain) {
var dstKeychain = map[keychain];
var dstKeychain = fromTo[keychain];
if (dstKeychain) {
setDeep(dst, dstKeychain, converter(value, keychain));
}
};

} else {
map = invert(map);
fromTo = invert(fromTo);

callback = function(value, keychain) {
var dstKeychainArray = map[keychain];
var dstKeychainArray = fromTo[keychain];
if (dstKeychainArray) {
var val = converter(value, keychain);
dstKeychainArray.forEach(function(dstKeychain) {
Expand All @@ -50,10 +58,10 @@ function noop(v) {
return v;
}

function invert(map) {
function invert(fromTo) {
var tmpMap = {};
Object.keys(map).forEach(function(key) {
var val = map[key];
Object.keys(fromTo).forEach(function(key) {
var val = fromTo[key];
if (!tmpMap[val]) {
tmpMap[val] = [];
}
Expand Down
31 changes: 31 additions & 0 deletions test/copy-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ testrun('copyProps', testfn, [
map: { 'a.b1': 'x.y.z', c: 'o.p' },
expected: { x: { y: { z: 999 }, }, o: { p: 'a' } },
},
{
name: 'And map is an array',
src: { x: { y: { z: 999 }, }, o: { p: 'a' } },
dst: { x: { y: { z: 1 }, }, },
map: ['x.y.z', 'o.p'],
expected: { x: { y: { z: 999 }, }, o: { p: 'a' } },
},
],
},
{
Expand Down Expand Up @@ -417,6 +424,14 @@ testrun('copyProps', testfn, [
rev: false,
expected: { x: { y: { z: 999 }, }, o: { p: 'a' } },
},
{
name: 'And map is an array',
src: { x: { y: { z: 123, zz: 'BBB' }, }, o: { p: true } },
dst: { x: { y: { z: 999 }, }, o: { p: 'a' } },
map: ['x.y.z'],
rev: false,
expected: { x: { y: { z: 123 }, }, o: { p: 'a' } },
},
],
},
{
Expand Down Expand Up @@ -473,6 +488,22 @@ testrun('copyProps', testfn, [
rev: true,
expected: { x: { y: { z: 999 }, }, o: { p: 'a' } },
},
{
name: 'And map has duplicated value',
src: { a: { b: { c: 'A', d: 'B' }, }, },
dst: { x: { y: { z: 1234 }, }, },
map: { 'a.b.c': 'x.y.z', 'a.b.d': 'x.y.z' },
rev: true,
expected: { a: { b: { c: 1234, d: 1234, }, }, },
},
{
name: 'And map is an array',
src: { x: { y: { z: 123, zz: 'BBB' }, }, o: { p: true } },
dst: { x: { y: { z: 999 }, }, o: { p: 'a' } },
map: ['x.y.z'],
rev: true,
expected: { x: { y: { z: 999, zz: 'BBB' }, }, o: { p: true } },
},
],
},
],
Expand Down
39 changes: 31 additions & 8 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ testrun('copyProps', testfn, [
map: { 'a.b1': 'x.y.z', c: 'o.p' },
expected: { x: { y: { z: 999 }, }, o: { p: 'a' } },
},
{
name: 'And map is an array',
src: { x: { y: { z: 999 }, }, o: { p: 'a' } },
dst: { x: { y: { z: 1 }, }, },
map: ['x.y.z', 'o.p'],
expected: { x: { y: { z: 999 }, }, o: { p: 'a' } },
},
],
},
{
Expand Down Expand Up @@ -356,14 +363,6 @@ testrun('copyProps', testfn, [
rev: true,
expected: { a: { a1: 3, a2: 2 }, b: { b1: 'bbb', b2: 'ccc' } },
},
{
name: 'And map has duplicated value',
src: { a: { b: { c: 'A', d: 'B' }, }, },
dst: { x: { y: { z: 1234 }, }, },
map: { 'a.b.c': 'x.y.z', 'a.b.d': 'x.y.z' },
rev: true,
expected: { a: { b: { c: 1234, d: 1234, }, }, },
},
],
},
],
Expand Down Expand Up @@ -425,6 +424,14 @@ testrun('copyProps', testfn, [
rev: false,
expected: { x: { y: { z: 999 }, }, o: { p: 'a' } },
},
{
name: 'And map is an array',
src: { x: { y: { z: 123, zz: 'BBB' }, }, o: { p: true } },
dst: { x: { y: { z: 999 }, }, o: { p: 'a' } },
map: ['x.y.z'],
rev: false,
expected: { x: { y: { z: 123 }, }, o: { p: 'a' } },
},
],
},
{
Expand Down Expand Up @@ -481,6 +488,22 @@ testrun('copyProps', testfn, [
rev: true,
expected: { x: { y: { z: 999 }, }, o: { p: 'a' } },
},
{
name: 'And map has duplicated value',
src: { a: { b: { c: 'A', d: 'B' }, }, },
dst: { x: { y: { z: 1234 }, }, },
map: { 'a.b.c': 'x.y.z', 'a.b.d': 'x.y.z' },
rev: true,
expected: { a: { b: { c: 1234, d: 1234, }, }, },
},
{
name: 'And map is an array',
src: { x: { y: { z: 123, zz: 'BBB' }, }, o: { p: true } },
dst: { x: { y: { z: 999 }, }, o: { p: 'a' } },
map: ['x.y.z'],
rev: true,
expected: { x: { y: { z: 999, zz: 'BBB' }, }, o: { p: true } },
},
],
},
],
Expand Down

0 comments on commit 8a4a497

Please sign in to comment.