wankdanker/node-object-mapper

Funky arrays of objects in getKeyValue

wankdanker opened this issue · 1 comments

I added failing tests in the arrays-of-objects-failing-tests branch for this issue.

From line 20 in src/get-key-value.js:

result = _getValue(fromObject, key[0], keys);

if (Array.isArray(result)) {
  result = result.reduce(function (a, b) {
    if (Array.isArray(a) && Array.isArray(b)) {
      return a.concat(b);
    }
    return [a, b];
  });
  if (!Array.isArray(result)) {
    result = [result];
  }
}

If result from the call to _getValue(...) is an array of objects ie:

[ { a : 1}, {a : 2}, {a : 3}, {a : 4}, {a : 5}]
  • Then the first time through the reduce function a is not an array and b is not an array so it returns [ { a : 1}, {a : 2}].
  • Then the second time through the reduce function a IS an array ( [{ a : 1}, {a : 2}] ) and b is NOT an array ( {a : 3} ), so it returns [[{ a : 1}, {a : 2}], {a : 3}].
  • Then the third time through the reduce function a IS an array ( [[{ a : 1}, {a : 2}], {a : 3}] ) and b is NOT an array ( {a : 4} ), so it returns [[[{ a : 1}, {a : 2}], {a : 3}], {a : 4}]
  • And so on...

Our test cases haven't caught this because our arrays of objects only contain two objects. The few test cases I made have three objects in the arrays.

I don't have a fix for this. I spent too much time tracking it down and had to revert to an old version so I can get other things done today.

Can you review the #15? Thanks