4 A custom Object ({}) implementation which does not account for prototypes when setting, getting, or iterating.
5 Useful because in JavaScript, we cannot use Object.prototype. Instead, we can use Hash.prototype!
8 Hash Method: constructor {#Hash:constructor}
9 --------------------------------------------
13 var myHash = new Hash([object]);
17 1. object - (*mixed*) A hash or object to implement.
21 * (*hash*) A new Hash instance.
25 var myHash = new Hash({
31 alert(myHash.has('aMethod')); //Returns true.
35 Hash Method: each {#Hash:each}
36 -------------------------------
38 Calls a function for each key-value pair in the object.
42 myHash.each(fn[, bind]);
46 1. fn - (*function*) The function which should be executed on each item in the Hash. This function is passed the item and its key in the Hash.
47 2. bind - (*object*, optional) The object to use as 'this' in the function. For more information, see [Function:bind][].
57 1. value - (*mixed*) The current value in the hash.
58 2. key - (*string*) The current value's key in the hash.
59 3. hash - (*hash*) The actual hash.
63 var hash = new Hash({first: "Sunday", second: "Monday", third: "Tuesday"});
64 hash.each(function(value, key){
65 alert("the " + key + " day of the week is " + value);
66 }); //Alerts "the first day of the week is Sunday", "the second day of the week is Monday", etc.
70 Hash Method: has {#Hash:has}
71 ----------------------------
73 Tests for the presence of a specified key in the Hash.
77 var inHash = myHash.has(item);
81 1. key - (*string*) The key to search for in the Hash.
85 * (*boolean*) If the Hash has a defined value for the specified key, returns true. Otherwise, returns false.
89 var hash = new Hash({'a': 'one', 'b': 'two', 'c': 'three'});
90 hash.has('a'); //returns true
91 hash.has('d'); //returns false
96 - Testing for a Hash prototype will never return true. Only testing the actual properties of the Hash will return true.
100 Hash Method: keyOf {#Hash:keyOf}
101 --------------------------------
103 Returns the key of the specified value. Synonymous with [Array:indexOf][].
107 var key = myHash.keyOf(item);
111 1. item - (*mixed*) The item to search for in the Hash.
115 * (*string*) If the Hash has a the specified item in it, returns the key of that item.
116 * (*boolean*) Otherwise, returns false.
120 var hash = new Hash({'a': 'one', 'b': 'two', 'c': 3});
121 hash.keyOf('two'); //returns 'b'
122 hash.keyOf(3); //returns 'c'
123 hash.keyOf('four') //returns false
127 - Testing for a Hash prototype will never return its key. Only the actual properties of the Hash will return their associated key.
131 Hash Method: hasValue {#Hash:hasValue}
132 --------------------------------------
134 Tests for the presence of a specified value in the Hash.
138 var inHash = myHash.hasValue(value);
142 1. value - (*mixed*) The value to search for in the Hash.
146 * (*boolean*) If the Hash has the passed in value in any of the keys, returns true. Otherwise, returns false.
150 var hash = new Hash({'a': 'one', 'b': 'two', 'c': 'three'});
151 hash.hasValue('one'); //returns true
152 hash.hasValue('four'); //returns false
155 Hash Method: extend {#Hash:extend}
156 ----------------------------------
158 Extends this Hash with the key-value pairs from the object passed in.
162 myHash.extend(properties);
166 1. properties - (*object*) The object whose items should be extended into this Hash
170 * (*hash*) This Hash, extended.
174 var hash = new Hash({
183 hash.extend(properties);
184 //hash now holds an object containing: { 'name': 'John', 'lastName': 'Dorian', 'age': '20', 'sex': 'male' };
188 Hash Method: combine {#Hash:combine}
189 --------------------------------
191 Combines this Hash with the key-value pairs of the object passed in. Does not allow duplicates (old values are **not** overwritten by new ones) and is case and type sensitive.
195 myHash.combine(properties);
199 1. properties - (*object*) The object whose items should be combined into this Hash.
203 * (*hash*) This Hash, combined with the new key-value pairs.
207 var hash = new Hash({
217 hash.combine(properties);
218 //hash now holds an object containing: { 'name': 'John', 'lastName': 'Doe', 'age': '20', 'sex': 'male' };
222 Hash Method: erase {#Hash:erase}
223 ----------------------------------
225 Removes the specified key from the Hash.
233 1. key - (*string*) The key to search for in the Hash.
237 * (*hash*) This Hash with the specified key and its value removed.
241 var hash = new Hash({
245 hash.erase('lastName');
246 //hash now holds an object containing: { 'name': 'John' };
250 Hash Method: get {#Hash:get}
251 ----------------------------
253 Retrieves a value from the hash.
261 1. key - (*string*) The key to retrieve in the Hash.
265 * (*mixed*) Returns the value that corresponds to the key if found.
266 * (*false*) null if the key doesn't exist
270 var hash = new Hash({
274 hash.get('name'); //returns 'John'
278 Hash Method: set {#Hash:set}
279 ----------------------------
281 Adds a key-value pair to the hash or replaces a previous value associated with the specified key.
285 myHash.set(key, value);
289 1. key - (*string*) The key to insert or modify in the Hash.
290 2. value - (*mixed*) The value to associate with the specified key in the Hash.
294 * (*hash*) This Hash with the specified key set to the specified value.
298 var hash = new Hash({
302 hash.set('name', 'Michelle'); //hash.name is now 'Michelle'
306 Hash Method: empty {#Hash:empty}
307 --------------------------------
317 var hash = new Hash({
322 //hash now holds an empty object: {}
326 Hash Method: include {#Hash:include}
327 ------------------------------------
329 Includes the specified key-value pair in the Hash if the key doesn't already exist.
333 myHash.include(key, value);
337 1. key - (*string*) The key to insert into the Hash.
338 2. value - (*mixed*) The value to associate with the specified key in the Hash.
342 * (*hash*) This Hash with the specified key included if it did not previously exist.
346 var hash = new Hash({
350 hash.include('name', 'Michelle'); //hash is unchanged
351 hash.include('age', 25); //hash.age is now 25
355 Hash Method: map {#Hash:map}
356 ----------------------------
358 Creates a new map with the results of calling a provided function on every value in the map.
362 var mappedHash = myHash.map(fn[, bind]);
366 1. fn - (*function*) The function to produce an element of the new Hash from an element of the current one.
367 2. bind - (*object*, optional) The object to use as 'this' in the function. For more information see [Function:bind][].
377 1. value - (mixed) The current value in the hash.
378 2. key - (string) The current value's key in the hash.
379 3. hash - (hash) The actual hash.
383 * (*hash*) The new mapped hash.
387 var timesTwo = new Hash({a: 1, b: 2, c: 3}).map(function(item, index){
389 }); //timesTwo now holds an object containing: {a: 2, b: 4, c: 6};
393 Hash Method: filter {#Hash:filter}
394 ----------------------------------
396 Creates a new Hash with all of the elements of the Hash for which the provided filtering function returns true.
400 var filteredHash = myHash.filter(fn[, bind]);
404 1. fn - (*function*) The function to test each element of the Hash. This function is passed the value and its key in the Hash.
405 2. bind - (*object*, optional) The object to use as 'this' in the function. For more information see [Function:bind][].
415 1. value - (*mixed*) The current value in the hash.
416 2. key - (*string*) The current value's key in the hash.
417 3. hash - (*hash*) The actual hash.
421 * (*hash*) The new filtered hash.
425 var biggerThanTwenty = new Hash({a: 10, b: 20, c: 30}).filter(function(value, key){
427 }); //biggerThanTwenty now holds an object containing: {c: 30}
430 Hash Method: every {#Hash:every}
431 --------------------------------
433 Returns true if every value in the object satisfies the provided testing function.
437 var allPassed = myHash.every(fn[, bind]);
441 1. fn - (*function*) The function to test each element of the Hash. This function is passed the value and its key in the Hash.
442 2. bind - (*object*, optional) The object to use as 'this' in the function. For more information see [Function:bind].
452 1. value - (*mixed*) The current value in the hash.
453 2. key - (*string*) The current value's key in the hash.
454 3. hash - (*hash*) The actual hash.
458 * (*boolean*) If every value in the Hash satisfies the provided testing function, returns true. Otherwise, returns false.
462 var areAllBigEnough = ({a: 10, b: 4, c: 25, d: 100}).every(function(value, key){
464 }); //areAllBigEnough = false
467 Hash Method: some {#Hash:some}
468 ------------------------------
470 Returns true if at least one value in the object satisfies the provided testing function.
474 var anyPassed = myHash.any(fn[, bind]);
478 1. fn - (*function*) The function to test each element of the Hash. This function is passed the value and its key in the Hash.
479 2. bind - (*object*, optional) The object to use as 'this' in the function. For more information see [Function:bind][].
490 1. value - (*mixed*) The current value in the hash.
491 2. key - (*string*) The current value's key in the hash.
492 3. hash - (*hash*) The actual hash.
496 * (*boolean*) If any value in the Hash satisfies the provided testing function, returns true. Otherwise, returns false.
500 var areAnyBigEnough = ({a: 10, b: 4, c: 25, d: 100}).some(function(value, key){
502 }); //isAnyBigEnough = true
506 Hash Method: getClean {#Hash:getClean}
507 --------------------------------------
509 Returns a a clean object from an Hash.
517 * (*object*) a clean object
521 var hash = new Hash({
525 hash = hash.getClean(); // hash doesnt contain Hash prototypes anymore
530 Hash Method: getKeys {#Hash:getKeys}
531 ------------------------------------
533 Returns an array containing all the keys, in the same order as the values returned by [Hash:getValues][].
537 var keys = myHash.getKeys();
541 * (*array*) An array containing all the keys of the hash.
545 Hash Method: getValues {#Hash:getValues}
546 ----------------------------------------
548 Returns an array containing all the values, in the same order as the keys returned by [Hash:getKeys][].
552 var values = myHash.getValues();
556 * (*array*) An array containing all the values of the hash.
560 Hash Method: getLength {#Hash:getLength}
561 ----------------------------------------
563 Returns the number of keys in the Hash.
567 var length = myHash.getLength();
571 * (*number*) The length of the Hash.
575 var hash = new Hash({
579 hash.getLength(); // returns 2
583 Hash Method: toQueryString {#Hash:toQueryString}
584 ------------------------------------------------
586 Generates a query string from key/value pairs in an object and URI encodes the values.
590 var queryString = myHash.toQueryString();
594 1. source - (*object*) The object to generate the query string from.
598 * (*string*) The query string.
602 #### Using Hash generic:
604 Hash.toQueryString({apple: "red", lemon: "yellow"}); //returns "apple=red&lemon=yellow"
606 #### Using Hash instance:
608 var myHash = new Hash({apple: "red", lemon: "yellow"});
609 myHash.toQueryString(); //returns "apple=red&lemon=yellow"
612 Utility Functions {#Utility}
613 ============================
618 Shortcut for the new [Hash](/Core/#Hash).
626 [Array:indexOf]: /Native/Array/#Array:indexOf
627 [Hash:getKeys]: #Hash:getKeys
628 [Hash:getValues]: #Hash:getValues
629 [Function:bind]: Native/Function#Function:bind