14 var hash2 = new Hash({ a: 'string', b: 233, c: {} });
16 describe("Hash Methods", function(){
20 it('should return a new hash', function(){
21 expect(Hash.type(new Hash())).to.equal(true);
24 it('should return a copy of a hash', function(){
25 var hash = new Hash({a: 1, b: 2, c: 3});
26 var copy = new Hash(hash);
27 expect(copy).to.not.equal(hash);
28 expect(copy).to.deep.equal(hash);
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.deep.equal(new Hash({b:2,c:3}));
36 expect(hash.erase('d')).to.deep.equal(new Hash({b:2,c:3}));
38 hash = new Hash({a: 1, b: 2, c: 3});
39 expect(hash.erase('a')).to.deep.equal(new Hash({b:2,c:3}));
40 expect(hash.erase('d')).to.deep.equal(new Hash({b:2,c:3}));
45 it('should return the value corresponding to the specified key otherwise null', function(){
46 var hash = new Hash({a: 1, b: 2, c: 3});
47 expect(hash.get('c')).to.equal(3);
48 expect(hash.get('d')).to.equal(null);
53 it('should set the key with the corresponding value', function(){
54 var myHash = new Hash({a: 1, b: 2, c: 3}).set('c', 7).set('d', 8);
55 expect(myHash).to.deep.equal(new Hash({a:1,b:2,c:7,d:8}));
60 it('should empty the hash', function(){
61 var hash = new Hash({a: 1, b: 2, c: 3});
62 expect(hash.empty()).to.deep.equal(new Hash());
67 it('should include a key value if the hash does not have the key otherwise ignore', function(){
68 var hash = new Hash({a: 1, b: 2, c: 3});
69 expect(hash.include('e', 7)).to.deep.equal(new Hash({a:1,b:2,c:3,e:7}));
70 expect(hash.include('a', 7)).to.deep.equal(new Hash({a:1,b:2,c:3,e:7}));
73 // Hash.keyOf | Hash.indexOf
75 it('should return the key of the value or null if not found', function(){
76 var hash = new Hash({a: 1, b: 2, c: 3, d: 1});
77 expect(hash.keyOf(1)).to.equal('a');
78 expect(hash.keyOf('not found')).to.equal(null);
80 expect(hash.indexOf(1)).to.equal('a');
81 expect(hash.indexOf('not found')).to.equal(null);
86 it('should return true if the hash has the key otherwise false', function(){
87 var hash = new Hash({a: 1, b: 2, c: 3});
88 expect(hash.has('a')).to.equal(true);
89 expect(hash.has('d')).to.equal(false);
92 // Hash.hasValue | Hash.contains
94 it('should return true if the hash hasValue otherwise false', function(){
95 var hash = new Hash({a: 1, b: 2, c: 3});
96 expect(hash.hasValue(1)).to.equal(true);
97 expect(hash.hasValue('not found')).to.equal(false);
99 expect(hash.contains(1)).to.equal(true);
100 expect(hash.contains('not found')).to.equal(false);
105 it('should getClean JavaScript object', function(){
106 var hash = new Hash({a: 1, b: 2, c: 3});
107 expect(hash.getClean()).to.deep.equal({a:1,b:2,c:3});
112 it('should extend a Hash with an object', function(){
113 var hash = new Hash({a: 1, b: 2, c: 3});
114 expect(hash.extend({a:4,d:7,e:8})).to.deep.equal(new Hash({a:4,b:2,c:3,d:7,e:8}));
117 it('should extend a Hash with another Hash', function(){
118 var hash = new Hash({a: 1, b: 2, c: 3});
119 expect(hash.extend(new Hash({a:4,d:7,e:8}))).to.deep.equal(new Hash({a:4,b:2,c:3,d:7,e:8}));
124 it('should merge a Hash with an object', function(){
125 var hash = new Hash({a: 1, b: 2, c: 3});
126 expect(hash.combine({a:4,d:7,e:8})).to.deep.equal(new Hash({a:1,b:2,c:3,d:7,e:8}));
129 it('should merge a Hash with another Hash', function(){
130 var hash = new Hash({a: 1, b: 2, c: 3});
131 expect(hash.combine(new Hash({a:4,d:7,e:8}))).to.deep.equal(new Hash({a:1,b:2,c:3,d:7,e:8}));
136 it('should iterate through each property', function(){
137 var newHash = new Hash();
138 var hash = new Hash({a: 1, b: 2, c: 3});
139 hash.each(function(value, key){
140 newHash.set(key, value);
142 expect(newHash).to.deep.equal(hash);
147 it('should map a new Hash according to the comparator', function(){
148 expect(hash2.map(Number.type)).to.deep.equal(new Hash({a:false,b:true,c:false}));
153 it('should filter the Hash according to the comparator', function(){
154 expect(hash2.filter(Number.type)).to.deep.equal(new Hash({b:233}));
159 it('should return true if every value matches the comparator, otherwise false', function(){
160 expect(hash2.every($defined)).to.equal(true);
161 expect(hash2.every(Number.type)).to.equal(false);
166 it('should return true if some of the values match the comparator, otherwise false', function(){
167 expect(hash2.some(Number.type)).to.equal(true);
168 expect(hash2.some(Array.type)).to.equal(false);
173 it('getKeys should return an empty array', function(){
174 expect(new Hash().getKeys()).to.deep.equal([]);
177 it('should return an array containing the keys of the hash', function(){
178 expect(hash2.getKeys()).to.deep.equal(['a', 'b', 'c']);
183 it('getValues should return an empty array', function(){
184 expect(new Hash().getValues()).to.deep.equal([]);
187 it('should return an array with the values of the hash', function(){
188 expect(hash2.getValues()).to.deep.equal(['string', 233, {}]);
191 // Hash.toQueryString
193 it('should return a query string', function(){
194 var myHash = new Hash({apple: "red", lemon: "yellow"});
195 expect(myHash.toQueryString()).to.equal('apple=red&lemon=yellow');
197 var myHash2 = new Hash({apple: ['red', 'yellow'], lemon: ['green', 'yellow']});
198 expect(myHash2.toQueryString()).to.equal('apple[0]=red&apple[1]=yellow&lemon[0]=green&lemon[1]=yellow');
200 var myHash3 = new Hash({fruits: {apple: ['red', 'yellow'], lemon: ['green', 'yellow']}});
201 expect(myHash3.toQueryString()).to.equal('fruits[apple][0]=red&fruits[apple][1]=yellow&fruits[lemon][0]=green&fruits[lemon][1]=yellow');