@daysnap/utils 0.0.41 → 0.0.42
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/README.md +21 -4
- package/docs/README.md +20 -4
- package/docs/modules.md +228 -93
- package/es/base64ToBlob.js +11 -17
- package/es/blobToBase64.js +10 -10
- package/es/canvasToBlob.js +4 -6
- package/es/clone.d.ts +4 -6
- package/es/clone.js +18 -8
- package/es/cloneSimple.d.ts +7 -0
- package/es/cloneSimple.js +9 -0
- package/es/compareVersion.js +27 -40
- package/es/compressImage.js +11 -13
- package/es/createHexColorByHash.js +59 -56
- package/es/debounce.d.ts +5 -0
- package/es/debounce.js +14 -0
- package/es/downloadFile.js +7 -7
- package/es/each.js +11 -10
- package/es/filterBankCardNo.js +4 -2
- package/es/filterCRLF.js +2 -2
- package/es/filterEmoji.js +13 -6
- package/es/filterIdCard.js +2 -2
- package/es/filterName.js +12 -12
- package/es/filterPhone.js +3 -4
- package/es/formatAmount.js +18 -20
- package/es/formatDate.js +28 -29
- package/es/getBlobByUrl.js +17 -18
- package/es/getDayMillisecond.js +3 -4
- package/es/getImageInfo.js +9 -9
- package/es/getRandomColor.js +2 -2
- package/es/getVideoInfo.js +11 -11
- package/es/inBrowser.js +2 -2
- package/es/index.d.ts +4 -0
- package/es/index.js +5 -1
- package/es/isAndroid.js +2 -2
- package/es/isArray.js +2 -2
- package/es/isBoolean.js +2 -2
- package/es/isEmail.js +2 -2
- package/es/isEmpty.js +2 -2
- package/es/isEmptyObject.js +2 -2
- package/es/isError.js +2 -2
- package/es/isFunction.js +2 -2
- package/es/isIOS.js +2 -2
- package/es/isIdCard.js +2 -2
- package/es/isJSONString.js +8 -7
- package/es/isLan.js +29 -28
- package/es/isLicenseCode.js +2 -2
- package/es/isMobile.js +7 -6
- package/es/isNull.js +2 -2
- package/es/isNumber.js +2 -2
- package/es/isObject.js +2 -2
- package/es/isPhone.js +2 -2
- package/es/isPromise.js +2 -2
- package/es/isRegExp.js +2 -2
- package/es/isString.js +2 -2
- package/es/isURL.d.ts +4 -0
- package/es/isURL.js +6 -0
- package/es/isUndefined.js +2 -2
- package/es/isWeixin.js +2 -2
- package/es/isWindow.js +2 -2
- package/es/loadResource.d.ts +1 -1
- package/es/loadResource.js +27 -23
- package/es/omit.js +7 -9
- package/es/padding.js +5 -5
- package/es/parseDate.js +7 -7
- package/es/parseError.js +15 -16
- package/es/parseObject.d.ts +2 -1
- package/es/parseObject.js +9 -8
- package/es/parsePath.js +7 -7
- package/es/parseQuery.d.ts +1 -1
- package/es/parseQuery.js +13 -24
- package/es/pick.js +7 -8
- package/es/replaceCrlf.js +2 -2
- package/es/reserve.js +2 -2
- package/es/round.js +38 -39
- package/es/sleep.d.ts +4 -2
- package/es/sleep.js +8 -7
- package/es/storage/factory.d.ts +2 -2
- package/es/storage/factory.js +44 -54
- package/es/storage/index.d.ts +4 -4
- package/es/storage/index.js +10 -10
- package/es/stringTrim.d.ts +3 -1
- package/es/stringTrim.js +17 -19
- package/es/stringifyQuery.d.ts +1 -1
- package/es/stringifyQuery.js +11 -8
- package/es/throttle.d.ts +5 -0
- package/es/throttle.js +17 -0
- package/es/typeOf.d.ts +1 -1
- package/es/typeOf.js +2 -2
- package/lib/base64ToBlob.js +14 -23
- package/lib/blobToBase64.js +13 -15
- package/lib/canvasToBlob.js +7 -11
- package/lib/clone.d.ts +4 -6
- package/lib/clone.js +21 -13
- package/lib/cloneSimple.d.ts +7 -0
- package/lib/cloneSimple.js +13 -0
- package/lib/compareVersion.js +30 -46
- package/lib/compressImage.js +15 -19
- package/lib/createHexColorByHash.js +60 -60
- package/lib/debounce.d.ts +5 -0
- package/lib/debounce.js +18 -0
- package/lib/downloadFile.js +10 -12
- package/lib/each.js +14 -15
- package/lib/filterBankCardNo.js +7 -7
- package/lib/filterCRLF.js +5 -7
- package/lib/filterEmoji.js +16 -11
- package/lib/filterIdCard.js +5 -7
- package/lib/filterName.js +15 -17
- package/lib/filterPhone.js +6 -9
- package/lib/formatAmount.js +21 -25
- package/lib/formatDate.js +32 -35
- package/lib/getBlobByUrl.js +20 -23
- package/lib/getDayMillisecond.js +6 -9
- package/lib/getImageInfo.js +12 -14
- package/lib/getRandomColor.js +5 -7
- package/lib/getVideoInfo.js +14 -16
- package/lib/inBrowser.js +5 -7
- package/lib/index.d.ts +4 -0
- package/lib/index.js +84 -708
- package/lib/isAndroid.js +5 -7
- package/lib/isArray.js +5 -7
- package/lib/isBoolean.js +5 -7
- package/lib/isEmail.js +5 -7
- package/lib/isEmpty.js +7 -9
- package/lib/isEmptyObject.js +5 -7
- package/lib/isError.js +5 -7
- package/lib/isFunction.js +5 -7
- package/lib/isIOS.js +5 -7
- package/lib/isIdCard.js +5 -7
- package/lib/isJSONString.js +12 -13
- package/lib/isLan.js +32 -33
- package/lib/isLicenseCode.js +5 -7
- package/lib/isMobile.js +10 -11
- package/lib/isNull.js +5 -7
- package/lib/isNumber.js +5 -7
- package/lib/isObject.js +5 -7
- package/lib/isPhone.js +5 -7
- package/lib/isPromise.js +5 -7
- package/lib/isRegExp.js +5 -7
- package/lib/isString.js +5 -7
- package/lib/isURL.d.ts +4 -0
- package/lib/isURL.js +10 -0
- package/lib/isUndefined.js +5 -7
- package/lib/isWeixin.js +5 -7
- package/lib/isWindow.js +5 -7
- package/lib/loadResource.d.ts +1 -1
- package/lib/loadResource.js +31 -29
- package/lib/omit.js +10 -15
- package/lib/padding.js +8 -10
- package/lib/parseDate.js +10 -12
- package/lib/parseError.js +21 -24
- package/lib/parseObject.d.ts +2 -1
- package/lib/parseObject.js +12 -13
- package/lib/parsePath.js +10 -12
- package/lib/parseQuery.d.ts +1 -1
- package/lib/parseQuery.js +17 -31
- package/lib/pick.js +10 -13
- package/lib/replaceCrlf.js +5 -7
- package/lib/reserve.js +6 -8
- package/lib/round.js +41 -44
- package/lib/sleep.d.ts +4 -2
- package/lib/sleep.js +11 -12
- package/lib/storage/factory.d.ts +2 -2
- package/lib/storage/factory.js +47 -60
- package/lib/storage/index.d.ts +4 -4
- package/lib/storage/index.js +12 -17
- package/lib/stringTrim.d.ts +3 -1
- package/lib/stringTrim.js +20 -24
- package/lib/stringifyQuery.d.ts +1 -1
- package/lib/stringifyQuery.js +15 -14
- package/lib/throttle.d.ts +5 -0
- package/lib/throttle.js +21 -0
- package/lib/typeOf.d.ts +1 -1
- package/lib/typeOf.js +5 -7
- package/package.json +15 -17
package/lib/omit.js
CHANGED
|
@@ -1,19 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
Object.defineProperty(exports, "__esModule", {
|
|
5
|
-
value: true
|
|
6
|
-
});
|
|
7
|
-
exports.omit = omit;
|
|
8
|
-
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.omit = void 0;
|
|
9
4
|
/**
|
|
10
5
|
* 排除对象里的某些属性,返回剩下的对象,浅拷贝
|
|
11
6
|
*/
|
|
12
|
-
function omit(obj) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
7
|
+
function omit(obj, fields = []) {
|
|
8
|
+
const clone = Object.assign({}, obj);
|
|
9
|
+
fields.forEach((key) => {
|
|
10
|
+
delete clone[key];
|
|
11
|
+
});
|
|
12
|
+
return clone;
|
|
13
|
+
}
|
|
14
|
+
exports.omit = omit;
|
package/lib/padding.js
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.padding = padding;
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.padding = void 0;
|
|
7
4
|
/**
|
|
8
5
|
* 增添空格
|
|
9
6
|
* @param message 文字信息
|
|
10
7
|
* @param before 前空格个数
|
|
11
8
|
* @param after 后空格个数
|
|
12
9
|
*/
|
|
13
|
-
function padding(message) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
10
|
+
function padding(message, before = 1, after = 1) {
|
|
11
|
+
return `${new Array(before).fill(' ').join('')}${message}${new Array(after)
|
|
12
|
+
.fill(' ')
|
|
13
|
+
.join('')}`;
|
|
14
|
+
}
|
|
15
|
+
exports.padding = padding;
|
package/lib/parseDate.js
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.parseDate = parseDate;
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseDate = void 0;
|
|
7
4
|
/**
|
|
8
5
|
* 解析成Date
|
|
9
6
|
*/
|
|
10
7
|
function parseDate(v) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
8
|
+
if (typeof v === 'string') {
|
|
9
|
+
// fix ios 格式日期错误
|
|
10
|
+
// eslint-disable-next-line no-param-reassign
|
|
11
|
+
v = v.replace(/-/g, '/');
|
|
12
|
+
}
|
|
13
|
+
return new Date(v);
|
|
14
|
+
}
|
|
15
|
+
exports.parseDate = parseDate;
|
package/lib/parseError.js
CHANGED
|
@@ -1,29 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var _isEmpty = require("./isEmpty");
|
|
8
|
-
var _isObject = require("./isObject");
|
|
9
|
-
var _isUndefined = require("./isUndefined");
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseError = void 0;
|
|
4
|
+
const isEmpty_1 = require("./isEmpty");
|
|
5
|
+
const isObject_1 = require("./isObject");
|
|
6
|
+
const isUndefined_1 = require("./isUndefined");
|
|
10
7
|
/**
|
|
11
8
|
* 解析格式化 error 获取对应的 message
|
|
12
9
|
*/
|
|
13
|
-
function parseError(val) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
10
|
+
function parseError(val, keys = ['message', 'msg', 'errorMsg']) {
|
|
11
|
+
if ((0, isEmpty_1.isEmpty)(val)) {
|
|
12
|
+
// null or '' or undefined
|
|
13
|
+
return '';
|
|
14
|
+
}
|
|
15
|
+
if ((0, isObject_1.isObject)(val)) {
|
|
16
|
+
for (let i = 0, len = keys.length; i < len; i++) {
|
|
17
|
+
const key = keys[i];
|
|
18
|
+
const v = val[key];
|
|
19
|
+
if (!(0, isUndefined_1.isUndefined)(v)) {
|
|
20
|
+
return v;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
26
23
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
return JSON.stringify(val);
|
|
25
|
+
}
|
|
26
|
+
exports.parseError = parseError;
|
package/lib/parseObject.d.ts
CHANGED
package/lib/parseObject.js
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.parseObject = parseObject;
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseObject = void 0;
|
|
7
4
|
/**
|
|
8
5
|
* 返回对象
|
|
6
|
+
* parseObject('a.b.c', 1) => { a: { b: { c: 1 } } }
|
|
9
7
|
*/
|
|
10
8
|
function parseObject(path, value) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
9
|
+
const result = {};
|
|
10
|
+
const segments = path ? path.split('.') : [];
|
|
11
|
+
segments.reduce((res, key, index, arr) => {
|
|
12
|
+
res[key] = index === arr.length - 1 ? value : {};
|
|
13
|
+
return res[key];
|
|
14
|
+
}, result);
|
|
15
|
+
return result;
|
|
16
|
+
}
|
|
17
|
+
exports.parseObject = parseObject;
|
package/lib/parsePath.js
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.parsePath = parsePath;
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parsePath = void 0;
|
|
7
4
|
/**
|
|
8
5
|
* 根据路径解析值
|
|
9
6
|
*/
|
|
10
7
|
function parsePath(obj, path) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
8
|
+
const segments = path ? path.split('.') : [];
|
|
9
|
+
segments.forEach((key) => {
|
|
10
|
+
// eslint-disable-next-line no-param-reassign
|
|
11
|
+
obj = obj[key];
|
|
12
|
+
});
|
|
13
|
+
return obj;
|
|
14
|
+
}
|
|
15
|
+
exports.parsePath = parsePath;
|
package/lib/parseQuery.d.ts
CHANGED
package/lib/parseQuery.js
CHANGED
|
@@ -1,39 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
value: true
|
|
6
|
-
});
|
|
7
|
-
exports.parseQuery = parseQuery;
|
|
8
|
-
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
9
|
-
var _createForOfIteratorHelper2 = _interopRequireDefault(require("@babel/runtime/helpers/createForOfIteratorHelper"));
|
|
10
|
-
var _isJSONString = require("./isJSONString");
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseQuery = void 0;
|
|
4
|
+
const isJSONString_1 = require("./isJSONString");
|
|
11
5
|
/**
|
|
12
6
|
* 解析url参数
|
|
13
7
|
* @param v URl
|
|
14
8
|
* @param k 键名
|
|
15
9
|
*/
|
|
16
10
|
function parseQuery(v, k) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
var _step$value = (0, _slicedToArray2["default"])(_step.value, 2),
|
|
29
|
-
key = _step$value[0],
|
|
30
|
-
value = _step$value[1];
|
|
31
|
-
res[key] = (0, _isJSONString.isJSONString)(value) ? JSON.parse(value) : value;
|
|
11
|
+
const url = new URL(v !== null && v !== void 0 ? v : '');
|
|
12
|
+
const searchStr = v ? url.search : window.location.search;
|
|
13
|
+
const query = new URLSearchParams(decodeURIComponent(searchStr));
|
|
14
|
+
// 单个直接返回
|
|
15
|
+
if (k) {
|
|
16
|
+
return query.get(k);
|
|
17
|
+
}
|
|
18
|
+
const res = {};
|
|
19
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
20
|
+
for (const [key, value] of query.entries()) {
|
|
21
|
+
res[key] = (0, isJSONString_1.isJSONString)(value) ? JSON.parse(value) : value;
|
|
32
22
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
_iterator.f();
|
|
37
|
-
}
|
|
38
|
-
return res;
|
|
39
|
-
}
|
|
23
|
+
return res;
|
|
24
|
+
}
|
|
25
|
+
exports.parseQuery = parseQuery;
|
package/lib/pick.js
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.pick = pick;
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.pick = void 0;
|
|
7
4
|
/**
|
|
8
5
|
* 挑选对象里的某些属性,新对象返回
|
|
9
6
|
*/
|
|
10
|
-
function pick(obj) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
7
|
+
function pick(obj, fields = []) {
|
|
8
|
+
const clone = {};
|
|
9
|
+
fields.forEach((key) => {
|
|
10
|
+
clone[key] = obj[key];
|
|
11
|
+
});
|
|
12
|
+
return clone;
|
|
13
|
+
}
|
|
14
|
+
exports.pick = pick;
|
package/lib/replaceCrlf.js
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.replaceCrlf = replaceCrlf;
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.replaceCrlf = void 0;
|
|
7
4
|
/**
|
|
8
5
|
* 替换字符串里面的回车换行
|
|
9
6
|
*/
|
|
10
7
|
function replaceCrlf(val, replaceVal) {
|
|
11
|
-
|
|
12
|
-
}
|
|
8
|
+
return val ? val.replace(/\\r|\\n/gi, replaceVal) : '';
|
|
9
|
+
}
|
|
10
|
+
exports.replaceCrlf = replaceCrlf;
|
package/lib/reserve.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
});
|
|
6
|
-
exports.reserve = reserve;
|
|
7
|
-
var _isEmpty = require("./isEmpty");
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.reserve = void 0;
|
|
4
|
+
const isEmpty_1 = require("./isEmpty");
|
|
8
5
|
/**
|
|
9
6
|
* 值为 ''、undefined、null 则取默认值
|
|
10
7
|
* 解决:
|
|
@@ -12,5 +9,6 @@ var _isEmpty = require("./isEmpty");
|
|
|
12
9
|
* reserve(0, '--') => 0
|
|
13
10
|
*/
|
|
14
11
|
function reserve(val, defaultVal) {
|
|
15
|
-
|
|
16
|
-
}
|
|
12
|
+
return (0, isEmpty_1.isEmpty)(val) ? defaultVal : val;
|
|
13
|
+
}
|
|
14
|
+
exports.reserve = reserve;
|
package/lib/round.js
CHANGED
|
@@ -1,54 +1,51 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.round = round;
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.round = void 0;
|
|
7
4
|
/**
|
|
8
5
|
* 四舍五入
|
|
9
6
|
* @param val 需要四舍五入的值
|
|
10
7
|
* @param fractionDigits 需要保留的小数点位数
|
|
11
8
|
*/
|
|
12
9
|
function round(val, fractionDigits) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
return number.toString();
|
|
17
|
-
}
|
|
18
|
-
if (typeof fractionDigits === 'undefined' || fractionDigits === 0) {
|
|
19
|
-
return Math.round(number).toString();
|
|
20
|
-
}
|
|
21
|
-
var result = number.toString();
|
|
22
|
-
var arr = result.split('.');
|
|
23
|
-
// 整数的情况
|
|
24
|
-
if (arr.length < 2) {
|
|
25
|
-
result += '.';
|
|
26
|
-
for (var i = 0; i < fractionDigits; i += 1) {
|
|
27
|
-
result += '0';
|
|
10
|
+
const number = val;
|
|
11
|
+
if (number >= Math.pow(10, 21)) {
|
|
12
|
+
return number.toString();
|
|
28
13
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
14
|
+
if (typeof fractionDigits === 'undefined' || fractionDigits === 0) {
|
|
15
|
+
return Math.round(number).toString();
|
|
16
|
+
}
|
|
17
|
+
let result = number.toString();
|
|
18
|
+
const arr = result.split('.');
|
|
19
|
+
// 整数的情况
|
|
20
|
+
if (arr.length < 2) {
|
|
21
|
+
result += '.';
|
|
22
|
+
for (let i = 0; i < fractionDigits; i += 1) {
|
|
23
|
+
result += '0';
|
|
24
|
+
}
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
const integer = arr[0];
|
|
28
|
+
const decimal = arr[1];
|
|
29
|
+
if (decimal.length === fractionDigits) {
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
32
|
+
if (decimal.length < fractionDigits) {
|
|
33
|
+
for (let i = 0; i < fractionDigits - decimal.length; i += 1) {
|
|
34
|
+
result += '0';
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
result = `${integer}.${decimal.substr(0, fractionDigits)}`;
|
|
39
|
+
let last = decimal.substr(fractionDigits, 1);
|
|
40
|
+
if (/^\d(9){5,}[0-9]$/.test(decimal.substr(fractionDigits))) {
|
|
41
|
+
last = (+last + 1).toString();
|
|
42
|
+
}
|
|
43
|
+
// 四舍五入,转换为整数再处理,避免浮点数精度的损失
|
|
44
|
+
if (parseInt(last, 10) >= 5) {
|
|
45
|
+
const x = Math.pow(10, fractionDigits);
|
|
46
|
+
result = (Math.round(parseFloat(result) * x) + 1) / x;
|
|
47
|
+
result = round(result, fractionDigits);
|
|
39
48
|
}
|
|
40
49
|
return result;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
var last = decimal.substr(fractionDigits, 1);
|
|
44
|
-
if (/^\d(9){5,}[0-9]$/.test(decimal.substr(fractionDigits))) {
|
|
45
|
-
last = (+last + 1).toString();
|
|
46
|
-
}
|
|
47
|
-
// 四舍五入,转换为整数再处理,避免浮点数精度的损失
|
|
48
|
-
if (parseInt(last, 10) >= 5) {
|
|
49
|
-
var x = Math.pow(10, fractionDigits);
|
|
50
|
-
result = (Math.round(parseFloat(result) * x) + 1) / x;
|
|
51
|
-
result = round(result, fractionDigits);
|
|
52
|
-
}
|
|
53
|
-
return result;
|
|
54
|
-
}
|
|
50
|
+
}
|
|
51
|
+
exports.round = round;
|
package/lib/sleep.d.ts
CHANGED
package/lib/sleep.js
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.sleep = sleep;
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sleep = void 0;
|
|
7
4
|
/**
|
|
8
|
-
*
|
|
5
|
+
* 休眠方法
|
|
6
|
+
* @param ms 休眠时长 毫秒
|
|
7
|
+
* @param isSuccess 默认执行成功
|
|
9
8
|
*/
|
|
10
|
-
function sleep(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
9
|
+
function sleep(ms, isSuccess = true) {
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
setTimeout(isSuccess ? resolve : reject, ms);
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
exports.sleep = sleep;
|
package/lib/storage/factory.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
export declare function factory(type: 'sessionStorage' | 'localStorage'): {
|
|
2
2
|
setItem: <T = any>(key: string, val: T) => T;
|
|
3
|
-
getItem: <T_1 = any>(key: string, defaultVal?: Partial<T_1>) => T_1;
|
|
3
|
+
getItem: <T_1 = any>(key: string, defaultVal?: Partial<T_1> | null) => T_1;
|
|
4
4
|
removeItem: (key: string) => void;
|
|
5
5
|
updateItem: <T_2 = any>(key: string, val: Partial<T_2>) => T_2;
|
|
6
6
|
clear: () => void;
|
|
7
7
|
generate: <T_3 = any>(key: string) => {
|
|
8
8
|
setItem: (val: T_3) => T_3;
|
|
9
|
-
getItem: (defaultVal?: Partial<T_3>) => T_3;
|
|
9
|
+
getItem: (defaultVal?: Partial<T_3> | undefined) => T_3;
|
|
10
10
|
removeItem: () => void;
|
|
11
11
|
updateItem: (val: Partial<T_3>) => T_3;
|
|
12
12
|
};
|
package/lib/storage/factory.js
CHANGED
|
@@ -1,64 +1,51 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
Object.defineProperty(exports, "__esModule", {
|
|
5
|
-
value: true
|
|
6
|
-
});
|
|
7
|
-
exports.factory = factory;
|
|
8
|
-
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.factory = void 0;
|
|
9
4
|
function factory(type) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
5
|
+
const fn = window[type];
|
|
6
|
+
// 设置
|
|
7
|
+
const setItem = (key, val) => {
|
|
8
|
+
fn.setItem(key, JSON.stringify(val));
|
|
9
|
+
return val;
|
|
10
|
+
};
|
|
11
|
+
// 获取
|
|
12
|
+
const getItem = (key, defaultVal = null) => {
|
|
13
|
+
const val = fn.getItem(key);
|
|
14
|
+
// 如果为 null 取默认值
|
|
15
|
+
if (val === null) {
|
|
16
|
+
return defaultVal;
|
|
17
|
+
}
|
|
18
|
+
return JSON.parse(val);
|
|
19
|
+
};
|
|
20
|
+
// 删除
|
|
21
|
+
const removeItem = (key) => {
|
|
22
|
+
fn.removeItem(key);
|
|
23
|
+
};
|
|
24
|
+
// 更新
|
|
25
|
+
const updateItem = (key, val) => {
|
|
26
|
+
const prev = getItem(key);
|
|
27
|
+
return setItem(key, Object.assign(Object.assign({}, prev), val));
|
|
28
|
+
};
|
|
29
|
+
// 清除
|
|
30
|
+
const clear = () => {
|
|
31
|
+
fn.clear();
|
|
32
|
+
};
|
|
33
|
+
// 生成
|
|
34
|
+
const generate = (key) => {
|
|
35
|
+
return {
|
|
36
|
+
setItem: (val) => setItem(key, val),
|
|
37
|
+
getItem: (defaultVal) => getItem(key, defaultVal),
|
|
38
|
+
removeItem: () => removeItem(key),
|
|
39
|
+
updateItem: (val) => updateItem(key, val),
|
|
40
|
+
};
|
|
41
|
+
};
|
|
41
42
|
return {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
removeItem: function removeItem() {
|
|
49
|
-
return _removeItem(key);
|
|
50
|
-
},
|
|
51
|
-
updateItem: function updateItem(val) {
|
|
52
|
-
return _updateItem(key, val);
|
|
53
|
-
}
|
|
43
|
+
setItem,
|
|
44
|
+
getItem,
|
|
45
|
+
removeItem,
|
|
46
|
+
updateItem,
|
|
47
|
+
clear,
|
|
48
|
+
generate,
|
|
54
49
|
};
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
setItem: _setItem,
|
|
58
|
-
getItem: _getItem,
|
|
59
|
-
removeItem: _removeItem,
|
|
60
|
-
updateItem: _updateItem,
|
|
61
|
-
clear: clear,
|
|
62
|
-
generate: generate
|
|
63
|
-
};
|
|
64
|
-
}
|
|
50
|
+
}
|
|
51
|
+
exports.factory = factory;
|
package/lib/storage/index.d.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
export declare const getCache: () => {
|
|
2
2
|
setItem: <T = any>(key: string, val: T) => T;
|
|
3
|
-
getItem: <T_1 = any>(key: string, defaultVal?: Partial<T_1>) => T_1;
|
|
3
|
+
getItem: <T_1 = any>(key: string, defaultVal?: Partial<T_1> | null) => T_1;
|
|
4
4
|
removeItem: (key: string) => void;
|
|
5
5
|
updateItem: <T_2 = any>(key: string, val: Partial<T_2>) => T_2;
|
|
6
6
|
clear: () => void;
|
|
7
7
|
generate: <T_3 = any>(key: string) => {
|
|
8
8
|
setItem: (val: T_3) => T_3;
|
|
9
|
-
getItem: (defaultVal?: Partial<T_3>) => T_3;
|
|
9
|
+
getItem: (defaultVal?: Partial<T_3> | undefined) => T_3;
|
|
10
10
|
removeItem: () => void;
|
|
11
11
|
updateItem: (val: Partial<T_3>) => T_3;
|
|
12
12
|
};
|
|
13
13
|
};
|
|
14
14
|
export declare const getLocal: () => {
|
|
15
15
|
setItem: <T = any>(key: string, val: T) => T;
|
|
16
|
-
getItem: <T_1 = any>(key: string, defaultVal?: Partial<T_1>) => T_1;
|
|
16
|
+
getItem: <T_1 = any>(key: string, defaultVal?: Partial<T_1> | null) => T_1;
|
|
17
17
|
removeItem: (key: string) => void;
|
|
18
18
|
updateItem: <T_2 = any>(key: string, val: Partial<T_2>) => T_2;
|
|
19
19
|
clear: () => void;
|
|
20
20
|
generate: <T_3 = any>(key: string) => {
|
|
21
21
|
setItem: (val: T_3) => T_3;
|
|
22
|
-
getItem: (defaultVal?: Partial<T_3>) => T_3;
|
|
22
|
+
getItem: (defaultVal?: Partial<T_3> | undefined) => T_3;
|
|
23
23
|
removeItem: () => void;
|
|
24
24
|
updateItem: (val: Partial<T_3>) => T_3;
|
|
25
25
|
};
|