s-object.js
12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* global describe, it, xit, expect, beforeEach, jasmine, window */
var supportsDescriptors = Object.defineProperty && (function () {
try {
var obj = {};
Object.defineProperty(obj, 'x', { enumerable: false, value: obj });
for (var _ in obj) { return false; }
return obj.x === obj;
} catch (e) { /* this is ES3 */
return false;
}
}());
var ifWindowIt = typeof window === 'undefined' ? xit : it;
var extensionsPreventible = typeof Object.preventExtensions === 'function' && (function () {
var obj = {};
Object.preventExtensions(obj);
obj.a = 3;
return obj.a !== 3;
}());
var ifExtensionsPreventibleIt = extensionsPreventible ? it : xit;
var canSeal = typeof Object.seal === 'function' && (function () {
var obj = { a: 3 };
Object.seal(obj);
delete obj.a;
return obj.a === 3;
}());
var ifCanSealIt = canSeal ? it : xit;
var canFreeze = typeof Object.freeze === 'function' && (function () {
var obj = {};
Object.freeze(obj);
obj.a = 3;
return obj.a !== 3;
}());
var ifCanFreezeIt = canFreeze ? it : xit;
describe('Object', function () {
'use strict';
describe('.keys()', function () {
var obj = {
str: 'boz',
obj: { },
arr: [],
bool: true,
num: 42,
'null': null,
undefined: undefined
};
var loopedValues = [];
for (var key in obj) {
loopedValues.push(key);
}
var keys = Object.keys(obj);
it('should have correct length', function () {
expect(keys.length).toBe(7);
});
describe('arguments objects', function () {
it('works with an arguments object', function () {
(function () {
expect(arguments.length).toBe(3);
expect(Object.keys(arguments).length).toBe(arguments.length);
expect(Object.keys(arguments)).toEqual(['0', '1', '2']);
}(1, 2, 3));
});
it('works with a legacy arguments object', function () {
var FakeArguments = function (args) {
args.forEach(function (arg, i) {
this[i] = arg;
}.bind(this));
};
FakeArguments.prototype.length = 3;
FakeArguments.prototype.callee = function () {};
var fakeOldArguments = new FakeArguments(['a', 'b', 'c']);
expect(Object.keys(fakeOldArguments)).toEqual(['0', '1', '2']);
});
});
it('should return an Array', function () {
expect(Array.isArray(keys)).toBe(true);
});
it('should return names which are own properties', function () {
keys.forEach(function (name) {
expect(obj.hasOwnProperty(name)).toBe(true);
});
});
it('should return names which are enumerable', function () {
keys.forEach(function (name) {
expect(loopedValues.indexOf(name)).toNotBe(-1);
});
});
// ES6 Object.keys does not require this throw
xit('should throw error for non object', function () {
var e = {};
expect(function () {
try {
Object.keys(42);
} catch (err) {
throw e;
}
}).toThrow(e);
});
describe('enumerating over non-enumerable properties', function () {
it('has no enumerable keys on a Function', function () {
var Foo = function () {};
expect(Object.keys(Foo.prototype)).toEqual([]);
});
it('has no enumerable keys on a boolean', function () {
expect(Object.keys(Boolean.prototype)).toEqual([]);
});
it('has no enumerable keys on an object', function () {
expect(Object.keys(Object.prototype)).toEqual([]);
});
});
it('works with boxed primitives', function () {
expect(Object.keys(Object('hello'))).toEqual(['0', '1', '2', '3', '4']);
});
it('works with boxed primitives with extra properties', function () {
var x = Object('x');
x.y = 1;
var actual = Object.keys(x);
var expected = ['0', 'y'];
actual.sort();
expected.sort();
expect(actual).toEqual(expected);
});
ifWindowIt('can serialize all objects on the `window`', function () {
var has = Object.prototype.hasOwnProperty;
var windowItemKeys, exception;
var blacklistedKeys = ['window', 'console', 'parent', 'self', 'frame', 'frames', 'frameElement', 'external'];
if (supportsDescriptors) {
Object.defineProperty(window, 'thrower', {
configurable: true,
get: function () { throw new RangeError('thrower!'); }
});
}
for (var k in window) {
windowItemKeys = exception = void 0;
if (blacklistedKeys.indexOf(k) === -1 && has.call(window, k) && window[k] !== null && typeof window[k] === 'object') {
try {
windowItemKeys = Object.keys(window[k]);
} catch (e) {
exception = e;
}
expect(Array.isArray(windowItemKeys)).toBe(true);
expect(exception).toBeUndefined();
}
}
if (supportsDescriptors) {
delete window.thrower;
}
});
});
describe('.isExtensible()', function () {
var obj = { };
it('should return true if object is extensible', function () {
expect(Object.isExtensible(obj)).toBe(true);
});
ifExtensionsPreventibleIt('should return false if object is not extensible', function () {
expect(Object.isExtensible(Object.preventExtensions(obj))).toBe(false);
});
ifCanSealIt('should return false if object is sealed', function () {
expect(Object.isExtensible(Object.seal(obj))).toBe(false);
});
ifCanFreezeIt('should return false if object is frozen', function () {
expect(Object.isExtensible(Object.freeze(obj))).toBe(false);
});
it('should throw error for non object', function () {
try {
// note: in ES6, this is expected to return false.
expect(Object.isExtensible(42)).toBe(false);
} catch (err) {
expect(err).toEqual(jasmine.any(TypeError));
}
});
});
describe('.defineProperty()', function () {
var obj;
beforeEach(function () {
obj = {};
Object.defineProperty(obj, 'name', {
value: 'Testing',
configurable: true,
enumerable: true,
writable: true
});
});
it('should return the initial value', function () {
expect(obj.hasOwnProperty('name')).toBeTruthy();
expect(obj.name).toBe('Testing');
});
it('should be setable', function () {
obj.name = 'Other';
expect(obj.name).toBe('Other');
});
it('should return the parent initial value', function () {
var child = Object.create(obj, {});
expect(child.name).toBe('Testing');
expect(child.hasOwnProperty('name')).toBeFalsy();
});
it('should not override the parent value', function () {
var child = Object.create(obj, {});
Object.defineProperty(child, 'name', {
value: 'Other'
});
expect(obj.name).toBe('Testing');
expect(child.name).toBe('Other');
});
it('should throw error for non object', function () {
expect(function () {
Object.defineProperty(42, 'name', {});
}).toThrow();
});
it('should not throw error for empty descriptor', function () {
expect(function () {
Object.defineProperty({}, 'name', {});
}).not.toThrow();
});
});
describe('.getOwnPropertyDescriptor()', function () {
it('should return undefined because the object does not own the property', function () {
var descr = Object.getOwnPropertyDescriptor({}, 'name');
expect(descr).toBeUndefined();
});
it('should return a data descriptor', function () {
var descr = Object.getOwnPropertyDescriptor({ name: 'Testing' }, 'name');
var expected = {
value: 'Testing',
enumerable: true,
writable: true,
configurable: true
};
expect(descr).toEqual(expected);
});
it('should return undefined because the object does not own the property', function () {
var descr = Object.getOwnPropertyDescriptor(Object.create({ name: 'Testing' }, {}), 'name');
expect(descr).toBeUndefined();
});
it('should return a data descriptor', function () {
var expected = {
value: 'Testing',
configurable: true,
enumerable: true,
writable: true
};
var obj = Object.create({}, { name: expected });
var descr = Object.getOwnPropertyDescriptor(obj, 'name');
expect(descr).toEqual(expected);
});
it('should throw error for non object', function () {
try {
// note: in ES6, we expect this to return undefined.
expect(Object.getOwnPropertyDescriptor(42, 'name')).toBeUndefined();
} catch (err) {
expect(err).toEqual(jasmine.any(TypeError));
}
});
});
describe('.getPrototypeOf()', function () {
it('should return the [[Prototype]] of an object', function () {
var Foo = function () {};
var proto = Object.getPrototypeOf(new Foo());
expect(proto).toBe(Foo.prototype);
});
it('should shamone to the `Object.prototype` if `object.constructor` is not a function', function () {
var Foo = function () {};
Foo.prototype.constructor = 1;
var proto = Object.getPrototypeOf(new Foo()),
fnToString = Function.prototype.toString;
if (fnToString.call(Object.getPrototypeOf).indexOf('[native code]') < 0) {
expect(proto).toBe(Object.prototype);
} else {
expect(proto).toBe(Foo.prototype);
}
});
it('should throw error for non object', function () {
try {
expect(Object.getPrototypeOf(1)).toBe(Number.prototype); // ES6 behavior
} catch (err) {
expect(err).toEqual(jasmine.any(TypeError));
}
});
it('should return null on Object.create(null)', function () {
var obj = Object.create(null);
expect(Object.getPrototypeOf(obj) === null).toBe(true);
});
});
describe('.defineProperties()', function () {
it('should define the constructor property', function () {
var target = {};
var newProperties = {
constructor: {
value: 'new constructor'
}
};
Object.defineProperties(target, newProperties);
expect(target.constructor).toBe('new constructor');
});
});
describe('.create()', function () {
it('should create objects with no properties when called as `Object.create(null)`', function () {
var obj = Object.create(null);
expect('hasOwnProperty' in obj).toBe(false);
expect('toString' in obj).toBe(false);
expect('constructor' in obj).toBe(false);
var protoIsEnumerable = false;
for (var k in obj) {
if (k === '__proto__') {
protoIsEnumerable = true;
}
}
expect(protoIsEnumerable).toBe(false);
expect(obj instanceof Object).toBe(false);
});
});
});