-
Notifications
You must be signed in to change notification settings - Fork 2
/
jsus.js
165 lines (152 loc) · 5 KB
/
jsus.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
/**
* # JSUS: JavaScript UtilS.
* Copyright(c) 2017 Stefano Balietti <[email protected]>
* MIT Licensed
*
* Collection of general purpose javascript functions. JSUS helps!
*
* See README.md for extra help.
* ---
*/
(function(exports) {
var JSUS = exports.JSUS = {};
// ## JSUS._classes
// Reference to all the extensions
JSUS._classes = {};
// Make sure that the console is available also in old browser, e.g. < IE8.
if ('undefined' === typeof console) console = {};
if ('undefined' === typeof console.log) console.log = function() {};
/**
* ## JSUS.log
*
* Reference to standard out, by default `console.log`
*
* Override to redirect the standard output of all JSUS functions.
*
* @param {string} txt Text to output
*/
JSUS.log = function(txt) { console.log(txt); };
/**
* ## JSUS.extend
*
* Extends JSUS with additional methods and or properties
*
* The first parameter can be an object literal or a function.
* A reference of the original extending object is stored in
* JSUS._classes
*
* If a second parameter is passed, that will be the target of the
* extension.
*
* @param {object} additional Text to output
* @param {object|function} target The object to extend
*
* @return {object|function} target The extended object
*/
JSUS.extend = function(additional, target) {
var name, prop;
if ('object' !== typeof additional &&
'function' !== typeof additional) {
return target;
}
// If we are extending JSUS, store a reference
// of the additional object into the hidden
// JSUS._classes object;
if ('undefined' === typeof target) {
target = target || this;
if ('function' === typeof additional) {
name = additional.toString();
name = name.substr('function '.length);
name = name.substr(0, name.indexOf('('));
}
// Must be object.
else {
name = additional.constructor ||
additional.__proto__.constructor;
}
if (name) {
this._classes[name] = additional;
}
}
for (prop in additional) {
if (additional.hasOwnProperty(prop)) {
if (typeof target[prop] !== 'object') {
target[prop] = additional[prop];
} else {
JSUS.extend(additional[prop], target[prop]);
}
}
}
// Additional is a class (Function)
// TODO: this is true also for {}
if (additional.prototype) {
JSUS.extend(additional.prototype, target.prototype || target);
}
return target;
};
/**
* ## JSUS.require
*
* Returns a copy/reference of one/all the JSUS components
*
* @param {string} component The name of the requested JSUS library.
* If undefined, all JSUS components are returned. Default: undefined.
* @param {boolean} clone Optional. If TRUE, the requested component
* is cloned before being returned. Default: TRUE
*
* @return {function|boolean} The copy of the JSUS component, or
* FALSE if the library does not exist, or cloning is not possible
*/
JSUS.require = function(component, clone) {
var out;
clone = 'undefined' === typeof clone ? true : clone;
if (clone && 'undefined' === typeof JSUS.clone) {
JSUS.log('JSUS.require: JSUS.clone not found, but clone ' +
'requested. Cannot continue.');
return false;
}
if ('undefined' === typeof component) {
out = JSUS._classes;
}
else {
out = JSUS._classes[component]
if ('undefined' === typeof out) {
JSUS.log('JSUS.require: could not find component ' + component);
return false;
}
}
return clone ? JSUS.clone(out) : out;
};
/**
* ## JSUS.isNodeJS
*
* Returns TRUE when executed inside Node.JS environment
*
* @return {boolean} TRUE when executed inside Node.JS environment
*/
JSUS.isNodeJS = function() {
return 'undefined' !== typeof module &&
'undefined' !== typeof module.exports &&
'function' === typeof require;
};
// ## Node.JS includes
if (JSUS.isNodeJS()) {
require('./lib/compatibility');
require('./lib/obj');
require('./lib/array');
require('./lib/time');
require('./lib/eval');
require('./lib/dom');
require('./lib/random');
require('./lib/parse');
require('./lib/queue');
require('./lib/fs');
}
else {
// Exports J in the browser.
exports.J = exports.JSUS;
}
})(
'undefined' !== typeof module && 'undefined' !== typeof module.exports ?
module.exports: window
);