Class Pot.Hash
Defined in: <pot.js>.
Constructor Attributes | Constructor Name and Description |
---|---|
Pot.Hash()
Hash.
|
Field Attributes | Field Name and Description |
---|---|
Length of Hash data.
|
|
StopIteration.
|
Method Attributes | Method Name and Description |
---|---|
clear()
Clear the data.
|
|
<static> |
Pot.Hash.every(callback, (context))
Tests whether all elements in the object pass the
test implemented by the provided function.
|
<static> |
Pot.Hash.filter(callback, (context))
Creates a new object with all elements that
pass the test implemented by the provided function.
|
<static> |
Pot.Hash.forEach(callback, (context))
Iterate each items with calls callback function.
|
get(key)
Get the item by the specified key name.
|
|
has(key)
Check whether a key exists.
|
|
hasValue(value)
Check whether a value exists.
|
|
isHash()
isHash.
|
|
keys()
Collect all key names as an array.
|
|
<static> |
Pot.Hash.map(callback, (context))
Creates a new object with the results of calling a
provided function on every element in object.
|
<static> |
Pot.Hash.reduce(callback, initial, (context))
Apply a function against an accumulator and each value of
the object (from left-to-right) as to reduce it to a single value.
|
remove(key)
Remove an item by specified key name.
|
|
set(key, value)
Set the item that is specified key name and value.
|
|
<static> |
Pot.Hash.some(callback, (context))
Tests whether some element in the object passes the
test implemented by the provided function.
|
toItems()
Convert all elements to the items() format array.
|
|
toJSON()
Convert all items to JSON format.
|
|
toObject()
Convert all items to plain object.
|
|
toString()
toString.
|
|
values()
Collect all values as an array.
|
Class Detail
Pot.Hash()
Hash.
Hash can contain any key names.
e.g. "__iterator__" and "hasOwnProperty" etc. will crush object.
The implementation to resolve them.
var hash = new Pot.Hash(); hash.set('key1', 'value1'); hash.set('key2', [1, 2, 3]); debug(hash.toJSON()); // @results {"key1":"value1","key2":[1,2,3]} // hash.clear(); // If you use the following keys to builtin Object, // object will no longer work usually. hash.set('__iterator__', 'iterator'); hash.set('hasOwnProperty', 'hasOwn'); hash.set('prototype', 'proto'); hash.set('constructor', 'construct'); debug(hash.toJSON()); // @results // { // "__iterator__": "iterator", // "hasOwnProperty": "hasOwn", // "prototype": "proto", // "constructor": "construct" // } // var result = hash.map(function(value, key, object) { return '[' + value + ']'; }).reduce(function(a, b) { return a + b; }); debug(result); // @results result = '[iterator][hasOwn][proto][construct]'
- Parameters:
- {Object|*} (...)
- (Optional) Items.
- Returns:
- {Pot.Hash} Returns an instance of Pot.Hash.
Field Detail
{Number}
length
Length of Hash data.
{Object}
StopIteration
StopIteration.
Method Detail
{Pot.Hash}
clear()
Clear the data.
All items will remove.
- Returns:
- {Pot.Hash} Return the Hash.
<static>
{Boolean}
Pot.Hash.every(callback, (context))
Tests whether all elements in the object pass the
test implemented by the provided function.
This method like Array.prototype.every
var hash = new Pot.Hash(); hash.set({A: 12, B: 5, C: 8, D: 130, E: 44}); var result = hash.every(function(value, key, object) { return (value >= 10); }); debug('[1] result = ' + result); // @results false hash.clear(); hash.set({A: 12, B: 54, C: 18, D: 130, E: 44}); result = hash.every(function(value, key, object) { return (value >= 10); }); debug('[2] result = ' + result); // @results true
- Parameters:
- {Function} callback
- A callback function.
- {*} (context)
- (Optional) Object to use as `this` when executing callback.
- Returns:
- {Boolean} Return the Boolean result by callback.
<static>
{Pot.Hash}
Pot.Hash.filter(callback, (context))
Creates a new object with all elements that
pass the test implemented by the provided function.
This method like Array.prototype.filter
var hash = new Pot.Hash({a: 1, b: 2, c: 3, d: 4, e: 5}); var result = hash.filter(function(value, key, obj) { return value % 2 == 0; }); debug(result.toObject()); // @results {b: 2, d: 4}
- Parameters:
- {Function} callback
- A callback function.
- {*} (context)
- (Optional) Object to use as `this` when executing callback.
- Returns:
- {Pot.Hash} Return a new Hash instance that has result of each callbacks.
<static>
{Pot.Hash}
Pot.Hash.forEach(callback, (context))
Iterate each items with calls callback function.
var hash = new Pot.Hash(); hash.set('key1', 'value1'); hash.set('key2', 'value2'); hash.set('key3', 'value3'); var s = ''; hash.forEach(function(value, key, object) { s += key + ':' + value + ';'; }); debug(s); // @results s = 'key1:value1;key2:value2;key3:value3;'
- Parameters:
- {Function} callback
- An iterable function. function(value, key, object) this == `context`. Throw Pot.StopIteration if you want to stop the loop.
- {*} (context)
- Optionally, context object. (i.e. this)
- Returns:
- {Pot.Hash} Return the Hash.
{*}
get(key)
Get the item by the specified key name.
- Parameters:
- {String} key
- The key name.
- Returns:
- {*} A value that related to the key name.
{Boolean}
has(key)
Check whether a key exists.
- Parameters:
- {String} key
- The key name to check.
- Returns:
- {Boolean} Return true if exists.
{Boolean}
hasValue(value)
Check whether a value exists.
- Parameters:
- {*} value
- The value to check.
- Returns:
- {Boolean} Return true if exists.
{Function}
isHash()
isHash.
{Array}
keys()
Collect all key names as an array.
- Returns:
- {Array} Return all key names as an array.
<static>
{Pot.Hash}
Pot.Hash.map(callback, (context))
Creates a new object with the results of calling a
provided function on every element in object.
This method like Array.prototype.map
var hash = new Pot.Hash(); hash.set({a: 1, b: 2, c: 3, d: 4, e: 5}); var result = hash.map(function(value, key) { return value * 100; }); debug(result.toObject()); // @results {a: 100, b: 200, c: 300, d: 400, e: 500}
- Parameters:
- {Function} callback
- A callback function.
- {*} (context)
- (Optional) Object to use as `this` when executing callback.
- Returns:
- {Pot.Hash} Return a new instance of Hash that has result of each callbacks.
<static>
{*}
Pot.Hash.reduce(callback, initial, (context))
Apply a function against an accumulator and each value of
the object (from left-to-right) as to reduce it to a single value.
This method like Array.prototype.reduce
var object = {a: 1, b: 2, c: 3}; var hash = new Pot.Hash(object); var total = hash.reduce(function(a, b) { return a + b; }); debug(total); // @results 6
- Parameters:
- {Function} callback
- A callback function.
- {*} initial
- An initial value passed as `callback` argument that will be used on first iteration.
- {*} (context)
- (Optional) Object to use as the first argument to the first call of the `callback`.
- Returns:
- {*} Return the result of each callbacks.
{Pot.Hash}
remove(key)
Remove an item by specified key name.
- Parameters:
- {String} key
- The key name.
- Returns:
- {Pot.Hash} Return the Hash.
{Pot.Hash}
set(key, value)
Set the item that is specified key name and value.
- Parameters:
- {String} key
- The key name.
- {*} value
- The value.
- Returns:
- {Pot.Hash} Return the Hash.
<static>
{Boolean}
Pot.Hash.some(callback, (context))
Tests whether some element in the object passes the
test implemented by the provided function.
This method like Array.prototype.some
var hash = new Pot.Hash(); hash.set({A: 2, B: 5, C: 8, D: 1, E: 4}); var result = hash.some(function(value, key, object) { return (value >= 10); }); debug('[1] result = ' + result); // @results false hash.clear(); hash.set({A: 12, B: 5, C: 8, D: 1, E: 4}); result = hash.some(function(value, key, object) { return (value >= 10); }); debug('[2] result = ' + result); // @results true
- Parameters:
- {Function} callback
- A callback function.
- {*} (context)
- (Optional) Object to use as `this` when executing callback.
- Returns:
- {Boolean} Return the Boolean result by callback.
{Array}
toItems()
Convert all elements to the items() format array.
var hash = new Pot.Hash(); hash.set('prototype', 'value1'); hash.set('__iterator__', 'value2'); hash.set('hasOwnProperty', 'value3'); var result = hash.toItems(); debug(result); // @results // [ // ['prototype', 'value1'], // ['__iterator__', 'value2'], // ['hasOwnProperty', 'value3'] // ]
- Returns:
- {Array} Return the items() format array.
- See:
- Pot.Struct.items
{String}
toJSON()
Convert all items to JSON format.
var hash = new Pot.Hash({foo: [1], bar: [2], baz: [3]}); hash.set({a: 4, b: 5, c: [6, 7, '"hoge"']}); var json = hash.toJSON(); debug(json); // @results // '{"foo":[1],"bar":[2],"baz":[3],"a":4,"b":5,"c":[6,7,"\"hoge\""]}'
- Returns:
- {String} Return a JSON string object that has all items.
{Object}
toObject()
Convert all items to plain object.
Notice:
if special keys existed (e.g. hasOwnProperty or __iterator__ etc.)
then object will be broken.
- Returns:
- {Object} Return an object that has all items.
{Function}
toString()
toString.
- Returns:
- Return formatted string of object.
{Array}
values()
Collect all values as an array.
- Returns:
- {Array} Return all values as an array.