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

[#80] fix null value check not iterating correctly in arrays #95

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
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();
});