A collection of unique values.
The constructor will add all of the values if any are given. If the values are a map, the keys will be lost.
The optional equals
and hash
arguments override the set’s contentEquals
and
contentCompare
methods and determine where to store values and whether they
are equivalent.
The optional getDefault
argument overrides the set’s getDefault(value)
method, which will be called by get
if it cannot find an equivalent value
within the set.
The purpose of get
is less obvious on a Set
than a Map
, since you would
not often need to find a value you already have.
However, by virtue of overriding contentEquals
and contentHash
, it is
possible to search for a value using an “equivalent” place-holder.
This is how maps use sets to find [key, value] entries when they only know the
key.
A Set
is backed by a FastSet
and a List
.
The List
is called order
and tracks the iteration order of the Set
.
Values are produced in the order they were first inserted.
The FastSet
is called store
and ensures uniqueness and very fast searches.
The FastSet
stores nodes form the order
list but hashes and compares them by
their value
property.
The Store
and Order
constructors can be overridden by inheritors.
The number of items in this collection.
Removes a value from the end of a collection, and returns that value.
Removes a value from the beginning of a collection, and returns that value.
Returns the set of values including all values from both of these sets.
Returns the set of values that are in both of these sets.
Returns the set of values that are in this set, excluding the values that are also in the other set.
Returns the set of values that are only in one of these sets.
Whether an equivalent value exists in this collection.
Retrieves the equivalent value from the collection.
Adds a value to a collection.
Deletes the first equivalent value. Returns whether the key was found and successfully deleted.
An alias for delete(value)
on sets that increases the overlap with the W3C DOMTokenList
interface, implemented by classList
.
An alias for has(value)
on sets that increases the overlap with the W3C DOMTokenList
interface, implemented by classList
.
Toggles the existence of a value in a set.
Copies values or entries from another collection into this collection, and then returns this.
Deletes every value or every value for each key. Returns the number of successful deletions.
Deletes every value equivalent to the given value from the collection.
Deletes all of the values in the collection.
Iterates every value in this collection.
Calls the callback for each entry in the collection.
Returns an array of the respective return values of a callback for each entry in this collection.
Returns an array with each value from this collection that passes the given test.
Aggregates every value in this collection with the result collected up to that index.
Aggregates every value in this collection, from right to left.
Returns an array of [key, class] entries where every value from the collection is placed into the same equivalence class if they return the same key through the given callback.
Returns whether any entry in this collection passes a given test.
Returns whether every entry in this collection passes a given test.
Returns whether any value in the collection is truthy.
Returns whether all values in the collection are truthy.
Returns one, arbitrary value from this collection, or undefined if there are none.
Returns the only value in this collection, or undefined if there is more than one value, or if there are no values in the collection.
Returns a sorted array of the values in this collection.
Returns a copy of this collection with the values in reverse order.
Returns a string of all the values in the collection delimited by the given string.
Returns the sum of all values in this collection.
Returns the arithmetic mean of the collection, by computing its sum and the count of values and returning the quotient.
Returns the smallest value in this collection.
Returns the largest value in this collection.
Returns an array of the respective values in this collection and in each collection provided as an argument.
Returns an array of [index, value] entries for each value in this collection, counting all values from the given index.
Returns a new collection of the same type containing all the values of itself and the values of any number of other iterable collections in order.
Assuming that this is a collection of collections, returns a new collection that contains all the values of each nested collection in order.
Returns an array of each value in this collection.
Returns an object with each property name and value corresponding to the entries in this collection.
Used by JSON.stringify
to create a JSON representation of the collection.
Returns whether this collection is equivalent to the given collection.
Creates a deep replica of this collection.
Creates a shallow clone of this collection.
The equals
function used to check whether values in this collection are equivalent.
The hash
function used by this collection to hash its own values.
Adds a listener for when values are added or removed at any position.
Unregisters a range change listener provided by addRangeChangeListener.
Informs range change listeners that values were removed then added at an index.
Adds a listener for before values are added or removed at any position.
Unregisters a range change listener provided by addBeforeRangeChangeListener or addRangeChangeListener with the beforeChange
flag.
Informs range change listeners that values will be removed then added at an index.
Adds a listener for an owned property with the given name.
Adds a listener for before a property changes.
Unregisters a property change listener provided by addOwnPropertyChangeListener.
Unregisters a property change listener provided by addBeforeOwnPropertyChangeListener or addOwnPropertyChangeListener with the beforeChange
flag.
Informs property change listeners that the value for a property name has changed.
Informs property change listeners that the value for a property name will change.
May perform internal changes necessary to dispatch property changes for a particular name.
Makes changes observable for this collection.
var Set = require("collections/set");
var object = {x: "hello"}; var set = new Set(["a", object]); set.add("b"); set.toArray(); set.add(object); set.toArray();
var nameSet = new Set(null, function (a, b) { return a.name === b.name; }, function (object) { return object.name; }); nameSet.add({name: "Kris", github: "kriskowal"}); nameSet.add({name: "Stuart", github: "stuk"}); nameSet.get({name: "Kris"}); nameSet.add({name: "Stuart", github: "wrong"}); nameSet.get({name: "Stuart"});