11 var object = { a: 'string', b: 233, c: {} };
13 describe("Object Methods", function(){
17 it('should return an object with only the specified keys', function(){
18 expect(Object.subset(object, ['a', 'b'])).to.eql({a:'string',b:233});
21 it('should ignore undefined keys', function(){
26 var subset = Object.subset(obj, ['a', 'b', 'c', 'd']);
27 expect(subset).to.eql({b: 'string', d: null});
28 // To equal doesn't check for undefined properties
29 expect('a' in subset).to.equal(false);
30 expect('c' in subset).to.equal(false);
35 it('should return the key of the value or null if not found', function(){
36 expect(Object.keyOf(object, 'string')).to.equal('a');
37 expect(Object.keyOf(object, 'not found')).to.equal(null);
42 it('should return true if the object contains value otherwise false', function(){
43 expect(Object.contains(object, 'string')).to.equal(true);
44 expect(Object.contains(object, 'not found')).to.equal(false);
49 it('should map a new object according to the comparator', function(){
50 expect(Object.map(object, Type.isNumber)).to.eql({a:false,b:true,c:false});
55 it('should filter the object according to the comparator', function(){
56 expect(Object.filter(object, Type.isNumber)).to.eql({b:233});
61 it('should return true if every value matches the comparator, otherwise false', function(){
62 expect(Object.every(object, typeOf)).to.equal(true);
63 expect(Object.every(object, Type.isNumber)).to.equal(false);
68 it('should return true if some of the values match the comparator, otherwise false', function(){
69 expect(Object.some(object, Type.isNumber)).to.equal(true);
70 expect(Object.some(object, Type.isArray)).to.equal(false);
75 it('values should return an empty array', function(){
76 expect(Object.values({})).to.eql([]);
79 it('should return an array with the values of the object', function(){
80 expect(Object.values(object)).to.eql(['string', 233, {}]);
83 // Object.toQueryString
85 it('should return a query string', function(){
86 var myObject = {apple: "red", lemon: "yellow"};
87 expect(Object.toQueryString(myObject)).to.equal('apple=red&lemon=yellow');
89 var myObject2 = {apple: ['red', 'yellow'], lemon: ['green', 'yellow']};
90 expect(Object.toQueryString(myObject2)).to.equal('apple[0]=red&apple[1]=yellow&lemon[0]=green&lemon[1]=yellow');
92 var myObject3 = {fruits: {apple: ['red', 'yellow'], lemon: ['green', 'yellow']}};
93 expect(Object.toQueryString(myObject3)).to.equal('fruits[apple][0]=red&fruits[apple][1]=yellow&fruits[lemon][0]=green&fruits[lemon][1]=yellow');
98 describe('Object.getLength', function(){
100 it('should get the amount of items in an object', function(){
103 s: "It's-me-Valerio!",
109 expect(Object.getLength(object)).to.equal(5);
113 expect(Object.getLength(object)).to.equal(5);
119 describe('Object hasOwnProperty', function(){
121 var dit = (typeof window === 'undefined' ? xit : it);
122 dit('should not fail on window', function(){
124 var fn = function(){};
125 Object.each(window, fn);
127 Object.values(window);
128 Object.map(window, fn);
129 Object.every(window, fn);
130 Object.some(window, fn);
131 Object.keyOf(window, document);
132 }).to.not.throwError();