11 var hash2 = new Hash({ a: 'string', b: 233, c: {} });
13 describe("Hash Methods", {
17 'should return a new hash': function(){
18 value_of(Hash.type(new Hash())).should_be_true();
21 'should return a copy of a hash': function(){
22 var hash = new Hash({a: 1, b: 2, c: 3});
23 var copy = new Hash(hash);
24 value_of(copy !== hash).should_be_true();
25 value_of(copy).should_be(hash);
30 'should remove a key and its value from the hash': function(){
31 var hash = new Hash({a: 1, b: 2, c: 3});
32 value_of(hash.erase('a')).should_be(new Hash({b:2,c:3}));
33 value_of(hash.erase('d')).should_be(new Hash({b:2,c:3}));
35 hash = new Hash({a: 1, b: 2, c: 3});
36 value_of(hash.erase('a')).should_be(new Hash({b:2,c:3}));
37 value_of(hash.erase('d')).should_be(new Hash({b:2,c:3}));
42 'should return the value corresponding to the specified key otherwise null': function(){
43 var hash = new Hash({a: 1, b: 2, c: 3});
44 value_of(hash.get('c')).should_be(3);
45 value_of(hash.get('d')).should_be_null();
50 'should set the key with the corresponding value': function(){
51 var myHash = new Hash({a: 1, b: 2, c: 3}).set('c', 7).set('d', 8);
52 value_of(myHash).should_be(new Hash({a:1,b:2,c:7,d:8}));
57 'should empty the hash': function(){
58 var hash = new Hash({a: 1, b: 2, c: 3});
59 value_of(hash.empty()).should_be(new Hash());
64 'should include a key value if the hash does not have the key otherwise ignore': function(){
65 var hash = new Hash({a: 1, b: 2, c: 3});
66 value_of(hash.include('e', 7)).should_be(new Hash({a:1,b:2,c:3,e:7}));
67 value_of(hash.include('a', 7)).should_be(new Hash({a:1,b:2,c:3,e:7}));
70 // Hash.keyOf | Hash.indexOf
72 'should return the key of the value or null if not found': function(){
73 var hash = new Hash({a: 1, b: 2, c: 3, d: 1});
74 value_of(hash.keyOf(1)).should_be('a');
75 value_of(hash.keyOf('not found')).should_be_null();
77 value_of(hash.indexOf(1)).should_be('a');
78 value_of(hash.indexOf('not found')).should_be_null();
83 'should return true if the hash has the key otherwise false': function(){
84 var hash = new Hash({a: 1, b: 2, c: 3});
85 value_of(hash.has('a')).should_be_true();
86 value_of(hash.has('d')).should_be_false();
89 // Hash.hasValue | Hash.contains
91 'should return true if the hash hasValue otherwise false': function(){
92 var hash = new Hash({a: 1, b: 2, c: 3});
93 value_of(hash.hasValue(1)).should_be_true();
94 value_of(hash.hasValue('not found')).should_be_false();
96 value_of(hash.contains(1)).should_be_true();
97 value_of(hash.contains('not found')).should_be_false();
102 'should getClean JavaScript object': function(){
103 var hash = new Hash({a: 1, b: 2, c: 3});
104 value_of(hash.getClean()).should_be({a:1,b:2,c:3});
109 'should extend a Hash with an object': function(){
110 var hash = new Hash({a: 1, b: 2, c: 3});
111 value_of(hash.extend({a:4,d:7,e:8})).should_be(new Hash({a:4,b:2,c:3,d:7,e:8}));
114 'should extend a Hash with another Hash': function(){
115 var hash = new Hash({a: 1, b: 2, c: 3});
116 value_of(hash.extend(new Hash({a:4,d:7,e:8}))).should_be(new Hash({a:4,b:2,c:3,d:7,e:8}));
121 'should merge a Hash with an object': function(){
122 var hash = new Hash({a: 1, b: 2, c: 3});
123 value_of(hash.combine({a:4,d:7,e:8})).should_be(new Hash({a:1,b:2,c:3,d:7,e:8}));
126 'should merge a Hash with another Hash': function(){
127 var hash = new Hash({a: 1, b: 2, c: 3});
128 value_of(hash.combine(new Hash({a:4,d:7,e:8}))).should_be(new Hash({a:1,b:2,c:3,d:7,e:8}));
133 'should iterate through each property': function(){
134 var newHash = new Hash();
135 var hash = new Hash({a: 1, b: 2, c: 3});
136 hash.each(function(value, key){
137 newHash.set(key, value);
139 value_of(newHash).should_be(hash);
144 'should map a new Hash according to the comparator': function(){
145 value_of(hash2.map(Number.type)).should_be(new Hash({a:false,b:true,c:false}));
150 'should filter the Hash according to the comparator': function(){
151 value_of(hash2.filter(Number.type)).should_be(new Hash({b:233}));
156 'should return true if every value matches the comparator, otherwise false': function(){
157 value_of(hash2.every($defined)).should_be_true();
158 value_of(hash2.every(Number.type)).should_be_false();
163 'should return true if some of the values match the comparator, otherwise false': function(){
164 value_of(hash2.some(Number.type)).should_be_true();
165 value_of(hash2.some(Array.type)).should_be_false();
170 'getKeys should return an empty array': function(){
171 value_of(new Hash().getKeys()).should_be([]);
174 'should return an array containing the keys of the hash': function(){
175 value_of(hash2.getKeys()).should_be(['a', 'b', 'c']);
180 'getValues should return an empty array': function(){
181 value_of(new Hash().getValues()).should_be([]);
184 'should return an array with the values of the hash': function(){
185 value_of(hash2.getValues()).should_be(['string', 233, {}]);
188 // Hash.toQueryString
190 'should return a query string': function(){
191 var myHash = new Hash({apple: "red", lemon: "yellow"});
192 value_of(myHash.toQueryString()).should_be('apple=red&lemon=yellow');
194 var myHash2 = new Hash({apple: ['red', 'yellow'], lemon: ['green', 'yellow']});
195 value_of(myHash2.toQueryString()).should_be('apple[0]=red&apple[1]=yellow&lemon[0]=green&lemon[1]=yellow');
197 var myHash3 = new Hash({fruits: {apple: ['red', 'yellow'], lemon: ['green', 'yellow']}});
198 value_of(myHash3.toQueryString()).should_be('fruits[apple][0]=red&fruits[apple][1]=yellow&fruits[lemon][0]=green&fruits[lemon][1]=yellow');