-
Notifications
You must be signed in to change notification settings - Fork 304
/
Copy pathcore.js
164 lines (142 loc) · 4.65 KB
/
core.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
'use strict';
/***
* @module Core
* @description Core method extension and restoration.
***/
// The global context
var globalContext = typeof global !== 'undefined' && global.Object ? global : this;
// Internal hasOwnProperty
var internalHasOwnProperty = Object.prototype.hasOwnProperty;
// Property descriptors exist in IE8 but will error when trying to define a property on
// native objects. IE8 does not have defineProperies, however, so this check saves a try/catch block.
var propertyDescriptorSupport = !!(Object.defineProperty && Object.defineProperties);
// Natives by name.
var natives = 'Boolean,Number,String,Array,Date,RegExp,Function'.split(',');
// A hash of all methods by Native class
var SugarMethods = {};
// Class extending methods
function initializeClasses() {
initializeClass(Object);
iterateOverObject(natives, function(i, name) {
initializeClass(globalContext[name]);
});
}
function initializeClass(klass) {
extend(klass, {
'extend': function(methods, instance) {
extend(klass, methods, instance !== false);
},
'sugarRestore': function(methods) {
restore(klass, methods);
},
'sugarRevert': function(methods) {
revert(klass, methods);
}
}, false);
}
function extend(klass, methods, instance, polyfill) {
var extendee;
instance = instance !== false;
extendee = instance ? klass.prototype : klass;
iterateOverObject(methods, function(name, prop) {
var existing = extendee[name],
original = checkOriginalMethod(klass, name);
if (typeof polyfill === 'function' && existing) {
prop = wrapExisting(existing, prop, polyfill);
}
storeMethod(klass, name, instance, existing, prop, polyfill);
if (polyfill !== true || !existing) {
setProperty(extendee, name, prop);
}
});
}
function alias(klass, target, source) {
var method = SugarMethods[klass][source];
var obj = {};
obj[target] = method.fn;
extend(klass, obj, method.instance);
}
function restore(klass, methods) {
return batchMethodExecute(klass, methods, function(target, name, m) {
setProperty(target, name, m.fn);
});
}
function revert(klass, methods) {
return batchMethodExecute(klass, methods, function(target, name, m) {
var original = checkOriginalMethod(klass, name);
if (m.original) {
setProperty(target, name, m.original);
} else {
delete target[name];
}
});
}
function batchMethodExecute(klass, methods, fn) {
var all = !methods, changed = false;
if (typeof methods === 'string') methods = [methods];
iterateOverObject(SugarMethods[klass], function(name, m) {
if (all || methods.indexOf(name) !== -1) {
changed = true;
fn(m.instance ? klass.prototype : klass, name, m);
}
});
return changed;
}
function checkOriginalMethod(klass, name) {
var methods = SugarMethods[klass];
var method = methods && methods[name];
return method && method.original;
}
function wrapExisting(originalFn, extendedFn, condition) {
return function(a) {
return condition.apply(this, arguments) ?
extendedFn.apply(this, arguments) :
originalFn.apply(this, arguments);
}
}
function wrapInstanceMethod(fn) {
return function(obj) {
var args = arguments, newArgs = [], i;
for(i = 1;i < args.length;i++) {
newArgs.push(args[i]);
}
return fn.apply(obj, newArgs);
};
}
function storeMethod(klass, name, instance, existing, prop, polyfill) {
var result = instance ? wrapInstanceMethod(prop) : prop;
var methods = SugarMethods[klass];
if (!methods) {
methods = SugarMethods[klass] = {};
}
setProperty(methods, name, result, true);
if (typeof prop === 'function') {
setProperty(result, 'fn', prop);
setProperty(result, 'original', existing);
setProperty(result, 'instance', instance);
setProperty(result, 'polyfill', polyfill);
}
}
function setProperty(target, name, property, enumerable) {
if (propertyDescriptorSupport) {
Object.defineProperty(target, name, {
value: property,
enumerable: !!enumerable,
configurable: true,
writable: true
});
} else {
target[name] = property;
}
}
function iterateOverObject(obj, fn) {
var key;
for(key in obj) {
if (!hasOwnProperty(obj, key)) continue;
if (fn.call(obj, key, obj[key], obj) === false) break;
}
}
function hasOwnProperty(obj, prop) {
return !!obj && internalHasOwnProperty.call(obj, prop);
}
initializeClasses();