Skip to content
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
3 changes: 2 additions & 1 deletion src/object-mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ function select_arr(src, key, keys)
// Check to see if we are at a 'leaf' (no more keys to parse). If so, return the data. If not, recurse
var d = (keys.length) ? select(src[i], keys.slice()) : src[i]
// If the data is populated, add it to the array. Make sure to keep the same array index so that traversing multi-level arrays work
if (d !== null)
// If data is null, the subsequent steps will take the default value if there is one, and null if null is allowed.
if ( typeof d !== 'undefined')
data[i] = d
}

Expand Down
46 changes: 46 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2653,6 +2653,52 @@ test("issue #74: mapping empty array should result in empty array", t => {

const result = om(src, map);

t.deepEqual(result, expect);
t.end();
});

test('Ensure that null value check is iterated correctly in arrays', function (t) {
const src = {
"source": [
{
"some": "value",
"empty": null
},
{
"some": "value",
"empty": null
},
{
"some": "value",
"empty": null
}
]
};

const map = {
"source[].some": "destination[].some",
"source[].empty": "destination[].empty?"
};

const expect = {
"destination": [
{
"some": "value",
"empty": null
},
{
"some": "value",
"empty": null
},
{
"some": "value",
"empty": null
}
]
}

const result = om(src, map);

t.deepEqual(result, expect);
t.end();
});