Use source object with a property that is an empty array
Attempt to map source array into target object
Actual behavior
result = result.reduce(function (a, b) {
^
TypeError: Reduce of empty array with no initial value
at Array.reduce (native)
at GetKeyValue (node_modules/object-mapper/src/get-key-value.js:25:23)
at _mapKey (node_modules/object-mapper/src/object-mapper.js:105:15)
at _map (node_modules/object-mapper/src/object-mapper.js:57:18)
at Function.ObjectMapper (node_modules/object-mapper/src/object-mapper.js:30:10)
at Object.<anonymous> (test-2.js:61:22)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
Expected Behavior
If the array is empty, no properties should be mapped onto the target objec.t
Test Case
'use strict';
var util = require('util');
var mapper = require('object-mapper');
var sourceOk = {
phone_numbers: [
{
number: '5552223333',
label: 'Work',
primary: false,
country_code: 'US'
},
{
number: '5553334444',
label: 'Cell',
primary: true,
country_code: 'US'
}
]
};
var sourceNotOk = { phone_numbers: [] };
var target = {
questionnaire: {
initial: {
cellPhoneNumber: {
code: 'US',
phone: '5551112222'
}
}
}
};
var map = {
'phone_numbers': {
key: 'questionnaire.initial.cellPhoneNumber',
transform: function (sourceValue) {
var i;
if (!Array.isArray(sourceValue)) {
return null;
}
for (i = 0; i < sourceValue.length; i++) {
if (sourceValue[i].primary) {
return {
code: sourceValue[i].country_code,
phone: sourceValue[i].number
};
}
}
}
}
};
var result1 = mapper.merge(sourceOk, target, map);
console.log(util.inspect(result1, false, null));
var result2 = mapper.merge(sourceNotOk, target, map);
console.log(util.inspect(result2, false, null));
Steps to reproduce
Actual behavior
Expected Behavior
If the array is empty, no properties should be mapped onto the target objec.t
Test Case