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/dist/index.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
declare interface LicenseOption {
|
|
2
|
+
author: string;
|
|
3
|
+
email?: string;
|
|
4
|
+
yearStart: string | number;
|
|
5
|
+
yearEnd?: string;
|
|
6
|
+
htmlBr?: boolean;
|
|
7
|
+
type: 'mit' | 'apache20';
|
|
8
|
+
}
|
|
9
|
+
declare type PositiveNumber<N extends number> = number extends N ? N : `${N}` extends `-${string}` ? never : N;
|
|
10
|
+
export default class Qsu {
|
|
11
|
+
static sleep<N extends number>(delay: PositiveNumber<N>): Promise<void>;
|
|
12
|
+
static numRandom(min: number, max: number): number;
|
|
13
|
+
static sum(...args: number[]): number;
|
|
14
|
+
static mul(...args: number[]): number;
|
|
15
|
+
static dayDiff(date1: Date, date2?: Date): number;
|
|
16
|
+
static today(dateFormat?: string): string;
|
|
17
|
+
static isRealDate(date: string | Date): boolean;
|
|
18
|
+
static convertDate(date: string, format?: string): string;
|
|
19
|
+
static arrShuffle(array: any[]): any[];
|
|
20
|
+
static arrWithDefault(defaultValue: any, length?: number): any[];
|
|
21
|
+
static arrUnique(array: any[]): any[];
|
|
22
|
+
static arrWithNumber(start: number, end: number): number[];
|
|
23
|
+
static average(array: number[]): number;
|
|
24
|
+
static arrMove<N extends number>(array: any[], from: PositiveNumber<N>, to: PositiveNumber<N>): any[];
|
|
25
|
+
static removeSpecialChar(str: string, withoutSpace?: boolean): string;
|
|
26
|
+
static removeNewLine(str: string, replaceTo?: string): string;
|
|
27
|
+
static capitalizeFirst(str: string): string;
|
|
28
|
+
static capitalizeEachWords(str: string, natural?: boolean): string;
|
|
29
|
+
static strNumberOf(str: string, search: string): number;
|
|
30
|
+
static strShuffle(str: string): string;
|
|
31
|
+
static strRandom<N extends number>(length: PositiveNumber<N>, additionalCharacters?: string): string;
|
|
32
|
+
static strBlindRandom<N extends number>(str: string, blindLength: PositiveNumber<N>, blindStr?: string): string;
|
|
33
|
+
static truncate<N extends number>(str: string, length: PositiveNumber<N>, ellipsis?: string): string;
|
|
34
|
+
static encrypt(str: string, secret: string, algorithm?: string, ivSize?: number): string;
|
|
35
|
+
static decrypt(str: string, secret: string, algorithm?: string): string;
|
|
36
|
+
static md5(str: string): string;
|
|
37
|
+
static sha1(str: string): string;
|
|
38
|
+
static sha256(str: string): string;
|
|
39
|
+
static encodeBase64(str: string): string;
|
|
40
|
+
static decodeBase64(encodedStr: string): string;
|
|
41
|
+
static strUnique(str: string): string;
|
|
42
|
+
static isEmpty(data?: any): boolean;
|
|
43
|
+
static isUrl(url: string, withProtocol?: boolean, strict?: boolean): boolean;
|
|
44
|
+
static contains(str: any[] | string, search: any[] | string, exact?: boolean): boolean;
|
|
45
|
+
static is2dArray(array: any[]): boolean;
|
|
46
|
+
static between(range: [number, number], number: number, inclusive?: boolean): boolean;
|
|
47
|
+
static len(data: any): number;
|
|
48
|
+
static isBotAgent(userAgent: string): boolean;
|
|
49
|
+
static numberFormat(number: number): string;
|
|
50
|
+
static fileName(filePath: string, withExtension?: boolean): string;
|
|
51
|
+
static fileSize<N extends number>(bytes: PositiveNumber<N>, decimals?: number): string;
|
|
52
|
+
static fileExt(filePath: string): string;
|
|
53
|
+
static msToTime(milliseconds?: number, withMilliseconds?: boolean, separator?: string): string;
|
|
54
|
+
static secToTime(seconds?: number, onlyHour?: boolean, separator?: string): string;
|
|
55
|
+
static license(options: LicenseOption): string;
|
|
56
|
+
}
|
|
57
|
+
export { Qsu, };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import moment from 'moment';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import crypto from 'crypto';
|
|
4
|
+
export default class Qsu {
|
|
5
|
+
/*
|
|
6
|
+
* Misc
|
|
7
|
+
* */
|
|
8
|
+
static sleep(delay) {
|
|
9
|
+
return new Promise((resolve) => {
|
|
10
|
+
setTimeout(resolve, delay);
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
/*
|
|
14
|
+
* Math
|
|
15
|
+
* */
|
|
16
|
+
static numRandom(min, max) {
|
|
17
|
+
if (!min && !max) {
|
|
18
|
+
return (Math.random() > 0.5) ? 1 : 0;
|
|
19
|
+
}
|
|
20
|
+
const limit = !max ? min : max;
|
|
21
|
+
const offset = (!max || min >= max) ? null : min;
|
|
22
|
+
return Math.floor(Math.random() * (offset ? (limit - offset + 1) : limit + 1)) + (offset || 0);
|
|
23
|
+
}
|
|
24
|
+
static sum(...args) {
|
|
25
|
+
const val = args.length > 0 && typeof args[0] === 'object' ? args[0] : args;
|
|
26
|
+
let total = 0;
|
|
27
|
+
for (let i = 0, iLen = val.length; i < iLen; i += 1) {
|
|
28
|
+
if (typeof val[i] === 'number') {
|
|
29
|
+
total += val[i];
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return total;
|
|
33
|
+
}
|
|
34
|
+
static mul(...args) {
|
|
35
|
+
const val = args.length > 0 && typeof args[0] === 'object' ? args[0] : args;
|
|
36
|
+
let total = val[0];
|
|
37
|
+
for (let i = 1, iLen = val.length; i < iLen; i += 1) {
|
|
38
|
+
if (typeof val[i] === 'number') {
|
|
39
|
+
total *= val[i];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return total;
|
|
43
|
+
}
|
|
44
|
+
/*
|
|
45
|
+
* Date
|
|
46
|
+
* */
|
|
47
|
+
static dayDiff(date1, date2) {
|
|
48
|
+
const date2c = date2 || new Date();
|
|
49
|
+
return Math.ceil(Math.abs(date2c.getTime() - date1.getTime()) / (1000 * 3600 * 24));
|
|
50
|
+
}
|
|
51
|
+
static today(dateFormat) {
|
|
52
|
+
return moment().format(dateFormat || 'YYYY-MM-DD');
|
|
53
|
+
}
|
|
54
|
+
static isRealDate(date) {
|
|
55
|
+
const dateConverted = typeof date === 'string' ? new Date(date) : date;
|
|
56
|
+
if (!dateConverted.getTime()) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
return dateConverted.toISOString().slice(0, 10) === date;
|
|
60
|
+
}
|
|
61
|
+
static convertDate(date, format) {
|
|
62
|
+
return moment(date).format(format || 'YYYY-MM-DD');
|
|
63
|
+
}
|
|
64
|
+
/*
|
|
65
|
+
* Array
|
|
66
|
+
* */
|
|
67
|
+
static arrShuffle(array) {
|
|
68
|
+
if (array.length === 1) {
|
|
69
|
+
return array[0];
|
|
70
|
+
}
|
|
71
|
+
const newArray = array;
|
|
72
|
+
for (let i = array.length - 1; i > 0; i -= 1) {
|
|
73
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
74
|
+
[newArray[i], newArray[j]] = [array[j], array[i]];
|
|
75
|
+
}
|
|
76
|
+
return newArray;
|
|
77
|
+
}
|
|
78
|
+
static arrWithDefault(defaultValue, length = 0) {
|
|
79
|
+
if (length < 1) {
|
|
80
|
+
return [];
|
|
81
|
+
}
|
|
82
|
+
return Array(length).fill(defaultValue);
|
|
83
|
+
}
|
|
84
|
+
static arrUnique(array) {
|
|
85
|
+
if (this.is2dArray(array)) {
|
|
86
|
+
return array.map((x) => JSON.stringify(x))
|
|
87
|
+
.reverse()
|
|
88
|
+
.filter((e, i, a) => a.indexOf(e, i + 1) === -1)
|
|
89
|
+
.reverse()
|
|
90
|
+
.map((x) => JSON.parse(x));
|
|
91
|
+
}
|
|
92
|
+
return [...new Set(array)];
|
|
93
|
+
}
|
|
94
|
+
static arrWithNumber(start, end) {
|
|
95
|
+
if (start > end) {
|
|
96
|
+
throw new Error('end is greater than start.');
|
|
97
|
+
}
|
|
98
|
+
return Array.from({ length: (end - start) + 1 }, (_, i) => i + start);
|
|
99
|
+
}
|
|
100
|
+
static average(array) {
|
|
101
|
+
return array.reduce((p, c) => p + c, 0) / array.length;
|
|
102
|
+
}
|
|
103
|
+
static arrMove(array, from, to) {
|
|
104
|
+
const arrayLength = array.length;
|
|
105
|
+
if (arrayLength <= from || arrayLength <= to) {
|
|
106
|
+
throw new Error('Invalid move params');
|
|
107
|
+
}
|
|
108
|
+
array.splice(to, 0, array.splice(from, 1)[0]);
|
|
109
|
+
return array;
|
|
110
|
+
}
|
|
111
|
+
/*
|
|
112
|
+
* String
|
|
113
|
+
* */
|
|
114
|
+
static removeSpecialChar(str, withoutSpace) {
|
|
115
|
+
return str.replace(new RegExp(`[^a-zA-Z가-힣ㄱ-ㅎㅏ-ㅣ0-9\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f${withoutSpace ? ' ' : ''}]`, 'gi'), '');
|
|
116
|
+
}
|
|
117
|
+
static removeNewLine(str, replaceTo = '') {
|
|
118
|
+
return str.replace(/(\r\n|\n|\r)/gm, replaceTo).trim();
|
|
119
|
+
}
|
|
120
|
+
static capitalizeFirst(str) {
|
|
121
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
122
|
+
}
|
|
123
|
+
static capitalizeEachWords(str, natural) {
|
|
124
|
+
const splitStr = str.trim().toLowerCase().split(' ');
|
|
125
|
+
for (let i = 0, iLen = splitStr.length; i < iLen; i += 1) {
|
|
126
|
+
if (!natural || !this.contains(splitStr[i], [
|
|
127
|
+
'in', 'on', 'the', 'at', 'and', 'or', 'of', 'for', 'to', 'that',
|
|
128
|
+
'a', 'by', 'it', 'is', 'as', 'are', 'were', 'was', 'nor', 'an',
|
|
129
|
+
], true)) {
|
|
130
|
+
splitStr[i] = this.capitalizeFirst(splitStr[i]);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return this.capitalizeFirst(splitStr.join(' '));
|
|
134
|
+
}
|
|
135
|
+
static strNumberOf(str, search) {
|
|
136
|
+
return (str.match(new RegExp(search, 'g')) || []).length;
|
|
137
|
+
}
|
|
138
|
+
static strShuffle(str) {
|
|
139
|
+
return [...str].sort(() => Math.random() - 0.5).join('');
|
|
140
|
+
}
|
|
141
|
+
static strRandom(length, additionalCharacters) {
|
|
142
|
+
const availCharacters = `abcdefghijklmnopqrstuvwxyz0123456789${additionalCharacters}`;
|
|
143
|
+
const availCharacterLength = availCharacters.length;
|
|
144
|
+
let result = '';
|
|
145
|
+
let newChar;
|
|
146
|
+
for (let i = 0; i < length; i += 1) {
|
|
147
|
+
newChar = availCharacters.charAt(Math.floor(Math.random() * availCharacterLength));
|
|
148
|
+
newChar = Math.random() < 0.5 ? newChar.toUpperCase() : newChar;
|
|
149
|
+
result += newChar;
|
|
150
|
+
}
|
|
151
|
+
return result;
|
|
152
|
+
}
|
|
153
|
+
static strBlindRandom(str, blindLength, blindStr = '*') {
|
|
154
|
+
let currentStr = str;
|
|
155
|
+
let hideCount = 0;
|
|
156
|
+
let tempIdx = 0;
|
|
157
|
+
let currentStrLength = 0;
|
|
158
|
+
const totalStrLength = currentStr.length;
|
|
159
|
+
while ((hideCount < blindLength) && (currentStrLength < totalStrLength)) {
|
|
160
|
+
tempIdx = this.numRandom(0, totalStrLength);
|
|
161
|
+
if (/[a-zA-Z가-힣]/.test(currentStr.substring(tempIdx, tempIdx + 1))) {
|
|
162
|
+
currentStr = `${currentStr.substring(0, tempIdx + 1)}${blindStr}${currentStr.substring(tempIdx + 2)}`;
|
|
163
|
+
hideCount += 1;
|
|
164
|
+
}
|
|
165
|
+
currentStrLength += 1;
|
|
166
|
+
}
|
|
167
|
+
return currentStr;
|
|
168
|
+
}
|
|
169
|
+
static truncate(str, length, ellipsis = '') {
|
|
170
|
+
let convStr = str;
|
|
171
|
+
if (str.length > length) {
|
|
172
|
+
convStr = str.substring(0, length) + ellipsis;
|
|
173
|
+
}
|
|
174
|
+
return convStr;
|
|
175
|
+
}
|
|
176
|
+
static encrypt(str, secret, algorithm = 'aes-256-cbc', ivSize = 16) {
|
|
177
|
+
if (str.length < 1) {
|
|
178
|
+
return '';
|
|
179
|
+
}
|
|
180
|
+
const iv = crypto.randomBytes(ivSize);
|
|
181
|
+
const cipher = crypto.createCipheriv(algorithm, secret, iv);
|
|
182
|
+
let enc = cipher.update(str);
|
|
183
|
+
enc = Buffer.concat([enc, cipher.final()]);
|
|
184
|
+
return `${iv.toString('hex')}:${enc.toString('hex')}`;
|
|
185
|
+
}
|
|
186
|
+
static decrypt(str, secret, algorithm = 'aes-256-cbc') {
|
|
187
|
+
if (str.length < 1) {
|
|
188
|
+
return '';
|
|
189
|
+
}
|
|
190
|
+
const arrStr = str.split(':');
|
|
191
|
+
const decipher = crypto.createDecipheriv(algorithm, secret, Buffer.from(arrStr.shift(), 'hex'));
|
|
192
|
+
let decrypted = decipher.update(Buffer.from(arrStr.join(':'), 'hex'));
|
|
193
|
+
decrypted = Buffer.concat([decrypted, decipher.final()]);
|
|
194
|
+
return decrypted.toString();
|
|
195
|
+
}
|
|
196
|
+
static md5(str) {
|
|
197
|
+
return crypto.createHash('md5').update(str).digest('hex');
|
|
198
|
+
}
|
|
199
|
+
static sha1(str) {
|
|
200
|
+
return crypto.createHash('sha1').update(str).digest('hex');
|
|
201
|
+
}
|
|
202
|
+
static sha256(str) {
|
|
203
|
+
return crypto.createHash('sha256').update(str).digest('hex');
|
|
204
|
+
}
|
|
205
|
+
static encodeBase64(str) {
|
|
206
|
+
return Buffer.from(str, 'utf8').toString('base64');
|
|
207
|
+
}
|
|
208
|
+
static decodeBase64(encodedStr) {
|
|
209
|
+
return Buffer.from(encodedStr, 'base64').toString('utf8');
|
|
210
|
+
}
|
|
211
|
+
static strUnique(str) {
|
|
212
|
+
return [...new Set(str)].join('');
|
|
213
|
+
}
|
|
214
|
+
/*
|
|
215
|
+
* Verify
|
|
216
|
+
* */
|
|
217
|
+
static isEmpty(data) {
|
|
218
|
+
if (!data) {
|
|
219
|
+
return true;
|
|
220
|
+
}
|
|
221
|
+
switch (typeof data) {
|
|
222
|
+
case 'string':
|
|
223
|
+
return data.length < 1;
|
|
224
|
+
case 'object':
|
|
225
|
+
if (Array.isArray(data)) {
|
|
226
|
+
return data.length < 1;
|
|
227
|
+
}
|
|
228
|
+
return Object.keys(data).length < 1;
|
|
229
|
+
default:
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
static isUrl(url, withProtocol = false, strict = false) {
|
|
234
|
+
if (strict && url.indexOf('.') === -1) {
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
try {
|
|
238
|
+
new URL(`${(withProtocol && url.indexOf('://') === -1) ? 'https://' : ''}${url}`).toString();
|
|
239
|
+
}
|
|
240
|
+
catch (e) {
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
return true;
|
|
244
|
+
}
|
|
245
|
+
static contains(str, search, exact = false) {
|
|
246
|
+
if (typeof search === 'string') {
|
|
247
|
+
return str.indexOf(search) !== -1;
|
|
248
|
+
}
|
|
249
|
+
for (let i = 0, iLen = search.length; i < iLen; i += 1) {
|
|
250
|
+
if (exact) {
|
|
251
|
+
if (str === search[i]) {
|
|
252
|
+
return true;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
else if (str.indexOf(search[i]) !== -1) {
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return false;
|
|
260
|
+
}
|
|
261
|
+
static is2dArray(array) {
|
|
262
|
+
return array.filter(Array.isArray).length > 0;
|
|
263
|
+
}
|
|
264
|
+
static between(range, number, inclusive = false) {
|
|
265
|
+
const minM = Math.min.apply(Math, [range[0], range[1]]);
|
|
266
|
+
const maxM = Math.max.apply(Math, [range[0], range[1]]);
|
|
267
|
+
return inclusive ? number >= minM && number <= maxM : number > minM && number < maxM;
|
|
268
|
+
}
|
|
269
|
+
static len(data) {
|
|
270
|
+
if (!data) {
|
|
271
|
+
return 0;
|
|
272
|
+
}
|
|
273
|
+
switch (typeof data) {
|
|
274
|
+
case 'object':
|
|
275
|
+
return Array.isArray(data) ? data.length : Object.keys(data).length;
|
|
276
|
+
case 'number':
|
|
277
|
+
case 'bigint':
|
|
278
|
+
return data.toString().length;
|
|
279
|
+
case 'boolean':
|
|
280
|
+
return data ? 4 : 5;
|
|
281
|
+
case 'function':
|
|
282
|
+
return data().length;
|
|
283
|
+
case 'string':
|
|
284
|
+
default:
|
|
285
|
+
return data.length;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
static isBotAgent(userAgent) {
|
|
289
|
+
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);
|
|
290
|
+
}
|
|
291
|
+
/*
|
|
292
|
+
* Format
|
|
293
|
+
* */
|
|
294
|
+
static numberFormat(number) {
|
|
295
|
+
return new Intl.NumberFormat().format(number);
|
|
296
|
+
}
|
|
297
|
+
static fileName(filePath, withExtension = false) {
|
|
298
|
+
if (withExtension) {
|
|
299
|
+
return path.basename(filePath);
|
|
300
|
+
}
|
|
301
|
+
return path.basename(filePath, path.extname(filePath));
|
|
302
|
+
}
|
|
303
|
+
static fileSize(bytes, decimals = 2) {
|
|
304
|
+
if (bytes === 0 || bytes < 0) {
|
|
305
|
+
return '0 Bytes';
|
|
306
|
+
}
|
|
307
|
+
const byteCalc = Math.floor(Math.log(bytes) / Math.log(1024));
|
|
308
|
+
return `${parseFloat((bytes / 1024 ** byteCalc).toFixed((decimals < 0 ? 0 : decimals)))} ${['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'][byteCalc]}`;
|
|
309
|
+
}
|
|
310
|
+
static fileExt(filePath) {
|
|
311
|
+
if (filePath.indexOf('.') === -1) {
|
|
312
|
+
return 'Unknown';
|
|
313
|
+
}
|
|
314
|
+
const pSpl = filePath.trim().toLowerCase().split('.');
|
|
315
|
+
return pSpl.length > 0 ? pSpl[pSpl.length - 1] : 'Unknown';
|
|
316
|
+
}
|
|
317
|
+
static msToTime(milliseconds = 0, withMilliseconds = false, separator = ':') {
|
|
318
|
+
const ms = Math.floor((milliseconds % 1000) / 100);
|
|
319
|
+
let sec = Math.floor((milliseconds / 1000) % 60);
|
|
320
|
+
let min = Math.floor((milliseconds / (1000 * 60)) % 60);
|
|
321
|
+
let hour = Math.floor(milliseconds / (1000 * 60 * 60));
|
|
322
|
+
hour = (hour < 10) ? `0${hour}` : hour;
|
|
323
|
+
min = (min < 10) ? `0${min}` : min;
|
|
324
|
+
sec = (sec < 10) ? `0${sec}` : sec;
|
|
325
|
+
return `${hour}${separator}${min}${separator}${sec}${withMilliseconds ? `.${ms}` : ''}`;
|
|
326
|
+
}
|
|
327
|
+
static secToTime(seconds = 0, onlyHour = false, separator = ':') {
|
|
328
|
+
let sec = Math.floor(seconds % 60);
|
|
329
|
+
let min = Math.floor((seconds / 60) % 60);
|
|
330
|
+
let hour = Math.floor(seconds / (60 * 60));
|
|
331
|
+
hour = (hour < 10) ? `0${hour}` : hour;
|
|
332
|
+
min = (min < 10) ? `0${min}` : min;
|
|
333
|
+
sec = (sec < 10) ? `0${sec}` : sec;
|
|
334
|
+
return onlyHour ? hour.toString() : `${hour}${separator}${min}${separator}${sec}`;
|
|
335
|
+
}
|
|
336
|
+
static license(options) {
|
|
337
|
+
const br = options.htmlBr ? '<br/>' : '\n';
|
|
338
|
+
const yearString = `${options.yearStart}${options.yearEnd ? `-${options.yearEnd}` : ''}`;
|
|
339
|
+
const authorString = `${options.author}${options.email ? ` <${options.email}>` : ''}`;
|
|
340
|
+
switch (options.type.replace(/\.-_,\s/g, '').toLowerCase()) {
|
|
341
|
+
case 'apache20':
|
|
342
|
+
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.`;
|
|
343
|
+
case 'mit':
|
|
344
|
+
default:
|
|
345
|
+
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.`;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
export { Qsu, };
|
package/package.json
CHANGED
|
@@ -1,38 +1,55 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "qsu",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Quick and Simple Utility for javascript",
|
|
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
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "qsu",
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "Quick and Simple Utility for javascript",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"test": "npm run lint && npm run build && mocha --parallel test/*.spec.js",
|
|
10
|
+
"lint": "eslint .",
|
|
11
|
+
"lint:fix": "eslint --fix ."
|
|
12
|
+
},
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">=14.0.0"
|
|
15
|
+
},
|
|
16
|
+
"author": "Jooy2 <jootc.help@gmail.com>",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"main": "dist/index.js",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": "./dist/index.js"
|
|
21
|
+
},
|
|
22
|
+
"typesVersions": {
|
|
23
|
+
"*": {
|
|
24
|
+
"index.d.ts": [
|
|
25
|
+
"dist/index.d.ts"
|
|
26
|
+
]
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "https://github.com/jooy2/qsu"
|
|
32
|
+
},
|
|
33
|
+
"keywords": [
|
|
34
|
+
"util",
|
|
35
|
+
"utility",
|
|
36
|
+
"tool",
|
|
37
|
+
"underscore",
|
|
38
|
+
"website",
|
|
39
|
+
"helper"
|
|
40
|
+
],
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/node": "^17.0.35",
|
|
43
|
+
"@typescript-eslint/eslint-plugin": "^5.25.0",
|
|
44
|
+
"@typescript-eslint/parser": "^5.25.0",
|
|
45
|
+
"eslint": "^8.16.0",
|
|
46
|
+
"eslint-config-airbnb": "^19.0.4",
|
|
47
|
+
"eslint-plugin-import": "^2.26.0",
|
|
48
|
+
"mocha": "^10.0.0",
|
|
49
|
+
"ts-node": "^10.8.0",
|
|
50
|
+
"typescript": "^4.6.4"
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"moment": "^2.29.3"
|
|
54
|
+
}
|
|
55
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"baseUrl": "lib",
|
|
4
|
+
"target": "es2020",
|
|
5
|
+
"module": "es2022",
|
|
6
|
+
"lib": [
|
|
7
|
+
"es2020"
|
|
8
|
+
],
|
|
9
|
+
"moduleResolution": "Node",
|
|
10
|
+
"allowSyntheticDefaultImports": true,
|
|
11
|
+
"declaration": true,
|
|
12
|
+
"outDir": "./dist",
|
|
13
|
+
"strict": true
|
|
14
|
+
},
|
|
15
|
+
"include": [
|
|
16
|
+
"lib/**/*.ts",
|
|
17
|
+
"lib/**/*.d.ts"
|
|
18
|
+
],
|
|
19
|
+
"exclude": [
|
|
20
|
+
"node_modules",
|
|
21
|
+
"**/*.spec.ts"
|
|
22
|
+
]
|
|
23
|
+
}
|
package/.babelrc
DELETED
package/.eslintrc.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
parserOptions: {
|
|
3
|
-
sourceType: 'module',
|
|
4
|
-
ecmaVersion: 2020,
|
|
5
|
-
},
|
|
6
|
-
env: {
|
|
7
|
-
node: true,
|
|
8
|
-
es6: true,
|
|
9
|
-
},
|
|
10
|
-
extends: [
|
|
11
|
-
'airbnb/base',
|
|
12
|
-
],
|
|
13
|
-
rules: {
|
|
14
|
-
'linebreak-style': ['error', 'windows'],
|
|
15
|
-
'arrow-parens': 0,
|
|
16
|
-
},
|
|
17
|
-
overrides: [
|
|
18
|
-
{
|
|
19
|
-
files: ['test/*.spec.js'],
|
|
20
|
-
rules: {
|
|
21
|
-
'no-undef': 0,
|
|
22
|
-
},
|
|
23
|
-
},
|
|
24
|
-
],
|
|
25
|
-
};
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
-
<project version="4">
|
|
3
|
-
<component name="GitToolBoxProjectSettings">
|
|
4
|
-
<option name="commitMessageIssueKeyValidationOverride">
|
|
5
|
-
<BoolValueOverride>
|
|
6
|
-
<option name="enabled" value="true" />
|
|
7
|
-
</BoolValueOverride>
|
|
8
|
-
</option>
|
|
9
|
-
<option name="commitMessageValidationConfigOverride">
|
|
10
|
-
<CommitMessageValidationOverride>
|
|
11
|
-
<option name="enabled" value="true" />
|
|
12
|
-
</CommitMessageValidationOverride>
|
|
13
|
-
</option>
|
|
14
|
-
<option name="commitMessageValidationEnabledOverride">
|
|
15
|
-
<BoolValueOverride>
|
|
16
|
-
<option name="enabled" value="true" />
|
|
17
|
-
</BoolValueOverride>
|
|
18
|
-
</option>
|
|
19
|
-
</component>
|
|
20
|
-
</project>
|
package/.idea/modules.xml
DELETED
package/.idea/qsu.iml
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
-
<module type="WEB_MODULE" version="4">
|
|
3
|
-
<component name="NewModuleRootManager">
|
|
4
|
-
<content url="file://$MODULE_DIR$">
|
|
5
|
-
<excludeFolder url="file://$MODULE_DIR$/temp" />
|
|
6
|
-
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
|
|
7
|
-
<excludeFolder url="file://$MODULE_DIR$/tmp" />
|
|
8
|
-
</content>
|
|
9
|
-
<orderEntry type="inheritedJdk" />
|
|
10
|
-
<orderEntry type="sourceFolder" forTests="false" />
|
|
11
|
-
</component>
|
|
12
|
-
</module>
|