From 564371f842c222d0d5a64d81391c4d7332171589 Mon Sep 17 00:00:00 2001 From: Nagarajan Chinnasamy Date: Sat, 16 Sep 2017 15:57:38 +0530 Subject: [PATCH 1/2] [#39] null as forced default value --- src/object-mapper.js | 8 ++++---- test/test.js | 2 ++ 2 files changed, 6 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..33c7c08 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); From 2daa19647344319557731c1a39c961ec25116585 Mon Sep 17 00:00:00 2001 From: Nagarajan Chinnasamy Date: Sat, 16 Sep 2017 18:09:05 +0530 Subject: [PATCH 2/2] [#41] Handling non-existent sub-array elements of source object --- src/get-key-value.js | 11 ++++++++ src/object-mapper.js | 3 ++- src/set-key-value.js | 63 ++++---------------------------------------- src/utils.js | 58 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 59 deletions(-) create mode 100644 src/utils.js diff --git a/src/get-key-value.js b/src/get-key-value.js index 9cb8591..6c7d3e4 100644 --- a/src/get-key-value.js +++ b/src/get-key-value.js @@ -1,4 +1,6 @@ 'use strict'; +var utils = require('./utils') + ; /** * Make the get of a value with the key in the passed object @@ -140,5 +142,14 @@ function _getValue(fromObject, key, keys) { } } + if(Array.isArray(result)) { + result = result.filter(function (item) { + return (typeof item !== 'undefined' || utils._isEmptyObject(item)); + }); + if (utils._isEmptyArray(result)) { + result = undefined; + } + } + return result; } diff --git a/src/object-mapper.js b/src/object-mapper.js index be5ff7c..7521c64 100644 --- a/src/object-mapper.js +++ b/src/object-mapper.js @@ -1,5 +1,6 @@ 'use strict'; -var getKeyValue = require('./get-key-value') +var utils = require('./utils') + , getKeyValue = require('./get-key-value') , setKeyValue = require('./set-key-value') , _undefined ; diff --git a/src/set-key-value.js b/src/set-key-value.js index 5c5e186..42ec2b2 100644 --- a/src/set-key-value.js +++ b/src/set-key-value.js @@ -1,4 +1,7 @@ 'use strict'; +var utils = require('./utils') + ; + /** * Make the set of a value withing the key in the passed object * @param baseObject @@ -64,7 +67,7 @@ function _setValue(destinationObject, key, keys, fromValue) { key = key.replace(regAppendArray, ''); } - if (_isEmpty(destinationObject)) { + if (utils._isEmpty(destinationObject)) { if (isPropertyArray) { arrayIndex = match[2] || 0; if (isValueArray) { @@ -104,7 +107,7 @@ function _setValue(destinationObject, key, keys, fromValue) { if (Array.isArray(destinationObject[key]) === false) { destinationObject[key] = []; } - if (Array.isArray(fromValue) && _isNextArrayProperty(keys) === false) { + if (Array.isArray(fromValue) && utils._isNextArrayProperty(keys) === false) { for (valueIndex = 0; valueIndex < fromValue.length; valueIndex++) { value = fromValue[valueIndex]; destinationObject[key][arrayIndex + valueIndex] = _setValue(destinationObject[key][arrayIndex + valueIndex], keys[0], keys.slice(1), value); @@ -130,59 +133,3 @@ function _setValue(destinationObject, key, keys, fromValue) { return destinationObject; } -/** - * Check if next key is a array lookup - * @param keys - * @returns {boolean} - * @private - */ -function _isNextArrayProperty(keys) { - var regArray = /(\[\]|\[(.*)\])$/g - ; - return regArray.test(keys[0]); -} - -/** - * Check if passed object is empty, checking for object and array types - * @param object - * @returns {boolean} - * @private - */ -function _isEmpty(object) { - var empty = false; - if (typeof object === 'undefined' || object === null) { - empty = true; - } else if (_isEmptyObject(object)) { - empty = true; - } else if (_isEmptyArray(object)) { - empty = true; - } - - return empty; -} - -/** - * Check if passed object is empty - * @param object - * @returns {boolean} - * @private - */ -function _isEmptyObject(object) { - return typeof object === 'object' - && Array.isArray(object) === false - && Object.keys(object).length === 0 - ; -} - -/** - * Check if passed array is empty or with empty values only - * @param object - * @returns {boolean} - * @private - */ -function _isEmptyArray(object) { - return Array.isArray(object) - && (object.length === 0 - || object.join('').length === 0) - ; -} diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..62facc9 --- /dev/null +++ b/src/utils.js @@ -0,0 +1,58 @@ +module.exports = { + /** + * Check if next key is a array lookup + * @param keys + * @returns {boolean} + * @private + */ + _isNextArrayProperty: function (keys) { + var regArray = /(\[\]|\[(.*)\])$/g + ; + return regArray.test(keys[0]); + }, + + /** + * Check if passed object is empty, checking for object and array types + * @param object + * @returns {boolean} + * @private + */ + _isEmpty: function (object) { + var empty = false; + if (typeof object === 'undefined' || object === null) { + empty = true; + } else if (this._isEmptyObject(object)) { + empty = true; + } else if (this._isEmptyArray(object)) { + empty = true; + } + + return empty; + }, + + /** + * Check if passed object is empty + * @param object + * @returns {boolean} + * @private + */ + _isEmptyObject: function (object) { + return typeof object === 'object' + && Array.isArray(object) === false + && Object.keys(object).length === 0 + ; + }, + + /** + * Check if passed array is empty or with empty values only + * @param object + * @returns {boolean} + * @private + */ + _isEmptyArray: function(object) { + return Array.isArray(object) + && (object.length === 0 + || object.join('').length === 0) + ; + } +};