@krainovsd/js-helpers 0.16.3 → 0.16.5

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.cjs CHANGED
@@ -6,8 +6,6 @@ const dayjs = require('dayjs');
6
6
  const todayPlugin = require('dayjs/plugin/isToday');
7
7
  const tomorrowPlugin = require('dayjs/plugin/isTomorrow');
8
8
  const yesterdayPlugin = require('dayjs/plugin/isYesterday');
9
- const get = require('lodash/get');
10
- const set = require('lodash/set');
11
9
 
12
10
  class ResponseError extends Error {
13
11
  status;
@@ -347,21 +345,185 @@ function arrayToMapByKey(array, key) {
347
345
  return map;
348
346
  }
349
347
 
348
+ /* eslint-disable max-depth */
349
+ /* eslint-disable @typescript-eslint/no-unsafe-member-access */
350
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
351
+ const startArray$1 = "[".codePointAt(0);
352
+ const endArray$1 = "]".codePointAt(0);
353
+ const separator$1 = ".".codePointAt(0);
354
+ const numbers$1 = new Set(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"].map((n) => n.codePointAt(0)));
350
355
  function getByPath(data, path, defaultValue) {
351
- if ((!isObject(data) && !isArray(data)) || !isString(path))
356
+ if (!isString(path) || (!isObject(data) && !isArray(data)))
357
+ return defaultValue;
358
+ let cursor = 0;
359
+ let pathStart = 0;
360
+ let arrayPathStart = -1;
361
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
362
+ let extractedData = data;
363
+ try {
364
+ while (cursor < path.length) {
365
+ const char = path.codePointAt(cursor);
366
+ if (char === separator$1) {
367
+ arrayPathStart = -1;
368
+ if (pathStart === cursor) {
369
+ pathStart++;
370
+ }
371
+ else {
372
+ if (extractedData == undefined || typeof extractedData !== "object")
373
+ return defaultValue;
374
+ extractedData = extractedData[path.slice(pathStart, cursor)];
375
+ pathStart = cursor + 1;
376
+ }
377
+ /** start array path if not started yet */
378
+ }
379
+ else if (char === startArray$1 && arrayPathStart === -1) {
380
+ arrayPathStart = cursor;
381
+ /** end array if its started min 2 char ago */
382
+ }
383
+ else if (char === endArray$1 && arrayPathStart !== -1 && cursor - arrayPathStart > 1) {
384
+ /** before array was an object path */
385
+ if (pathStart < arrayPathStart) {
386
+ if (extractedData == undefined || typeof extractedData !== "object")
387
+ return defaultValue;
388
+ extractedData = extractedData[path.slice(pathStart, arrayPathStart)];
389
+ }
390
+ const index = +path.slice(arrayPathStart + 1, cursor);
391
+ if (!Array.isArray(extractedData))
392
+ return defaultValue;
393
+ extractedData = extractedData[index];
394
+ arrayPathStart = -1;
395
+ pathStart = cursor + 1;
396
+ /** if array started but char is not number */
397
+ }
398
+ else if (arrayPathStart !== -1 && !numbers$1.has(char)) {
399
+ arrayPathStart = -1;
400
+ }
401
+ cursor++;
402
+ }
403
+ if (pathStart < path.length) {
404
+ if (extractedData == undefined || typeof extractedData !== "object")
405
+ return defaultValue;
406
+ extractedData = extractedData[path.slice(pathStart, cursor)];
407
+ }
408
+ }
409
+ catch {
410
+ return defaultValue;
411
+ }
412
+ if (extractedData === undefined) {
352
413
  return defaultValue;
353
- return get(data, path, defaultValue);
414
+ }
415
+ return extractedData;
354
416
  }
355
417
 
356
- function setByPath(data, path, value) {
418
+ /* eslint-disable max-depth */
419
+ /* eslint-disable @typescript-eslint/no-unsafe-member-access */
420
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
421
+ const startArray = "[".codePointAt(0);
422
+ const endArray = "]".codePointAt(0);
423
+ const separator = ".".codePointAt(0);
424
+ const numbers = new Set(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"].map((n) => n.codePointAt(0)));
425
+ function setByPath(data, path, settableValue) {
426
+ if (!isString(path) || (!isObject(data) && !isArray(data)))
427
+ return false;
428
+ let cursor = 0;
429
+ let pathStart = 0;
430
+ let arrayPathStart = -1;
431
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
432
+ let extractedData = data;
433
+ let prevPath;
357
434
  try {
358
- if (!isObject(data) && !isArray(data))
359
- throw new Error("bad data");
360
- set(data, path, value);
435
+ while (cursor < path.length) {
436
+ const char = path.codePointAt(cursor);
437
+ /** get */
438
+ if (char === separator) {
439
+ arrayPathStart = -1;
440
+ if (pathStart === cursor) {
441
+ pathStart++;
442
+ }
443
+ else {
444
+ if (prevPath) {
445
+ const object = {};
446
+ extractedData[prevPath] = object;
447
+ extractedData = object;
448
+ }
449
+ if (cursor === path.length - 1) {
450
+ extractedData[path.slice(pathStart, cursor)] = settableValue;
451
+ }
452
+ else {
453
+ const currentPath = path.slice(pathStart, cursor);
454
+ const extractedValue = extractedData[currentPath];
455
+ if (extractedValue == undefined) {
456
+ prevPath = currentPath;
457
+ }
458
+ else {
459
+ extractedData = extractedValue;
460
+ }
461
+ pathStart = cursor + 1;
462
+ }
463
+ }
464
+ /** start array path if not started yet */
465
+ }
466
+ else if (char === startArray && arrayPathStart === -1) {
467
+ arrayPathStart = cursor;
468
+ /** end array if its started min 2 char ago */
469
+ }
470
+ else if (char === endArray && arrayPathStart !== -1 && cursor - arrayPathStart > 1) {
471
+ /** before array was an object path */
472
+ if (pathStart < arrayPathStart) {
473
+ if (prevPath) {
474
+ const object = {};
475
+ extractedData[prevPath] = object;
476
+ extractedData = object;
477
+ }
478
+ const currentPath = path.slice(pathStart, arrayPathStart);
479
+ const extractedValue = extractedData[currentPath];
480
+ if (extractedValue == undefined) {
481
+ prevPath = currentPath;
482
+ }
483
+ else {
484
+ extractedData = extractedValue;
485
+ }
486
+ }
487
+ if (prevPath) {
488
+ const array = [];
489
+ extractedData[prevPath] = array;
490
+ extractedData = array;
491
+ }
492
+ const index = +path.slice(arrayPathStart + 1, cursor);
493
+ if (cursor === path.length - 1) {
494
+ extractedData[index] = settableValue;
495
+ }
496
+ else {
497
+ const extractedValue = extractedData[index];
498
+ if (extractedValue == undefined) {
499
+ prevPath = index;
500
+ }
501
+ else {
502
+ extractedData = extractedValue;
503
+ }
504
+ }
505
+ arrayPathStart = -1;
506
+ pathStart = cursor + 1;
507
+ /** if array started but char is not number */
508
+ }
509
+ else if (arrayPathStart !== -1 && !numbers.has(char)) {
510
+ arrayPathStart = -1;
511
+ }
512
+ cursor++;
513
+ }
514
+ if (pathStart < path.length) {
515
+ if (prevPath) {
516
+ const object = {};
517
+ extractedData[prevPath] = object;
518
+ extractedData = object;
519
+ }
520
+ extractedData[path.slice(pathStart, cursor)] = settableValue;
521
+ }
361
522
  }
362
- catch (error) {
363
- console.warn(error);
523
+ catch {
524
+ return false;
364
525
  }
526
+ return true;
365
527
  }
366
528
 
367
529
  function syncObjectValues(oldObj, newObj, exception = []) {
@@ -634,11 +796,11 @@ function speedTest(cb, opts) {
634
796
  if (type === "performance") {
635
797
  const start = performance.now();
636
798
  if (iterations <= 1) {
637
- result = cb();
799
+ result = cb(0);
638
800
  }
639
801
  else {
640
802
  for (let i = 0; i < iterations; i++) {
641
- result = cb();
803
+ result = cb(i);
642
804
  }
643
805
  }
644
806
  const end = performance.now();
@@ -649,11 +811,11 @@ function speedTest(cb, opts) {
649
811
  // eslint-disable-next-line no-console
650
812
  console.time(name);
651
813
  if (iterations <= 1) {
652
- result = cb();
814
+ result = cb(0);
653
815
  }
654
816
  else {
655
817
  for (let i = 0; i < iterations; i++) {
656
- result = cb();
818
+ result = cb(i);
657
819
  }
658
820
  }
659
821
  // eslint-disable-next-line no-console
@@ -2083,7 +2245,7 @@ function loggerAfterHandler(options = {}) {
2083
2245
  (options.filterStatus && !options.filterStatus(response.status)) ||
2084
2246
  (options.filterUrl && !options.filterUrl(response.url)) ||
2085
2247
  (options.filterHeaders && !options.filterHeaders(response.headers))) {
2086
- resolve(true);
2248
+ resolve();
2087
2249
  return;
2088
2250
  }
2089
2251
  const contentType = response.headers.get("content-type");
@@ -2109,16 +2271,16 @@ function loggerAfterHandler(options = {}) {
2109
2271
  headers: response.headers,
2110
2272
  body: result,
2111
2273
  });
2112
- resolve(true);
2274
+ resolve();
2113
2275
  }
2114
2276
  catch {
2115
2277
  if (response) {
2116
2278
  console.log({ url: response.url, status: response.status, headers: response.headers });
2117
2279
  }
2118
- resolve(true);
2280
+ resolve();
2119
2281
  }
2120
2282
  })().finally(() => {
2121
- resolve(true);
2283
+ resolve();
2122
2284
  });
2123
2285
  });
2124
2286
  };
@@ -2133,12 +2295,12 @@ function loggerBeforeHandler(options = {}) {
2133
2295
  (options.filterMethod && !options.filterMethod(request.method)) ||
2134
2296
  (options.filterParams && !options.filterParams(request.queries)) ||
2135
2297
  (options.filterPath && !options.filterPath(request.path))) {
2136
- resolve(true);
2298
+ resolve();
2137
2299
  return;
2138
2300
  }
2139
2301
  // eslint-disable-next-line no-console
2140
2302
  console.log(request);
2141
- resolve(true);
2303
+ resolve();
2142
2304
  });
2143
2305
  };
2144
2306
  }