node-object-mapper icon indicating copy to clipboard operation
node-object-mapper copied to clipboard

Missing fromObject creates empty object

Open alemures opened this issue 7 years ago • 5 comments

The following code produces an empty object as result:

var obj = {
  a: 'b'
};
var expect = {
};
var map = {
  z: 'a.b'
};
om(obj, map); // --> { a: {} }

I think it's an unexpected behavior, what do you think?

alemures avatar Jan 26 '18 20:01 alemures

I agree, it should just return an empty object. Is that what you are thinking? I mean like this:

var obj = {
  a: 'b'
};
var expect = {
};
var map = {
  z: 'a.b'
};
om(obj, map); // --> {}

wankdanker avatar Jan 26 '18 20:01 wankdanker

Yep, i may have a chance to try to fix it through the weekend ;).

alemures avatar Jan 26 '18 21:01 alemures

I have actually a solution for this weird behaviour but, one of the previous tests have been broken. The broken test is called "map object to another - prevent null values from being mapped", and it's exactly reproducing the behaviour we want to remove when an empty object is created even if it doesn't match a key on "map" object.

That's the code of the broken test:

var obj = {
  "a" : 1234,
  "foo": {
    "bar": null
  }
};

var expect = {
  foo:{
    a:1234
  },
  bar:{
  }
};

var map = {
  'foo.bar' : 'bar.bar',
  'a': 'foo.a'
 };

Based on what we spoke, the key "bar:{}" should not be expected so, i suggest that a change in that specific test is necessary in order to add this update along with a version bump.

Any thoughts about that?

alemures avatar Feb 12 '18 16:02 alemures

Hi there. Checking to see if this issue has been fixed or if there is a valid work around?

davisc avatar Nov 17 '18 12:11 davisc

I added this test to my branch. My expectation differs from yours though:

  var expect = {
    foo:{
      a:1234
    }
  };

Note that ObjectMapper does not store any key that has a NULL value, unless the null key is set to true in object notation. If you think differently, please let me know and this can be debated.

switzer avatar Oct 11 '19 21:10 switzer