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('keys should return an empty array', function(){
76 expect(Object.keys({})).toEqual([]);
79 it('should return an array containing the keys of the object', function(){
80 expect(Object.keys(object)).toEqual(['a', 'b', 'c']);
85 it('values should return an empty array', function(){
86 expect(Object.values({})).toEqual([]);
89 it('should return an array with the values of the object', function(){
90 expect(Object.values(object)).toEqual(['string', 233, {}]);
93 // Object.toQueryString
95 it('should return a query string', function(){
96 var myObject = {apple: "red", lemon: "yellow"};
97 expect(Object.toQueryString(myObject)).toEqual('apple=red&lemon=yellow');
99 var myObject2 = {apple: ['red', 'yellow'], lemon: ['green', 'yellow']};
100 expect(Object.toQueryString(myObject2)).toEqual('apple[0]=red&apple[1]=yellow&lemon[0]=green&lemon[1]=yellow');
102 var myObject3 = {fruits: {apple: ['red', 'yellow'], lemon: ['green', 'yellow']}};
103 expect(Object.toQueryString(myObject3)).toEqual('fruits[apple][0]=red&fruits[apple][1]=yellow&fruits[lemon][0]=green&fruits[lemon][1]=yellow');
108 describe('Object.getLength', function(){
110 it('should get the amount of items in an object', function(){
113 s: "It's-me-Valerio!",
119 expect(Object.getLength(object)).toEqual(5);
123 expect(Object.getLength(object)).toEqual(5);
129 describe('Object hasOwnProperty', function(){
131 it('should not fail on window', function(){
133 var fn = function(){};
134 Object.each(window, fn);
136 Object.values(window);
137 Object.map(window, fn);
138 Object.every(window, fn);
139 Object.some(window, fn);
140 Object.keyOf(window, document);