@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 CHANGED
@@ -542,10 +542,64 @@ var LeUtils = {
542
542
  });
543
543
  return result;
544
544
  },
545
+ /**
546
+ * @callback LeUtils~__findIndexValueCallback
547
+ * @param {*} value
548
+ * @param {*} index
549
+ * @returns {boolean|undefined}
550
+ */
551
+ /**
552
+ * 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.
553
+ *
554
+ * @param {*[]|object|Function} elements
555
+ * @param {LeUtils~__findIndexValueCallback} callback
556
+ * @param {boolean} [optionalSkipHasOwnPropertyCheck]
557
+ * @returns {{index:*, value:*}|null}
558
+ */
559
+ findIndexValue: function findIndexValue(elements, callback) {
560
+ var result = null;
561
+ LeUtils.each(elements, function (value, index) {
562
+ if (callback.call(elements[index], elements[index], index)) {
563
+ result = {
564
+ index: index,
565
+ value: value
566
+ };
567
+ return false;
568
+ }
569
+ });
570
+ return result;
571
+ },
572
+ /**
573
+ * Finds the first element in the given array or object that returns true from the callback, and returns the index.
574
+ *
575
+ * @param {*[]|object|Function} elements
576
+ * @param {LeUtils~__findIndexValueCallback} callback
577
+ * @param {boolean} [optionalSkipHasOwnPropertyCheck]
578
+ * @returns {*|null}
579
+ */
580
+ findIndex: function findIndex(elements, callback) {
581
+ var _LeUtils$findIndexVal, _LeUtils$findIndexVal2;
582
+ var optionalSkipHasOwnPropertyCheck = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
583
+ return (_LeUtils$findIndexVal = (_LeUtils$findIndexVal2 = LeUtils.findIndexValue(elements, callback, optionalSkipHasOwnPropertyCheck)) === null || _LeUtils$findIndexVal2 === void 0 ? void 0 : _LeUtils$findIndexVal2.index) !== null && _LeUtils$findIndexVal !== void 0 ? _LeUtils$findIndexVal : null;
584
+ },
585
+ /**
586
+ * Finds the first element in the given array or object that returns true from the callback, and returns the value.
587
+ *
588
+ * @param {*[]|object|Function} elements
589
+ * @param {LeUtils~__findIndexValueCallback} callback
590
+ * @param {boolean} [optionalSkipHasOwnPropertyCheck]
591
+ * @returns {*|null}
592
+ */
593
+ find: function find(elements, callback) {
594
+ var _LeUtils$findIndexVal3, _LeUtils$findIndexVal4;
595
+ var optionalSkipHasOwnPropertyCheck = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
596
+ return (_LeUtils$findIndexVal3 = (_LeUtils$findIndexVal4 = LeUtils.findIndexValue(elements, callback, optionalSkipHasOwnPropertyCheck)) === null || _LeUtils$findIndexVal4 === void 0 ? void 0 : _LeUtils$findIndexVal4.value) !== null && _LeUtils$findIndexVal3 !== void 0 ? _LeUtils$findIndexVal3 : null;
597
+ },
545
598
  /**
546
599
  * @callback LeUtils~__eachCallback
547
600
  * @param {*} value
548
601
  * @param {*} index
602
+ * @returns {boolean|undefined}
549
603
  */
550
604
  /**
551
605
  * Loops through each element in the given array or object, and calls the callback for each element.
@@ -792,6 +846,7 @@ var LeUtils = {
792
846
  * @callback LeUtils~__filterCallback
793
847
  * @param {*} value
794
848
  * @param {*} index
849
+ * @returns {boolean|undefined}
795
850
  */
796
851
  /**
797
852
  * Loops through the given elements, and returns a new array or object, with only the elements that didn't return false from the callback.
@@ -832,6 +887,7 @@ var LeUtils = {
832
887
  * @callback LeUtils~__mapCallback
833
888
  * @param {*} value
834
889
  * @param {*} index
890
+ * @returns {*}
835
891
  */
