qsu 0.5.5 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +2 -0
- package/.eslintrc.cjs +44 -0
- package/LICENSE +21 -21
- package/README.md +522 -159
- package/dist/index.d.ts +57 -0
- package/dist/index.js +349 -0
- package/package.json +55 -38
- package/tsconfig.json +23 -0
- package/.babelrc +0 -7
- package/.eslintrc.js +0 -25
- package/.idea/git_toolbox_prj.xml +0 -20
- package/.idea/inspectionProfiles/Project_Default.xml +0 -6
- package/.idea/jsLibraryMappings.xml +0 -6
- package/.idea/jsLinters/eslint.xml +0 -6
- package/.idea/modules.xml +0 -8
- package/.idea/qsu.iml +0 -12
- package/.idea/vcs.xml +0 -6
- package/array.js +0 -57
- package/date.js +0 -36
- package/format.js +0 -80
- package/index.js +0 -17
- package/math.js +0 -32
- package/misc.js +0 -7
- package/string.js +0 -145
- package/verify.js +0 -88
package/array.js
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
const { is2dArray } = require('./verify');
|
|
2
|
-
|
|
3
|
-
const shuffle = (arr) => {
|
|
4
|
-
if (!arr || typeof arr !== 'object' || arr.length < 1) return null;
|
|
5
|
-
if (arr.length === 1) return arr[0];
|
|
6
|
-
const newArr = arr;
|
|
7
|
-
for (let i = arr.length - 1; i > 0; i -= 1) {
|
|
8
|
-
const j = Math.floor(Math.random() * (i + 1));
|
|
9
|
-
[newArr[i], newArr[j]] = [arr[j], arr[i]];
|
|
10
|
-
}
|
|
11
|
-
return newArr;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
const setWithDefault = (value = null, length = 1) => {
|
|
15
|
-
if (length < 1) return [];
|
|
16
|
-
return Array(length).fill(value);
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const unique = (arr) => {
|
|
20
|
-
if (!arr || typeof arr !== 'object' || arr.length < 1) return null;
|
|
21
|
-
if (is2dArray(arr)) {
|
|
22
|
-
return arr.map(JSON.stringify)
|
|
23
|
-
.reverse()
|
|
24
|
-
.filter((e, i, a) => a.indexOf(e, i + 1) === -1)
|
|
25
|
-
.reverse()
|
|
26
|
-
.map(JSON.parse);
|
|
27
|
-
}
|
|
28
|
-
return [...new Set(arr)];
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
const setWithNumber = (start, end) => {
|
|
32
|
-
if (start === null || end === null || start > end) return null;
|
|
33
|
-
return Array.from({ length: (end - start) + 1 }, (_, i) => i + start);
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
const average = (arr) => {
|
|
37
|
-
if (!arr) return null;
|
|
38
|
-
return arr.reduce((p, c) => p + c, 0) / arr.length;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
const move = (arr, from, to) => {
|
|
42
|
-
const arrayLength = arr.length;
|
|
43
|
-
if (!arr || from === null || to === null || arrayLength <= from || arrayLength <= to) {
|
|
44
|
-
throw new Error('Invalid move params');
|
|
45
|
-
}
|
|
46
|
-
arr.splice(to, 0, arr.splice(from, 1)[0]);
|
|
47
|
-
return arr;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
module.exports = {
|
|
51
|
-
shuffle,
|
|
52
|
-
setWithDefault,
|
|
53
|
-
unique,
|
|
54
|
-
setWithNumber,
|
|
55
|
-
average,
|
|
56
|
-
move,
|
|
57
|
-
};
|
package/date.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
const moment = require('moment');
|
|
2
|
-
|
|
3
|
-
const dayDiff = (d1, d2) => {
|
|
4
|
-
const d2a = d2 || new Date();
|
|
5
|
-
let d1ff1 = d1 instanceof Date ? d1 : new Date(d1);
|
|
6
|
-
let d1ff2 = d2a instanceof Date ? d2a : new Date(d2a);
|
|
7
|
-
d1ff1 = new Date(d1ff1.getFullYear(), d1ff1.getMonth() + 1, d1ff1.getDate());
|
|
8
|
-
d1ff2 = new Date(d1ff2.getFullYear(), d1ff2.getMonth() + 1, d1ff2.getDate());
|
|
9
|
-
return Math.ceil(Math.abs(d1ff2.getTime() - d1ff1.getTime()) / (1000 * 3600 * 24));
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
const today = (dateType) => {
|
|
13
|
-
if (dateType !== undefined && typeof dateType !== 'string') {
|
|
14
|
-
return null;
|
|
15
|
-
}
|
|
16
|
-
return moment().format(dateType || 'YYYY-MM-DD');
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const isRealDate = (d) => {
|
|
20
|
-
if (!d || typeof d !== 'string') return false;
|
|
21
|
-
const date = new Date(d);
|
|
22
|
-
if (!date.getTime() || !d.match(/^\d{4}-\d{2}-\d{2}$/)) return false;
|
|
23
|
-
return date.toISOString().slice(0, 10) === d;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
const convertDate = (date, format) => {
|
|
27
|
-
if (!date || typeof date !== 'string') return null;
|
|
28
|
-
return moment(date).format(format || 'YYYY-MM-DD');
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
module.exports = {
|
|
32
|
-
dayDiff,
|
|
33
|
-
today,
|
|
34
|
-
isRealDate,
|
|
35
|
-
convertDate,
|
|
36
|
-
};
|
package/format.js
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
const path = require('path');
|
|
2
|
-
|
|
3
|
-
const number = (v) => new Intl.NumberFormat().format(v);
|
|
4
|
-
|
|
5
|
-
const fileName = (filePath, withExtension) => {
|
|
6
|
-
if (!filePath || typeof filePath !== 'string' || filePath.length < 1) {
|
|
7
|
-
throw new Error('Unknown file path');
|
|
8
|
-
}
|
|
9
|
-
if (withExtension) {
|
|
10
|
-
return path.basename(filePath);
|
|
11
|
-
}
|
|
12
|
-
return path.basename(filePath, path.extname(filePath));
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
const fileSize = (bytes, decimals = 2) => {
|
|
16
|
-
if (bytes === null || typeof bytes !== 'number' || typeof decimals !== 'number') return 'Unknown';
|
|
17
|
-
if (bytes === 0 || bytes < 0) return '0 Bytes';
|
|
18
|
-
const byteCalc = Math.floor(Math.log(bytes) / Math.log(1024));
|
|
19
|
-
return `${parseFloat((bytes / 1024 ** byteCalc).toFixed((decimals < 0 ? 0 : decimals)))} ${['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'][byteCalc]}`;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
const fileExt = (filePath) => {
|
|
23
|
-
if (!filePath || typeof filePath !== 'string' || filePath.indexOf('.') === -1) return 'Unknown';
|
|
24
|
-
const p = filePath.trim().toLowerCase();
|
|
25
|
-
const pSpl = p.split('.');
|
|
26
|
-
return pSpl.length > 0 ? pSpl[pSpl.length - 1] : 'Unknown';
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
const msToTime = (milliseconds = 0, withMilliseconds = false, separator = ':') => {
|
|
30
|
-
if (!milliseconds || typeof milliseconds !== 'number' || typeof separator !== 'string') return 'Unknown';
|
|
31
|
-
const ms = Math.floor((milliseconds % 1000) / 100);
|
|
32
|
-
let sec = Math.floor((milliseconds / 1000) % 60);
|
|
33
|
-
let min = Math.floor((milliseconds / (1000 * 60)) % 60);
|
|
34
|
-
let hour = Math.floor(milliseconds / (1000 * 60 * 60));
|
|
35
|
-
hour = (hour < 10) ? `0${hour}` : hour;
|
|
36
|
-
min = (min < 10) ? `0${min}` : min;
|
|
37
|
-
sec = (sec < 10) ? `0${sec}` : sec;
|
|
38
|
-
return `${hour}${separator}${min}${separator}${sec}${withMilliseconds ? `.${ms}` : ''}`;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
const secToTime = (seconds = 0, separator = ':', onlyHour = false) => {
|
|
42
|
-
if (!seconds || typeof seconds !== 'number' || typeof separator !== 'string') return 'Unknown';
|
|
43
|
-
let sec = Math.floor(seconds % 60);
|
|
44
|
-
let min = Math.floor((seconds / 60) % 60);
|
|
45
|
-
let hour = Math.floor(seconds / (60 * 60));
|
|
46
|
-
hour = (hour < 10) ? `0${hour}` : hour;
|
|
47
|
-
min = (min < 10) ? `0${min}` : min;
|
|
48
|
-
sec = (sec < 10) ? `0${sec}` : sec;
|
|
49
|
-
return onlyHour ? hour : `${hour}${separator}${min}${separator}${sec}`;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
const license = (args = {}) => {
|
|
53
|
-
if (typeof args !== 'object' || args.length < 1) {
|
|
54
|
-
throw new Error('An argument of type Object is required. Please refer to README.md.');
|
|
55
|
-
}
|
|
56
|
-
if (!args.author || !args.yearStart) {
|
|
57
|
-
throw new Error('holder and startYear are required.');
|
|
58
|
-
}
|
|
59
|
-
const br = args.htmlBr ? '<br/>' : '\n';
|
|
60
|
-
const type = args?.type?.replace(/\./g, '').toLowerCase() || 'mit';
|
|
61
|
-
const yearString = `${args.yearStart}${args.yearEnd ? `-${args.yearEnd}` : ''}`;
|
|
62
|
-
const authorString = `${args.author}${args.email ? ` <${args.email}>` : ''}`;
|
|
63
|
-
switch (type) {
|
|
64
|
-
case 'apache20':
|
|
65
|
-
return `Copyright ${yearString} ${authorString}${br}${br}Licensed under the Apache License, Version 2.0 (the "License");${br}you may not use this file except in compliance with the License.${br}You may obtain a copy of the License at${br}${br} http://www.apache.org/licenses/LICENSE-2.0${br}${br}Unless required by applicable law or agreed to in writing, software${br}distributed under the License is distributed on an "AS IS" BASIS,${br}WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.${br}See the License for the specific language governing permissions and${br}limitations under the License.`;
|
|
66
|
-
case 'mit':
|
|
67
|
-
default:
|
|
68
|
-
return `Copyright (c) ${yearString} ${authorString}${br}${br}Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:${br}${br}The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.${br}${br}THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.`;
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
module.exports = {
|
|
73
|
-
number,
|
|
74
|
-
fileName,
|
|
75
|
-
fileSize,
|
|
76
|
-
fileExt,
|
|
77
|
-
msToTime,
|
|
78
|
-
secToTime,
|
|
79
|
-
license,
|
|
80
|
-
};
|
package/index.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
const date = require('./date');
|
|
2
|
-
const format = require('./format');
|
|
3
|
-
const misc = require('./misc');
|
|
4
|
-
const math = require('./math');
|
|
5
|
-
const array = require('./array');
|
|
6
|
-
const string = require('./string');
|
|
7
|
-
const verify = require('./verify');
|
|
8
|
-
|
|
9
|
-
module.exports = {
|
|
10
|
-
date,
|
|
11
|
-
format,
|
|
12
|
-
misc,
|
|
13
|
-
math,
|
|
14
|
-
array,
|
|
15
|
-
string,
|
|
16
|
-
verify,
|
|
17
|
-
};
|
package/math.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
const rand = (min, max) => {
|
|
2
|
-
if (!min && !max) return (Math.random() > 0.5) ? 1 : 0;
|
|
3
|
-
const limit = !max ? min : max;
|
|
4
|
-
const offset = (!max || min >= max) ? null : min;
|
|
5
|
-
return Math.floor(Math.random() * (offset ? (limit - offset + 1) : limit + 1)) + (offset || 0);
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
const add = (...args) => {
|
|
9
|
-
const val = args.length > 0 && typeof args[0] === 'object' ? args[0] : args;
|
|
10
|
-
if (val.length < 1) throw new Error('Invalid argument format!');
|
|
11
|
-
let total = 0;
|
|
12
|
-
for (let i = 0, iLen = val.length; i < iLen; i += 1) {
|
|
13
|
-
if (typeof val[i] === 'number') total += val[i];
|
|
14
|
-
}
|
|
15
|
-
return total;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
const mul = (...args) => {
|
|
19
|
-
const val = args.length > 0 && typeof args[0] === 'object' ? args[0] : args;
|
|
20
|
-
if (val.length < 1) throw new Error('Invalid argument format!');
|
|
21
|
-
let total = val[0];
|
|
22
|
-
for (let i = 1, iLen = val.length; i < iLen; i += 1) {
|
|
23
|
-
if (typeof val[i] === 'number') total *= val[i];
|
|
24
|
-
}
|
|
25
|
-
return total;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
module.exports = {
|
|
29
|
-
rand,
|
|
30
|
-
add,
|
|
31
|
-
mul,
|
|
32
|
-
};
|
package/misc.js
DELETED
package/string.js
DELETED
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
const crypto = require('crypto');
|
|
2
|
-
const { rand } = require('./math');
|
|
3
|
-
const { contains } = require('./verify');
|
|
4
|
-
|
|
5
|
-
const removeSpecialChar = (str, withoutSpace) => {
|
|
6
|
-
if (!str || typeof str !== 'string') return str;
|
|
7
|
-
return str.replace(new RegExp(`[^a-zA-Z가-힣ㄱ-ㅎㅏ-ㅣ0-9\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f${withoutSpace ? ' ' : ''}]`, 'gi'), '');
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
const removeNewLine = (str, replaceTo = '') => {
|
|
11
|
-
if (!str || typeof str !== 'string') return null;
|
|
12
|
-
return str.replace(/(\r\n|\n|\r)/gm, replaceTo).trim();
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
const capitalizeFirst = (str) => {
|
|
16
|
-
if (!str || typeof str !== 'string' || str.length < 1) return null;
|
|
17
|
-
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
const capitalizeEachWords = (str, naturally) => {
|
|
21
|
-
if (!str || typeof str !== 'string' || str.length < 1) return null;
|
|
22
|
-
const splitStr = str.trim().toLowerCase().split(' ');
|
|
23
|
-
for (let i = 0, iLen = splitStr.length; i < iLen; i += 1) {
|
|
24
|
-
if (!naturally || !contains(splitStr[i], [
|
|
25
|
-
'in', 'on', 'the', 'at', 'and', 'or', 'of', 'for', 'to', 'that',
|
|
26
|
-
'a', 'by', 'it', 'is', 'as', 'are', 'were', 'was', 'nor', 'an',
|
|
27
|
-
], true)) {
|
|
28
|
-
splitStr[i] = capitalizeFirst(splitStr[i]);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
return capitalizeFirst(splitStr.join(' '));
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
const count = (str, search) => {
|
|
35
|
-
if (!str || typeof str !== 'string' || !search || typeof search !== 'string') return 0;
|
|
36
|
-
return (str.match(new RegExp(search, 'g')) || []).length;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
const shuffle = (str) => {
|
|
40
|
-
if (!str || typeof str !== 'string') return null;
|
|
41
|
-
return [...str].sort(() => Math.random() - 0.5).join('');
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
const createRandom = (length = 12) => {
|
|
45
|
-
if (typeof length !== 'number') {
|
|
46
|
-
return null;
|
|
47
|
-
}
|
|
48
|
-
let result = '';
|
|
49
|
-
let newChar;
|
|
50
|
-
const AVAIL_CHARACTERS = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
|
51
|
-
const AVAIL_CHARACTERS_LENGTH = AVAIL_CHARACTERS.length;
|
|
52
|
-
for (let i = 0; i < length; i += 1) {
|
|
53
|
-
newChar = AVAIL_CHARACTERS.charAt(Math.floor(Math.random() * AVAIL_CHARACTERS_LENGTH));
|
|
54
|
-
newChar = Math.random() < 0.5 ? newChar.toUpperCase() : newChar;
|
|
55
|
-
result += newChar;
|
|
56
|
-
}
|
|
57
|
-
return result;
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
const hideRandom = (str, hideLength = 1, hideStr = '*') => {
|
|
61
|
-
let currentStr = str;
|
|
62
|
-
let hideCount = 0;
|
|
63
|
-
let tempIdx = 0;
|
|
64
|
-
const totalStrLength = currentStr.length;
|
|
65
|
-
let currentStrLength = 0;
|
|
66
|
-
while ((hideCount < hideLength) && (currentStrLength < totalStrLength)) {
|
|
67
|
-
tempIdx = rand(0, totalStrLength);
|
|
68
|
-
if (/[a-zA-Z가-힣]/.test(currentStr.substr(tempIdx, 1))) {
|
|
69
|
-
currentStr = `${currentStr.substr(0, tempIdx)}${hideStr}${currentStr.substr(tempIdx + 1)}`;
|
|
70
|
-
hideCount += 1;
|
|
71
|
-
}
|
|
72
|
-
currentStrLength += 1;
|
|
73
|
-
}
|
|
74
|
-
return currentStr;
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
const truncate = (str, length = 0, ellipsis = '') => {
|
|
78
|
-
let convStr = str;
|
|
79
|
-
if (str.length > length) {
|
|
80
|
-
convStr = str.substring(0, length) + ellipsis;
|
|
81
|
-
}
|
|
82
|
-
return convStr;
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
const encrypt = (str, secret, algorithm = 'aes-256-cbc', ivSize = 16) => {
|
|
86
|
-
const iv = crypto.randomBytes(ivSize);
|
|
87
|
-
const cipher = crypto.createCipheriv(algorithm, secret, iv);
|
|
88
|
-
let enc = cipher.update(str);
|
|
89
|
-
enc = Buffer.concat([enc, cipher.final()]);
|
|
90
|
-
return `${iv.toString('hex')}:${enc.toString('hex')}`;
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
const decrypt = (str, secret, algorithm = 'aes-256-cbc') => {
|
|
94
|
-
const arrStr = str.split(':');
|
|
95
|
-
const decipher = crypto.createDecipheriv(algorithm, secret, Buffer.from(arrStr.shift(), 'hex'));
|
|
96
|
-
let decrypted = decipher.update(Buffer.from(arrStr.join(':'), 'hex'));
|
|
97
|
-
decrypted = Buffer.concat([decrypted, decipher.final()]);
|
|
98
|
-
return decrypted.toString();
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
const md5 = (str) => {
|
|
102
|
-
if (!str || typeof str !== 'string' || str.length < 1) {
|
|
103
|
-
throw new Error('string arguments required');
|
|
104
|
-
}
|
|
105
|
-
return crypto.createHash('md5').update(str).digest('hex');
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
const sha1 = (str) => {
|
|
109
|
-
if (!str || typeof str !== 'string' || str.length < 1) {
|
|
110
|
-
throw new Error('string arguments required');
|
|
111
|
-
}
|
|
112
|
-
return crypto.createHash('sha1').update(str).digest('hex');
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
const sha256 = (str) => {
|
|
116
|
-
if (!str || typeof str !== 'string' || str.length < 1) {
|
|
117
|
-
throw new Error('string arguments required');
|
|
118
|
-
}
|
|
119
|
-
return crypto.createHash('sha256').update(str).digest('hex');
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
const unique = (str) => {
|
|
123
|
-
if (!str || typeof str !== 'string' || str.length < 1) {
|
|
124
|
-
throw new Error('string arguments required');
|
|
125
|
-
}
|
|
126
|
-
return String.prototype.concat(...new Set(str));
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
module.exports = {
|
|
130
|
-
removeSpecialChar,
|
|
131
|
-
removeNewLine,
|
|
132
|
-
capitalizeFirst,
|
|
133
|
-
capitalizeEachWords,
|
|
134
|
-
count,
|
|
135
|
-
shuffle,
|
|
136
|
-
createRandom,
|
|
137
|
-
hideRandom,
|
|
138
|
-
truncate,
|
|
139
|
-
encrypt,
|
|
140
|
-
decrypt,
|
|
141
|
-
md5,
|
|
142
|
-
sha1,
|
|
143
|
-
sha256,
|
|
144
|
-
unique,
|
|
145
|
-
};
|
package/verify.js
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
const empty = (data) => {
|
|
2
|
-
if (!data) return true;
|
|
3
|
-
switch (typeof data) {
|
|
4
|
-
case 'string':
|
|
5
|
-
return data.length < 1;
|
|
6
|
-
case 'object':
|
|
7
|
-
if (Array.isArray(data)) {
|
|
8
|
-
return data.length < 1;
|
|
9
|
-
}
|
|
10
|
-
return Object.keys(data).length < 1;
|
|
11
|
-
default:
|
|
12
|
-
return false;
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
const isUrl = (url, withProtocol = false, strict = false) => {
|
|
17
|
-
if (!url || typeof url !== 'string') return false;
|
|
18
|
-
const temp = () => null;
|
|
19
|
-
if (strict && url.indexOf('.') === -1) return false;
|
|
20
|
-
try {
|
|
21
|
-
temp(new URL(`${(withProtocol && url.indexOf('://') === -1) ? 'https://' : ''}${url}`));
|
|
22
|
-
} catch (e) {
|
|
23
|
-
return false;
|
|
24
|
-
}
|
|
25
|
-
return true;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const contains = (str, search, exact) => {
|
|
29
|
-
if (!str || !search || (typeof str !== 'string' && typeof str !== 'object')
|
|
30
|
-
|| (typeof str === 'object' && !Array.isArray(str))) return false;
|
|
31
|
-
if (typeof search === 'string') return str.indexOf(search) !== -1;
|
|
32
|
-
for (let i = 0, iLen = search.length; i < iLen; i += 1) {
|
|
33
|
-
if (exact) {
|
|
34
|
-
if (str === search[i]) {
|
|
35
|
-
return true;
|
|
36
|
-
}
|
|
37
|
-
} else if (str.indexOf(search[i]) !== -1) {
|
|
38
|
-
return true;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
return false;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
const is2dArray = (arr) => {
|
|
45
|
-
if (!arr || typeof arr !== 'object' || !Array.isArray(arr)) return false;
|
|
46
|
-
return arr.filter(Array.isArray).length > 0;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
const between = (number, range, inclusive = false) => {
|
|
50
|
-
if (!number) throw new Error('Need a value to compare with range');
|
|
51
|
-
if (!range || typeof range !== 'object' || range.length !== 2) throw new Error('You should use range like this: [min, max]');
|
|
52
|
-
const minM = Math.min.apply(Math, [range[0], range[1]]);
|
|
53
|
-
const maxM = Math.max.apply(Math, [range[0], range[1]]);
|
|
54
|
-
return inclusive ? number >= minM && number <= maxM : number > minM && number < maxM;
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
const length = (data) => {
|
|
58
|
-
if (!data || typeof data === 'undefined') return 0;
|
|
59
|
-
switch (typeof data) {
|
|
60
|
-
case 'object':
|
|
61
|
-
return Array.isArray(data) ? data.length : Object.keys(data).length;
|
|
62
|
-
case 'number':
|
|
63
|
-
case 'bigint':
|
|
64
|
-
return data.toString().length;
|
|
65
|
-
case 'boolean':
|
|
66
|
-
return data ? 4 : 5;
|
|
67
|
-
case 'function':
|
|
68
|
-
return length(data());
|
|
69
|
-
case 'string':
|
|
70
|
-
default:
|
|
71
|
-
return data.length;
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
const isBotAgent = (userAgent) => {
|
|
76
|
-
if (!userAgent || typeof userAgent !== 'string' || userAgent.length < 1) return false;
|
|
77
|
-
return /bot|naverbot|google|Googlebot|Googlebot-Mobile|Googlebot-Image|Google favicon|Mediapartners-Google|bingbot|slurp|java|wget|curl|Commons-HttpClient|Python-urllib|libwww|httpunit|nutch|phpcrawl|msnbot|jyxobot|FAST-WebCrawler|FAST Enterprise Crawler|biglotron|teoma|convera|seekbot|gigablast|exabot|ngbot|ia_archiver|GingerCrawler|webmon |httrack|webcrawler|grub.org|UsineNouvelleCrawler|antibot|netresearchserver|speedy|fluffy|bibnum.bnf|findlink|msrbot|panscient|yacybot|AISearchBot|IOI|ips-agent|tagoobot|MJ12bot|dotbot|woriobot|yanga|buzzbot|mlbot|yandexbot|purebot|Linguee Bot|Voyager|CyberPatrol|voilabot|baiduspider|citeseerxbot|spbot|twengabot|postrank|turnitinbot|scribdbot|page2rss|sitebot|linkdex|Adidxbot|blekkobot|ezooms|dotbot|Mail.RU_Bot|discobot|heritrix|findthatfile|europarchive.org|NerdByNature.Bot|sistrix crawler|ahrefsbot|Aboundex|domaincrawler|wbsearchbot|summify|ccbot|edisterbot|seznambot|ec2linkfinder|gslfbot|aihitbot|intelium_bot|facebookexternalhit|yeti|RetrevoPageAnalyzer|lb-spider|sogou|lssbot|careerbot|wotbox|wocbot|ichiro|DuckDuckBot|lssrocketcrawler|drupact|webcompanycrawler|acoonbot|openindexspider|gnam gnam spider|web-archive-net.com.bot|backlinkcrawler|coccoc|integromedb|content crawler spider|toplistbot|seokicks-robot|it2media-domain-crawler|ip-web-crawler.com|siteexplorer.info|elisabot|proximic|changedetection|blexbot|arabot|WeSEE:Search|niki-bot|CrystalSemanticsBot|rogerbot|360Spider|psbot|InterfaxScanBot|Lipperhey SEO Service|CC Metadata Scaper|g00g1e.net|GrapeshotCrawler|urlappendbot|brainobot|fr-crawler|binlar|SimpleCrawler|Livelapbot|Twitterbot|cXensebot|smtbot|bnf.fr_bot|A6-Indexer|ADmantX|Facebot|Twitterbot|OrangeBot|memorybot|AdvBot|MegaIndex|SemanticScholarBot|ltx71|nerdybot|xovibot|BUbiNG|Qwantify|archive.org_bot|Applebot|TweetmemeBot|crawler4j|findxbot|SemrushBot|yoozBot|lipperhey|y!j-asr|Domain Re-Animator Bot|AddThis/i.test(userAgent);
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
module.exports = {
|
|
81
|
-
empty,
|
|
82
|
-
isUrl,
|
|
83
|
-
contains,
|
|
84
|
-
is2dArray,
|
|
85
|
-
between,
|
|
86
|
-
length,
|
|
87
|
-
isBotAgent,
|
|
88
|
-
};
|