@lowentry/utils 1.11.4 → 1.12.1
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/index.js +140 -33
- package/index.js.map +1 -1
- package/package.json +1 -1
- package/src/LeUtils.js +80 -21
package/package.json
CHANGED
package/src/LeUtils.js
CHANGED
|
@@ -435,10 +435,62 @@ export const LeUtils = {
|
|
|
435
435
|
return result;
|
|
436
436
|
},
|
|
437
437
|
|
|
438
|
+
/**
|
|
439
|
+
* @callback LeUtils~__findIndexValueCallback
|
|
440
|
+
* @param {*} value
|
|
441
|
+
* @param {*} index
|
|
442
|
+
* @returns {boolean|undefined}
|
|
443
|
+
*/
|
|
444
|
+
/**
|
|
445
|
+
* Finds the first element in the given array or object that returns true from the callback, and returns an object with the index and value.
|
|
446
|
+
*
|
|
447
|
+
* @param {*[]|object|Function} elements
|
|
448
|
+
* @param {LeUtils~__findIndexValueCallback} callback
|
|
449
|
+
* @param {boolean} [optionalSkipHasOwnPropertyCheck]
|
|
450
|
+
* @returns {{index:*, value:*}|null}
|
|
451
|
+
*/
|
|
452
|
+
findIndexValue:
|
|
453
|
+
(elements, callback, optionalSkipHasOwnPropertyCheck = false) =>
|
|
454
|
+
{
|
|
455
|
+
let result = null;
|
|
456
|
+
LeUtils.each(elements, (value, index) =>
|
|
457
|
+
{
|
|
458
|
+
if(callback.call(elements[index], elements[index], index))
|
|
459
|
+
{
|
|
460
|
+
result = {index, value};
|
|
461
|
+
return false;
|
|
462
|
+
}
|
|
463
|
+
});
|
|
464
|
+
return result;
|
|
465
|
+
},
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* Finds the first element in the given array or object that returns true from the callback, and returns the index.
|
|
469
|
+
*
|
|
470
|
+
* @param {*[]|object|Function} elements
|
|
471
|
+
* @param {LeUtils~__findIndexValueCallback} callback
|
|
472
|
+
* @param {boolean} [optionalSkipHasOwnPropertyCheck]
|
|
473
|
+
* @returns {*|null}
|
|
474
|
+
*/
|
|
475
|
+
findIndex:
|
|
476
|
+
(elements, callback, optionalSkipHasOwnPropertyCheck = false) => LeUtils.findIndexValue(elements, callback, optionalSkipHasOwnPropertyCheck)?.index ?? null,
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Finds the first element in the given array or object that returns true from the callback, and returns the value.
|
|
480
|
+
*
|
|
481
|
+
* @param {*[]|object|Function} elements
|
|
482
|
+
* @param {LeUtils~__findIndexValueCallback} callback
|
|
483
|
+
* @param {boolean} [optionalSkipHasOwnPropertyCheck]
|
|
484
|
+
* @returns {*|null}
|
|
485
|
+
*/
|
|
486
|
+
find:
|
|
487
|
+
(elements, callback, optionalSkipHasOwnPropertyCheck = false) => LeUtils.findIndexValue(elements, callback, optionalSkipHasOwnPropertyCheck)?.value ?? null,
|
|
488
|
+
|
|
438
489
|
/**
|
|
439
490
|
* @callback LeUtils~__eachCallback
|
|
440
491
|
* @param {*} value
|
|
441
492
|
* @param {*} index
|
|
493
|
+
* @returns {boolean|undefined}
|
|
442
494
|
*/
|
|
443
495
|
/**
|
|
444
496
|
* Loops through each element in the given array or object, and calls the callback for each element.
|
|
@@ -584,6 +636,7 @@ export const LeUtils = {
|
|
|
584
636
|
* @callback LeUtils~__filterCallback
|
|
585
637
|
* @param {*} value
|
|
586
638
|
* @param {*} index
|
|
639
|
+
* @returns {boolean|undefined}
|
|
587
640
|
*/
|
|
588
641
|
/**
|
|
589
642
|
* Loops through the given elements, and returns a new array or object, with only the elements that didn't return false from the callback.
|
|
@@ -637,6 +690,7 @@ export const LeUtils = {
|
|
|
637
690
|
* @callback LeUtils~__mapCallback
|
|
638
691
|
* @param {*} value
|
|
639
692
|
* @param {*} index
|
|
693
|
+
* @returns {*}
|
|
640
694
|
*/
|
|
641
695
|
/**
|
|
642
696
|
* Loops through the given elements, and returns a new array or object, with the elements that were returned from the callback.
|
|
@@ -684,6 +738,7 @@ export const LeUtils = {
|
|
|
684
738
|
* @callback LeUtils~__mapToArrayCallback
|
|
685
739
|
* @param {*} value
|
|
686
740
|
* @param {*} index
|
|
741
|
+
* @returns {*}
|
|
687
742
|
*/
|
|
688
743
|
/**
|
|
689
744
|
* Loops through the given elements, and returns a new array, with the elements that were returned from the callback. Always returns an array.
|
|
@@ -728,6 +783,7 @@ export const LeUtils = {
|
|
|
728
783
|
* @callback LeUtils~__mapToArraySortedCallback
|
|
729
784
|
* @param {*} value
|
|
730
785
|
* @param {*} index
|
|
786
|
+
* @returns {*}
|
|
731
787
|
*/
|
|
732
788
|
/**
|
|
733
789
|
* Loops through the given elements, and returns a new array, with the elements that were returned from the callback. The elements will be sorted by the result from the given comparator. Always returns an array.
|
|
@@ -754,6 +810,7 @@ export const LeUtils = {
|
|
|
754
810
|
* @callback LeUtils~__sortKeysComparatorCallback
|
|
755
811
|
* @param {*} elementA
|
|
756
812
|
* @param {*} elementB
|
|
813
|
+
* @returns {number}
|
|
757
814
|
*/
|
|
758
815
|
/**
|
|
759
816
|
* Loops through the given elements, and returns a new array, with the keys from the given elements, sorted by the result from the given comparator. Always returns an array.
|
|
@@ -1303,44 +1360,46 @@ export const LeUtils = {
|
|
|
1303
1360
|
controller = new AbortController();
|
|
1304
1361
|
}
|
|
1305
1362
|
|
|
1306
|
-
let promise =
|
|
1363
|
+
let promise = (async () =>
|
|
1307
1364
|
{
|
|
1308
|
-
const attemptFetch = () =>
|
|
1365
|
+
const attemptFetch = async () =>
|
|
1309
1366
|
{
|
|
1310
|
-
if(controllerAborted ||
|
|
1367
|
+
if(controllerAborted || controller?.signal?.aborted)
|
|
1311
1368
|
{
|
|
1312
|
-
|
|
1313
|
-
return;
|
|
1369
|
+
throw new Error('Aborted');
|
|
1314
1370
|
}
|
|
1315
1371
|
|
|
1316
|
-
|
|
1317
|
-
signal:controller?.signal,
|
|
1318
|
-
...(options ?? {}),
|
|
1319
|
-
retries:undefined,
|
|
1320
|
-
delay: undefined,
|
|
1321
|
-
}).then(response =>
|
|
1372
|
+
try
|
|
1322
1373
|
{
|
|
1374
|
+
const response = await fetch(url, {
|
|
1375
|
+
signal:controller?.signal,
|
|
1376
|
+
...(options ?? {}),
|
|
1377
|
+
retries:undefined,
|
|
1378
|
+
delay: undefined,
|
|
1379
|
+
});
|
|
1323
1380
|
if(!response.ok)
|
|
1324
1381
|
{
|
|
1325
1382
|
throw new Error('Network request failed: ' + response.status + ' ' + response.statusText);
|
|
1326
1383
|
}
|
|
1327
1384
|
return response;
|
|
1328
|
-
}
|
|
1329
|
-
|
|
1330
|
-
resolve(response);
|
|
1331
|
-
}).catch(error =>
|
|
1385
|
+
}
|
|
1386
|
+
catch(error)
|
|
1332
1387
|
{
|
|
1388
|
+
if(controllerAborted || controller?.signal?.aborted)
|
|
1389
|
+
{
|
|
1390
|
+
throw new Error('Aborted');
|
|
1391
|
+
}
|
|
1333
1392
|
if(currentRetries >= retries)
|
|
1334
1393
|
{
|
|
1335
|
-
|
|
1336
|
-
return;
|
|
1394
|
+
throw error;
|
|
1337
1395
|
}
|
|
1338
1396
|
currentRetries++;
|
|
1339
|
-
|
|
1340
|
-
|
|
1397
|
+
await LeUtils.promiseTimeout((typeof options?.delay === 'function') ? INT_LAX_ANY(options?.delay(currentRetries), 500) : (INT_LAX_ANY(options?.delay, 500)));
|
|
1398
|
+
return await attemptFetch();
|
|
1399
|
+
}
|
|
1341
1400
|
};
|
|
1342
|
-
attemptFetch();
|
|
1343
|
-
});
|
|
1401
|
+
return await attemptFetch();
|
|
1402
|
+
})();
|
|
1344
1403
|
|
|
1345
1404
|
let result = {};
|
|
1346
1405
|
result.then = (...args) =>
|