@krainovsd/js-helpers 0.10.6 → 0.11.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
@@ -4,24 +4,28 @@ The library of helpers for JS in Browser and NodeJS environments.
4
4
 
5
5
  ## Installing
6
6
 
7
- ### Package manager
8
-
9
7
  Using pnpm:
10
8
  ```
11
9
  pnpm install @krainovsd/js-helpers
12
10
  ```
13
11
 
14
- Using yarn:
15
- ```
16
- yarn add @krainovsd/js-helpers
17
- ```
12
+ ## Monorepo
18
13
 
19
- Using npm:
20
- ```
21
- npm install @krainovsd/js-helpers
14
+ Using in frontend app with monorepo make sure that node-fetch will be excluded from bundle.
15
+ ```js
16
+ export default defineConfig({
17
+ base: "/",
18
+ plugins: [
19
+ react(),
20
+ ],
21
+ build: {
22
+ rollupOptions: {
23
+ external: ["node-fetch"],
24
+ },
25
+ },
26
+ });
22
27
  ```
23
28
 
24
-
25
29
  ## Usage
26
30
 
27
31
  ```js
package/lib/cjs/index.cjs CHANGED
@@ -27,18 +27,18 @@ const POST_API_MIDDLEWARES = {
27
27
  Logger: "logger",
28
28
  };
29
29
 
30
- /* eslint-disable no-restricted-globals */
31
30
  /* eslint-disable @typescript-eslint/no-unsafe-member-access */
32
31
  /* eslint-disable @typescript-eslint/ban-ts-comment */
33
32
  const IS_JEST = typeof process != "undefined" && process.env.JEST_WORKER_ID !== undefined;
34
- const IS_BUN = typeof process !== "undefined" && process.versions != null && process.versions.bun != null;
35
- const IS_DENO = // @ts-expect-error
36
- typeof Deno !== "undefined" &&
33
+ const IS_BUN = typeof process !== "undefined" && process?.versions?.bun != undefined;
34
+ const IS_DENO =
35
+ // @ts-expect-error
36
+ typeof Deno !== "undefined" &&
37
37
  // @ts-expect-error
38
38
  typeof Deno.version !== "undefined" &&
39
39
  // @ts-expect-error
40
40
  typeof Deno.version.deno !== "undefined";
41
- const IS_NODE = typeof process !== "undefined" && process.versions != null && process.versions.node != null;
41
+ const IS_NODE = typeof process !== "undefined" && process?.versions?.node != undefined;
42
42
  const IS_WEB_WORKER = typeof self === "object" &&
43
43
  self.constructor &&
44
44
  self.constructor.name === "DedicatedWorkerGlobalScope";
@@ -126,7 +126,7 @@ function getByPath(data, path, defaultValue = undefined) {
126
126
  return get(data, path, defaultValue);
127
127
  }
128
128
 
129
- function dateDifference(type, firstDate, secondDate = new Date(), float = false) {
129
+ function dateDifference({ firstDate, float = false, secondDate = new Date(), type, }) {
130
130
  const first = dayjs(firstDate);
131
131
  return first.diff(secondDate, type, float);
132
132
  }
@@ -277,7 +277,6 @@ function setByPath(data, path, value) {
277
277
  set(data, path, value);
278
278
  }
279
279
  catch (error) {
280
- // eslint-disable-next-line no-console
281
280
  console.warn(error);
282
281
  }
283
282
  }
