melperjs 11.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 -130
- package/lib/cjs/index.js +10 -11
- package/lib/cjs/node.js +37 -4
- package/lib/esm/index.js +10 -11
- package/lib/esm/node.js +34 -2
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -14,136 +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.isIntlHttpError(e));
|
|
38
|
-
}
|
|
39
|
-
const errorPronePromise = helper.retryFn(async () => {
|
|
40
|
-
console.log("Retry this function");
|
|
41
|
-
throw new Error("error")
|
|
42
|
-
}, 5, (attempt, error, result) => {
|
|
43
|
-
if (attempt % 2 === 0) {
|
|
44
|
-
console.error("Even attempt error");
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
helper.promiseSilent(errorPronePromise);
|
|
48
|
-
console.log("Is valid URL ?", helper.isValidURL("https://google.com"));
|
|
49
|
-
console.log(helper.splitClear(`
|
|
50
|
-
2.satır
|
|
51
|
-
|
|
52
|
-
4.satır
|
|
53
|
-
`))
|
|
54
|
-
console.log(helper.findKeyNode("c", {
|
|
55
|
-
a: {
|
|
56
|
-
b: {
|
|
57
|
-
x: 1,
|
|
58
|
-
y: 2,
|
|
59
|
-
c: {
|
|
60
|
-
d: true
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}));
|
|
65
|
-
console.log("'' empty ?", helper.checkEmpty(''));
|
|
66
|
-
console.log("1 empty ?", helper.checkEmpty(1));
|
|
67
|
-
console.log("0 empty ?", helper.checkEmpty(1));
|
|
68
|
-
console.log("[] empty ?", helper.checkEmpty([]));
|
|
69
|
-
console.log(helper.pascalCase("pascal case"));
|
|
70
|
-
console.log(helper.titleCase("THIS mUsT be Title"));
|
|
71
|
-
console.log(helper.parseNumFromObj({
|
|
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.parseIntFromObj({
|
|
81
|
-
a: "123",
|
|
82
|
-
b: 456,
|
|
83
|
-
c: "789.01",
|
|
84
|
-
d: "hello",
|
|
85
|
-
e: "0",
|
|
86
|
-
f: true,
|
|
87
|
-
g: "5000"
|
|
88
|
-
}))
|
|
89
|
-
console.log(helper.objectStringify({
|
|
90
|
-
a: "hello",
|
|
91
|
-
b: 1,
|
|
92
|
-
c: undefined,
|
|
93
|
-
d: null,
|
|
94
|
-
e: {
|
|
95
|
-
ea: 2
|
|
96
|
-
},
|
|
97
|
-
f: [3, 4, 5],
|
|
98
|
-
g: false
|
|
99
|
-
}));
|
|
100
|
-
console.log(helper.modifyObjectKeys({"A": "B"}, (key) => key.toLowerCase()));
|
|
101
|
-
console.log(helper.limitString("LONG TEXT", 7));
|
|
102
|
-
console.log(helper.safeString("<strong>SAFE TEXT</strong>"));
|
|
103
|
-
console.log(helper.shuffleString("ABC123"));
|
|
104
|
-
console.log(helper.randomString(32, true, true));
|
|
105
|
-
console.log(helper.randomHex(8));
|
|
106
|
-
console.log(helper.randomInteger(100, 1000));
|
|
107
|
-
console.log(helper.randomUuid(true));
|
|
108
|
-
console.log(helper.randomWeighted({strongProbability: 1000, lowProbability: 1}));
|
|
109
|
-
console.log(helper.randomElement({a: "vA", b: "vB", c: "vC"}));
|
|
110
|
-
console.log(nodeHelper.tokenString(32, true, true));
|
|
111
|
-
console.log(nodeHelper.tokenHex(8));
|
|
112
|
-
console.log(nodeHelper.tokenUuid(true));
|
|
113
|
-
console.log(nodeHelper.tokenWeighted({strongProbability: 1000, lowProbability: 1}));
|
|
114
|
-
console.log(nodeHelper.tokenElement(["vA", "vB", "vC"]));
|
|
115
|
-
console.log(nodeHelper.hash("md5", "data"));
|
|
116
|
-
const password = nodeHelper.hashBcrypt("plain", "encryptionKey");
|
|
117
|
-
console.log(password)
|
|
118
|
-
console.log("passwordHash verified ?", nodeHelper.verifyBcrypt("plain", password, "encryptionKey"));
|
|
119
|
-
console.log(await nodeHelper.executeCommand("python --version"));
|
|
120
|
-
console.log(helper.indexByTime(5));
|
|
121
|
-
const cookies = helper.cookieDict(await axios.get("https://google.com"));
|
|
122
|
-
console.log(cookies);
|
|
123
|
-
console.log(helper.cookieHeader(cookies));
|
|
124
|
-
const proxy = nodeHelper.formatProxy("127.0.0.1:8080:id:pw-{SESSION}");
|
|
125
|
-
console.log(proxy);
|
|
126
|
-
console.log(nodeHelper.proxyObject(proxy));
|
|
127
|
-
console.log(nodeHelper.proxyValue(proxy));
|
|
128
|
-
console.log(nodeHelper.serverIp());
|
|
129
|
-
console.log("HTTP CODE: 400 (Bad Request) ?", helper.isIntlHttpCode(401));
|
|
130
|
-
console.log("HTTP CODE: 407 (Failed Proxy Auth) ?", helper.isIntlHttpCode(407));
|
|
131
|
-
nodeHelper.createNumDir("test");
|
|
132
|
-
await nodeHelper.writeJsonFile("test.json", {test: "test json file"});
|
|
133
|
-
console.log(nodeHelper.readJsonFileSync("test.json"));
|
|
134
|
-
console.log("VERSIONED BY .GIT", "v" + nodeHelper.getVersion());
|
|
135
|
-
console.log(helper.Exception("something went wrong", {status: 400}, "bad request error"));
|
|
136
|
-
await helper.forever(1000, async () => {
|
|
137
|
-
await helper.sleep(1);
|
|
138
|
-
console.log("Do something!")
|
|
139
|
-
}, (e) => {
|
|
140
|
-
console.error("Error!", e);
|
|
141
|
-
return 5000
|
|
142
|
-
}, () => {
|
|
143
|
-
console.log("Run this process forever!")
|
|
144
|
-
}
|
|
145
|
-
);
|
|
146
|
-
```
|
|
17
|
+
## Documentation
|
|
18
|
+
The documentation is available at [docs folder](docs/index.md).
|
|
147
19
|
|
|
148
20
|
## License
|
|
149
21
|
|
package/lib/cjs/index.js
CHANGED
|
@@ -50,15 +50,13 @@ const CONSTANTS = exports.CONSTANTS = {
|
|
|
50
50
|
NUMBERS: "0123456789"
|
|
51
51
|
};
|
|
52
52
|
function Exception(message, response = {}, name = null) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
this.name = name ? pascalCase(name) : "Exception";
|
|
59
|
-
}
|
|
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;
|
|
60
58
|
}
|
|
61
|
-
return
|
|
59
|
+
return error;
|
|
62
60
|
}
|
|
63
61
|
async function forever(cooldown, onSuccess, onError = null, onCompleted = null) {
|
|
64
62
|
const checkCooldown = value => value && !isNaN(value) && value > 0;
|
|
@@ -152,15 +150,16 @@ function checkEmpty(value) {
|
|
|
152
150
|
function pascalCase(str) {
|
|
153
151
|
return (0, _upperFirst.default)((0, _camelCase.default)(str));
|
|
154
152
|
}
|
|
155
|
-
function titleCase(str) {
|
|
153
|
+
function titleCase(str, separator = " ") {
|
|
156
154
|
str = str || "";
|
|
157
|
-
|
|
155
|
+
const words = str.split(separator);
|
|
156
|
+
return words.map(word => (0, _upperFirst.default)(word)).join(separator);
|
|
158
157
|
}
|
|
159
158
|
function parseNumFromObj(obj) {
|
|
160
159
|
for (let key in obj) {
|
|
161
160
|
let value = obj[key];
|
|
162
161
|
let number = parseFloat(value);
|
|
163
|
-
if (typeof value === 'string' && !isNaN(number)) {
|
|
162
|
+
if (typeof value === 'string' && !isNaN(number) && !value.includes("_")) {
|
|
164
163
|
value = number;
|
|
165
164
|
}
|
|
166
165
|
obj[key] = value;
|
package/lib/cjs/node.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
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;
|
|
@@ -18,6 +19,7 @@ exports.serverIp = serverIp;
|
|
|
18
19
|
exports.sha256 = sha256;
|
|
19
20
|
exports.tokenElement = tokenElement;
|
|
20
21
|
exports.tokenHex = tokenHex;
|
|
22
|
+
exports.tokenInteger = tokenInteger;
|
|
21
23
|
exports.tokenString = tokenString;
|
|
22
24
|
exports.tokenUuid = tokenUuid;
|
|
23
25
|
exports.tokenWeighted = tokenWeighted;
|
|
@@ -32,8 +34,7 @@ var _child_process = require("child_process");
|
|
|
32
34
|
var _bcryptjs = _interopRequireDefault(require("bcryptjs"));
|
|
33
35
|
var _index = require("./index.js");
|
|
34
36
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
35
|
-
function
|
|
36
|
-
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); }
|
|
37
38
|
function tokenString(length, useNumbers = true, useUppercase = false) {
|
|
38
39
|
const lowercaseChars = _index.CONSTANTS.LOWER_CASE;
|
|
39
40
|
const uppercaseChars = _index.CONSTANTS.UPPER_CASE;
|
|
@@ -54,6 +55,9 @@ function tokenString(length, useNumbers = true, useUppercase = false) {
|
|
|
54
55
|
function tokenHex(length) {
|
|
55
56
|
return _crypto.default.randomBytes(Math.ceil(length / 2)).toString('hex').slice(0, length);
|
|
56
57
|
}
|
|
58
|
+
function tokenInteger(min, max) {
|
|
59
|
+
return _crypto.default.randomInt(min, max);
|
|
60
|
+
}
|
|
57
61
|
function tokenUuid(useDashes = true) {
|
|
58
62
|
const uuid = _crypto.default.randomUUID().toString();
|
|
59
63
|
return useDashes ? uuid : uuid.replaceAll("-", "");
|
|
@@ -106,11 +110,11 @@ function getVersion() {
|
|
|
106
110
|
return "1.0";
|
|
107
111
|
}
|
|
108
112
|
}
|
|
109
|
-
function createNumDir(mainDirectory) {
|
|
113
|
+
function createNumDir(mainDirectory, start = 0, end = 9) {
|
|
110
114
|
_fs.default.mkdirSync(mainDirectory, {
|
|
111
115
|
recursive: true
|
|
112
116
|
});
|
|
113
|
-
for (let i =
|
|
117
|
+
for (let i = start; i <= end; i++) {
|
|
114
118
|
try {
|
|
115
119
|
_fs.default.mkdirSync(_path.default.join(mainDirectory, i.toString()), {
|
|
116
120
|
recursive: true
|
|
@@ -210,4 +214,33 @@ async function writeJsonFile(filePath, data) {
|
|
|
210
214
|
function writeJsonFileSync(filePath, data) {
|
|
211
215
|
const jsonData = JSON.stringify(data);
|
|
212
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
|
+
}
|
|
213
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;
|
|
@@ -113,15 +111,16 @@ export function checkEmpty(value) {
|
|
|
113
111
|
export function pascalCase(str) {
|
|
114
112
|
return upperFirst(camelCase(str));
|
|
115
113
|
}
|
|
116
|
-
export function titleCase(str) {
|
|
114
|
+
export function titleCase(str, separator = " ") {
|
|
117
115
|
str = str || "";
|
|
118
|
-
|
|
116
|
+
const words = str.split(separator);
|
|
117
|
+
return words.map(word => upperFirst(word)).join(separator);
|
|
119
118
|
}
|
|
120
119
|
export function parseNumFromObj(obj) {
|
|
121
120
|
for (let key in obj) {
|
|
122
121
|
let value = obj[key];
|
|
123
122
|
let number = parseFloat(value);
|
|
124
|
-
if (typeof value === 'string' && !isNaN(number)) {
|
|
123
|
+
if (typeof value === 'string' && !isNaN(number) && !value.includes("_")) {
|
|
125
124
|
value = number;
|
|
126
125
|
}
|
|
127
126
|
obj[key] = value;
|
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
|
|
@@ -182,4 +185,33 @@ export async function writeJsonFile(filePath, data) {
|
|
|
182
185
|
export function writeJsonFileSync(filePath, data) {
|
|
183
186
|
const jsonData = JSON.stringify(data);
|
|
184
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
|
+
}
|
|
185
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
|
}
|