venn
Motivation
One of these days I was looking for a library that would allow me to do simple set operations, mostly intersection and union. There's a lot of libraries that do this but I didn't find one with a fluent API. This a stab at one:
Compatible with AMD and node.
getting started
A venn set is just an array on steroids:
var venn = venn console // [1,2]
union
You can now chain operations to this set, using union
or or
:
venn // [1,2,3,4,5]
union
and or
are just alias for the same operation.
intersection
venn // [1]
if you prefer you can also use and
instead of intersection
not
If you want to be negative about it, you can check out not
venn // [1,2,3]
chaining everything
Or a mix of everything
venn // [1,5,2]
Objects
If you're crazy you can use venn without a key function. We'll just figure it out how to index the objects.
venn // vitor, khov and nuno
Key function
But really, write your own key function:
var { return itemname } venn
Some notes
- The keyFunction only needs to be set once for each venn object (as you would expect)
- Don't forget if you apply a built-in array function like
filter
ormap
the returning object is not a venn object (as you would once again expect)
Let me know if there are improvements I can do to the library. I might take some time to implement the other less used set operations.