@@ -456,7 +455,7 @@ function getCallerFunctionName() {
456
455
  const stack = error.stack.split("\n");
457
456
  if (stack[2]) {
458
457
  const callerLine = stack[2].trim();
459
- const match = callerLine.match(/at (\w+)/);
458
+ const match = /at (\w+)/.exec(callerLine);
460
459
  if (match) {
461
460
  return match[1];
462
461
  }
@@ -466,6 +465,7 @@ function getCallerFunctionName() {
466
465
  return null;
467
466
  }
468
467
 
468
+ /* eslint-disable id-length */
469
469
  const translitDict = {
470
470
  А: "A",
471
471
  Б: "B",
@@ -699,14 +699,14 @@ function getVisiblePosition({ initialPosition, node, visibleArea, placement = "b
699
699
  let targetHeight = 0;
700
700
  let targetWidth = 0;
701
701
  let { top: targetTopPosition, left: targetLeftPosition, height: nodeHeight, width: nodeWidth, } = node.getBoundingClientRect();
702
- if (initialPosition && initialPosition.targetNode) {
702
+ if (initialPosition?.targetNode) {
703
703
  const { top: childTop, left: childLeft, height, width, } = initialPosition.targetNode.getBoundingClientRect();
704
704
  targetHeight = height;
705
705
  targetWidth = width;
706
706
  targetTopPosition = childTop;
707
707
  targetLeftPosition = childLeft;
708
708
  }
709
- if (initialPosition && initialPosition.position) {
709
+ if (initialPosition?.position) {
710
710
  if (initialPosition.position.x) {
711
711
  targetLeftPosition = initialPosition.position.x;
712
712
  }
@@ -1268,6 +1268,26 @@ function getFlexVisiblePosition({ initialLeft, initialTop, isCompletelyVisibleX,
1268
1268
  };
1269
1269
  }
1270
1270
 
1271
+ function getQueryValues(keys) {
1272
+ if (!IS_BROWSER && !IS_JEST)
1273
+ return null;
1274
+ const object = {};
1275
+ const queries = window.location.search.substring(1).split("&");
1276
+ for (const query of queries) {
1277
+ const [key, value] = query.split("=");
1278
+ if (keys.includes(key) && value) {
1279
+ const prev = object[key];
1280
+ if (isArray(prev))
1281
+ prev.push(value);
1282
+ else if (isString(prev))
1283
+ object[key] = [prev, value];
1284
+ else
1285
+ object[key] = value;
1286
+ }
1287
+ }
1288
+ return object;
1289
+ }
1290
+
1271
1291
  const RESPONSE_DATA_SYMBOL = Symbol("response data");
1272
1292
 
1273
1293
  async function updateAuthToken(options) {
@@ -1358,18 +1378,21 @@ function updateAuthTokenNoRefresh(options) {
1358
1378
  let expires = localStorage.getItem(options.storageTokenExpiresName);
1359
1379
  if (!expires || Number.isNaN(+expires) || Date.now() > +expires)
1360
1380
  expires = null;
1361
- let hasExpiresQuery = false;
1362
- if (!expires) {
1363
- let lastQuery;
1364
- const queries = window.location.search.substring(1).split("&");
1365
- for (const query of queries) {
1366
- const [key, value] = query.split("=");
1367
- if (key === options.queryTokenExpiresName && value) {
1368
- lastQuery = value;
1369
- hasExpiresQuery = true;
1370
- }
1371
- }
1372
- expires = lastQuery;
1381
+ const queries = getQueryValues([options.queryTokenExpiresName, options.queryIsRefreshTokenName]);
1382
+ const refreshQuery = queries?.[options.queryIsRefreshTokenName];
1383
+ const expiresQuery = queries?.[options.queryTokenExpiresName];
1384
+ const isRefresh = isString(refreshQuery)
1385
+ ? refreshQuery === "true"
1386
+ : isArray(refreshQuery)
1387
+ ? refreshQuery[refreshQuery.length - 1] === "true"
1388
+ : false;
1389
+ const expiresFromQuery = isString(expiresQuery)
1390
+ ? expiresQuery
1391
+ : isArray(expiresQuery)
1392
+ ? expiresQuery[expiresQuery.length - 1]
1393
+ : false;
1394
+ if (!expires && expiresFromQuery) {
1395
+ expires = expiresFromQuery;
1373
1396
  if (!expires || Number.isNaN(+expires) || Date.now() > +expires)
1374
1397
  expires = null;
1375
1398
  }
@@ -1378,17 +1401,13 @@ function updateAuthTokenNoRefresh(options) {
1378
1401
  return null;
1379
1402
  }
1380
1403
  localStorage.setItem(options.storageTokenExpiresName, expires);
1381
- const queries = window.location.search.substring(1).split("&");
1382
- for (const query of queries) {
1383
- const [key, value] = query.split("=");
1384
- if (key === options.queryIsRefreshTokenName && value === "true") {
1385
- const channel = new BroadcastChannel(options.queryIsRefreshTokenName);
1386
- channel.postMessage(true);
1387
- channel.close();
1388
- window.close();
1389
- }
1404
+ if (isRefresh) {
1405
+ const channel = new BroadcastChannel(options.queryIsRefreshTokenName);
1406
+ channel.postMessage(true);
1407
+ channel.close();
1408
+ window.close();
1390
1409
  }
1391
- if (hasExpiresQuery) {
1410
+ if (expiresFromQuery) {
1392
1411
  const url = new URL(window.location.href);
1393
1412
  url.searchParams.delete(options.queryTokenExpiresName);
1394
1413
  window.location.replace(url.toString());
@@ -1903,6 +1922,7 @@ exports.getCallerFunctionName = getCallerFunctionName;
1903
1922
  exports.getColorFormat = getColorFormat;
1904
1923
  exports.getDateByRules = getDateByRules;
1905
1924
  exports.getFileNameFromHeader = getFileNameFromHeader;
1925
+ exports.getQueryValues = getQueryValues;
1906
1926
  exports.getRandomColor = getRandomColor;
1907
1927
  exports.getToday = getToday;
1908
1928
  exports.getTomorrow = getTomorrow;