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.37k stars 194 forks source link

How do we measure the size of an Immutable data structure? #168

Closed slashwhatever closed 7 years ago

slashwhatever commented 7 years ago

Immutable.js has a simple size member that allows you to check the existence of data in a collection (Map or List)

How do we do the same in seamless-immutable?

So we should be able to do something similar to this;

const myImmutable = Immutable({the: "forests", will: "echo", with: "laughter"})
return myImmutable.size > 0 ? 'Collection populated :)' : 'Collection empty :('
crudh commented 7 years ago

@slashwhatever the result in your example is a javascript object, not a collection of any type. So you have to check the number of keys in the object with Object.keys(myImmutable).length or use something like _.size from lodash.

If you make an array immutable you can just use .length on it or again _.size from lodash.

slashwhatever commented 7 years ago

Thanks for the input @crudh. The fact we have to do something different for objects vs arrays is kinda my point. ImmutableJS allows you to use the same method name regardless of the type you're working with.

crudh commented 7 years ago

@slashwhatever yes, but the reasoning of seamless is that the types should be plain javascript types, with as few modifications as possible. So _.size is probably the best suggestion for this use case.

slashwhatever commented 7 years ago

@crudh understood :)