12 var hash2 = new Hash({ a: 'string', b: 233, c: {} });
14 describe('Hash Method', function(){
16 describe('Hash.constructor', function(){
18 it('should return a new hash', function(){
19 expect(Hash.type(new Hash())).to.equal(true);
22 it('should return a copy of a hash', function(){
23 var hash = new Hash({a: 1, b: 2, c: 3});
24 var copy = new Hash(hash);
25 expect(copy).to.not.equal(hash);
26 expect(copy).to.eql(hash);
31 describe('Hash.erase', function(){
33 it('should remove a key and its value from the hash', function(){
34 var hash = new Hash({a: 1, b: 2, c: 3});
35 expect(hash.erase('a')).to.eql(new Hash({b:2, c:3}));
36 expect(hash.erase('d')).to.eql(new Hash({b:2, c:3}));
38 hash = new Hash({a: 1, b: 2, c: 3});
39 expect(hash.erase('a')).to.eql(new Hash({b:2, c:3}));
40 expect(hash.erase('d')).to.eql(new Hash({b:2, c:3}));
45 describe('Hash.get', function(){
47 it('should return the value corresponding to the specified key otherwise null', function(){
48 var hash = new Hash({a: 1, b: 2, c: 3});
49 expect(hash.get('c')).to.equal(3);
50 expect(hash.get('d')).to.equal(null);
55 describe('Hash.set', function(){
57 it('should set the key with the corresponding value', function(){
58 var myHash = new Hash({a: 1, b: 2, c: 3}).set('c', 7).set('d', 8);
59 expect(myHash).to.eql(new Hash({a:1, b:2, c:7, d:8}));
64 describe('Hash.empty', function(){
66 it('should empty the hash', function(){
67 var hash = new Hash({a: 1, b: 2, c: 3});
68 expect(hash.empty()).to.eql(new Hash());
73 describe('Hash.include', function(){
75 it('should include a key value if the hash does not have the key otherwise ignore', function(){
76 var hash = new Hash({a: 1, b: 2, c: 3});
77 expect(hash.include('e', 7)).to.eql(new Hash({a:1, b:2, c:3, e:7}));
78 expect(hash.include('a', 7)).to.eql(new Hash({a:1, b:2, c:3, e:7}));
83 describe('Hash.keyOf | Hash.indexOf', function(){
85 it('should return the key of the value or null if not found', function(){
86 var hash = new Hash({a: 1, b: 2, c: 3, d: 1});
87 expect(hash.keyOf(1)).to.equal('a');
88 expect(hash.keyOf('not found')).to.equal(null);
90 expect(hash.indexOf(1)).to.equal('a');
91 expect(hash.indexOf('not found')).to.equal(null);
96 describe('Hash.has', function(){
98 it('should return true if the hash has the key otherwise false', function(){
99 var hash = new Hash({a: 1, b: 2, c: 3});
100 expect(hash.has('a')).to.equal(true);
101 expect(hash.has('d')).to.equal(false);
106 describe('Hash.hasValue | Hash.contains', function(){
108 it('should return true if the hash hasValue otherwise false', function(){
109 var hash = new Hash({a: 1, b: 2, c: 3});
110 expect(hash.hasValue(1)).to.equal(true);
111 expect(hash.hasValue('not found')).to.equal(false);
113 expect(hash.contains(1)).to.equal(true);
114 expect(hash.contains('not found')).to.equal(false);
119 describe('Hash.getClean', function(){
121 it('should getClean JavaScript object', function(){
122 var hash = new Hash({a: 1, b: 2, c: 3});
123 expect(hash.getClean()).to.eql({a:1, b:2, c:3});
128 describe('Hash.extend', function(){
130 it('should extend a Hash with an object', function(){
131 var hash = new Hash({a: 1, b: 2, c: 3});
132 expect(hash.extend({a:4, d:7, e:8})).to.eql(new Hash({a:4, b:2, c:3, d:7, e:8}));
135 it('should extend a Hash with another Hash', function(){
136 var hash = new Hash({a: 1, b: 2, c: 3});
137 expect(hash.extend(new Hash({a:4, d:7, e:8}))).to.eql(new Hash({a:4, b:2, c:3, d:7, e:8}));
142 describe('Hash.combine', function(){
144 it('should merge a Hash with an object', function(){
145 var hash = new Hash({a: 1, b: 2, c: 3});
146 expect(hash.combine({a:4, d:7, e:8})).to.eql(new Hash({a:1, b:2, c:3, d:7, e:8}));
149 it('should merge a Hash with another Hash', function(){
150 var hash = new Hash({a: 1, b: 2, c: 3});
151 expect(hash.combine(new Hash({a:4, d:7, e:8}))).to.eql(new Hash({a:1, b:2, c:3, d:7, e:8}));
156 describe('Hash.each', function(){
158 it('should iterate through each property', function(){
159 var newHash = new Hash();
160 var hash = new Hash({a: 1, b: 2, c: 3});
161 hash.each(function(value, key){
162 newHash.set(key, value);
164 expect(newHash).to.eql(hash);
169 describe('Hash.map', function(){
171 it('should map a new Hash according to the comparator', function(){
172 expect(hash2.map(Number.type)).to.eql(new Hash({a:false, b:true, c:false}));
177 describe('Hash.filter', function(){
179 it('should filter the Hash according to the comparator', function(){
180 expect(hash2.filter(Number.type)).to.eql(new Hash({b:233}));
185 describe('Hash.every', function(){
187 it('should return true if every value matches the comparator, otherwise false', function(){
188 expect(hash2.every($defined)).to.equal(true);
189 expect(hash2.every(Number.type)).to.equal(false);
194 describe('Hash.some', function(){
196 it('should return true if some of the values match the comparator, otherwise false', function(){
197 expect(hash2.some(Number.type)).to.equal(true);
198 expect(hash2.some(Array.type)).to.equal(false);
203 describe('Hash.getKeys', function(){
205 it('getKeys should return an empty array', function(){
206 expect(new Hash().getKeys()).to.eql([]);
209 it('should return an array containing the keys of the hash', function(){
210 expect(hash2.getKeys()).to.eql(['a', 'b', 'c']);
215 describe('Hash.getValues', function(){
217 it('getValues should return an empty array', function(){
218 expect(new Hash().getValues()).to.eql([]);
221 it('should return an array with the values of the hash', function(){
222 expect(hash2.getValues()).to.eql(['string', 233, {}]);
227 describe('Hash.toQueryString', function(){
229 it('should return a query string', function(){
230 var myHash = new Hash({apple: 'red', lemon: 'yellow'});
231 expect(myHash.toQueryString()).to.equal('apple=red&lemon=yellow');
233 var myHash2 = new Hash({apple: ['red', 'yellow'], lemon: ['green', 'yellow']});
234 expect(myHash2.toQueryString()).to.equal('apple[0]=red&apple[1]=yellow&lemon[0]=green&lemon[1]=yellow');
236 var myHash3 = new Hash({fruits: {apple: ['red', 'yellow'], lemon: ['green', 'yellow']}});
237 expect(myHash3.toQueryString()).to.equal('fruits[apple][0]=red&fruits[apple][1]=yellow&fruits[lemon][0]=green&fruits[lemon][1]=yellow');