Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#41] Handling non-existent sub-array elements of source object #42

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/get-key-value.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
'use strict';
var utils = require('./utils')
;

/**
* Make the get of a value with the key in the passed object
Expand Down Expand Up @@ -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;
}
11 changes: 6 additions & 5 deletions src/object-mapper.js
Original file line number Diff line number Diff line change
@@ -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
;
Expand Down Expand Up @@ -76,7 +77,7 @@ function _map(fromObject, toObject, propertyMap, propertyKeys) {
function _mapKey(fromObject, fromKey, toObject, toKey) {
var fromValue
, restToKeys
, _default = null
, _default // = null
, transform
;

Expand All @@ -87,14 +88,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];
}

Expand All @@ -103,7 +104,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;
}

Expand Down
63 changes: 5 additions & 58 deletions src/set-key-value.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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)
;
}
58 changes: 58 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -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)
;
}
};
2 changes: 2 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down