This repository has been archived by the owner on Jul 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
redefine.js
484 lines (443 loc) · 13.7 KB
/
redefine.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
var _ = this._ = function(_, Function, Object) {
/*! (C) WebReflection *//** @preserve https://github.com/WebReflection/redefine */
var
// scoped shortcuts/constants
BOUND = 'bound',
CONFIGURABLE = 'configurable',
CONSTRUCTOR = 'constructor',
ENUMERABLE = 'enumerable',
EXTEND = 'extend',
GET = 'get',
MIXIN = 'mixin',
PRIVATE = '__@',
PROTO = '__proto__',
PROTOTYPE = 'prototype',
SET = 'set',
STATICS = 'statics',
SUPER = 'super',
VALUE = 'value',
WRITABLE = 'writable',
createFunction = Function,
bind = Function.bind || function bind(self) {
var cb = this;
return function() {
return cb.apply(self, arguments);
};
},
getTheRightOne = function (property, dflt) {
return _[property] || Object[property] || dflt;
},
// from Object || Object.prototype if _ has no polyfills;
defineProperty = getTheRightOne('defineProperty'),
hasOwnProperty = getTheRightOne('hasOwnProperty'),
getOwnPropertyDescriptor = getTheRightOne('getOwnPropertyDescriptor'),
getOwnPropertyNames = getTheRightOne('getOwnPropertyNames', Object.keys),
getPrototypeOf = getTheRightOne('getPrototypeOf',
function getPrototypeOf(object) {
return object[PROTO];
}),
// ES7 proposal (cannot use lo-dash for this)
mixin = Object.mixin || function mixin(
target, source
) {
for(var
keys = getOwnPropertyNames(source),
i = keys.length; i--; defineMagic(
target,
keys[i],
nullObject,
getOwnPropertyDescriptor(
source,
keys[i]
)
)
);
return target;
},
// from _ enriched through other libraries or just Object.create
create = _.create || _.inherit || Object.create,
// ES5 descriptor properties
commonProperties = [
CONFIGURABLE,
ENUMERABLE,
GET,
SET,
VALUE,
WRITABLE
],
// delete all ES5 properties
clear = createFunction('o', 'delete o.' +
commonProperties.join(';delete o.')
),
// recycled object for a happier GC
boundDescriptor = create(null),
superDescriptor = create(null),
nullObject = create(null),
staticsDescriptor = {},
valueObject = {},
hasDescriptorBug = false,
// defined later on
i, assign, remove,
O_o
;
staticsDescriptor[WRITABLE] = true;
staticsDescriptor[ENUMERABLE] = true;
for(i = 0; i < commonProperties.length; i++) {
commonProperties[i] = ['if(h.call(a,"', '"))b.', '=a.',';'].join(commonProperties[i]);
}
// assign only object own properties
assign = createFunction('h','return function(a,b){' + commonProperties.join('') + '}')(hasOwnProperty);
function clone(o, p) {
for (var
descriptor = {},
keys = getOwnPropertyNames(o),
i = 0,
length = keys.length,
key;
i < length; i++
) {
key = keys[i];
descriptor[key] = getOwnPropertyDescriptor(o, key);
}
return create(
p === undefined ? getPrototypeOf(o) : p,
descriptor
);
}
function defineMagic(object, key, defaults, descriptor) {
assign(defaults || redefine.defaults || {}, nullObject);
assign(descriptor, nullObject);
if (
hasOwnProperty.call(descriptor, GET) ||
hasOwnProperty.call(descriptor, SET)
) {
delete nullObject[WRITABLE];
delete nullObject[VALUE];
}
defineProperty(object, key, nullObject);
clear(nullObject);
}
// define a single property with a specific value
function define(object, key, value, defaults) {
defineMagic(
object,
key,
defaults,
value instanceof As ?
value : (
value instanceof Later ?
lazy(object, key, value) : (
valueObject[VALUE] = value, valueObject
)
)
);
delete valueObject[VALUE];
}
function defineAll(object, values, defaults) {
// this is actually cheaper than an
// Object.keys(values).forEach(callback)
// it is also faster so ...
for (var key in values) {
if (hasOwnProperty.call(values, key)) {
define(object, key, values[key], defaults);
}
}
/* one day this should probably be ...
for(var
keys = getOwnPropertyNames(values),
i = keys.length; i--; define(
object, keys[i], values[keys[i]], defaults
)
);
// */
}
function mixins(target, source) {
for (var i = 0, current, tmp; i < source.length; i++) {
current = source[i];
if (isFunction(current)) {
current = (current.type || current.name) === 'mixin' ?
current.call(current) || current :
current[PROTOTYPE];
}
mixin(target, current);
}
}
function lazy(object, key, descriptor) {
// trap these properties at definition time
// and don't bother ever again!
var
callback = descriptor._,
configurable = hasOwnProperty.call(descriptor, CONFIGURABLE) ?
!!descriptor[CONFIGURABLE] : true,
enumerable = hasOwnProperty.call(descriptor, ENUMERABLE) && descriptor[ENUMERABLE],
writable = hasOwnProperty.call(descriptor, WRITABLE) && descriptor[WRITABLE],
self
;
descriptor[GET] = function get() {
if(hasDescriptorBug) {
descriptor = getOwnPropertyDescriptor(object, key);
delete object[key];
}
nullObject[VALUE] = callback.call(self = this);
nullObject[CONFIGURABLE] = configurable;
nullObject[ENUMERABLE] = enumerable;
nullObject[WRITABLE] = writable;
defineProperty(self, key, nullObject);
clear(nullObject);
if (hasDescriptorBug) {
assign(descriptor, nullObject);
defineProperty(object, key, nullObject);
clear(nullObject);
}
return self[key];
};
// to know more about this
// see hasDescriptorBug = true part at the bottom
if(hasDescriptorBug) {
// unfortunately inevitable for Android 2.2 and 2.3 devices
descriptor[CONFIGURABLE] = true;
}
return descriptor;
}
// internal/private class
// checked for descriptors
function As(descriptor) {
assign(descriptor, this);
}
// public As factory
function as(descriptor) {
return new As(descriptor);
}
// common simplified internal utility
function createFromGeneric(object) {
return create(isFunction(object) ?
object[PROTOTYPE] : object
);
}
// creates an instanceof the first argument
// then applies properties, if specified
// using defaults too
function from(object, properties, defaults) {
var instance = createFromGeneric(object);
return properties ?
redefine(instance, properties, defaults) :
instance
;
}
// common simplified internal utility
function isFunction(object) {
return typeof object === 'function';
}
// internal/private class
// checked for lazy property assignment
function Later(callback) {
this._ = isFunction(callback) ?
callback :
assign(callback, this) || callback[VALUE]
;
}
// public Later factory
function later(callback) {
return new Later(callback);
}
// the actual exported library out there
function redefine(
object, // an object where property/ies should be re/defined
key, // the property name or the object with all of them
value, // the property value or defaults, if key i san object
defaults // the optional defaults or undefined, if specified in value
) {
// exactly same things:
// obj <== redefine(obj, key, value[, defaults]);
// obj <== redefine(obj, objProps[, defaults]);
return (typeof key == 'string' ?
define(object, key, value, defaults) :
defineAll(object, key, value)
) || object;
}
// create a partial implementation
// with defaults pre assigned
function using(defaults) {
return function redefine(object, key, value) {
return (typeof key == 'string' ?
define(object, key, value, defaults) :
defineAll(object, key, defaults)
) || object;
};
};
// magic, freaking cool, only, real
// Java like, this.super(); YEAH!
// works only in a non strict environment though
// but you don't have to use it
function getSuperMethod(caller, proto) {
var i, key, keys, parent;
while ((proto = getPrototypeOf(proto))) {
keys = getOwnPropertyNames(proto);
i = keys.length;
while (i--) {
if (proto[key = keys[i]] === caller) {
do {
parent = getPrototypeOf(proto);
proto = parent;
} while (parent[key] === caller);
return parent[key];
}
}
}
}
function Super() {
return getSuperMethod(Super.caller, this).apply(this, arguments);
}
superDescriptor[VALUE] = function superBound(context) {
return bind.apply(
getSuperMethod(superBound.caller, context),
arguments
);
};
superDescriptor[CONFIGURABLE] =
superDescriptor[ENUMERABLE] =
superDescriptor[WRITABLE] = false;
defineProperty(Super, 'bind', superDescriptor);
superDescriptor[VALUE] = Super;
function bound(object, methodName) {
return typeof object === 'string' ?
bound(this, object) :
object[PRIVATE + methodName] || defineBoundMethod(object, methodName);
}
function defineBoundMethod(self, methodName) {
boundDescriptor[VALUE] = bind.call(self[methodName], self);
defineProperty(self, PRIVATE + methodName, boundDescriptor);
boundDescriptor[VALUE] = bound;
return self[PRIVATE + methodName];
}
boundDescriptor[ENUMERABLE] = false;
boundDescriptor[CONFIGURABLE] =
boundDescriptor[WRITABLE] = true;
boundDescriptor[VALUE] = bound;
// Classes with semantics and power you need!
// var Lib = redefine.Class({
//
// extend: SuperLib, // inheritance
//
// mixin: oneOrMoreObject, // mixin
// Constructor
//
// statics: { // statics
// someMethod: function () {},
// someProperty: 0
// },
// // common definition
// method1: function () {},
// property1: null
//
// // constructor
// constructor: function Lib() {
// // implicit initialization
// // never invoked if extended via other classes
// }
// });
function Class(definition, defaults) {
var
constructor = hasOwnProperty.call(definition, CONSTRUCTOR) ?
definition[CONSTRUCTOR] :
function Constructor() {},
statics = hasOwnProperty.call(definition, STATICS) && definition[STATICS],
extend = hasOwnProperty.call(definition, EXTEND) && definition[EXTEND],
key
;
if (!defaults) {
defaults = {};
//defaults[CONFIGURABLE] = true;
defaults[WRITABLE] = true;
}
delete definition[CONSTRUCTOR];
if (extend) {
delete definition[EXTEND];
redefine(
constructor[PROTOTYPE] = createFromGeneric(extend),
'constructor',
constructor
);
if (isFunction(extend)) {
for(key in extend) {
if (hasOwnProperty.call(extend, key) &&
key !== 'name' && key !== 'length'
) {
defineMagic(
constructor,
key,
nullObject,
getOwnPropertyDescriptor(extend, key)
);
}
}
}
}
if (statics) {
delete definition[STATICS];
defineAll(constructor, statics, staticsDescriptor);
}
if (hasOwnProperty.call(definition, MIXIN)) {
mixins(constructor[PROTOTYPE], [].concat(definition[MIXIN]));
delete definition[MIXIN];
}
defineAll(constructor[PROTOTYPE], definition, defaults);
withSuper(constructor[PROTOTYPE]);
if (!(BOUND in constructor[PROTOTYPE])) {
defineProperty(
constructor[PROTOTYPE],
BOUND,
boundDescriptor
);
}
return constructor;
}
function withSuper(proto) {
return hasOwnProperty.call(proto, SUPER) ?
object : defineProperty(proto, SUPER, superDescriptor);
}
// utilities
redefine.from = from; // similar to Object.create
// with redefine like second argument
redefine.Class = Class; // class utility to organize code
redefine[SUPER]= withSuper; // magic .super() behavior
redefine.mixin = mixin; // Object.mixin() ES6 like proposal
redefine.bound = bound; // ensure a bound method once
redefine.clone = clone; // a proper ES5 way to clone objects
// (deep/recursive clone not supported)
// sub-utilities
redefine.as = as; // specify exatc descriptor
redefine.later = later; // lazy variable assignment
redefine.using = using; // specify defaults
redefine.defaults = {}; // set explicit generic defaults
// var redefine = require('redefine');
if ('undefined' !== typeof module && module.exports) {
(module.exports = redefine).redefine = redefine;
}
// there you are ...
if (_.mixin) {
// Lo-Dash or Underscore
// _(object).redefine(property, value)
// _(object).redefine(properties)
// _.redefine(object, properties)
_.mixin({redefine: redefine});
} else {
_.redefine = redefine;
}
try {
// Android 2.2 and 2.3 and webOS
// plus Dolphin in older Androids
// have a really weird bug where inherited
// getters cannot be set as value
// in that case is a bit more complicated
// to obtain a later() behavior
// but at least it's consistent ^_^
// Opera Mobile or all other browsers
// won't be affected
O_o = create(redefine({},{_:later(Object)}))._;
} catch(o_O) {
clear(nullObject);
hasDescriptorBug = true;
}
return _;
}(_ || this, Function, Object);