@krainovsd/js-helpers 0.16.4 → 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 +176 -14
- package/lib/cjs/index.cjs.map +1 -1
- package/lib/esm/api/api.js +2 -2
- package/lib/esm/api/before/oauth-before-handler.js +2 -2
- package/lib/esm/api/oauth.js +2 -2
- package/lib/esm/lib/browser/download-file.js +2 -2
- package/lib/esm/lib/lodash/get-by-path.js +66 -3
- package/lib/esm/lib/lodash/get-by-path.js.map +1 -1
- package/lib/esm/lib/lodash/set-by-path.js +107 -7
- package/lib/esm/lib/lodash/set-by-path.js.map +1 -1
- package/lib/esm/lib/utils/speed-test.js +4 -4
- package/lib/esm/lib/utils/speed-test.js.map +1 -1
- package/lib/index.d.ts +3 -3
- package/package.json +2 -4
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))
|
|
356
|
+
if (!isString(path) || (!isObject(data) && !isArray(data)))
|
|
352
357
|
return defaultValue;
|
|
353
|
-
|
|
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) {
|
|
413
|
+
return defaultValue;
|
|
414
|
+
}
|
|
415
|
+
return extractedData;
|
|
354
416
|
}
|
|
355
417
|
|
|
356
|
-
|
|
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
|
-
|
|
359
|
-
|
|
360
|
-
|
|
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
|
|
363
|
-
|
|
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
|