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'])).toEqual({a:'string',b:233});
21 it('should ignore undefined keys', function(){
26 var subset = Object.subset(obj, ['a', 'b', 'c', 'd']);
27 expect(subset).toEqual({b: 'string', d: null});
28 // To equal doesn't check for undefined properties
29 expect('a' in subset).toBeFalsy();
30 expect('c' in subset).toBeFalsy();
35 it('should return the key of the value or null if not found', function(){
36 expect(Object.keyOf(object, 'string')).toEqual('a');
37 expect(Object.keyOf(object, 'not found')).toBeNull();
42 it('should return true if the object contains value otherwise false', function(){
43 expect(Object.contains(object, 'string')).toBeTruthy();
44 expect(Object.contains(object, 'not found')).toBeFalsy();
49 it('should map a new object according to the comparator', function(){
50 expect(Object.map(object, Type.isNumber)).toEqual({a:false,b:true,c:false});
55 it('should filter the object according to the comparator', function(){
56 expect(Object.filter(object, Type.isNumber)).toEqual({b:233});
61 it('should return true if every value matches the comparator, otherwise false', function(){
62 expect(Object.every(object, typeOf)).toBeTruthy();
63 expect(Object.every(object, Type.isNumber)).toBeFalsy();
68 it('should return true if some of the values match the comparator, otherwise false', function(){
69 expect(Object.some(object, Type.isNumber)).toBeTruthy();
70 expect(Object.some(object, Type.isArray)).toBeFalsy();
75 it('values should return an empty array', function(){
76 expect(Object.values({})).toEqual([]);
79 it('should return an array with the values of the object', function(){
80 expect(Object.values(object)).toEqual(['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)).toEqual('apple=red&lemon=yellow');
89 var myObject2 = {apple: ['red', 'yellow'], lemon: ['green', 'yellow']};
90 expect(Object.toQueryString(myObject2)).toEqual('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)).toEqual('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)).toEqual(5);
113 expect(Object.getLength(object)).toEqual(5);
119 describe('Object hasOwnProperty', function(){
121 it('should not fail on window', function(){
123 var fn = function(){};
124 Object.each(window, fn);
126 Object.values(window);
127 Object.map(window, fn);
128 Object.every(window, fn);
129 Object.some(window, fn);
130 Object.keyOf(window, document);