836
892
  /**
837
893
  * Loops through the given elements, and returns a new array or object, with the elements that were returned from the callback.
@@ -868,6 +924,7 @@ var LeUtils = {
868
924
  * @callback LeUtils~__mapToArrayCallback
869
925
  * @param {*} value
870
926
  * @param {*} index
927
+ * @returns {*}
871
928
  */
872
929
  /**
873
930
  * Loops through the given elements, and returns a new array, with the elements that were returned from the callback. Always returns an array.
@@ -901,6 +958,7 @@ var LeUtils = {
901
958
  * @callback LeUtils~__mapToArraySortedCallback
902
959
  * @param {*} value
903
960
  * @param {*} index
961
+ * @returns {*}
904
962
  */
905
963
  /**
906
964
  * 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.
@@ -924,6 +982,7 @@ var LeUtils = {
924
982
  * @callback LeUtils~__sortKeysComparatorCallback
925
983
  * @param {*} elementA
926
984
  * @param {*} elementB
985
+ * @returns {number}
927
986
  */
928
987
  /**
929
988
  * 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.
@@ -1382,36 +1441,84 @@ var LeUtils = {
1382
1441
  if (typeof window !== 'undefined' && typeof window.AbortController !== 'undefined') {
1383
1442
  controller = new AbortController();
1384
1443
  }
1385
- var promise = new Promise(function (resolve, reject) {
1386
- var attemptFetch = function attemptFetch() {
1387
- var _controller, _controller2;
1388
- if (controllerAborted || !!((_controller = controller) !== null && _controller !== void 0 && (_controller = _controller.signal) !== null && _controller !== void 0 && _controller.aborted)) {
1389
- reject(new Error('Aborted'));
1390
- return;
1444
+ var promise = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8() {
1445
+ var attemptFetch;
1446
+ return _regeneratorRuntime.wrap(function _callee8$(_context9) {
1447
+ while (1) switch (_context9.prev = _context9.next) {
1448
+ case 0:
1449
+ attemptFetch = /*#__PURE__*/function () {
1450
+ var _ref7 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7() {
1451
+ var _controller;
1452
+ var _controller2, response, _controller3;
1453
+ return _regeneratorRuntime.wrap(function _callee7$(_context8) {
1454
+ while (1) switch (_context8.prev = _context8.next) {
1455
+ case 0:
1456
+ if (!(controllerAborted || (_controller = controller) !== null && _controller !== void 0 && (_controller = _controller.signal) !== null && _controller !== void 0 && _controller.aborted)) {
1457
+ _context8.next = 2;
1458
+ break;
1459
+ }
1460
+ throw new Error('Aborted');
1461
+ case 2:
1462
+ _context8.prev = 2;
1463
+ _context8.next = 5;
1464
+ return fetch(url, _objectSpread(_objectSpread({
1465
+ signal: (_controller2 = controller) === null || _controller2 === void 0 ? void 0 : _controller2.signal
1466
+ }, options !== null && options !== void 0 ? options : {}), {}, {
1467
+ retries: undefined,
1468
+ delay: undefined
1469
+ }));
1470
+ case 5:
1471
+ response = _context8.sent;
1472
+ if (response.ok) {
1473
+ _context8.next = 8;
1474
+ break;
1475
+ }
1476
+ throw new Error('Network request failed: ' + response.status + ' ' + response.statusText);
1477
+ case 8:
1478
+ return _context8.abrupt("return", response);
1479
+ case 11:
1480
+ _context8.prev = 11;
1481
+ _context8.t0 = _context8["catch"](2);
1482
+ if (!(controllerAborted || (_controller3 = controller) !== null && _controller3 !== void 0 && (_controller3 = _controller3.signal) !== null && _controller3 !== void 0 && _controller3.aborted)) {
1483
+ _context8.next = 15;
1484
+ break;
1485
+ }
1486
+ throw new Error('Aborted');
1487
+ case 15:
1488
+ if (!(currentRetries >= retries)) {
1489
+ _context8.next = 17;
1490
+ break;
1491
+ }
1492
+ throw _context8.t0;
1493
+ case 17:
1494
+ currentRetries++;
1495
+ _context8.next = 20;
1496
+ return LeUtils.promiseTimeout(typeof (options === null || options === void 0 ? void 0 : options.delay) === 'function' ? INT_LAX_ANY(options === null || options === void 0 ? void 0 : options.delay(currentRetries), 500) : INT_LAX_ANY(options === null || options === void 0 ? void 0 : options.delay, 500));
1497
+ case 20:
1498
+ _context8.next = 22;
1499
+ return attemptFetch();
1500
+ case 22:
1501
+ return _context8.abrupt("return", _context8.sent);
1502
+ case 23:
1503
+ case "end":
1504
+ return _context8.stop();
1505
+ }
1506
+ }, _callee7, null, [[2, 11]]);
1507
+ }));
1508
+ return function attemptFetch() {
1509
+ return _ref7.apply(this, arguments);
1510
+ };
1511
+ }();
1512
+ _context9.next = 3;
1513
+ return attemptFetch();
1514
+ case 3:
1515
+ return _context9.abrupt("return", _context9.sent);
1516
+ case 4:
1517
+ case "end":
1518
+ return _context9.stop();
1391
1519
  }
