Skip to content

Commit 2a35e0b

Browse files
committed
feat: build new
1 parent 92df248 commit 2a35e0b

File tree

5 files changed

+96
-48
lines changed

5 files changed

+96
-48
lines changed

dist/bbo.js

+91-43
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* bbo is a utility library of zero dependencies for javascript.
44
* (c) 2011 - 2020
55
* https://github.com/tnfe/bbo.git
6-
* version 1.1.22
6+
* version 1.1.23
77
*/
88

99
(function (global, factory) {
@@ -118,7 +118,7 @@
118118
return getTag(func) === '[object Function]';
119119
}
120120

121-
var version = '1.1.22';
121+
var version = '1.1.23';
122122

123123
var globalObject = null;
124124

@@ -530,6 +530,43 @@
530530
});
531531
}
532532

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+
533570
/**
534571
* string hash map
535572
* From https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript-jquery
@@ -540,7 +577,7 @@
540577
var hash = 0;
541578
var i;
542579
var chr;
543-
if (_str.length === 0) return hash;
580+
if (size(_str) === 0) return hash;
544581

545582
for (i = 0; i < _str.length; i++) {
546583
chr = _str.charCodeAt(i);
@@ -570,7 +607,8 @@
570607
if (strict) {
571608
if (v === vals[key]) return true;
572609
} else {
573-
if (v === vals[key]) return true;
610+
// eslint-disable-next-line eqeqeq
611+
if (v == vals[key]) return true;
574612
}
575613
}
576614

@@ -597,10 +635,6 @@
597635
return value !== null && (type === 'object' || type === 'function');
598636
}
599637

600-
function isArray(arr) {
601-
return getTag(arr) === '[object Array]';
602-
}
603-
604638
function forEach(src, func) {
605639
var i = 0;
606640

@@ -914,14 +948,6 @@
914948
var properObject = o => isObject(o) && !o.hasOwnProperty ? { ...o
915949
} : o;
916950

917-
function isMap(map) {
918-
return getTag(map) === '[object Map]';
919-
}
920-
921-
function isSet(set) {
922-
return getTag(set) === '[object Set]';
923-
}
924-
925951
function isEmpty(obj) {
926952
if (obj === null) {
927953
return true;
@@ -1049,31 +1075,6 @@
10491075
updated: updatedDiff(lhs, rhs)
10501076
});
10511077

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-
10771078
function loadImages(options) {
10781079
var len = 0;
10791080
var index = 0;
@@ -1721,7 +1722,7 @@
17211722

17221723
doItem(func, action) {
17231724
try {
1724-
if (typeof func === 'function') {
1725+
if (isFunction(func)) {
17251726
return func();
17261727
}
17271728
} catch (err) {
@@ -1734,7 +1735,7 @@
17341735
}
17351736

17361737
setItem(key, value) {
1737-
if (typeof key === 'object') {
1738+
if (isObject(key)) {
17381739
Object.keys(key).forEach((k, index) => {
17391740
this.doItem(() => this._storage.setItem(`${this.prefix}.${k}`, JSON.stringify(key[k])), 'setItem');
17401741
});
@@ -2112,6 +2113,52 @@
21122113
return promiseFunction;
21132114
}
21142115

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+
21152162
var floor = function (n) {
21162163
var m = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
21172164
return Math.floor(n * Math.pow(10, m)) / Math.pow(10, m);
@@ -3765,6 +3812,7 @@
37653812
formatRemainTime: formatRemainTime,
37663813
formatDuration: formatDuration,
37673814
sleep: sleep,
3815+
retry: retry,
37683816
// fill
37693817
fill0: fill0,
37703818
floor: floor,

dist/bbo.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/bbo.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bbo",
3-
"version": "1.1.22",
3+
"version": "1.1.23",
44
"description": "bbo is a utility library of zero dependencies for javascript.",
55
"homepage": "https://tnfe.github.io/bbo",
66
"author": "halldwang",

src/util/version.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
const version = '1.1.22';
1+
const version = '1.1.23';
22

33
export default version;

0 commit comments

Comments
 (0)