rtfeldman / seamless-immutable

Immutable data structures for JavaScript which are backwards-compatible with normal JS Arrays and Objects.
BSD 3-Clause "New" or "Revised" License
5.36k stars 194 forks source link

flatMap not working #263

Open ghost opened 5 years ago

ghost commented 5 years ago

I tried this example in the docs and the returned array isn't changed at all

var array = Immutable(["drop the numbers!", 3, 2, 1, 0, null, undefined]);
Immutable.flatMap(array, function(value) {
  if (typeof value === "number") {
    return [];
  } else {
    return value;
  }
});
// expected: ["drop the numbers!", null, undefined]
// reality: ["drop the numbers!", 3, 2, 1, 0, null, undefined]

I'm using seamless-immutable v7.1.4. Is this a bug or am I doing anything wrong?

crudh commented 5 years ago

@lightnguyen not sure, but I added this test case and it seems to work as expected?

    it("filters out things as expected when returning empty array", function() {
      var array = Immutable(["drop the numbers!", 3, 2, 1, 0, null, undefined]);
      var expected = Immutable(["drop the numbers!", null, undefined]);
      var actual = Immutable.flatMap(array, function(value) {
        if (typeof value === "number") {
          return [];
        } else {
          return value;
        }
      });

      TestUtils.assertJsonEqual(actual, expected);
    });
Liaozhenting commented 4 years ago

Looks like you expecte immutable variable 'array' is ["drop the numbers!", 3, 2, 1, 0, null, undefined]. This is not right. You need a another variable to take the return of Immutable.flatMap