1392
- fetch(url, _objectSpread(_objectSpread({
1393
- signal: (_controller2 = controller) === null || _controller2 === void 0 ? void 0 : _controller2.signal
1394
- }, options !== null && options !== void 0 ? options : {}), {}, {
1395
- retries: undefined,
1396
- delay: undefined
1397
- })).then(function (response) {
1398
- if (!response.ok) {
1399
- throw new Error('Network request failed: ' + response.status + ' ' + response.statusText);
1400
- }
1401
- return response;
1402
- }).then(function (response) {
1403
- resolve(response);
1404
- })["catch"](function (error) {
1405
- if (currentRetries >= retries) {
1406
- reject(error);
1407
- return;
1408
- }
1409
- currentRetries++;
1410
- setTimeout(attemptFetch, typeof (options === null || options === void 0 ? void 0 : options.delay) === 'function' ? INT_LAX_ANY(options === null || options === void 0 ? void 0 : options.delay(currentRetries), 500) : INT_LAX_ANY(options === null || options === void 0 ? void 0 : options.delay, 500));
1411
- });
1412
- };
1413
- attemptFetch();
1414
- });
1520
+ }, _callee8);
1521
+ }))();
1415
1522
  var result = {};
1416
1523
  result.then = function () {
1417
1524
  var _promise;
@@ -1431,14 +1538,14 @@ var LeUtils = {
1431
1538
  result.remove = function () {
1432
1539
  controllerAborted = true;
1433
1540
  if (controller) {
1434
- var _controller3;
1435
- (_controller3 = controller).abort.apply(_controller3, arguments);
1541
+ var _controller4;
1542
+ (_controller4 = controller).abort.apply(_controller4, arguments);
1436
1543
  }
1437
1544
  return result;
1438
1545
  };
1439
1546
  result.isRemoved = function () {
1440
- var _controller4;
1441
- return controllerAborted || !!((_controller4 = controller) !== null && _controller4 !== void 0 && (_controller4 = _controller4.signal) !== null && _controller4 !== void 0 && _controller4.aborted);
1547
+ var _controller5;
1548
+ return controllerAborted || !!((_controller5 = controller) !== null && _controller5 !== void 0 && (_controller5 = _controller5.signal) !== null && _controller5 !== void 0 && _controller5.aborted);
1442
1549
  };
1443
1550
  return result;
1444
1551
  }),