indexed-values
Example
import {IndexedValues} from 'indexed-values';
// create a Set of unique values
const main = new IndexedValues;
// derive a set
const sub1 = main.bindValues(["a", "b", "c"]);
const sub2 = main.bindValues(["a"]);
// derived sets can make the main Set grow
if (!sub2.has("d"))
sub2.add("d");
// {"main":["a","b","c","d"],"data":[[0,1,2],[0,3]]}
console.log(JSON.stringify({
main,
data: [sub1, sub2]
}));
API
The IndexedValues
constructor extends the Set
class, but it's a facade with the following peculiarities:
- it is not posible to
delete
a single value, orclear
the instance.IndexedValues
instances are ever-growing -
.toJSON()
method returns an array of values held internally -
.valueOf()
method returns a realSet
that can resistpostMessage
, or be stored asIndexedDB
-
.bindValues(values = [])
returns aSet
that can contain, add, or remove, any value. Added values will be reflected in theIndexedValues
instance that created the boundSet
. -
.bindIndexes(indexes = [])
is like.bindValues()
but uses indexes instead, to initialize the derivedSet
. This is mostly useful for revival. -
.mapKeys(entries = [])
returns aMap
where keys are derived from theIndexedValues
. Added keys that are not known in theIndexedValues
source, will be automatically added as part of theSet
. Once serialized as JSON, these maps will represent all keys as indexes, because indeed these maps never hold keys as values, these hold keys as indexes. -
.mapEntries(entries = [])
is the revival counter-part of.mapKeys()
. It returns a map with keys revived from theIndexedValues
instance. -
.fromIndexes(indexes)
can revive right awaySet
instances, upgrading their prototype. This is a quick and dirty.bindIndexes()
equivalent, available for performance reasons. -
.fromEntries(entries)
can revive right awayMap
instances, upgrading their prototype. This is a quick and dirty.mapEntries()
equivalent, available for performance reasons.