From 096413cb68a10476a6384019f104987448110176 Mon Sep 17 00:00:00 2001 From: Nagarajan Chinnasamy Date: Mon, 25 Sep 2017 14:29:15 +0530 Subject: [PATCH] [#39] null as forced default value --- test/test.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/test/test.js b/test/test.js index 33c7c08..2a01050 100644 --- a/test/test.js +++ b/test/test.js @@ -1720,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(); +});