-
Notifications
You must be signed in to change notification settings - Fork 0
/
constants.js
157 lines (133 loc) · 3.54 KB
/
constants.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
const nil = function () {};
const nl = "\r\n";
const _1h = 3600;
const _1d = 86400;
const _1m = 86400 * 30;
const _1y = 86400 * 365;
var isset = function (value) {
return value !== undefined && value !== null;
};
function isJson(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
const isPrime = num => {
for (let i = 2, s = Math.sqrt(num); i <= s; i++) {
if (num % i === 0)
return false;
}
return num > 1;
};
function isDate(myDate) {
return myDate.constructor === Date;
}
function isArray(myArray) {
return myArray.constructor === Array;
}
var isFloat = function (n) {
return n === +n && n !== (n | 0);
};
// checks is a string is an integer (including negative)
var isInt = function (value) {
return /^-?[0-9]+$/.test(value.toString());
};
// checks is a string numeric or not
function isNumeric(str) {
return /^\s*[+-]?((\d+(\.\d*)?)|(\.\d+))([eE][+-]?\d+)?\s*$|^\s*0[xX][0-9a-fA-F]+\s*$/.test(str.toString());
}
var isAlphabetic = function (e) {
return /^[a-zA-Z]+$/.test(e);
};
/////////////////////////////////////////////////////////////////////////////
var parseBit = function (n) {
if (n === 1 || n === "1" || n === true) {
return 1;
}
return 0;
};
var parseBool = function (n) {
if (n === 1 || n === "1" || n === true) {
return true;
}
return false;
};
var parseNumber = function (str) {
if (str.startsWith('0x') || str.startsWith('0X')) {
return parseInt(str, 16);
} else if (str.includes('.')) {
return parseFloat(str);
} else {
return parseInt(str, 10);
}
};
/////////////////////////////////////////////////////////////////////////////
function extractFileExt(filename) {
if (typeof filename === "undefined") {
return "";
}
if (filename.toString().indexOf(".") === -1) {
return "";
}
return filename.split('.').pop().toLowerCase();
}
function extractFileName(path) {
return path.split('\\').pop().split('/').pop();
}
function extractFilePath(path) {
path = path.replace(/\\/g, '/');
const lastSlashIndex = path.lastIndexOf('/');
return path.substring(0, lastSlashIndex);
}
/////////////////////////////////////////////////////////////////////////////
var urlEncode = function (s) {
return encodeURIComponent(s);
};
var urlDecode = function (s) {
return decodeURIComponent(s);
};
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
Boolean.random = function () {
return Math.random() >= 0.5;
};
var inspect = function (obj, _it) {
_it = _it || 0;
_it++;
var d = {};
if (_it >= 32) {
return "<...>";
}
if (obj === null) {
return null;
}
if (Array.isArray(obj)) {
return JSON.pretty(obj);
}
var k = Object.keys(obj);
var v = Object.values(obj);
for (var i = 0; i < k.length; i++) {
var type = typeof v[i];
if (type === "object") {
d[k[i]] = inspect(v[i], _it);
continue;
}
switch (type) {
case "function":
d[k[i]] = "<func>";
break;
case "string":
case "number":
case "boolean":
d[k[i]] = v[i];
break;
default:
d[k[i]] = type;
}
}
return d;
};