-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.js
156 lines (128 loc) · 3.93 KB
/
utils.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
module.exports = {
decorate: decorate,
inherit: inherit
};
var isArray = Array.isArray,
_apply = Function.prototype.apply,
defineProperty = Object.defineProperty,
getOwnDescriptor = Object.getOwnPropertyDescriptor,
getOwnNames = Object.getOwnPropertyNames,
getOwnKeys = Object.keys;
function isObject(o){
return o != null && typeof o === 'object' || typeof o === 'function';
}
function Definable(desc){
for (var k in desc)
this[k] = desc[k];
this.value = null;
}
Definable.prototype.define = function define(o, key, v){
this.value = v;
defineProperty(o, key, this);
this.value = null;
};
var hiddenDesc = { enumerable: false },
normalVal = Definable.normal = new Definable({ enumerable: true, configurable: true, writable: true }),
hiddenVal = Definable.hidden = new Definable({ enumerable: false, configurable: true, writable: true }),
readonlyVal = Definable.readonly = new Definable({ enumerable: true, configurable: true, writable: false });
function decorate(o){
var a, b, c, d,
hidden = arguments[1] === true,
overwrite = arguments[2] !== false;
for (a=0; a < arguments.length; a++) {
if (isArray(b = arguments[a])) {
for (c = 0; c < b.length; c++) {
if (typeof b[c] === 'function' && (d = b[c].name)) {
if (hidden || d === 'toString') {
hiddenVal.define(o, d, b[c]);
} else {
o[d] = b[c];
}
}
}
} else if (isObject(b)) {
for (c in b) {
try {
if (d = getOwnDescriptor(b, c)) {
if (d.get || d.set) {
defineProperty(o, c, d);
} else {
o[c] = d.value;
if (hidden || c === 'toString' || c === 'constructor') {
defineProperty(o, c, hiddenDesc);
}
}
}
} catch (e) {}
}
}
}
return o;
}
function inherit(Ctor, Super, props){
Ctor.prototype = Object.create(Super.prototype);
decorate(Ctor.prototype, true, props);
decorate(Ctor.prototype, true, {
constructor: Ctor
});
return Ctor;
}
[Map, WeakMap].forEach(function(Ctor){
decorate(Ctor, true, [
function createStorage(creator){
creator = creator || Object.create.bind(null, null, {});
var map = new Ctor;
return function storage(o, v){
if (1 in arguments) {
map.set(o, v);
} else {
v = map.get(o);
if (v == null) {
v = creator(o);
map.set(o, v);
}
}
return v;
};
}
]);
});
decorate(Function, true, [
function build(name, body, args, scope){
if (body === undefined) {
body = name;
name = 'anonymous';
}
body = Array.isArray(body) ? body.join('\n') : body || '';
if (!Array.isArray(args)) {
if (isObject(args) && !scope) {
scope = args;
args = [];
} else {
args = [].concat(args);
}
}
var scopeNames = Object.keys(Object(scope)),
scopeVals = scopeNames.map(function(key){ return scope[key] });
scopeNames.push('return function '+name+'('+args.join(', ')+'){\n'+body+'\n}');
return Function.apply(null, scopeNames).apply(null, scopeVals);
},
function create(name, call, construct, proto, descriptors){
construct = typeof construct === 'function' ? construct : function construct(){
var instance = Object.create((isObject(func.prototype) ? func : Object).prototype),
result = _apply.call(call, instance, arguments);
return isObject(result) ? result : instance;
};
var func = Function.build(name, 'return (this instanceof '+name+' ? construct : call).apply(this, arguments)', {
call: call,
construct: construct
});
if (typeof proto === 'object' || typeof proto === 'function') {
func.__proto__ = proto;
}
if (isObject(descriptors)) {
Object.defineProperties(func, descriptors);
}
return func;
}
]);