Skip to content

Commit 50ae568

Browse files
[#39] null as forced default value
[#39] null as forced default value [#39] null as forced default value
1 parent 291a524 commit 50ae568

2 files changed

Lines changed: 60 additions & 4 deletions

File tree

src/object-mapper.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function _map(fromObject, toObject, propertyMap, propertyKeys) {
7676
function _mapKey(fromObject, fromKey, toObject, toKey) {
7777
var fromValue
7878
, restToKeys
79-
, _default = null
79+
, _default // = null
8080
, transform
8181
;
8282

@@ -87,14 +87,14 @@ function _mapKey(fromObject, fromKey, toObject, toKey) {
8787
}
8888

8989
if (toKey instanceof Object && Object.getPrototypeOf(toKey) === Object.prototype) {
90-
_default = toKey.default || null;
90+
_default = toKey.default; // || null;
9191
transform = toKey.transform;
9292
toKey = toKey.key;
9393
}
9494

9595
if (Array.isArray(toKey)) {
9696
transform = toKey[1];
97-
_default = toKey[2] || null;
97+
_default = toKey[2]; // || null;
9898
toKey = toKey[0];
9999
}
100100

@@ -103,7 +103,7 @@ function _mapKey(fromObject, fromKey, toObject, toKey) {
103103
}
104104

105105
fromValue = getKeyValue(fromObject, fromKey);
106-
if (typeof fromValue === 'undefined' || fromValue === null) {
106+
if (typeof fromValue === 'undefined') { // || fromValue === null) {
107107
fromValue = _default;
108108
}
109109

test/test.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,6 +1653,8 @@ test('original various tests', function (t) {
16531653
t.deepEqual(result, expected, 'override sku');
16541654

16551655
obj["inventory"] = null;
1656+
map["inventory.onHandQty"] = {key: ["Envelope.Request.Item.Inventory?", null, null]};
1657+
map["inventory.replenishQty"] = {key: ["Envelope.Request.Item.RelpenishQuantity?", null, null]};
16561658
expected.Envelope.Request.Item.Inventory = null;
16571659

16581660
result = merge(obj, {}, map);
@@ -1718,3 +1720,57 @@ test('map array inside array to property', function (t) {
17181720
t.deepEqual(result, expect);
17191721
t.end();
17201722
});
1723+
1724+
test('do not map when a property not present in source and default is not given in map', function (t) {
1725+
var expect = {
1726+
mapThis: 'defaultVal',
1727+
mapThisToo: [ {
1728+
property: 'defaultVal',
1729+
subArray: [ { property: null } ]
1730+
} ],
1731+
other: 'otherVal'
1732+
};
1733+
1734+
var map = {
1735+
"doNotMapThis": {
1736+
"key": "doNotMapThis",
1737+
"transform": function(val) {
1738+
return "_" + val;
1739+
}
1740+
},
1741+
"doNotMapThisToo[].property": {
1742+
"key": "doNotMapThisToo[].property",
1743+
"transform": function(val) {
1744+
return "_" + val;
1745+
}
1746+
},
1747+
"doNotMapThisToo[].subArray[].property": {
1748+
"key": "doNotMapThisToo[].subArray[].property",
1749+
"transform": function(val) {
1750+
return "_" + val;
1751+
}
1752+
},
1753+
"mapThis": {
1754+
"key": "mapThis",
1755+
"default": "defaultVal"
1756+
},
1757+
"mapThisToo[].property": {
1758+
"key": "mapThisToo[].property",
1759+
"default": "defaultVal"
1760+
},
1761+
"mapThisToo[].subArray[].property": {
1762+
"key": "mapThisToo[].subArray[].property?",
1763+
"default": null
1764+
},
1765+
"other": "other"
1766+
};
1767+
1768+
var obj = {
1769+
"other": "otherVal"
1770+
};
1771+
1772+
var result = om(obj, map);
1773+
1774+
t.deepEqual(result, expect);
1775+
t.end();
1776+
});

0 commit comments

Comments
 (0)