-
Notifications
You must be signed in to change notification settings - Fork 1
/
objct.js
258 lines (241 loc) · 7.59 KB
/
objct.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
/*!
* objct - v0.x (https://github.com/greenish/js-objct)
*
* Copyright (c) 2015 Philipp Adrian (www.philippadrian.com)
*
* The MIT Licence (http://opensource.org/licenses/MIT)
*/
////////////////////////////////////////////////////////////////////////////////
(function(commonJSmodule, undefined){
"use strict";
////////////////////////////////////////////////////////////////////////////////
var Objct = function(){};
var NewObj = function(){return {};};
var hash = "jmuMMRs6AUUG293HXcs8Z0ofQlkG0hqiNAJlZq2hHYakBQmyfnRuCsh2yf-d7n";
var testExecutable = new RegExp("\\b"+hash+"\\b");
var strFunction = "function";
var strObject = "object";
var strArray = Array.toString();
var objectKeys = Object.keys || Objct;
////////////////////////////////////////////////////////////////////////////////
var checkType = function(value){
var type = typeof value;
return type === strFunction || (type === strObject && !factory.isArray(value));
};
////////////////////////////////////////////////////////////////////////////////
var instantiateFunction = function(fn, args){
Objct.prototype = fn.prototype;
var f = new Objct();
Objct.prototype = null;
var r = fn.apply(f, args);
if(checkType(r)) return r;
return f;
};
////////////////////////////////////////////////////////////////////////////////
var decoratedProperty = function(target, source, k, data) {
target[k] = typeof source[k] === strFunction && source[k].hash === hash?
source[k].call(data.i, {
args:data.a,
modules:data.m,
target:target,
key:k
}):
source[k];
};
////////////////////////////////////////////////////////////////////////////////
var decoratedModule = function(module, data, instance) {
return typeof module === strFunction && module.hash === hash ?
module.call(instance, {
args:data.a,
modules:data.m
}):
module;
}
////////////////////////////////////////////////////////////////////////////////
var mixinObject = function(target, source, data, keys) {
var k = -1, length;
keys = keys || objectKeys(source);
if(typeof keys === strObject) {
length = keys.length;
if(data.d)
while(++k < length)
decoratedProperty(target, source, keys[k], data);
else
while(++k < length)
target[keys[k]]=source[keys[k]];
}
else {
if(data.d)
for(k in source)
decoratedProperty(target, source, k, data);
else
for(k in source)
target[k]=source[k];
}
};
////////////////////////////////////////////////////////////////////////////////
var mixinFunction = function(target, fn, data){
var proto = fn.prototype;
var instance, keys;
mixinObject(target, proto, data);
if(!data.d) {
fn.apply(target, data.a);
return;
}
fn.prototype = target;
instance = instantiateFunction(fn, data.a);
fn.prototype = proto;
keys = typeof Object.getOwnPropertyNames === strFunction ?
Object.getOwnPropertyNames(instance):
undefined;
mixinObject(target, instance, data, keys);
};
////////////////////////////////////////////////////////////////////////////////
var build = function(modules, data){
var isFunction, i=-1;
var instance, obj, length = modules.length;
while(++i<length) {
obj = data.d ?
decoratedModule(modules[i].obj, data, data.i):
modules[i].obj;
// first Module
if(i === 0) {
// very first module
if(data.i === null) {
data.i = obj === NewObj ?
NewObj():
typeof obj === strFunction ?
instantiateFunction(obj, data.a):
obj;
// call first modules decorators
data.d && mixinObject(data.i, data.i, data);
continue;
}
else if(obj === NewObj) continue;
}
//module is factory? -> call it
modules[i].isFactory ?
obj.call({hash:hash}, modules[i], data):
// module is function?
typeof obj === strFunction ?
mixinFunction(data.i, obj, data):
// module is object
mixinObject(data.i, obj, data);
}
return data.i;
};
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
var factory = function(){
Array.prototype.unshift.call(arguments, NewObj);
return factory.extend.call({
hash:hash,
i : this instanceof factory,
d : false,
arguments : arguments
});
};
//////////////////////////////////////////////////////////////////////////////
factory.e = function(){
Array.prototype.unshift.call(arguments, NewObj);
return factory.extend.call({
hash:hash,
i : this instanceof factory.e,
d : true,
arguments : arguments
});
};
//////////////////////////////////////////////////////////////////////////////
factory.e.extend = function(){
return factory.extend.call({
hash:hash,
i : this instanceof factory.e.extend,
d : true,
arguments : arguments
});
};
//////////////////////////////////////////////////////////////////////////////
factory.extend = function(){
////////////////////////////////////////////////////////////////////////
var Executable = function Executable(module, data){
"jmuMMRs6AUUG293HXcs8Z0ofQlkG0hqiNAJlZq2hHYakBQmyfnRuCsh2yf-d7n";
var that = this || {};
//////////////////////////
// Continue building process
//////////////////////////
if(that && typeof that.hash === "string" && that.hash === hash) {
// pass up modules
module.m = thisData.m;
return build(thisData.m, data);
}
//////////////////////////
// Start building process
//////////////////////////
thisData.a = arguments;
return build(thisData.m, thisData);
};
////////////////////////////////////////////////////////////////////////
var that = this || {};
var thisData = {
a : [], // args
m : [], // modules
i : null, // instance
d : that.hash === hash ? that.d : false // decorated
};
var type;
var args = arguments.length > 0 ? arguments : that.arguments;
var instant = this instanceof factory.extend || that.i;
//setup modules
var i=-1;
while(++i < args.length) {
type = typeof args[i];
if(!checkType(args[i])) {
if(type !== strObject) {
throw("objct: Unexpected '"+type+"'! Only 'functions' and 'objects' can be used with objct.");
}
else {
Array.prototype.splice.apply(args, [i,1].concat(args[i]));
i--;
continue;
}
}
thisData.m.push({
obj : args[i],
isFactory : type === strFunction && testExecutable.test(args[i])
});
// if module is a function and not a decorator, copy static properties to Executable
if(!instant && type === strFunction && args[i].hash !== hash)
mixinObject(Executable, args[i], thisData);
}
return instant ? new Executable() : Executable;
};
////////////////////////////////////////////////////////////////////////////////
factory.e.decorator = function(fn){
var type = typeof fn;
if(type !== strFunction)
throw("objct.decorator: Unexpected '"+type+"'! Objct.decorator only takes one function as argument.");
return function(){
var args = Array.prototype.slice.call(arguments);
var f = function(decoratorData){
return fn.apply(this, [decoratorData].concat(args));
};
f.hash=hash;
return f;
};
};
////////////////////////////////////////////////////////////////////////////////
factory.isObjct = function(obj){
return testExecutable.test(obj);
};
////////////////////////////////////////////////////////////////////////////////
// isArray fallback for ie<9
factory.isArray = Array.isArray || function (obj) {
return (typeof obj == strObject
&& Object.prototype.toString.call(obj) === "[object Array]")
|| ("constructor" in obj && String(obj.constructor) === strArray);
};
////////////////////////////////////////////////////////////////////////////////
// Connect to Environment
commonJSmodule.exports = factory;
////////////////////////////////////////////////////////////////////////////////
})(typeof module === "undefined"? {} : module);