When I execute the following code:
var createMapper = require("map-factory")
const testObject = {
item: [
{foo: true, bar: null},
{foo: null, bar: null}
`]`
}
function transform(input) {
console.log(input)
}
const map = createMapper({alwaysTransform: true})
map('item.foo').to('foo', transform)
map('item.bar').to('bar', transform)
map.execute(testObject)
I would expect to get [true, null] as a transform argument for the foo mapping and [null, null] for the bar mapping. The problem is, I noticed when all the occurrences of an object's property in an array are null, we instead get undefined. So in this case we get [true, null] and undefined.
This way, using my custom transform I cannot transform the value of bar, as I cannot infer the length of the array from undefined. I think just passing the [null, null] array would be a better solution, as it gives more possibilities to map the null value in the custom transform.
The reason for this seems to be this part of the ./src/lib/object-mapper/get-key-value.js :
function scanArrayForValue_(arrayToScan, defaultValue) {
for (const item of arrayToScan) {
if (item !== undefined && item !== null) {
return defaultValue;
}
}
return undefined;
}
When I execute the following code:
I would expect to get
[true, null]as a transform argument for the foo mapping and[null, null]for the bar mapping. The problem is, I noticed when all the occurrences of an object's property in an array are null, we instead getundefined. So in this case we get[true, null]andundefined.This way, using my custom transform I cannot transform the value of bar, as I cannot infer the length of the array from
undefined. I think just passing the[null, null]array would be a better solution, as it gives more possibilities to map the null value in the custom transform.The reason for this seems to be this part of the ./src/lib/object-mapper/get-key-value.js :