From 50ae5684f0b059283e5a77d4b1e2590e2db9ce75 Mon Sep 17 00:00:00 2001 From: Nagarajan Chinnasamy Date: Sat, 16 Sep 2017 15:57:38 +0530 Subject: [PATCH] [#39] null as forced default value [#39] null as forced default value [#39] null as forced default value --- src/object-mapper.js | 8 +++---- test/test.js | 56 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/src/object-mapper.js b/src/object-mapper.js index e6451a7..be5ff7c 100644 --- a/src/object-mapper.js +++ b/src/object-mapper.js @@ -76,7 +76,7 @@ function _map(fromObject, toObject, propertyMap, propertyKeys) { function _mapKey(fromObject, fromKey, toObject, toKey) { var fromValue , restToKeys - , _default = null + , _default // = null , transform ; @@ -87,14 +87,14 @@ function _mapKey(fromObject, fromKey, toObject, toKey) { } if (toKey instanceof Object && Object.getPrototypeOf(toKey) === Object.prototype) { - _default = toKey.default || null; + _default = toKey.default; // || null; transform = toKey.transform; toKey = toKey.key; } if (Array.isArray(toKey)) { transform = toKey[1]; - _default = toKey[2] || null; + _default = toKey[2]; // || null; toKey = toKey[0]; } @@ -103,7 +103,7 @@ function _mapKey(fromObject, fromKey, toObject, toKey) { } fromValue = getKeyValue(fromObject, fromKey); - if (typeof fromValue === 'undefined' || fromValue === null) { + if (typeof fromValue === 'undefined') { // || fromValue === null) { fromValue = _default; } diff --git a/test/test.js b/test/test.js index 741ee16..2a01050 100644 --- a/test/test.js +++ b/test/test.js @@ -1653,6 +1653,8 @@ test('original various tests', function (t) { t.deepEqual(result, expected, 'override sku'); obj["inventory"] = null; + map["inventory.onHandQty"] = {key: ["Envelope.Request.Item.Inventory?", null, null]}; + map["inventory.replenishQty"] = {key: ["Envelope.Request.Item.RelpenishQuantity?", null, null]}; expected.Envelope.Request.Item.Inventory = null; result = merge(obj, {}, map); @@ -1718,3 +1720,57 @@ test('map array inside array to property', function (t) { t.deepEqual(result, expect); t.end(); }); + +test('do not map when a property not present in source and default is not given in map', function (t) { + var expect = { + mapThis: 'defaultVal', + mapThisToo: [ { + property: 'defaultVal', + subArray: [ { property: null } ] + } ], + other: 'otherVal' + }; + + var map = { + "doNotMapThis": { + "key": "doNotMapThis", + "transform": function(val) { + return "_" + val; + } + }, + "doNotMapThisToo[].property": { + "key": "doNotMapThisToo[].property", + "transform": function(val) { + return "_" + val; + } + }, + "doNotMapThisToo[].subArray[].property": { + "key": "doNotMapThisToo[].subArray[].property", + "transform": function(val) { + return "_" + val; + } + }, + "mapThis": { + "key": "mapThis", + "default": "defaultVal" + }, + "mapThisToo[].property": { + "key": "mapThisToo[].property", + "default": "defaultVal" + }, + "mapThisToo[].subArray[].property": { + "key": "mapThisToo[].subArray[].property?", + "default": null + }, + "other": "other" + }; + + var obj = { + "other": "otherVal" + }; + + var result = om(obj, map); + + t.deepEqual(result, expect); + t.end(); +});