Skip to content

Commit

Permalink
bug: now apply transform in arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
pguijarr committed Dec 10, 2019
1 parent e247321 commit 897dd2a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/object-mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,15 +271,15 @@ function update_arr(dest, key, data, keys, context)
if (key.add) {
if (data !== null && typeof data !== 'undefined') {
dest = dest || []
dest.push(data)
dest.push(applyTransform(data,dest,context))
// dest = dest.concat(data)
}
return dest
}

// Just update a single array node
if (key.ix !== '') {
return update_arr_ix(dest, key.ix, data, keys, context)
return update_arr_ix(dest, key.ix, applyTransform(data,dest,context), keys, context)
}

// If the data is in an array format then make sure that there is a dest index for each data index
Expand All @@ -289,7 +289,7 @@ function update_arr(dest, key, data, keys, context)
dest = data.reduce(function(dest,d,i) {
// If the instruction is to update all array indices ('') or the current index, update the child data element. Otherwise, don't bother
if (key.ix == '' || key.ix == i) {
return update_arr_ix(dest, i, d, keys.slice(), context)
return update_arr_ix(dest, i, applyTransform(d,dest,context), keys.slice(), context)
}
}, dest)

Expand All @@ -301,6 +301,14 @@ function update_arr(dest, key, data, keys, context)
return update_arr_ix(dest, '0', data, keys, context)
}

function applyTransform(data, dest, context){
if (typeof context.transform == 'function') {
return context.transform(data, context.src, dest, context.srckey, context.destkey)
}else{
return data;
}
}

function update_arr_ix(dest, ix, data, keys, context)
{
let o
Expand Down
21 changes: 21 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2530,3 +2530,24 @@ test('MAP Should correctly create an array and add if undlerlying data structure
t.deepEqual(result, expect)
t.end()
});



test("MAP Should correctly apply transform in array data #68", t => {
var src = {
test: ["1234", "5678", "9101"]
};
var map = {
"test[]": {
key: "check[]",
transform: val => parseInt(val)
}
};
var expect = {
check: [1234, 5678, 9101]
};
var result = om(src, map);

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

0 comments on commit 897dd2a

Please sign in to comment.