HexagonJS
Edit Page
Set
A set collection type. Proper sets are coming in ECMAScript 6 (which at time of writing has a scheduled release date of June 2015). This set object tries to keep as close as possible to the currently planned spec for sets, which should make it easier to swap out for the native implementation when it arrives and is well supported enough in browsers.
Examples
// create an empty set
var set = new hx.Set;

set.add('a');
set.add('b');
set.add('c');

// will log 3
console.log(set.size);

// remove an item from the set by key
set.delete('a');

// will log ['b', 'c']
console.log(set.values());

// will log [['b', 'b'], ['c', 'c']]
console.log(set.entries());

// will log ['b', 'c']
console.log(set.keys());

// removes everything from the set
set.clear();

// create a set prefilled with some values
set = new hx.Set(['a', 'b', 'c', 'd', 'e']);

// will log true
console.log(set.has('d'));

// will log false
console.log(set.has('g'));

// this will log each key and value in the set
set.forEach(function(v) {
  console.log(v)
});
Api
Prototypes
hx.Setdeprecated
Deprecated
Deprecated in favour of native ES6 Set
Constructors
hx.SetarrayArray
A set collection type.
Arguments
arrayArray
An optional array containing the values to initialise the set with
Properties
sizeNumber
The current size of the Set
Methods
addvalueAnySet
Adds the value to the set. Returns this Set for chaining.
Arguments
valueAny
The key to add.
Returns
Set
This Set for chaining
clear
Removes all entries from the Set, resulting in an empty set with size 0. Returns undefined.
deleteitemAnyBoolean
Removes an entry from the Set.
Arguments
itemAny
The item to remove.
Returns
Whether or not the value was removed (true if it was)
entriesArray
Returns the items in the set as an array of 2-element arrays. The first entry in each of these arrays is the value, the second entry is also the value. This is just how the ES6 spec currently says to do things, presumably to stay consistent with the Map collection.
Returns
An array of all the values from the set
forEachfFunctionthisArgObject
Calls the function supplied for each entry in the set. Returns this Set for chaining.
Arguments
fvalueAny
The function that should be called for each entry
Arguments
valueAny
A value from the set.
thisArg
An optional parameter which lets you supply the 'this' context to be used when calling f. The default is to use this Set.
hasitemAnyBoolean
Checks if the set contains an item. Returns true if the value is in the set, false otherwise.
Arguments
itemAny
The item to check existence of.
Returns
True if the value is in the set
keysArray
Does the same as values()
Returns
An array of all the values from the set
valuesArray
Returns an array of the items in this Set.
Returns
An array of all the values from the set