melperjs 10.0.0 → 12.0.0
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/README.md +2 -120
- package/lib/cjs/index.js +46 -15
- package/lib/cjs/node.js +46 -5
- package/lib/esm/index.js +42 -14
- package/lib/esm/node.js +41 -3
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -14,126 +14,8 @@ To install use npm:
|
|
|
14
14
|
npm i melperjs
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
##
|
|
18
|
-
|
|
19
|
-
```javascript
|
|
20
|
-
// const helper = require("melperjs");
|
|
21
|
-
// const nodeHelper = require("melperjs/node");
|
|
22
|
-
// const axios = require("axios");
|
|
23
|
-
import * as helper from "melperjs";
|
|
24
|
-
import * as nodeHelper from "melperjs/node";
|
|
25
|
-
import axios from "axios";
|
|
26
|
-
|
|
27
|
-
console.log(helper.CONSTANTS);
|
|
28
|
-
console.log(helper.time());
|
|
29
|
-
await helper.sleepMs(1000);
|
|
30
|
-
console.log(helper.time());
|
|
31
|
-
await helper.sleep(1);
|
|
32
|
-
console.log(helper.time());
|
|
33
|
-
try {
|
|
34
|
-
await helper.promiseTimeout(1000, helper.sleepMs(2000));
|
|
35
|
-
} catch (e) {
|
|
36
|
-
console.error(e.message);
|
|
37
|
-
console.log("Timeout, Internal Error ?", helper.isIntlError(e));
|
|
38
|
-
}
|
|
39
|
-
helper.promiseSilent(helper.sleep(5))
|
|
40
|
-
console.log(helper.splitClear(`
|
|
41
|
-
2.satır
|
|
42
|
-
|
|
43
|
-
4.satır
|
|
44
|
-
`))
|
|
45
|
-
console.log(helper.findKeyNode("c", {
|
|
46
|
-
a: {
|
|
47
|
-
b: {
|
|
48
|
-
x: 1,
|
|
49
|
-
y: 2,
|
|
50
|
-
c: {
|
|
51
|
-
d: true
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}));
|
|
56
|
-
console.log("'' empty ?", helper.checkEmpty(''));
|
|
57
|
-
console.log("1 empty ?", helper.checkEmpty(1));
|
|
58
|
-
console.log("0 empty ?", helper.checkEmpty(1));
|
|
59
|
-
console.log("[] empty ?", helper.checkEmpty([]));
|
|
60
|
-
console.log(helper.pascalCase("pascal case"));
|
|
61
|
-
console.log(helper.titleCase("THIS mUsT be Title"));
|
|
62
|
-
console.log(helper.parseNumFromObj({
|
|
63
|
-
a: "123",
|
|
64
|
-
b: 456,
|
|
65
|
-
c: "789.01",
|
|
66
|
-
d: "hello",
|
|
67
|
-
e: "0",
|
|
68
|
-
f: true,
|
|
69
|
-
g: "5000"
|
|
70
|
-
}));
|
|
71
|
-
console.log(helper.parseIntFromObj({
|
|
72
|
-
a: "123",
|
|
73
|
-
b: 456,
|
|
74
|
-
c: "789.01",
|
|
75
|
-
d: "hello",
|
|
76
|
-
e: "0",
|
|
77
|
-
f: true,
|
|
78
|
-
g: "5000"
|
|
79
|
-
}))
|
|
80
|
-
console.log(helper.objectStringify({
|
|
81
|
-
a: "hello",
|
|
82
|
-
b: 1,
|
|
83
|
-
c: undefined,
|
|
84
|
-
d: null,
|
|
85
|
-
e: {
|
|
86
|
-
ea: 2
|
|
87
|
-
},
|
|
88
|
-
f: [3, 4, 5],
|
|
89
|
-
g: false
|
|
90
|
-
}));
|
|
91
|
-
console.log(helper.limitString("LONG TEXT", 7));
|
|
92
|
-
console.log(helper.safeString("<strong>SAFE TEXT</strong>"));
|
|
93
|
-
console.log(helper.shuffleString("ABC123"));
|
|
94
|
-
console.log(helper.randomString(32, true, true));
|
|
95
|
-
console.log(helper.randomHex(8));
|
|
96
|
-
console.log(helper.randomInteger(100, 1000));
|
|
97
|
-
console.log(helper.randomUuid(true));
|
|
98
|
-
console.log(helper.randomWeighted({strongProbability: 1000, lowProbability: 1}));
|
|
99
|
-
console.log(helper.randomElement({a: "vA", b: "vB", c: "vC"}));
|
|
100
|
-
console.log(nodeHelper.tokenString(32, true, true));
|
|
101
|
-
console.log(nodeHelper.tokenHex(8));
|
|
102
|
-
console.log(nodeHelper.tokenUuid(true));
|
|
103
|
-
console.log(nodeHelper.tokenWeighted({strongProbability: 1000, lowProbability: 1}));
|
|
104
|
-
console.log(nodeHelper.tokenElement(["vA", "vB", "vC"]));
|
|
105
|
-
console.log(nodeHelper.md5("data"));
|
|
106
|
-
const password = nodeHelper.hashBcrypt("plain", "encryptionKey");
|
|
107
|
-
console.log(password)
|
|
108
|
-
console.log("passwordHash verified ?", nodeHelper.verifyBcrypt("plain", password, "encryptionKey"));
|
|
109
|
-
console.log(await nodeHelper.executeCommand("python --version"));
|
|
110
|
-
console.log(helper.indexByTime(5));
|
|
111
|
-
const cookies = helper.cookieDict(await axios.get("https://google.com"));
|
|
112
|
-
console.log(cookies);
|
|
113
|
-
console.log(helper.cookieHeader(cookies));
|
|
114
|
-
const proxy = nodeHelper.formatProxy("127.0.0.1:8080:id:pw-{SESSION}");
|
|
115
|
-
console.log(proxy);
|
|
116
|
-
console.log(nodeHelper.proxyObject(proxy));
|
|
117
|
-
console.log(nodeHelper.proxyValue(proxy));
|
|
118
|
-
console.log(nodeHelper.serverIp());
|
|
119
|
-
console.log("HTTP CODE: 400 (Bad Request) ?", helper.isIntlHttpCode(401));
|
|
120
|
-
console.log("HTTP CODE: 407 (Failed Proxy Auth) ?", helper.isIntlHttpCode(407));
|
|
121
|
-
nodeHelper.createNumDir("test");
|
|
122
|
-
await nodeHelper.writeJsonFile("test.json", {test: "test json file"});
|
|
123
|
-
console.log(nodeHelper.readJsonFileSync("test.json"));
|
|
124
|
-
console.log("VERSIONED BY .GIT", "v" + nodeHelper.getVersion());
|
|
125
|
-
console.log(helper.Exception("something went wrong", {status: 400}, "bad request error"));
|
|
126
|
-
await helper.forever(1000, async () => {
|
|
127
|
-
await helper.sleep(1);
|
|
128
|
-
console.log("Do something!")
|
|
129
|
-
}, (e) => {
|
|
130
|
-
console.error("Error!", e);
|
|
131
|
-
return 5000
|
|
132
|
-
}, () => {
|
|
133
|
-
console.log("Run this process forever!")
|
|
134
|
-
}
|
|
135
|
-
);
|
|
136
|
-
```
|
|
17
|
+
## Documentation
|
|
18
|
+
The documentation is available at [docs folder](docs/index.md).
|
|
137
19
|
|
|
138
20
|
## License
|
|
139
21
|
|
package/lib/cjs/index.js
CHANGED
|
@@ -11,9 +11,11 @@ exports.cookieHeader = cookieHeader;
|
|
|
11
11
|
exports.findKeyNode = findKeyNode;
|
|
12
12
|
exports.forever = forever;
|
|
13
13
|
exports.indexByTime = indexByTime;
|
|
14
|
-
exports.isIntlError = isIntlError;
|
|
15
14
|
exports.isIntlHttpCode = isIntlHttpCode;
|
|
15
|
+
exports.isIntlHttpError = isIntlHttpError;
|
|
16
|
+
exports.isValidURL = isValidURL;
|
|
16
17
|
exports.limitString = limitString;
|
|
18
|
+
exports.modifyObjectKeys = modifyObjectKeys;
|
|
17
19
|
exports.objectStringify = objectStringify;
|
|
18
20
|
exports.parseIntFromObj = parseIntFromObj;
|
|
19
21
|
exports.parseNumFromObj = parseNumFromObj;
|
|
@@ -26,6 +28,7 @@ exports.randomInteger = randomInteger;
|
|
|
26
28
|
exports.randomString = randomString;
|
|
27
29
|
exports.randomUuid = randomUuid;
|
|
28
30
|
exports.randomWeighted = randomWeighted;
|
|
31
|
+
exports.retryFn = retryFn;
|
|
29
32
|
exports.safeString = safeString;
|
|
30
33
|
exports.shuffleString = shuffleString;
|
|
31
34
|
exports.sleep = sleep;
|
|
@@ -47,15 +50,13 @@ const CONSTANTS = exports.CONSTANTS = {
|
|
|
47
50
|
NUMBERS: "0123456789"
|
|
48
51
|
};
|
|
49
52
|
function Exception(message, response = {}, name = null) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
this.name = name ? pascalCase(name) : "Exception";
|
|
56
|
-
}
|
|
53
|
+
const error = new Error(message);
|
|
54
|
+
error.name = name || "Exception";
|
|
55
|
+
error.response = response;
|
|
56
|
+
if (!error.response?.status && typeof error.response === "object") {
|
|
57
|
+
error.response.status = 400;
|
|
57
58
|
}
|
|
58
|
-
return
|
|
59
|
+
return error;
|
|
59
60
|
}
|
|
60
61
|
async function forever(cooldown, onSuccess, onError = null, onCompleted = null) {
|
|
61
62
|
const checkCooldown = value => value && !isNaN(value) && value > 0;
|
|
@@ -100,6 +101,28 @@ function promiseTimeout(milliseconds, promise) {
|
|
|
100
101
|
function promiseSilent(promise) {
|
|
101
102
|
return promise.then(() => {}).catch(() => {});
|
|
102
103
|
}
|
|
104
|
+
async function retryFn(fn, retries, errorFn = null) {
|
|
105
|
+
let result = null;
|
|
106
|
+
for (let attempt = 1; attempt <= retries; attempt++) {
|
|
107
|
+
try {
|
|
108
|
+
result = await fn();
|
|
109
|
+
return result;
|
|
110
|
+
} catch (error) {
|
|
111
|
+
errorFn?.(attempt, error, result);
|
|
112
|
+
if (attempt >= retries) {
|
|
113
|
+
throw error;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function isValidURL(url) {
|
|
119
|
+
try {
|
|
120
|
+
new URL(url);
|
|
121
|
+
return true;
|
|
122
|
+
} catch {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
103
126
|
function splitClear(rawText, separator = null) {
|
|
104
127
|
separator = separator ?? /\r?\n/;
|
|
105
128
|
return rawText.split(separator).map(item => item.trim()).filter(item => !(0, _isEmpty.default)(item));
|
|
@@ -127,15 +150,16 @@ function checkEmpty(value) {
|
|
|
127
150
|
function pascalCase(str) {
|
|
128
151
|
return (0, _upperFirst.default)((0, _camelCase.default)(str));
|
|
129
152
|
}
|
|
130
|
-
function titleCase(str) {
|
|
153
|
+
function titleCase(str, separator = " ") {
|
|
131
154
|
str = str || "";
|
|
132
|
-
|
|
155
|
+
const words = str.split(separator);
|
|
156
|
+
return words.map(word => (0, _upperFirst.default)(word)).join(separator);
|
|
133
157
|
}
|
|
134
158
|
function parseNumFromObj(obj) {
|
|
135
159
|
for (let key in obj) {
|
|
136
160
|
let value = obj[key];
|
|
137
161
|
let number = parseFloat(value);
|
|
138
|
-
if (typeof value === 'string' && !isNaN(number)) {
|
|
162
|
+
if (typeof value === 'string' && !isNaN(number) && !value.includes("_")) {
|
|
139
163
|
value = number;
|
|
140
164
|
}
|
|
141
165
|
obj[key] = value;
|
|
@@ -169,6 +193,12 @@ function objectStringify(obj) {
|
|
|
169
193
|
}
|
|
170
194
|
return obj;
|
|
171
195
|
}
|
|
196
|
+
function modifyObjectKeys(obj, callFn) {
|
|
197
|
+
return Object.keys(obj).reduce((acc, key) => {
|
|
198
|
+
acc[callFn(key)] = obj[key];
|
|
199
|
+
return acc;
|
|
200
|
+
}, {});
|
|
201
|
+
}
|
|
172
202
|
function limitString(str, limit = 35, omission = "...") {
|
|
173
203
|
str = str || "";
|
|
174
204
|
if (str.length <= limit) {
|
|
@@ -289,8 +319,9 @@ function cookieHeader(cookieDict) {
|
|
|
289
319
|
return Object.entries(cookieDict).map(([key, value]) => `${key}=${value}`).join(';');
|
|
290
320
|
}
|
|
291
321
|
function isIntlHttpCode(httpCode) {
|
|
292
|
-
return httpCode === undefined || httpCode === null || isNaN(httpCode) || httpCode === 0 || httpCode === 100 || httpCode === 402 || httpCode === 407 || 460 <= httpCode && httpCode < 470 || 500 <= httpCode;
|
|
322
|
+
return httpCode === undefined || httpCode === null || isNaN(httpCode) || httpCode === 0 || httpCode === 100 || httpCode === 402 || httpCode === 407 || httpCode === 417 || 460 <= httpCode && httpCode < 470 || 500 <= httpCode;
|
|
293
323
|
}
|
|
294
|
-
function
|
|
295
|
-
|
|
324
|
+
function isIntlHttpError(e) {
|
|
325
|
+
const message = e?.message?.toLowerCase?.() || "";
|
|
326
|
+
return message.includes("timeout") || message.includes("aborted") || message.includes("socket hang") || message.includes("proxy") || message.includes("tls connection") || message.includes("payment") || message.includes("expectation") || isIntlHttpCode(e?.response?.status);
|
|
296
327
|
}
|
package/lib/cjs/node.js
CHANGED
|
@@ -3,10 +3,12 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
exports.cleanDirectory = cleanDirectory;
|
|
6
7
|
exports.createNumDir = createNumDir;
|
|
7
8
|
exports.executeCommand = executeCommand;
|
|
8
9
|
exports.formatProxy = formatProxy;
|
|
9
10
|
exports.getVersion = getVersion;
|
|
11
|
+
exports.hash = hash;
|
|
10
12
|
exports.hashBcrypt = hashBcrypt;
|
|
11
13
|
exports.md5 = md5;
|
|
12
14
|
exports.proxyObject = proxyObject;
|
|
@@ -14,8 +16,10 @@ exports.proxyValue = proxyValue;
|
|
|
14
16
|
exports.readJsonFile = readJsonFile;
|
|
15
17
|
exports.readJsonFileSync = readJsonFileSync;
|
|
16
18
|
exports.serverIp = serverIp;
|
|
19
|
+
exports.sha256 = sha256;
|
|
17
20
|
exports.tokenElement = tokenElement;
|
|
18
21
|
exports.tokenHex = tokenHex;
|
|
22
|
+
exports.tokenInteger = tokenInteger;
|
|
19
23
|
exports.tokenString = tokenString;
|
|
20
24
|
exports.tokenUuid = tokenUuid;
|
|
21
25
|
exports.tokenWeighted = tokenWeighted;
|
|
@@ -30,8 +34,7 @@ var _child_process = require("child_process");
|
|
|
30
34
|
var _bcryptjs = _interopRequireDefault(require("bcryptjs"));
|
|
31
35
|
var _index = require("./index.js");
|
|
32
36
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
33
|
-
function
|
|
34
|
-
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
37
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
|
|
35
38
|
function tokenString(length, useNumbers = true, useUppercase = false) {
|
|
36
39
|
const lowercaseChars = _index.CONSTANTS.LOWER_CASE;
|
|
37
40
|
const uppercaseChars = _index.CONSTANTS.UPPER_CASE;
|
|
@@ -52,6 +55,9 @@ function tokenString(length, useNumbers = true, useUppercase = false) {
|
|
|
52
55
|
function tokenHex(length) {
|
|
53
56
|
return _crypto.default.randomBytes(Math.ceil(length / 2)).toString('hex').slice(0, length);
|
|
54
57
|
}
|
|
58
|
+
function tokenInteger(min, max) {
|
|
59
|
+
return _crypto.default.randomInt(min, max);
|
|
60
|
+
}
|
|
55
61
|
function tokenUuid(useDashes = true) {
|
|
56
62
|
const uuid = _crypto.default.randomUUID().toString();
|
|
57
63
|
return useDashes ? uuid : uuid.replaceAll("-", "");
|
|
@@ -104,11 +110,11 @@ function getVersion() {
|
|
|
104
110
|
return "1.0";
|
|
105
111
|
}
|
|
106
112
|
}
|
|
107
|
-
function createNumDir(mainDirectory) {
|
|
113
|
+
function createNumDir(mainDirectory, start = 0, end = 9) {
|
|
108
114
|
_fs.default.mkdirSync(mainDirectory, {
|
|
109
115
|
recursive: true
|
|
110
116
|
});
|
|
111
|
-
for (let i =
|
|
117
|
+
for (let i = start; i <= end; i++) {
|
|
112
118
|
try {
|
|
113
119
|
_fs.default.mkdirSync(_path.default.join(mainDirectory, i.toString()), {
|
|
114
120
|
recursive: true
|
|
@@ -118,8 +124,14 @@ function createNumDir(mainDirectory) {
|
|
|
118
124
|
}
|
|
119
125
|
}
|
|
120
126
|
}
|
|
127
|
+
function hash(algorithm, data) {
|
|
128
|
+
return _crypto.default.createHash(algorithm).update(data).digest("hex");
|
|
129
|
+
}
|
|
121
130
|
function md5(data) {
|
|
122
|
-
return
|
|
131
|
+
return hash("md5", data);
|
|
132
|
+
}
|
|
133
|
+
function sha256(data) {
|
|
134
|
+
return hash("sha256", data);
|
|
123
135
|
}
|
|
124
136
|
function hashBcrypt(plainText, encryptionKey = "") {
|
|
125
137
|
return _bcryptjs.default.hashSync(plainText + encryptionKey, _bcryptjs.default.genSaltSync(10));
|
|
@@ -202,4 +214,33 @@ async function writeJsonFile(filePath, data) {
|
|
|
202
214
|
function writeJsonFileSync(filePath, data) {
|
|
203
215
|
const jsonData = JSON.stringify(data);
|
|
204
216
|
return _fs.default.writeFileSync(filePath, jsonData, 'utf8');
|
|
217
|
+
}
|
|
218
|
+
async function cleanDirectory(directoryPath, keepDir = true) {
|
|
219
|
+
try {
|
|
220
|
+
const stats = await _fs.promises.stat(directoryPath).catch(() => null);
|
|
221
|
+
if (!stats) {
|
|
222
|
+
if (keepDir) {
|
|
223
|
+
await _fs.promises.mkdir(directoryPath, {
|
|
224
|
+
recursive: true
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
const files = await _fs.promises.readdir(directoryPath);
|
|
230
|
+
for (const file of files) {
|
|
231
|
+
const filePath = _path.default.join(directoryPath, file);
|
|
232
|
+
const fileStat = await _fs.promises.stat(filePath);
|
|
233
|
+
if (fileStat.isDirectory()) {
|
|
234
|
+
await cleanDirectory(filePath, false);
|
|
235
|
+
} else {
|
|
236
|
+
await _fs.promises.unlink(filePath);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
if (!keepDir) {
|
|
240
|
+
await _fs.promises.rmdir(directoryPath);
|
|
241
|
+
}
|
|
242
|
+
return true;
|
|
243
|
+
} catch (error) {
|
|
244
|
+
throw error;
|
|
245
|
+
}
|
|
205
246
|
}
|
package/lib/esm/index.js
CHANGED
|
@@ -11,15 +11,13 @@ export const CONSTANTS = {
|
|
|
11
11
|
NUMBERS: "0123456789"
|
|
12
12
|
};
|
|
13
13
|
export function Exception(message, response = {}, name = null) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
this.name = name ? pascalCase(name) : "Exception";
|
|
20
|
-
}
|
|
14
|
+
const error = new Error(message);
|
|
15
|
+
error.name = name || "Exception";
|
|
16
|
+
error.response = response;
|
|
17
|
+
if (!error.response?.status && typeof error.response === "object") {
|
|
18
|
+
error.response.status = 400;
|
|
21
19
|
}
|
|
22
|
-
return
|
|
20
|
+
return error;
|
|
23
21
|
}
|
|
24
22
|
export async function forever(cooldown, onSuccess, onError = null, onCompleted = null) {
|
|
25
23
|
const checkCooldown = value => value && !isNaN(value) && value > 0;
|
|
@@ -64,6 +62,28 @@ export function promiseTimeout(milliseconds, promise) {
|
|
|
64
62
|
export function promiseSilent(promise) {
|
|
65
63
|
return promise.then(() => {}).catch(() => {});
|
|
66
64
|
}
|
|
65
|
+
export async function retryFn(fn, retries, errorFn = null) {
|
|
66
|
+
let result = null;
|
|
67
|
+
for (let attempt = 1; attempt <= retries; attempt++) {
|
|
68
|
+
try {
|
|
69
|
+
result = await fn();
|
|
70
|
+
return result;
|
|
71
|
+
} catch (error) {
|
|
72
|
+
errorFn?.(attempt, error, result);
|
|
73
|
+
if (attempt >= retries) {
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
export function isValidURL(url) {
|
|
80
|
+
try {
|
|
81
|
+
new URL(url);
|
|
82
|
+
return true;
|
|
83
|
+
} catch {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
67
87
|
export function splitClear(rawText, separator = null) {
|
|
68
88
|
separator = separator ?? /\r?\n/;
|
|
69
89
|
return rawText.split(separator).map(item => item.trim()).filter(item => !isEmpty(item));
|
|
@@ -91,15 +111,16 @@ export function checkEmpty(value) {
|
|
|
91
111
|
export function pascalCase(str) {
|
|
92
112
|
return upperFirst(camelCase(str));
|
|
93
113
|
}
|
|
94
|
-
export function titleCase(str) {
|
|
114
|
+
export function titleCase(str, separator = " ") {
|
|
95
115
|
str = str || "";
|
|
96
|
-
|
|
116
|
+
const words = str.split(separator);
|
|
117
|
+
return words.map(word => upperFirst(word)).join(separator);
|
|
97
118
|
}
|
|
98
119
|
export function parseNumFromObj(obj) {
|
|
99
120
|
for (let key in obj) {
|
|
100
121
|
let value = obj[key];
|
|
101
122
|
let number = parseFloat(value);
|
|
102
|
-
if (typeof value === 'string' && !isNaN(number)) {
|
|
123
|
+
if (typeof value === 'string' && !isNaN(number) && !value.includes("_")) {
|
|
103
124
|
value = number;
|
|
104
125
|
}
|
|
105
126
|
obj[key] = value;
|
|
@@ -133,6 +154,12 @@ export function objectStringify(obj) {
|
|
|
133
154
|
}
|
|
134
155
|
return obj;
|
|
135
156
|
}
|
|
157
|
+
export function modifyObjectKeys(obj, callFn) {
|
|
158
|
+
return Object.keys(obj).reduce((acc, key) => {
|
|
159
|
+
acc[callFn(key)] = obj[key];
|
|
160
|
+
return acc;
|
|
161
|
+
}, {});
|
|
162
|
+
}
|
|
136
163
|
export function limitString(str, limit = 35, omission = "...") {
|
|
137
164
|
str = str || "";
|
|
138
165
|
if (str.length <= limit) {
|
|
@@ -253,8 +280,9 @@ export function cookieHeader(cookieDict) {
|
|
|
253
280
|
return Object.entries(cookieDict).map(([key, value]) => `${key}=${value}`).join(';');
|
|
254
281
|
}
|
|
255
282
|
export function isIntlHttpCode(httpCode) {
|
|
256
|
-
return httpCode === undefined || httpCode === null || isNaN(httpCode) || httpCode === 0 || httpCode === 100 || httpCode === 402 || httpCode === 407 || 460 <= httpCode && httpCode < 470 || 500 <= httpCode;
|
|
283
|
+
return httpCode === undefined || httpCode === null || isNaN(httpCode) || httpCode === 0 || httpCode === 100 || httpCode === 402 || httpCode === 407 || httpCode === 417 || 460 <= httpCode && httpCode < 470 || 500 <= httpCode;
|
|
257
284
|
}
|
|
258
|
-
export function
|
|
259
|
-
|
|
285
|
+
export function isIntlHttpError(e) {
|
|
286
|
+
const message = e?.message?.toLowerCase?.() || "";
|
|
287
|
+
return message.includes("timeout") || message.includes("aborted") || message.includes("socket hang") || message.includes("proxy") || message.includes("tls connection") || message.includes("payment") || message.includes("expectation") || isIntlHttpCode(e?.response?.status);
|
|
260
288
|
}
|
package/lib/esm/node.js
CHANGED
|
@@ -26,6 +26,9 @@ export function tokenString(length, useNumbers = true, useUppercase = false) {
|
|
|
26
26
|
export function tokenHex(length) {
|
|
27
27
|
return crypto.randomBytes(Math.ceil(length / 2)).toString('hex').slice(0, length);
|
|
28
28
|
}
|
|
29
|
+
export function tokenInteger(min, max) {
|
|
30
|
+
return crypto.randomInt(min, max);
|
|
31
|
+
}
|
|
29
32
|
export function tokenUuid(useDashes = true) {
|
|
30
33
|
const uuid = crypto.randomUUID().toString();
|
|
31
34
|
return useDashes ? uuid : uuid.replaceAll("-", "");
|
|
@@ -78,11 +81,11 @@ export function getVersion() {
|
|
|
78
81
|
return "1.0";
|
|
79
82
|
}
|
|
80
83
|
}
|
|
81
|
-
export function createNumDir(mainDirectory) {
|
|
84
|
+
export function createNumDir(mainDirectory, start = 0, end = 9) {
|
|
82
85
|
fs.mkdirSync(mainDirectory, {
|
|
83
86
|
recursive: true
|
|
84
87
|
});
|
|
85
|
-
for (let i =
|
|
88
|
+
for (let i = start; i <= end; i++) {
|
|
86
89
|
try {
|
|
87
90
|
fs.mkdirSync(path.join(mainDirectory, i.toString()), {
|
|
88
91
|
recursive: true
|
|
@@ -92,8 +95,14 @@ export function createNumDir(mainDirectory) {
|
|
|
92
95
|
}
|
|
93
96
|
}
|
|
94
97
|
}
|
|
98
|
+
export function hash(algorithm, data) {
|
|
99
|
+
return crypto.createHash(algorithm).update(data).digest("hex");
|
|
100
|
+
}
|
|
95
101
|
export function md5(data) {
|
|
96
|
-
return
|
|
102
|
+
return hash("md5", data);
|
|
103
|
+
}
|
|
104
|
+
export function sha256(data) {
|
|
105
|
+
return hash("sha256", data);
|
|
97
106
|
}
|
|
98
107
|
export function hashBcrypt(plainText, encryptionKey = "") {
|
|
99
108
|
return bcrypt.hashSync(plainText + encryptionKey, bcrypt.genSaltSync(10));
|
|
@@ -176,4 +185,33 @@ export async function writeJsonFile(filePath, data) {
|
|
|
176
185
|
export function writeJsonFileSync(filePath, data) {
|
|
177
186
|
const jsonData = JSON.stringify(data);
|
|
178
187
|
return fs.writeFileSync(filePath, jsonData, 'utf8');
|
|
188
|
+
}
|
|
189
|
+
export async function cleanDirectory(directoryPath, keepDir = true) {
|
|
190
|
+
try {
|
|
191
|
+
const stats = await fsp.stat(directoryPath).catch(() => null);
|
|
192
|
+
if (!stats) {
|
|
193
|
+
if (keepDir) {
|
|
194
|
+
await fsp.mkdir(directoryPath, {
|
|
195
|
+
recursive: true
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
const files = await fsp.readdir(directoryPath);
|
|
201
|
+
for (const file of files) {
|
|
202
|
+
const filePath = path.join(directoryPath, file);
|
|
203
|
+
const fileStat = await fsp.stat(filePath);
|
|
204
|
+
if (fileStat.isDirectory()) {
|
|
205
|
+
await cleanDirectory(filePath, false);
|
|
206
|
+
} else {
|
|
207
|
+
await fsp.unlink(filePath);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
if (!keepDir) {
|
|
211
|
+
await fsp.rmdir(directoryPath);
|
|
212
|
+
}
|
|
213
|
+
return true;
|
|
214
|
+
} catch (error) {
|
|
215
|
+
throw error;
|
|
216
|
+
}
|
|
179
217
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "melperjs",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "12.0.0",
|
|
4
4
|
"description": "Javascript module to use predefined common functions and utilities",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"melperjs",
|
|
@@ -39,17 +39,17 @@
|
|
|
39
39
|
"test": "node test/script.js"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"bcryptjs": "^
|
|
42
|
+
"bcryptjs": "^3.0.2",
|
|
43
43
|
"hpagent": "^1.2.0",
|
|
44
44
|
"lodash": "^4.17.21",
|
|
45
|
-
"set-cookie-parser": "^2.
|
|
45
|
+
"set-cookie-parser": "^2.7.1",
|
|
46
46
|
"xss": "^1.0.15"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"@babel/cli": "^7.
|
|
50
|
-
"@babel/core": "^7.
|
|
51
|
-
"@babel/preset-env": "^7.
|
|
52
|
-
"axios": "^1.
|
|
49
|
+
"@babel/cli": "^7.27.2",
|
|
50
|
+
"@babel/core": "^7.27.1",
|
|
51
|
+
"@babel/preset-env": "^7.27.2",
|
|
52
|
+
"axios": "^1.9.0",
|
|
53
53
|
"cross-env": "^7.0.3"
|
|
54
54
|
}
|
|
55
55
|
}
|