Object buckets.MultiDictionary

A multi dictionary is a special kind of dictionary that holds multiple values against each key. Setting a value into the dictionary will add the value to a list at that key. Getting a key will return a list holding all the values associated with that key. This implementation accepts any kind of objects as keys.

If the keys are custom objects, a function that converts keys to unique strings must be provided at construction time.

Example:

function petToString(pet) {
 return pet.type + ' ' + pet.name;
}

If the values are custom objects, a function to check equality between values must be provided.

Example:

function petsAreEqualByAge(pet1,pet2) {
 return pet1.age===pet2.age;
}

Object Summary
Constructor Name and Description
buckets.MultiDictionary(toStrFunction, valuesEqualsFunction)
Creates an empty multi dictionary.
Method Summary
Method Name and Description
buckets.MultiDictionary.clear()
Removes all keys and values from the dictionary.
buckets.MultiDictionary.containsKey(key)
Returns true if the dictionary has at least one value associatted with the specified key.
buckets.MultiDictionary.equals(other)
Returns true if the multi dictionary is equal to another multi dictionary.
buckets.MultiDictionary.forEach(callback)
Executes the provided function once per key present in the multi dictionary.
buckets.MultiDictionary.get(key)
Returns an array holding the values associated with the specified key.
buckets.MultiDictionary.isEmpty()
Returns true if the dictionary contains no mappings.
buckets.MultiDictionary.keys()
Returns an array containing all the keys in the dictionary.
buckets.MultiDictionary.remove(key, value)
Removes the specified value from the list of values associated with the provided key.
buckets.MultiDictionary.set(key, value)
Associates the specified value with the specified key if it's not already present.
buckets.MultiDictionary.size()
Returns the number of keys in the dictionary.
buckets.MultiDictionary.values()
Returns an array containing all the values in the dictionary.
Object Detail
buckets.MultiDictionary(toStrFunction, valuesEqualsFunction)
Creates an empty multi dictionary.
Parameters:
{function(Object):string=} toStrFunction
optional function to convert keys to strings. If the keys aren't strings or if toString() is not appropriate, a custom function which receives a key and returns a unique string must be provided.
{function(Object|Object):boolean=} valuesEqualsFunction
optional function to check if two values are equal.
Method Detail
<static> buckets.MultiDictionary.clear()
Removes all keys and values from the dictionary.

<static> {boolean} buckets.MultiDictionary.containsKey(key)
Returns true if the dictionary has at least one value associatted with the specified key.
Parameters:
{Object} key
The key.
Returns:
{boolean} True if the dictionary has at least one value associatted the specified key.

<static> {boolean} buckets.MultiDictionary.equals(other)
Returns true if the multi dictionary is equal to another multi dictionary. Two dictionaries are equal if they have the same keys and the same values per key.
Parameters:
{buckets.MultiDictionary} other
The other dictionary.
Returns:
{boolean} True if the dictionary is equal to the given dictionary.

<static> buckets.MultiDictionary.forEach(callback)
Executes the provided function once per key present in the multi dictionary.
Parameters:
{function(Object|Array):*} callback
Function to execute. Receives 2 arguments: key and an array of values. To break the iteration you can optionally return false inside the callback.

<static> {Array} buckets.MultiDictionary.get(key)
Returns an array holding the values associated with the specified key.
Parameters:
{Object} key
The key.
Returns:
{Array} An array holding the values or an empty array if the dictionary contains no mappings for the provided key.

<static> {boolean} buckets.MultiDictionary.isEmpty()
Returns true if the dictionary contains no mappings.
Returns:
{boolean} True if the dictionary contains no mappings.

<static> {Array} buckets.MultiDictionary.keys()
Returns an array containing all the keys in the dictionary.
Returns:
{Array} An array containing all the keys in the dictionary.

<static> {*} buckets.MultiDictionary.remove(key, value)
Removes the specified value from the list of values associated with the provided key. If a value isn't given, all values associated with the specified key are removed.
Parameters:
{Object} key
The key.
{Object=} value
Optional argument to specify the element to remove from the list of values associated with the given key.
Returns:
{*} True if the dictionary changed, false if the key doesn't exist or if the specified value isn't associated with the given key.

<static> {boolean} buckets.MultiDictionary.set(key, value)
Associates the specified value with the specified key if it's not already present.
Parameters:
{Object} key
The Key.
{Object} value
The value to associate.
Returns:
{boolean} True if the value was not already associated with that key.

<static> {number} buckets.MultiDictionary.size()
Returns the number of keys in the dictionary.
Returns:
{number} The number of keys in the dictionary.

<static> {Array} buckets.MultiDictionary.values()
Returns an array containing all the values in the dictionary.
Returns:
{Array} An array containing all the values in the dictionary.