Skip to content

transform arguments are not passed correctly #137

@xyse

Description

@xyse

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;

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions