Plugins: fleegix.hash.Hash
Sortable collection object which allows items to be indexed both by string key or by index.
Download fleegix.hash.Hash (7KB)
Contents
- addItem
- addItemCreateKey
- getItem
- setItem
- removeItem
- getByKey
- setByKey
- removeByKey
- getByIndex
- setByIndex
- removeByIndex
- hasKey
- hasValue
- getAllKeys
- replaceKey
- insertAtIndex
- insertAfterKey
- pop
- each
- eachKey
- eachValue
- concat
- sort
- sortByKey
addItem
Syntax
addItem(key, value);
Parameters
key (String) -- A string key to use for accessing the item.
value (any datatype) -- the item to be saved in the Hash.
Description
Adds a new item into the Hash.
Examples
var badSciFi = new fleegix.hash.Hash();
badSciFi.addItem("Connery", "Zardoz");
badSciFi.addItem("York", "Logan's Run");
badSciFi.addItem("Douglas", "Saturn 3");
addItemCreateKey
Syntax
addItemCreateKey(value);
Parameters
value (any datatype) -- the item to be saved in the Hash.
Description
Adds a new item into the Hash and returns a random string key for it.
Examples
var foo = new fleegix.hash.Hash();
var key = foo.addItemCreateKey('howdy');
key;
=> "P0rp6asXWMJBQAms"
foo.getItem(key);
=> "howdy"
getItem
Syntax
getItem(key/index);
Parameters
key/index (String/Number) -- The key or index to use for accessing the item.
Description
Retrieves an item from the Hash using either string key, or its index position according to the sort.
Examples
badSciFi.getItem(2);
=> "Saturn 3"
badSciFi.getItem("York");
=> "Logan's Run"
setItem
Syntax
setByIndex(key/index, value);
Parameters
key/index (String/Number) -- The string key or index to use for accessing the item.
value (any datatype) -- the item to be saved in the Hash.
Description
Sets an existing Hash item to the desired value.
Examples
badSciFi.setItem('Connery', 'Zardoz');
badSciFi.getItem('Connery');
=> "Zardoz"
badSciFi.setItem(0, 'Outland');
badSciFi.getItem(0);
=> "Outland"
removeItem
Syntax
removeItem(key/index);
Parameters
key/index (String/Number) -- The string key or index for the Hash item to remove.
Description
Removes the desired item in the Hash.
Examples
badSciFi.count;
=> 3;
badSciFi.removeItem(0);
badSciFi.count;
=> 2
badSciFi.removeItem('York');
typeof badSciFi.getItem('York');
=> "undefined"
getByKey
Syntax
getByKey(key);
Parameters
key (String) -- A string key to use for accessing the item.
Description
Retrieves an item from the Hash using its string key.
Examples
badSciFi.getByKey('Connery');
=> "Zardoz"
setByKey
Syntax
setByKey(key, value);
Parameters
key (String) -- A string key to use for accessing the item.
value (any datatype) -- the item to be saved in the Hash.
Description
Sets an existing Hash item to the desired value.
Examples
badSciFi.setByKey('Connery', 'Outland');
badSciFi.getByKey('Connery');
=> "Outland"
removeByKey
Syntax
removeByKey(key);
Parameters
key (String) -- A string key for the Hash item to remove.
Description
Removes the desired item in the Hash.
Examples
badSciFi.removeByKey('Connery');
typeof badSciFi.getByKey('Connery');
=> "undefined"
getByIndex
Syntax
getByIndex(index);
Parameters
index (Number) -- The index to use for accessing the item.
Description
Retrieves an item from the Hash using its index position according to the sort.
Examples
badSciFi.getByIndex(2);
=> "Saturn 3"
setByIndex
Syntax
setByIndex(index, value);
Parameters
index (Number) -- The index to use for accessing the item.
value (any datatype) -- the item to be saved in the Hash.
Description
Sets an existing Hash item to the desired value.
Examples
badSciFi.setByIndex(0, 'Outland');
badSciFi.getByIndex(0);
=> "Outland"
removeByIndex
Syntax
removeByIndex(index);
Parameters
index (Number) -- The index for the Hash item to remove.
Description
Removes the desired item in the Hash.
Examples
badSciFi.count;
=> 3;
badSciFi.removeByIndex(0);
badSciFi.count;
=> 2
hasKey
Syntax
hasKey(key);
Parameters
key (String) -- A string key to check for the existence of in the Hash.
Description
Checks for the existence of a particular key in the Hash.
Examples
badSciFi.hasKey('Shatner');
=> false
badSciFi.addItem('Shatner',
'Any classic Star Trek episode');
badSciFi.hasKey('Shatner');
=> true
hasValue
Syntax
hasValue(value);
Parameters
key (Any) -- A value to to check for the existence of in the Hash.
Description
Checks for the existence of a particular key in the Hash.
Examples
badSciFi.hasValue("Westworld");
=> false
badSciFi.hasValue("Logan's Run");
=> true
getAllKeys
Syntax
getAllKeys();
Parameters
(None)
Description
Returns a comma-delimited list of all the Hash keys.
Examples
badSciFi.getAllKeys();
=> "Connery,York,Douglas"
replaceKey
Syntax
replaceKey(oldKey, newKey);
Parameters
oldKey, newKey (String) -- the original and replacement string keys for the desired item in the Hash.
Description
Replaces a string key for an item in the Hash with a new one.
Examples
badSciFi.getItem("York");
=> "Logan's Run"
badSciFi.replaceKey("York", "Ustinov");
badSciFi.getItem("Ustinov");
=> "Logan's Run"
insertAtIndex
Syntax
insertAtIndex(index, key, value);
Parameters
index (Number) -- The index position in the Hash to insert the new item.
key (String) -- The string key for the new item.
value (any datatype) -- the item to be saved in the Hash.
Description
Inserts a new item at the specified index position in the Hash.
Examples
badSciFi.getItem(1);
=> "Logan's Run"
badSciFi.insertAtIndex(1, 'Russell',
'Big Trouble In Little China');
badSciFi.getItem(1);
=> "Big Trouble in Little China"
badSciFi.getItem(2);
=> "Logan's Run"
insertAfterKey
Syntax
insertAfterKey(locationKey, itemKey, itemValue);
Parameters
locationKey (String) -- The key for the item to insert the new item after in the Hash.
itemKey (String) -- The string key for the new item.
itemValue (any datatype) -- the item to be saved in the Hash.
Description
Inserts a new item after the item with the specified key in the Hash.
Examples
badSciFi.insertAfterKey('York',
'Lambert', 'Highlander 2');
badSciFi.getItem(1);
=> "Logan's Run"
badSciFi.getItem(2);
=> "Highlander 2"
pop
Syntax
pop()
Parameters
(None)
Description
Returns the last item in the Hash according to the current sort, and removes it from the Hash.
Examples
badSciFi.count;
=> 3
badSciFi.pop();
=> "Saturn 3"
badSciFi.count;
=> 2
badSciFi.pop();
=> "Logan's Run"
badSciFi.count;
=> 1
each
Syntax
each(iterateFunc);
Parameters
iterateFunc (Function) -- Function to be called against each item in the Hash. Should take two parameters, the value and the key.
Description
Calls the passed function once for each item in the Hash, passing it the value and the key as parameters.
Examples
var f = function (value, key) {
print(key + " is in the movie " + value + ".");
};
badSciFi.each(f);
=> "Connery is in Zardoz."
=> "York is in Logan's Run."
=> "Douglas is in Saturn 3."
eachKey
Syntax
eachKey(iterateFunc);
Parameters
iterateFunc (Function) -- Function to be called against each item in the Hash. Should take one parameter, the key of the Hash item.
Description
Calls the passed function once for each item in the Hash, passing it the key as a parameter.
Examples
var f = function (key) {
print(key + " is in a bad sci-fi flick.");
};
badSciFi.each(f);
=> "Connery is in a bad sci-fi flick."
=> "York is in a bad sci-fi flick."
=> "Douglas is in a bad sci-fi flick."
eachValue
Syntax
eachValue(iterateFunc);
Parameters
iterateFunc (Function) -- Function to be called against each item in the Hash. Should take one parameter, the value of the Hash item.
Description
Calls the passed function once for each item in the Hash, passing it the value as a parameter.
Examples
var f = function (value) {
print(value + " is so bad it's good.");
};
badSciFi.each(f);
=> "Zardoz is so bad it's good."
=> "Logan's Run is so bad it's good."
=> "Saturn 3 is so bad it's good."
concat
Syntax
concat(otherHash);
Parameters
otherHash (fleegix.hash.Hash) -- New hash to append to the original one.
Description
Adds a new Hash to the end of the calling one.
Examples
newBadSciFi = fleegix.hash.Hash();
newBadSciFi.addItem('Lambert', 'Highlander 2');
newBadSciFi.addItem('Russell',
'Big Trouble in Little China');
badSciFi.count;
=> 3
badSciFi.concat(newBadSciFi);
badSciFi.count;
=> 5
sort
Syntax
sort([comparator]);
Parameters
comparator (Function) Function to use for comparison in sort -- if not passed, defaults to case-insensitive, ascending alphabetical sort. (See fleegix.hash.sorts.)
Description
Sorts the items in the Hash into the desired order according to their values.
Examples
badSciFi.count
=> 3
badSciFi.sort();
badSciFi.getItem(2);
=> "Zardoz"
badSciFi.sort(fleegix.hash.sorts.DESCENDING_NOCASE);
badSciFi.getItem(0);
=> "Zardoz"
sortByKey
Syntax
sortByKey([comparator]);
Parameters
comparator (Function) Function to use for comparison in sort -- if not passed, defaults to case-insensitive, ascending alphabetical sort. (See fleegix.hash.sorts.)
Description
Sorts the items in the Hash into the desired order according to their keys.
Examples
badSciFi.count
=> 3
badSciFi.sortByKey();
badSciFi.getItem(2);
=> "York"
badSciFi.sortByKey(fleegix.hash.sorts.DESCENDING_NOCASE);
badSciFi.getItem(0);
=> "York"