Module documentation for hash.js

Class Index

Hash
Hashtable implementation.

Classes

Hash

Hashtable implementation. This provides a 'length' property that's maintained throughout all updates, and has no restrictions upon the types of keys that may be used.

Methods

init

Creates a new Hash. If the copy_from object is specified, it copies the keys and values from that.

Params
copy_from (Object)
Copy fields/values from this object (optional)

get

Returns the value of a key, or undefined if not set.

Params
key (String)
Key to retrieve.

contains

Returns true if this hashtable contains the specified key.

Params
key (String)
Key to test.

put

Sets key to val, then returns val.

Params
key (String)
Key to set.
val (Object)
Value to set it to.

remove

Removes a key, returning the hashtable itself.

Params
key (String)
Key to remove.

ensure

Sets a key if it doesn't already exist.

Params
key (String)
Key to set.
default_val (Object)
Value to set it to if key doesn't exist.

lazy_ensure

Sets a key if it doesn't already exist. Instead of taking a default value, takes a function of the key and evaluates that to get the default. This lets you defer construction costs until they're really needed.

Params
key (String)
Key to set.
default_fn (Function(key))
Function to invoke if key doesn't exist.

pop

Removes and returns the specified key.

Params
key

update

Copies all properties of the specified object to this hash. Does not consider values inherited from the prototype.

Params
obj

empty

Removes all entries from this hash.

keys

Returns a list of all keys in this hash. The list is in unspecified order, and does not share structure with the Hash.

values

Returns a list of all values in this hash. The list is in unspecified order, and does not share structure with the Hash. Values retain their identities.

items

Returns a list of [key, value] arrays for this hash.

items_obj

Returns a { key: value } JavaScript object with the contents of this hash. This shares structure with the hash; mutations to it affect the original Hash object and vis versa. Make a copy if you don't want this behavior.