montagejs / collections

This package contains JavaScript implementations of common data structures with idiomatic interfaces.
http://www.collectionsjs.com
Other
2.09k stars 185 forks source link

SortedArrayMap.filter() does not return an array of values #161

Open lmj0011 opened 7 years ago

lmj0011 commented 7 years ago

http://www.collectionsjs.com/method/filter

assume itemArrayMap is a new SortedArrayMap() with values

this returns a Map and not an array

let arr = itemArrayMap.filter(a=>{
      return true;
    });

I have to do this in order to get the array

let arr = itemArrayMap.filter(a=>{
      return true;
    }).toArray();
hendrul commented 7 years ago

Docs are inconsistent with actual behavior. It must be decided if this methods (filter, map, and so) return a Set (what I expected) or an array. In the docs for Set.filter:

Returns an array with each value from this collection that passes the given test

require('collections/set')
let set = new Set.CollectionsSet([1,2,3])
set.filter((it)=> it < 2) //returns the filtered set
//while for map()
set.map((it)=> it) //returns the array [1, 2, 3]

I think they should return a Set because otherwise it is difficult to chain boolean operations like:

current.difference(prev).union(
prev.difference(current).map((it)=> _.pick(it, assocPk)).union(
current.intersection(prev).map((c)=> {
if(_.isInteger(c)) return c
let modified = c.modified()
return !_.isEmpty(modified) ? modified : undefined
})
)
).map((it)=> it instanceof Instance ? it.get(options) : it)

map should return the same collection