forked from andrewplummer/Sugar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.js
214 lines (182 loc) · 6.74 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
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
'use strict';
/***
* @package Core
* @description Core method extension and restoration.
***/
// The global to export.
var Sugar = {};
// An optimization for GCC.
var object = Object;
// The global context
var globalContext = typeof global !== 'undefined' ? global : window;
// Is the environment node?
var hasExports = typeof module !== 'undefined' && module.exports;
// No conflict mode
var noConflict = hasExports && typeof process !== 'undefined' ? process.env['SUGAR_NO_CONFLICT'] : false;
// 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(',');
// Proxy objects by class.
var proxies = {};
function initializeGlobal() {
Sugar = {
/***
* @method Sugar.extend(<target>, <methods>, [instance] = true)
* @short This method exposes Sugar's core ability to extend Javascript natives, and is useful for creating custom plugins.
* @extra <target> should be the Javascript native function such as %String%, %Number%, etc. <methods> is an object containing the methods to extend. When [instance] is true the methods will be mapped to <target>'s prototype, if false they will be mapped onto <target> itself. For more see %global%.
***/
'extend': extend,
/***
* @method Sugar.restore(<target>, ...)
* @short Restores Sugar methods that may have been overwritten by other scripts.
* @extra <target> should be the Javascript native function such as %String%, %Number%, etc. Arguments after this may be an enumerated list of method names to restore or can be omitted to restore all.
***/
'restore': restore,
/***
* @method Sugar.revert(<target>, ...)
* @short Reverts Sugar methods to what the were before they were added.
* @extra This method can be useful if Sugar methods are causing conflicts with other scripts. <target> should be the Javascript native function such as %String%, %Number%, etc. Arguments after this may be an enumerated list of method names to revert or can be omitted to revert all.
* @short Reverts stuff.
***/
'revert': revert,
'noConflict': noConflict
};
if (hasExports) {
module.exports = Sugar;
} else {
globalContext.Sugar = Sugar;
}
}
function initializeNatives() {
iterateOverObject(natives.concat('Object'), function(i, name) {
proxies[globalContext[name]] = name;
Sugar[name] = {};
});
}
// Class extending methods
function extend(klass, methods, instance, polyfill, override) {
var extendee;
instance = instance !== false;
extendee = instance ? klass.prototype : klass;
iterateOverObject(methods, function(name, prop) {
var existing = checkGlobal('fn', klass, name, extendee),
original = checkGlobal('original', klass, name, extendee),
existed = name in extendee;
if (typeof polyfill === 'function' && existing) {
prop = wrapExisting(existing, prop, polyfill);
}
defineMethodOnNamespace(klass, name, instance, original, prop, existed, polyfill);
if (canDefineOnNative(klass, polyfill, existing, override)) {
setProperty(extendee, name, prop);
}
});
}
function alias(klass, target, source) {
var method = getProxy(klass)[source];
var obj = {};
obj[target] = method.fn;
extend(klass, obj, method.instance);
}
function restore(klass, methods) {
if (noConflict) return;
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) {
if (m.existed) {
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(getProxy(klass), function(name, m) {
if (all || methods.indexOf(name) !== -1) {
changed = true;
fn(m.instance ? klass.prototype : klass, name, m);
}
});
return changed;
}
function checkGlobal(type, klass, name, extendee) {
var proxy = getProxy(klass), methodExists;
methodExists = proxy && hasOwnProperty(proxy, name) && !proxy[name].polyfill;
if (methodExists) {
return proxy[name][type];
} else {
return extendee[name];
}
}
function canDefineOnNative(klass, polyfill, existing, override) {
if (override) {
return true;
} else if (polyfill === true) {
return !existing;
}
return !noConflict || !proxies[klass];
}
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 defineMethodOnNamespace(klass, name, instance, original, prop, existed, polyfill) {
var proxy = getProxy(klass), result;
if (!proxy) return;
result = instance ? wrapInstanceMethod(prop) : prop;
setProperty(proxy, name, result, true);
if (typeof prop === 'function') {
setProperty(result, 'fn', prop);
setProperty(result, 'original', original);
setProperty(result, 'existed', existed);
setProperty(result, 'instance', instance);
setProperty(result, 'polyfill', polyfill);
}
}
function getProxy(klass) {
return Sugar[proxies[klass]];
}
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);
}
initializeGlobal();
initializeNatives();