-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
76 lines (69 loc) · 2.1 KB
/
util.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
'use strict';
function isEmpty(obj) {
if (obj === NaN || obj === null || obj === undefined || obj === '') return true;
else if (Array.isArray(obj) && !obj.find((e) => e)) return true;
return obj.constructor === Object && Object.keys(obj).length === 0;
}
function runSequentially(asyncTasks) {
return asyncTasks.reduce((acc, cur) => {
if (typeof (cur) === 'object' &&
cur.function &&
typeof (cur.function) === 'function') {
return acc.then(res => {
if (!res) res = [];
cur.accumulator = res;
return cur.function(cur.params, cur.accumulator).then((res) => {
cur.accumulator.push(res);
return cur.accumulator;
});
});
} else if (typeof (cur) === 'function') {
return acc.then(cur);
}
}, Promise.resolve());
}
function getCircularReplacer(replacer) {
const seen = new WeakSet();
return (key, value) => {
if (typeof value === 'object' && value !== null) {
if (seen.has(value)) {
if (replacer) return replacer;
return '***';
}
seen.add(value);
}
return value;
};
}
function onErr() {
console.trace(toString(arguments));
}
function log() {
let e = new Error();
if (!e.stack)
try {
// IE requires the Error to actually be thrown or else the
// Error's 'stack' property is undefined.
throw e;
} catch (e) {
if (!e.stack) {
//return 0; // IE < 10, likely
}
}
let stack = e.stack.toString().split(/\r\n|\n/);
if ((navigator.userAgent.indexOf("Opera") || navigator.userAgent.indexOf('OPR')) !== -1) {
alert('Opera');
} else if (navigator.userAgent.indexOf("Chrome") !== -1) {
console.log(stack[2] + toString(arguments));
} else if (navigator.userAgent.indexOf("Safari") !== -1) {
alert('Safari');
} else if (navigator.userAgent.indexOf("Firefox") !== -1) {
console.log(stack[1] + toString(arguments));
} else if ((navigator.userAgent.indexOf("MSIE") !== -1) || (!!document.documentMode == true)) {
//IF IE > 10
alert('IE');
} else {
alert('unknown');
}
}
export { isEmpty, runSequentially, log, onErr};