1
|
|
|
var isNode = false; |
2
|
|
|
var root: any; |
3
|
|
|
declare var global: any; |
4
|
|
|
|
5
|
|
|
(function () { |
6
|
|
|
if (typeof global == "undefined" || (global && !global)) { |
7
|
|
|
global = this; |
8
|
|
|
} |
9
|
|
|
// Establish the root object, `window` in the browser, or `global` on the server. |
10
|
|
|
root = this; |
11
|
|
|
|
12
|
|
|
// Export the Underscore object for **CommonJS**, with backwards-compatibility |
13
|
|
|
// for the old `require()` API. If we're not in CommonJS, add `_` to the |
14
|
|
|
// global object. |
15
|
|
|
if (typeof module !== "undefined" && module.exports) { |
16
|
|
|
isNode = true; |
17
|
|
|
} |
18
|
|
|
})(); |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Is Node ? |
22
|
|
|
*/ |
23
|
|
|
function isnode() { |
24
|
|
|
return isNode; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
if (isnode()) { |
28
|
|
|
module.exports.isnode = isnode; |
29
|
|
|
} else { |
30
|
|
|
global.isnode = isnode; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* call_user_func |
35
|
|
|
* @param functionName function name |
36
|
|
|
*/ |
37
|
|
|
function ___call(functionName: string, context?: Window, args?: any) { |
38
|
|
|
var args = Array.prototype.slice.call(arguments, 2); |
39
|
|
|
var namespaces = functionName.split("."); |
40
|
|
|
var func = namespaces.pop(); |
41
|
|
|
if (typeof window[func] != "undefined") { |
42
|
|
|
window[func](args); |
43
|
|
|
} else if (context && context instanceof Window) { |
44
|
|
|
if (typeof context[func] != "undefined") { |
45
|
|
|
context[func](args); |
46
|
|
|
} |
47
|
|
|
} else { |
48
|
|
|
try { |
49
|
|
|
eval(functionName); |
50
|
|
|
} catch (error) { |
51
|
|
|
console.error(error); |
52
|
|
|
console.error(functionName + " is not function"); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (isnode()) { |
58
|
|
|
module.exports.___call = ___call; |
59
|
|
|
} else { |
60
|
|
|
global.___call = ___call; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* call_user_func |
65
|
|
|
* @param functionName |
66
|
|
|
* @param context |
67
|
|
|
* @param args |
68
|
|
|
*/ |
69
|
|
|
function call_user_func( |
70
|
|
|
functionName: string, |
71
|
|
|
context: Window & typeof globalThis, |
72
|
|
|
args: any |
73
|
|
|
) { |
74
|
|
|
var args = Array.prototype.slice.call(arguments, 2); |
75
|
|
|
var namespaces = functionName.split("."); |
76
|
|
|
var func = namespaces.pop(); |
77
|
|
|
for (var i = 0; i < namespaces.length; i++) { |
78
|
|
|
context = context[namespaces[i]]; |
79
|
|
|
} |
80
|
|
|
return context[func].apply(context, args); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (isnode()) { |
84
|
|
|
module.exports.call_user_func = call_user_func; |
85
|
|
|
} else { |
86
|
|
|
global.call_user_func = call_user_func; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Make function async |
91
|
|
|
* @param callback |
92
|
|
|
*/ |
93
|
|
|
function async_this(callback: Function): Promise<any> { |
94
|
|
|
return new Promise(function (resolve, reject) { |
95
|
|
|
if (typeof callback == "function") { |
96
|
|
|
callback(); |
97
|
|
|
resolve(true); |
98
|
|
|
} else { |
99
|
|
|
reject(new Error("callback is not function")); |
100
|
|
|
} |
101
|
|
|
}); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (isnode()) { |
105
|
|
|
module.exports.async_this = async_this; |
106
|
|
|
} else { |
107
|
|
|
global.async_this = async_this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* call_user_func |
112
|
|
|
* @param func function name |
113
|
|
|
*/ |
114
|
|
|
function __call(func: string) { |
115
|
|
|
this[func].apply(this, Array.prototype.slice.call(arguments, 1)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if (isnode()) { |
119
|
|
|
module.exports.__call = __call; |
120
|
|
|
} else { |
121
|
|
|
global.__call = __call; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* check empty |
126
|
|
|
* @param str |
127
|
|
|
*/ |
128
|
|
|
function empty(str: string | null | undefined | number | boolean) { |
129
|
|
|
var type = typeof str; |
130
|
|
|
if (type == "string" || type == "number") { |
131
|
|
|
str = str.toString().trim(); |
132
|
|
|
} |
133
|
|
|
switch (str) { |
134
|
|
|
case "": |
135
|
|
|
case null: |
136
|
|
|
case false: |
137
|
|
|
case type == "undefined": //typeof (str) == "undefined" |
138
|
|
|
return true; |
139
|
|
|
default: |
140
|
|
|
return false; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if (isnode()) { |
145
|
|
|
module.exports.empty = empty; |
146
|
|
|
} else { |
147
|
|
|
global.empty = empty; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get current function name |
152
|
|
|
*/ |
153
|
|
|
function getFuncName() { |
154
|
|
|
return getFuncName.caller.name; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if (isnode()) { |
158
|
|
|
module.exports.getFuncName = getFuncName; |
159
|
|
|
} else { |
160
|
|
|
global.getFuncName = getFuncName; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Is Development Mode |
165
|
|
|
*/ |
166
|
|
|
function is_development() { |
167
|
|
|
return ( |
168
|
|
|
document.getElementsByTagName("html")[0].getAttribute("environtment") == |
169
|
|
|
"development" |
170
|
|
|
); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if (isnode()) { |
174
|
|
|
module.exports.is_development = is_development; |
175
|
|
|
} else { |
176
|
|
|
global.is_development = is_development; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Generate random string with length |
181
|
|
|
* @param length length to generate |
182
|
|
|
* @see https://dev.to/oyetoket/fastest-way-to-generate-random-strings-in-javascript-2k5a |
183
|
|
|
*/ |
184
|
|
|
const generateRandomString = function (length = 6) { |
185
|
|
|
return Math.random().toString(20).substr(2, length); |
186
|
|
|
}; |
187
|
|
|
|
188
|
|
|
if (isnode()) { |
189
|
|
|
module.exports.generateRandomString = generateRandomString; |
190
|
|
|
} else { |
191
|
|
|
global.generateRandomString = generateRandomString; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Create uniqueid with prefix or suffix |
196
|
|
|
* @param prefix |
197
|
|
|
* @param suffix |
198
|
|
|
*/ |
199
|
|
|
function uniqid(prefix: any, suffix: any) { |
200
|
|
|
return ( |
201
|
|
|
(prefix ? prefix : "") + |
202
|
|
|
generateRandomString() + |
203
|
|
|
(suffix ? suffix : "") |
204
|
|
|
).toString(); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
if (isnode()) { |
208
|
|
|
module.exports.uniqid = uniqid; |
209
|
|
|
} else { |
210
|
|
|
global.uniqid = uniqid; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
if (typeof now == "undefined") { |
214
|
|
|
function now() { |
215
|
|
|
return Date.now(); |
216
|
|
|
} |
217
|
|
|
if (isnode()) { |
218
|
|
|
module.exports.now = now; |
219
|
|
|
} else { |
220
|
|
|
global.now = now; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Get unique array |
226
|
|
|
* @param {any} value |
227
|
|
|
* @param {any} index |
228
|
|
|
* @param {any[]} self |
229
|
|
|
* @example dataArray.filter(onlyUnique) |
230
|
|
|
*/ |
231
|
|
|
function onlyUnique(value, index, self) { |
232
|
|
|
return self.indexOf(value) === index; |
233
|
|
|
} |
234
|
|
|
|