melperjs 10.0.0 → 11.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 CHANGED
@@ -34,9 +34,18 @@ try {
34
34
  await helper.promiseTimeout(1000, helper.sleepMs(2000));
35
35
  } catch (e) {
36
36
  console.error(e.message);
37
- console.log("Timeout, Internal Error ?", helper.isIntlError(e));
37
+ console.log("Timeout, Internal Error ?", helper.isIntlHttpError(e));
38
38
  }
39
- helper.promiseSilent(helper.sleep(5))
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"));
40
49
  console.log(helper.splitClear(`
41
50
  2.satır
42
51
 
@@ -88,6 +97,7 @@ console.log(helper.objectStringify({
88
97
  f: [3, 4, 5],
89
98
  g: false
90
99
  }));
100
+ console.log(helper.modifyObjectKeys({"A": "B"}, (key) => key.toLowerCase()));
91
101
  console.log(helper.limitString("LONG TEXT", 7));
92
102
  console.log(helper.safeString("<strong>SAFE TEXT</strong>"));
93
103
  console.log(helper.shuffleString("ABC123"));
@@ -102,7 +112,7 @@ console.log(nodeHelper.tokenHex(8));
102
112
  console.log(nodeHelper.tokenUuid(true));
103
113
  console.log(nodeHelper.tokenWeighted({strongProbability: 1000, lowProbability: 1}));
104
114
  console.log(nodeHelper.tokenElement(["vA", "vB", "vC"]));
105
- console.log(nodeHelper.md5("data"));
115
+ console.log(nodeHelper.hash("md5", "data"));
106
116
  const password = nodeHelper.hashBcrypt("plain", "encryptionKey");
107
117
  console.log(password)
108
118
  console.log("passwordHash verified ?", nodeHelper.verifyBcrypt("plain", password, "encryptionKey"));
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;
@@ -100,6 +103,28 @@ function promiseTimeout(milliseconds, promise) {
100
103
  function promiseSilent(promise) {
101
104
  return promise.then(() => {}).catch(() => {});
102
105
  }
106
+ async function retryFn(fn, retries, errorFn = null) {
107
+ let result = null;
108
+ for (let attempt = 1; attempt <= retries; attempt++) {
109
+ try {
110
+ result = await fn();
111
+ return result;
112
+ } catch (error) {
113
+ errorFn?.(attempt, error, result);
114
+ if (attempt >= retries) {
115
+ throw error;
116
+ }
117
+ }
118
+ }
119
+ }
120
+ function isValidURL(url) {
121
+ try {
122
+ new URL(url);
123
+ return true;
124
+ } catch {
125
+ return false;
126
+ }
127
+ }
103
128
  function splitClear(rawText, separator = null) {
104
129
  separator = separator ?? /\r?\n/;
105
130
  return rawText.split(separator).map(item => item.trim()).filter(item => !(0, _isEmpty.default)(item));
@@ -169,6 +194,12 @@ function objectStringify(obj) {
169
194
  }
170
195
  return obj;
171
196
  }
197
+ function modifyObjectKeys(obj, callFn) {
198
+ return Object.keys(obj).reduce((acc, key) => {
199
+ acc[callFn(key)] = obj[key];
200
+ return acc;
201
+ }, {});
202
+ }
172
203
  function limitString(str, limit = 35, omission = "...") {
173
204
  str = str || "";
174
205
  if (str.length <= limit) {
@@ -289,8 +320,9 @@ function cookieHeader(cookieDict) {
289
320
  return Object.entries(cookieDict).map(([key, value]) => `${key}=${value}`).join(';');
290
321
  }
291
322
  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;
323
+ return httpCode === undefined || httpCode === null || isNaN(httpCode) || httpCode === 0 || httpCode === 100 || httpCode === 402 || httpCode === 407 || httpCode === 417 || 460 <= httpCode && httpCode < 470 || 500 <= httpCode;
293
324
  }
294
- function isIntlError(e) {
295
- return e?.message?.toLowerCase?.()?.includes?.("timeout") || e?.message?.toLowerCase?.()?.includes?.("aborted") || e?.message?.toLowerCase?.()?.includes?.("tls connection") || e?.message?.toLowerCase?.()?.includes?.("socket hang") || isIntlHttpCode(e?.response?.status);
325
+ function isIntlHttpError(e) {
326
+ const message = e?.message?.toLowerCase?.() || "";
327
+ 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
328
  }
package/lib/cjs/node.js CHANGED
@@ -7,6 +7,7 @@ exports.createNumDir = createNumDir;
7
7
  exports.executeCommand = executeCommand;
8
8
  exports.formatProxy = formatProxy;
9
9
  exports.getVersion = getVersion;
10
+ exports.hash = hash;
10
11
  exports.hashBcrypt = hashBcrypt;
11
12
  exports.md5 = md5;
12
13
  exports.proxyObject = proxyObject;
@@ -14,6 +15,7 @@ exports.proxyValue = proxyValue;
14
15
  exports.readJsonFile = readJsonFile;
15
16
  exports.readJsonFileSync = readJsonFileSync;
16
17
  exports.serverIp = serverIp;
18
+ exports.sha256 = sha256;
17
19
  exports.tokenElement = tokenElement;
18
20
  exports.tokenHex = tokenHex;
19
21
  exports.tokenString = tokenString;
@@ -118,8 +120,14 @@ function createNumDir(mainDirectory) {
118
120
  }
119
121
  }
120
122
  }
123
+ function hash(algorithm, data) {
124
+ return _crypto.default.createHash(algorithm).update(data).digest("hex");
125
+ }
121
126
  function md5(data) {
122
- return _crypto.default.createHash('md5').update(data).digest("hex");
127
+ return hash("md5", data);
128
+ }
129
+ function sha256(data) {
130
+ return hash("sha256", data);
123
131
  }
124
132
  function hashBcrypt(plainText, encryptionKey = "") {
125
133
  return _bcryptjs.default.hashSync(plainText + encryptionKey, _bcryptjs.default.genSaltSync(10));
package/lib/esm/index.js CHANGED
@@ -64,6 +64,28 @@ export function promiseTimeout(milliseconds, promise) {
64
64
  export function promiseSilent(promise) {
65
65
  return promise.then(() => {}).catch(() => {});
66
66
  }
67
+ export async function retryFn(fn, retries, errorFn = null) {
68
+ let result = null;
69
+ for (let attempt = 1; attempt <= retries; attempt++) {
70
+ try {
71
+ result = await fn();
72
+ return result;
73
+ } catch (error) {
74
+ errorFn?.(attempt, error, result);
75
+ if (attempt >= retries) {
76
+ throw error;
77
+ }
78
+ }
79
+ }
80
+ }
81
+ export function isValidURL(url) {
82
+ try {
83
+ new URL(url);
84
+ return true;
85
+ } catch {
86
+ return false;
87
+ }
88
+ }
67
89
  export function splitClear(rawText, separator = null) {
68
90
  separator = separator ?? /\r?\n/;
69
91
  return rawText.split(separator).map(item => item.trim()).filter(item => !isEmpty(item));
@@ -133,6 +155,12 @@ export function objectStringify(obj) {
133
155
  }
134
156
  return obj;
135
157
  }
158
+ export function modifyObjectKeys(obj, callFn) {
159
+ return Object.keys(obj).reduce((acc, key) => {
160
+ acc[callFn(key)] = obj[key];
161
+ return acc;
162
+ }, {});
163
+ }
136
164
  export function limitString(str, limit = 35, omission = "...") {
137
165
  str = str || "";
138
166
  if (str.length <= limit) {
@@ -253,8 +281,9 @@ export function cookieHeader(cookieDict) {
253
281
  return Object.entries(cookieDict).map(([key, value]) => `${key}=${value}`).join(';');
254
282
  }
255
283
  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;
284
+ return httpCode === undefined || httpCode === null || isNaN(httpCode) || httpCode === 0 || httpCode === 100 || httpCode === 402 || httpCode === 407 || httpCode === 417 || 460 <= httpCode && httpCode < 470 || 500 <= httpCode;
257
285
  }
258
- export function isIntlError(e) {
259
- return e?.message?.toLowerCase?.()?.includes?.("timeout") || e?.message?.toLowerCase?.()?.includes?.("aborted") || e?.message?.toLowerCase?.()?.includes?.("tls connection") || e?.message?.toLowerCase?.()?.includes?.("socket hang") || isIntlHttpCode(e?.response?.status);
286
+ export function isIntlHttpError(e) {
287
+ const message = e?.message?.toLowerCase?.() || "";
288
+ 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
289
  }
package/lib/esm/node.js CHANGED
@@ -92,8 +92,14 @@ export function createNumDir(mainDirectory) {
92
92
  }
93
93
  }
94
94
  }
95
+ export function hash(algorithm, data) {
96
+ return crypto.createHash(algorithm).update(data).digest("hex");
97
+ }
95
98
  export function md5(data) {
96
- return crypto.createHash('md5').update(data).digest("hex");
99
+ return hash("md5", data);
100
+ }
101
+ export function sha256(data) {
102
+ return hash("sha256", data);
97
103
  }
98
104
  export function hashBcrypt(plainText, encryptionKey = "") {
99
105
  return bcrypt.hashSync(plainText + encryptionKey, bcrypt.genSaltSync(10));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "melperjs",
3
- "version": "10.0.0",
3
+ "version": "11.0.0",
4
4
  "description": "Javascript module to use predefined common functions and utilities",
5
5
  "keywords": [
6
6
  "melperjs",