|
3 | 3 | * bbo is a utility library of zero dependencies for javascript.
|
4 | 4 | * (c) 2011 - 2020
|
5 | 5 | * https://github.com/tnfe/bbo.git
|
6 |
| - * version 1.1.22 |
| 6 | + * version 1.1.23 |
7 | 7 | */
|
8 | 8 |
|
9 | 9 | (function (global, factory) {
|
|
118 | 118 | return getTag(func) === '[object Function]';
|
119 | 119 | }
|
120 | 120 |
|
121 |
| - var version = '1.1.22'; |
| 121 | + var version = '1.1.23'; |
122 | 122 |
|
123 | 123 | var globalObject = null;
|
124 | 124 |
|
|
530 | 530 | });
|
531 | 531 | }
|
532 | 532 |
|
| 533 | + function isArray(arr) { |
| 534 | + return getTag(arr) === '[object Array]'; |
| 535 | + } |
| 536 | + |
| 537 | + function isMap(map) { |
| 538 | + return getTag(map) === '[object Map]'; |
| 539 | + } |
| 540 | + |
| 541 | + function isSet(set) { |
| 542 | + return getTag(set) === '[object Set]'; |
| 543 | + } |
| 544 | + |
| 545 | + /** |
| 546 | + * Gets the size of `collection` by returning its length for array-like |
| 547 | + * values or the number of own enumerable string keyed properties for objects. |
| 548 | + * |
| 549 | + * @category Collection |
| 550 | + * @param {Array|Object|string} collection The collection to inspect. |
| 551 | + * @returns {number} Returns the collection size. |
| 552 | + */ |
| 553 | + |
| 554 | + function size(collection) { |
| 555 | + if (collection === null || collection === undefined) { |
| 556 | + return 0; |
| 557 | + } |
| 558 | + |
| 559 | + if (isArray(collection) || isString(collection)) { |
| 560 | + return collection.length; |
| 561 | + } |
| 562 | + |
| 563 | + if (isMap(collection) || isSet(collection)) { |
| 564 | + return collection.size; |
| 565 | + } |
| 566 | + |
| 567 | + return Object.keys(collection).length; |
| 568 | + } |
| 569 | + |
533 | 570 | /**
|
534 | 571 | * string hash map
|
535 | 572 | * From https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript-jquery
|
|
540 | 577 | var hash = 0;
|
541 | 578 | var i;
|
542 | 579 | var chr;
|
543 |
| - if (_str.length === 0) return hash; |
| 580 | + if (size(_str) === 0) return hash; |
544 | 581 |
|
545 | 582 | for (i = 0; i < _str.length; i++) {
|
546 | 583 | chr = _str.charCodeAt(i);
|
|
570 | 607 | if (strict) {
|
571 | 608 | if (v === vals[key]) return true;
|
572 | 609 | } else {
|
573 |
| - if (v === vals[key]) return true; |
| 610 | + // eslint-disable-next-line eqeqeq |
| 611 | + if (v == vals[key]) return true; |
574 | 612 | }
|
575 | 613 | }
|
576 | 614 |
|
|
597 | 635 | return value !== null && (type === 'object' || type === 'function');
|
598 | 636 | }
|
599 | 637 |
|
600 |
| - function isArray(arr) { |
601 |
| - return getTag(arr) === '[object Array]'; |
602 |
| - } |
603 |
| - |
604 | 638 | function forEach(src, func) {
|
605 | 639 | var i = 0;
|
606 | 640 |
|
|
914 | 948 | var properObject = o => isObject(o) && !o.hasOwnProperty ? { ...o
|
915 | 949 | } : o;
|
916 | 950 |
|
917 |
| - function isMap(map) { |
918 |
| - return getTag(map) === '[object Map]'; |
919 |
| - } |
920 |
| - |
921 |
| - function isSet(set) { |
922 |
| - return getTag(set) === '[object Set]'; |
923 |
| - } |
924 |
| - |
925 | 951 | function isEmpty(obj) {
|
926 | 952 | if (obj === null) {
|
927 | 953 | return true;
|
|
1049 | 1075 | updated: updatedDiff(lhs, rhs)
|
1050 | 1076 | });
|
1051 | 1077 |
|
1052 |
| - /** |
1053 |
| - * Gets the size of `collection` by returning its length for array-like |
1054 |
| - * values or the number of own enumerable string keyed properties for objects. |
1055 |
| - * |
1056 |
| - * @category Collection |
1057 |
| - * @param {Array|Object|string} collection The collection to inspect. |
1058 |
| - * @returns {number} Returns the collection size. |
1059 |
| - */ |
1060 |
| - |
1061 |
| - function size(collection) { |
1062 |
| - if (collection === null || collection === undefined) { |
1063 |
| - return 0; |
1064 |
| - } |
1065 |
| - |
1066 |
| - if (isArray(collection) || isString(collection)) { |
1067 |
| - return collection.length; |
1068 |
| - } |
1069 |
| - |
1070 |
| - if (isMap(collection) || isSet(collection)) { |
1071 |
| - return collection.size; |
1072 |
| - } |
1073 |
| - |
1074 |
| - return Object.keys(collection).length; |
1075 |
| - } |
1076 |
| - |
1077 | 1078 | function loadImages(options) {
|
1078 | 1079 | var len = 0;
|
1079 | 1080 | var index = 0;
|
|
1721 | 1722 |
|
1722 | 1723 | doItem(func, action) {
|
1723 | 1724 | try {
|
1724 |
| - if (typeof func === 'function') { |
| 1725 | + if (isFunction(func)) { |
1725 | 1726 | return func();
|
1726 | 1727 | }
|
1727 | 1728 | } catch (err) {
|
|
1734 | 1735 | }
|
1735 | 1736 |
|
1736 | 1737 | setItem(key, value) {
|
1737 |
| - if (typeof key === 'object') { |
| 1738 | + if (isObject(key)) { |
1738 | 1739 | Object.keys(key).forEach((k, index) => {
|
1739 | 1740 | this.doItem(() => this._storage.setItem(`${this.prefix}.${k}`, JSON.stringify(key[k])), 'setItem');
|
1740 | 1741 | });
|
|
2112 | 2113 | return promiseFunction;
|
2113 | 2114 | }
|
2114 | 2115 |
|
| 2116 | + /* eslint-disable prefer-promise-reject-errors */ |
| 2117 | + |
| 2118 | + /** |
| 2119 | + * @param {any} attempt |
| 2120 | + * @param {any} options |
| 2121 | + * interval: 200, //ms |
| 2122 | + * retries: 2 |
| 2123 | + * timeout: 8000 //ms |
| 2124 | + */ |
| 2125 | + function retry (attempt, options) { |
| 2126 | + var option = options || {}; |
| 2127 | + var interval = option.interval || 400; |
| 2128 | + var retries = option.retries || 2; |
| 2129 | + var timeout = option.timeout || 8000; |
| 2130 | + |
| 2131 | + function rejectDelay(reason) { |
| 2132 | + return new Promise(function (resolve, reject) { |
| 2133 | + setTimeout(reject.bind(null, reason), interval); |
| 2134 | + }); |
| 2135 | + } |
| 2136 | + |
| 2137 | + var p = Promise.reject(); |
| 2138 | + |
| 2139 | + for (var i = 0; i < retries; i++) { |
| 2140 | + p = p.catch(timeoutReject(attempt, timeout)).catch(rejectDelay); |
| 2141 | + } |
| 2142 | + |
| 2143 | + return p; |
| 2144 | + } |
| 2145 | + |
| 2146 | + function timeoutReject(task, timeout) { |
| 2147 | + var timer; |
| 2148 | + return function () { |
| 2149 | + return Promise.race([Promise.reject().catch(task), new Promise(function (rs, rj) { |
| 2150 | + timer = setTimeout(function () { |
| 2151 | + rj('timeout.'); |
| 2152 | + }, timeout || 8000); |
| 2153 | + })]).then(function (r) { |
| 2154 | + clearTimeout(timer); |
| 2155 | + return r; |
| 2156 | + }, function (err) { |
| 2157 | + return Promise.reject(err); |
| 2158 | + }); |
| 2159 | + }; |
| 2160 | + } |
| 2161 | + |
2115 | 2162 | var floor = function (n) {
|
2116 | 2163 | var m = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
2117 | 2164 | return Math.floor(n * Math.pow(10, m)) / Math.pow(10, m);
|
|
3765 | 3812 | formatRemainTime: formatRemainTime,
|
3766 | 3813 | formatDuration: formatDuration,
|
3767 | 3814 | sleep: sleep,
|
| 3815 | + retry: retry, |
3768 | 3816 | // fill
|
3769 | 3817 | fill0: fill0,
|
3770 | 3818 | floor: floor,
|
|
0 commit comments