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
4 changes: 2 additions & 2 deletions src/object-mapper-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ async function updateArrIx(dest, ix, data, keys, context) {
typeof dest[ix] !== "undefined"
)
o = keys.length ? await update(dest[ix], data, keys, context) : data;
else o = keys.length ? await update(null, data, keys, context) : data;
else o = keys.length ? await update(null, data, keys, context) : applyTransform(data, dest, context);
Copy link
Owner

@rojasraul rojasraul Mar 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Spencerhutch maybe the await statement is needed here


// Only update (and create if needed) dest if there is data to be saved
if (o !== null) {
Expand Down Expand Up @@ -160,7 +160,7 @@ async function updateArr(dest, key, data, keys, context) {
return await updateArrIx(
await dest,
i,
await applyTransform(d, dest, context),
d,
keys.slice(),
context
);
Expand Down
31 changes: 31 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2270,4 +2270,35 @@ describe("Object Mapper Async", () => {
const result = await om(src, map);
expect(result).toEqual(expected);
});

it("map array with function", async () => {
var obj = {
theArray: [
{text:"textvalue"},
{text:"text"}
]
};

var expect = {
newArray: [
{textSize:9},
{textSize:4}
]
};

let transformFunc = {
key:'newArray[].textSize',
transform: function (value, src, dest, srcKey, destKey){
return value.length
}
};
var map = {
'theArray[].text': transformFunc
};

var result = om(obj, map);

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