melperjs 13.0.0 → 14.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/lib/cjs/index.js CHANGED
@@ -11,7 +11,9 @@ exports.cookieHeader = cookieHeader;
11
11
  exports.findKeyNode = findKeyNode;
12
12
  exports.flipObject = flipObject;
13
13
  exports.forever = forever;
14
+ exports.getResponseError = getResponseError;
14
15
  exports.indexByTime = indexByTime;
16
+ exports.isInt32 = isInt32;
15
17
  exports.isIntlHttpCode = isIntlHttpCode;
16
18
  exports.isIntlHttpError = isIntlHttpError;
17
19
  exports.isValidURL = isValidURL;
@@ -31,6 +33,7 @@ exports.randomUuid = randomUuid;
31
33
  exports.randomWeighted = randomWeighted;
32
34
  exports.retryFn = retryFn;
33
35
  exports.safeString = safeString;
36
+ exports.shuffleObject = shuffleObject;
34
37
  exports.shuffleString = shuffleString;
35
38
  exports.sleep = sleep;
36
39
  exports.sleepMs = sleepMs;
@@ -57,7 +60,10 @@ function Exception(message, response = {}, name = null) {
57
60
  const error = new Error(message);
58
61
  error.name = name || "Exception";
59
62
  error.response = response;
60
- if (!error.response?.status && typeof error.response === "object") {
63
+ if (checkEmpty(response)) {
64
+ error.response = {};
65
+ }
66
+ if (!error.response?.status) {
61
67
  error.response.status = 400;
62
68
  }
63
69
  return error;
@@ -184,9 +190,6 @@ function findKeyNode(key, node, pair = null) {
184
190
  }
185
191
  return null;
186
192
  }
187
- function flipObject(obj) {
188
- return Object.fromEntries(Object.entries(obj).map(([key, value]) => [value, key]));
189
- }
190
193
  function waitForProperty(obj, propertyName, timeout = 5000, interval = 100) {
191
194
  return new Promise((resolve, reject) => {
192
195
  const startTime = Date.now();
@@ -201,6 +204,17 @@ function waitForProperty(obj, propertyName, timeout = 5000, interval = 100) {
201
204
  }, interval);
202
205
  });
203
206
  }
207
+ function flipObject(obj) {
208
+ return Object.fromEntries(Object.entries(obj).map(([key, value]) => [value, key]));
209
+ }
210
+ function shuffleObject(obj) {
211
+ const arr = Object.entries(obj);
212
+ for (let i = arr.length - 1; i > 0; i--) {
213
+ const j = Math.floor(Math.random() * (i + 1));
214
+ [arr[i], arr[j]] = [arr[j], arr[i]];
215
+ }
216
+ return Object.fromEntries(arr);
217
+ }
204
218
  function objectStringify(obj) {
205
219
  for (let key in obj) {
206
220
  if (obj.hasOwnProperty(key)) {
@@ -348,4 +362,13 @@ function isIntlHttpCode(httpCode) {
348
362
  function isIntlHttpError(e) {
349
363
  const message = e?.message?.toLowerCase?.() || "";
350
364
  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);
365
+ }
366
+ function getResponseError(e, limit = 115) {
367
+ let response;
368
+ if (e?.response?.status && e.response.data) {
369
+ response = `${e.response.status}|${e.response.data}`;
370
+ } else if (e?.response?.data) {
371
+ response = e.response.data;
372
+ }
373
+ return limitString(response || e.message, limit).trim();
351
374
  }
package/lib/esm/index.js CHANGED
@@ -16,7 +16,10 @@ export function Exception(message, response = {}, name = null) {
16
16
  const error = new Error(message);
17
17
  error.name = name || "Exception";
18
18
  error.response = response;
19
- if (!error.response?.status && typeof error.response === "object") {
19
+ if (checkEmpty(response)) {
20
+ error.response = {};
21
+ }
22
+ if (!error.response?.status) {
20
23
  error.response.status = 400;
21
24
  }
22
25
  return error;
@@ -105,7 +108,7 @@ export function titleCase(str, separator = " ") {
105
108
  const words = str.split(separator);
106
109
  return words.map(word => upperFirst(word)).join(separator);
107
110
  }
108
- function isInt32(value) {
111
+ export function isInt32(value) {
109
112
  return Number.isInteger(value) && value >= CONSTANTS.INT32_MIN && value <= CONSTANTS.INT32_MAX;
110
113
  }
111
114
  export function parseNumFromObj(obj) {
@@ -143,9 +146,6 @@ export function findKeyNode(key, node, pair = null) {
143
146
  }
144
147
  return null;
145
148
  }
146
- export function flipObject(obj) {
147
- return Object.fromEntries(Object.entries(obj).map(([key, value]) => [value, key]));
148
- }
149
149
  export function waitForProperty(obj, propertyName, timeout = 5000, interval = 100) {
150
150
  return new Promise((resolve, reject) => {
151
151
  const startTime = Date.now();
@@ -160,6 +160,17 @@ export function waitForProperty(obj, propertyName, timeout = 5000, interval = 10
160
160
  }, interval);
161
161
  });
162
162
  }
163
+ export function flipObject(obj) {
164
+ return Object.fromEntries(Object.entries(obj).map(([key, value]) => [value, key]));
165
+ }
166
+ export function shuffleObject(obj) {
167
+ const arr = Object.entries(obj);
168
+ for (let i = arr.length - 1; i > 0; i--) {
169
+ const j = Math.floor(Math.random() * (i + 1));
170
+ [arr[i], arr[j]] = [arr[j], arr[i]];
171
+ }
172
+ return Object.fromEntries(arr);
173
+ }
163
174
  export function objectStringify(obj) {
164
175
  for (let key in obj) {
165
176
  if (obj.hasOwnProperty(key)) {
@@ -307,4 +318,13 @@ export function isIntlHttpCode(httpCode) {
307
318
  export function isIntlHttpError(e) {
308
319
  const message = e?.message?.toLowerCase?.() || "";
309
320
  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);
321
+ }
322
+ export function getResponseError(e, limit = 115) {
323
+ let response;
324
+ if (e?.response?.status && e.response.data) {
325
+ response = `${e.response.status}|${e.response.data}`;
326
+ } else if (e?.response?.data) {
327
+ response = e.response.data;
328
+ }
329
+ return limitString(response || e.message, limit).trim();
310
330
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "melperjs",
3
- "version": "13.0.0",
3
+ "version": "14.0.0",
4
4
  "description": "Javascript module to use predefined common functions and utilities",
5
5
  "keywords": [
6
6
  "melperjs",