-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathobject.js
369 lines (308 loc) · 11.2 KB
/
object.js
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
// ExtendScript Polyfill Object bundle
// https://github.com/ExtendScript/extendscript-modules
/**
* Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
*/
if (typeof Object.assign != 'function') {
Object.prototype.assign = function(target, varArgs) {
'use strict';
if (target == null) { // TypeError if undefined or null
throw new TypeError('Cannot convert undefined or null to object');
}
var to = Object(target);
for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];
if (nextSource != null) { // Skip over if undefined or null
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
};
};
if (!Object.create) {
// Production steps of ECMA-262, Edition 5, 15.2.3.5
// Reference: http://es5.github.io/#x15.2.3.5
Object.create = (function() {
// To save on memory, use a shared constructor
function Temp() {}
// make a safe reference to Object.prototype.hasOwnProperty
var hasOwn = Object.prototype.hasOwnProperty;
return function(O) {
// 1. If Type(O) is not Object or Null throw a TypeError exception.
if (Object(O) !== O && O !== null) {
throw TypeError('Object prototype may only be an Object or null');
}
// 2. Let obj be the result of creating a new object as if by the
// expression new Object() where Object is the standard built-in
// constructor with that name
// 3. Set the [[Prototype]] internal property of obj to O.
Temp.prototype = O;
var obj = new Temp();
Temp.prototype = null; // Let's not keep a stray reference to O...
// 4. If the argument Properties is present and not undefined, add
// own properties to obj as if by calling the standard built-in
// function Object.defineProperties with arguments obj and
// Properties.
if (arguments.length > 1) {
// Object.defineProperties does ToObject on its first argument.
var Properties = Object(arguments[1]);
for (var prop in Properties) {
if (hasOwn.call(Properties, prop)) {
var descriptor = Properties[prop];
if (Object(descriptor) !== descriptor) {
throw TypeError(prop + 'must be an object');
}
if ('get' in descriptor || 'set' in descriptor) {
throw new TypeError('getters & setters can not be defined on this javascript engine');
}
if ('value' in descriptor) {
obj[prop] = Properties[prop];
}
}
}
}
// 5. Return obj
return obj;
};
})();
}
/*
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties#Polyfill
*/
if (!Object.defineProperties) {
Object.defineProperties = function(object, props) {
function hasProperty(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
function convertToDescriptor(desc) {
if (Object(desc) !== desc) {
throw new TypeError('Descriptor can only be an Object.');
}
var d = {};
if (hasProperty(desc, "enumerable")) {
d.enumerable = !!desc.enumerable;
}
if (hasProperty(desc, "configurable")) {
d.configurable = !!desc.configurable;
}
if (hasProperty(desc, "value")) {
d.value = desc.value;
}
if (hasProperty(desc, "writable")) {
d.writable = !!desc.writable;
}
if (hasProperty(desc, "get")) {
throw new TypeError('getters & setters can not be defined on this javascript engine');
}
if (hasProperty(desc, "set")) {
throw new TypeError('getters & setters can not be defined on this javascript engine');
}
return d;
}
if (Object(object) !== object) {
throw new TypeError('Object.defineProperties can only be called on Objects.');
}
if (Object(props) !== props) {
throw new TypeError('Properties can only be an Object.');
}
var properties = Object(props);
for (propName in properties) {
if (hasOwnProperty.call(properties, propName)) {
var descr = convertToDescriptor(properties[propName]);
object[propName] = descr.value;
}
}
return object;
}
}
if (!Object.defineProperty) {
Object.defineProperty = function defineProperty(object, property, descriptor) {
if (Object(object) !== object) {
throw new TypeError('Object.defineProperty can only be called on Objects.');
}
if (Object(descriptor) !== descriptor) {
throw new TypeError('Property description can only be an Object.');
}
if ('get' in descriptor || 'set' in descriptor) {
throw new TypeError('getters & setters can not be defined on this javascript engine');
}
// If it's a data property.
if ('value' in descriptor) {
// fail silently if 'writable', 'enumerable', or 'configurable'
// are requested but not supported
// can't implement these features; allow true but not false
/* if (
('writable' in descriptor && !descriptor.writable) ||
('enumerable' in descriptor && !descriptor.enumerable) ||
('configurable' in descriptor && !descriptor.configurable)
)
{
throw new RangeError('This implementation of Object.defineProperty does not support configurable, enumerable, or writable properties SET to FALSE.');
}*/
object[property] = descriptor.value;
}
return object;
}
}
/*
https://github.com/es-shims/es5-shim/blob/master/es5-sham.js
*/
// ES5 15.2.3.9
// http://es5.github.com/#x15.2.3.9
if (!Object.freeze) {
Object.freeze = function freeze(object) {
if (Object(object) !== object) {
throw new TypeError('Object.freeze can only be called on Objects.');
}
// this is misleading and breaks feature-detection, but
// allows "securable" code to "gracefully" degrade to working
// but insecure code.
return object;
};
}
if (!Object.getOwnPropertyDescriptor) {
Object.getOwnPropertyDescriptor = function getOwnPropertyDescriptor(object, property) {
if (Object(object) !== object) {
throw new TypeError('Object.getOwnPropertyDescriptor can only be called on Objects.');
}
var descriptor;
if (!Object.prototype.hasOwnProperty.call(object, property)) {
return descriptor;
}
descriptor = {
enumerable: Object.prototype.propertyIsEnumerable.call(object, property),
configurable: true
};
descriptor.value = object[property];
var psPropertyType = object.reflect.find(property).type;
descriptor.writable = !(psPropertyType === "readonly");
return descriptor;
}
}
// Source: https://github.com/ExtendScript/extendscript-es5-shim/blob/master/Object/getOwnPropertyNames.js
if (!Object.getOwnPropertyNames) {
Object.getOwnPropertyNames = function(obj) {
if (Object(obj) !== obj) {
throw new TypeError('Object.getOwnPropertyNames can only be called on Objects.');
}
var result = [];
var hasOwnProperty = Object.prototype.hasOwnProperty;
var propertyIsEnumerable = Object.prototype.propertyIsEnumerable;
for (var prop in obj) {
if (hasOwnProperty.call(obj, prop)) {
result.push(prop);
}
}
var properties = obj.reflect.properties;
var methods = obj.reflect.methods;
var all = methods.concat(properties);
for (var i = 0; i < all.length; i++) {
var prop = all[i].name;
if (hasOwnProperty.call(obj, prop) && !(propertyIsEnumerable.call(obj, prop))) {
result.push(prop);
}
};
return result;
};
}
if (!Object.getPrototypeOf) {
Object.getPrototypeOf = function(object) {
if (Object(object) !== object) {
throw new TypeError('Object.getPrototypeOf can only be called on Objects.');
}
return object.__proto__;
}
}
// ES5 15.2.3.13
// http://es5.github.com/#x15.2.3.13
if (!Object.isExtensible) {
Object.isExtensible = function isExtensible(object) {
if (Object(object) !== object) {
throw new TypeError('Object.isExtensible can only be called on Objects.');
}
return true;
};
}
/*
https://github.com/es-shims/es5-shim/blob/master/es5-sham.js
*/
// ES5 15.2.3.12
// http://es5.github.com/#x15.2.3.12
if (!Object.isFrozen) {
Object.isFrozen = function isFrozen(object) {
if (Object(object) !== object) {
throw new TypeError('Object.isFrozen can only be called on Objects.');
}
return false;
};
}
/*
https://github.com/es-shims/es5-shim/blob/master/es5-sham.js
*/
// ES5 15.2.3.11
// http://es5.github.com/#x15.2.3.11
if (!Object.isSealed) {
Object.isSealed = function isSealed(object) {
if (Object(object) !== object) {
throw new TypeError('Object.isSealed can only be called on Objects.');
}
return false;
};
}
if (!Object.keys) {
Object.keys = function(object) {
if (Object(object) !== object) {
throw new TypeError('Object.keys can only be called on Objects.');
}
var hasOwnProperty = Object.prototype.hasOwnProperty;
var result = [];
for (var prop in object) {
if (hasOwnProperty.call(object, prop)) {
result.push(prop);
}
}
return result;
};
}
/*
https://github.com/es-shims/es5-shim/blob/master/es5-sham.js
*/
// ES5 15.2.3.10
// http://es5.github.com/#x15.2.3.10
if (!Object.preventExtensions) {
Object.preventExtensions = function preventExtensions(object) {
if (Object(object) !== object) {
throw new TypeError('Object.preventExtensions can only be called on Objects.');
}
// this is misleading and breaks feature-detection, but
// allows "securable" code to "gracefully" degrade to working
// but insecure code.
return object;
};
}
/*
https://github.com/es-shims/es5-shim/blob/master/es5-sham.js
*/
// ES5 15.2.3.8
// http://es5.github.com/#x15.2.3.8
if (!Object.seal) {
Object.seal = function seal(object) {
if (Object(object) !== object) {
throw new TypeError('Object.seal can only be called on Objects.');
}
// this is misleading and breaks feature-detection, but
// allows "securable" code to "gracefully" degrade to working
// but insecure code.
return object;
};
}
// Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf
Object.setPrototypeOf = Object.setPrototypeOf || function(obj, proto) {
obj.__proto__ = proto;
return obj;
}