@daysnap/utils 0.0.41 → 0.0.43

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.
Files changed (174) hide show
  1. package/README.md +24 -4
  2. package/docs/README.md +20 -4
  3. package/docs/modules.md +228 -93
  4. package/es/base64ToBlob.js +11 -17
  5. package/es/blobToBase64.js +10 -10
  6. package/es/canvasToBlob.js +4 -6
  7. package/es/clone.d.ts +4 -6
  8. package/es/clone.js +18 -8
  9. package/es/cloneSimple.d.ts +7 -0
  10. package/es/cloneSimple.js +9 -0
  11. package/es/compareVersion.js +27 -40
  12. package/es/compressImage.js +11 -13
  13. package/es/createHexColorByHash.js +59 -56
  14. package/es/debounce.d.ts +5 -0
  15. package/es/debounce.js +14 -0
  16. package/es/downloadFile.js +7 -7
  17. package/es/each.js +11 -10
  18. package/es/filterBankCardNo.js +4 -2
  19. package/es/filterCRLF.js +2 -2
  20. package/es/filterEmoji.js +13 -6
  21. package/es/filterIdCard.js +2 -2
  22. package/es/filterName.js +12 -12
  23. package/es/filterPhone.js +3 -4
  24. package/es/formatAmount.js +18 -20
  25. package/es/formatDate.js +28 -29
  26. package/es/getBlobByUrl.js +17 -18
  27. package/es/getDayMillisecond.js +3 -4
  28. package/es/getImageInfo.js +9 -9
  29. package/es/getRandomColor.js +2 -2
  30. package/es/getVideoInfo.js +11 -11
  31. package/es/inBrowser.js +2 -2
  32. package/es/index.d.ts +4 -0
  33. package/es/index.js +5 -1
  34. package/es/isAndroid.js +2 -2
  35. package/es/isArray.js +2 -2
  36. package/es/isBoolean.js +2 -2
  37. package/es/isEmail.js +2 -2
  38. package/es/isEmpty.js +2 -2
  39. package/es/isEmptyObject.js +2 -2
  40. package/es/isError.js +2 -2
  41. package/es/isFunction.js +2 -2
  42. package/es/isIOS.js +2 -2
  43. package/es/isIdCard.js +2 -2
  44. package/es/isJSONString.js +8 -7
  45. package/es/isLan.js +29 -28
  46. package/es/isLicenseCode.js +2 -2
  47. package/es/isMobile.js +7 -6
  48. package/es/isNull.js +2 -2
  49. package/es/isNumber.js +2 -2
  50. package/es/isObject.js +2 -2
  51. package/es/isPhone.js +2 -2
  52. package/es/isPromise.js +2 -2
  53. package/es/isRegExp.js +2 -2
  54. package/es/isString.js +2 -2
  55. package/es/isURL.d.ts +4 -0
  56. package/es/isURL.js +6 -0
  57. package/es/isUndefined.js +2 -2
  58. package/es/isWeixin.js +2 -2
  59. package/es/isWindow.js +2 -2
  60. package/es/loadResource.d.ts +1 -1
  61. package/es/loadResource.js +27 -23
  62. package/es/omit.js +7 -9
  63. package/es/padding.js +5 -5
  64. package/es/parseDate.js +7 -7
  65. package/es/parseError.js +15 -16
  66. package/es/parseObject.d.ts +2 -1
  67. package/es/parseObject.js +9 -8
  68. package/es/parsePath.js +7 -7
  69. package/es/parseQuery.d.ts +1 -1
  70. package/es/parseQuery.js +13 -24
  71. package/es/pick.js +7 -8
  72. package/es/replaceCrlf.js +2 -2
  73. package/es/reserve.js +2 -2
  74. package/es/round.js +38 -39
  75. package/es/sleep.d.ts +4 -2
  76. package/es/sleep.js +8 -7
  77. package/es/storage/factory.d.ts +2 -2
  78. package/es/storage/factory.js +44 -54
  79. package/es/storage/index.d.ts +4 -4
  80. package/es/storage/index.js +10 -10
  81. package/es/stringTrim.d.ts +3 -1
  82. package/es/stringTrim.js +17 -19
  83. package/es/stringifyQuery.d.ts +1 -1
  84. package/es/stringifyQuery.js +11 -8
  85. package/es/throttle.d.ts +5 -0
  86. package/es/throttle.js +17 -0
  87. package/es/typeOf.d.ts +1 -1
  88. package/es/typeOf.js +2 -2
  89. package/lib/base64ToBlob.js +14 -23
  90. package/lib/blobToBase64.js +13 -15
  91. package/lib/canvasToBlob.js +7 -11
  92. package/lib/clone.d.ts +4 -6
  93. package/lib/clone.js +21 -13
  94. package/lib/cloneSimple.d.ts +7 -0
  95. package/lib/cloneSimple.js +13 -0
  96. package/lib/compareVersion.js +30 -46
  97. package/lib/compressImage.js +15 -19
  98. package/lib/createHexColorByHash.js +60 -60
  99. package/lib/debounce.d.ts +5 -0
  100. package/lib/debounce.js +18 -0
  101. package/lib/downloadFile.js +10 -12
  102. package/lib/each.js +14 -15
  103. package/lib/filterBankCardNo.js +7 -7
  104. package/lib/filterCRLF.js +5 -7
  105. package/lib/filterEmoji.js +16 -11
  106. package/lib/filterIdCard.js +5 -7
  107. package/lib/filterName.js +15 -17
  108. package/lib/filterPhone.js +6 -9
  109. package/lib/formatAmount.js +21 -25
  110. package/lib/formatDate.js +32 -35
  111. package/lib/getBlobByUrl.js +20 -23
  112. package/lib/getDayMillisecond.js +6 -9
  113. package/lib/getImageInfo.js +12 -14
  114. package/lib/getRandomColor.js +5 -7
  115. package/lib/getVideoInfo.js +14 -16
  116. package/lib/inBrowser.js +5 -7
  117. package/lib/index.d.ts +4 -0
  118. package/lib/index.js +84 -708
  119. package/lib/isAndroid.js +5 -7
  120. package/lib/isArray.js +5 -7
  121. package/lib/isBoolean.js +5 -7
  122. package/lib/isEmail.js +5 -7
  123. package/lib/isEmpty.js +7 -9
  124. package/lib/isEmptyObject.js +5 -7
  125. package/lib/isError.js +5 -7
  126. package/lib/isFunction.js +5 -7
  127. package/lib/isIOS.js +5 -7
  128. package/lib/isIdCard.js +5 -7
  129. package/lib/isJSONString.js +12 -13
  130. package/lib/isLan.js +32 -33
  131. package/lib/isLicenseCode.js +5 -7
  132. package/lib/isMobile.js +10 -11
  133. package/lib/isNull.js +5 -7
  134. package/lib/isNumber.js +5 -7
  135. package/lib/isObject.js +5 -7
  136. package/lib/isPhone.js +5 -7
  137. package/lib/isPromise.js +5 -7
  138. package/lib/isRegExp.js +5 -7
  139. package/lib/isString.js +5 -7
  140. package/lib/isURL.d.ts +4 -0
  141. package/lib/isURL.js +10 -0
  142. package/lib/isUndefined.js +5 -7
  143. package/lib/isWeixin.js +5 -7
  144. package/lib/isWindow.js +5 -7
  145. package/lib/loadResource.d.ts +1 -1
  146. package/lib/loadResource.js +31 -29
  147. package/lib/omit.js +10 -15
  148. package/lib/padding.js +8 -10
  149. package/lib/parseDate.js +10 -12
  150. package/lib/parseError.js +21 -24
  151. package/lib/parseObject.d.ts +2 -1
  152. package/lib/parseObject.js +12 -13
  153. package/lib/parsePath.js +10 -12
  154. package/lib/parseQuery.d.ts +1 -1
  155. package/lib/parseQuery.js +17 -31
  156. package/lib/pick.js +10 -13
  157. package/lib/replaceCrlf.js +5 -7
  158. package/lib/reserve.js +6 -8
  159. package/lib/round.js +41 -44
  160. package/lib/sleep.d.ts +4 -2
  161. package/lib/sleep.js +11 -12
  162. package/lib/storage/factory.d.ts +2 -2
  163. package/lib/storage/factory.js +47 -60
  164. package/lib/storage/index.d.ts +4 -4
  165. package/lib/storage/index.js +12 -17
  166. package/lib/stringTrim.d.ts +3 -1
  167. package/lib/stringTrim.js +20 -24
  168. package/lib/stringifyQuery.d.ts +1 -1
  169. package/lib/stringifyQuery.js +15 -14
  170. package/lib/throttle.d.ts +5 -0
  171. package/lib/throttle.js +21 -0
  172. package/lib/typeOf.d.ts +1 -1
  173. package/lib/typeOf.js +5 -7
  174. package/package.json +23 -18
package/lib/omit.js CHANGED
@@ -1,19 +1,14 @@
1
1
  "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
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
- var fields = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
14
- var clone = (0, _objectSpread2["default"])({}, obj);
15
- fields.forEach(function (key) {
16
- delete clone[key];
17
- });
18
- return clone;
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
- Object.defineProperty(exports, "__esModule", {
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
- var before = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
15
- var after = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
16
- return "".concat(new Array(before).fill(' ').join('')).concat(message).concat(new Array(after).fill(' ').join(''));
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
- Object.defineProperty(exports, "__esModule", {
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
- if (typeof v === 'string') {
12
- // fix ios 格式日期错误
13
- // eslint-disable-next-line no-param-reassign
14
- v = v.replace(/-/g, '/');
15
- }
16
- return new Date(v);
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
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.parseError = parseError;
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
- var keys = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ['message', 'msg', 'errorMsg'];
15
- if ((0, _isEmpty.isEmpty)(val)) {
16
- // null or '' or undefined
17
- return '';
18
- }
19
- if ((0, _isObject.isObject)(val)) {
20
- for (var i = 0, len = keys.length; i < len; i++) {
21
- var key = keys[i];
22
- var v = val[key];
23
- if (!(0, _isUndefined.isUndefined)(v)) {
24
- return v;
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
- return JSON.stringify(val);
29
- }
24
+ return JSON.stringify(val);
25
+ }
26
+ exports.parseError = parseError;
@@ -1,4 +1,5 @@
1
1
  /**
2
2
  * 返回对象
3
+ * parseObject('a.b.c', 1) => { a: { b: { c: 1 } } }
3
4
  */
4
- export declare function parseObject<T = any>(path: string, value: any): T;
5
+ export declare function parseObject<T = Record<string, any>>(path: string, value: any): T;
@@ -1,18 +1,17 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
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
- var result = {};
12
- var segments = path ? path.split('.') : [];
13
- segments.reduce(function (res, key, index, arr) {
14
- res[key] = index === arr.length - 1 ? value : {};
15
- return res[key];
16
- }, result);
17
- return result;
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
- Object.defineProperty(exports, "__esModule", {
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
- var segments = path ? path.split('.') : [];
12
- segments.forEach(function (key) {
13
- // eslint-disable-next-line no-param-reassign
14
- obj = obj[key];
15
- });
16
- return obj;
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;
@@ -3,4 +3,4 @@
3
3
  * @param v URl
4
4
  * @param k 键名
5
5
  */
6
- export declare function parseQuery(v?: string, k?: string): {};
6
+ export declare function parseQuery(v?: string, k?: string): any;
package/lib/parseQuery.js CHANGED
@@ -1,39 +1,25 @@
1
1
  "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
4
- Object.defineProperty(exports, "__esModule", {
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
- var url = new URL(v);
18
- var searchStr = v ? url.search : window.location.search;
19
- var query = new URLSearchParams(decodeURIComponent(searchStr));
20
- // 单个直接返回
21
- if (k) return query.get(k);
22
- var res = {};
23
- // eslint-disable-next-line no-restricted-syntax
24
- var _iterator = (0, _createForOfIteratorHelper2["default"])(query.entries()),
25
- _step;
26
- try {
27
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
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
- } catch (err) {
34
- _iterator.e(err);
35
- } finally {
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
- Object.defineProperty(exports, "__esModule", {
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
- var fields = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
12
- var clone = {};
13
- fields.forEach(function (key) {
14
- clone[key] = obj[key];
15
- });
16
- return clone;
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;
@@ -1,12 +1,10 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
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
- return val ? val.replace(/\\r|\\n/gi, replaceVal) : '';
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
- Object.defineProperty(exports, "__esModule", {
4
- value: true
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
- return (0, _isEmpty.isEmpty)(val) ? defaultVal : val;
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
- Object.defineProperty(exports, "__esModule", {
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
- // todo
14
- var number = val;
15
- if (number >= Math.pow(10, 21)) {
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
- return result;
30
- }
31
- var integer = arr[0];
32
- var decimal = arr[1];
33
- if (decimal.length === fractionDigits) {
34
- return result;
35
- }
36
- if (decimal.length < fractionDigits) {
37
- for (var _i = 0; _i < fractionDigits - decimal.length; _i += 1) {
38
- result += '0';
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
- result = "".concat(integer, ".").concat(decimal.substr(0, fractionDigits));
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
@@ -1,4 +1,6 @@
1
1
  /**
2
- * 说明
2
+ * 休眠方法
3
+ * @param ms 休眠时长 毫秒
4
+ * @param isSuccess 默认执行成功
3
5
  */
4
- export declare function sleep(timeout: number, isSuccess?: boolean): Promise<void>;
6
+ export declare function sleep(ms: number, isSuccess?: boolean): Promise<void>;
package/lib/sleep.js CHANGED
@@ -1,15 +1,14 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
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(timeout) {
11
- var isSuccess = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
12
- return new Promise(function (resolve, reject) {
13
- setTimeout(isSuccess ? resolve : reject, timeout);
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;
@@ -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
  };
@@ -1,64 +1,51 @@
1
1
  "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
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
- var fn = window[type];
11
- // 设置
12
- var _setItem = function setItem(key, val) {
13
- fn.setItem(key, JSON.stringify(val));
14
- return val;
15
- };
16
- // 获取
17
- var _getItem = function getItem(key) {
18
- var defaultVal = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
19
- var val = fn.getItem(key);
20
- // 如果为 null 取默认值
21
- if (val === null) {
22
- return defaultVal;
23
- }
24
- return JSON.parse(val);
25
- };
26
- // 删除
27
- var _removeItem = function removeItem(key) {
28
- fn.removeItem(key);
29
- };
30
- // 更新
31
- var _updateItem = function updateItem(key, val) {
32
- var prev = _getItem(key);
33
- return _setItem(key, (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, prev), val));
34
- };
35
- // 清除
36
- var clear = function clear() {
37
- fn.clear();
38
- };
39
- // 生成
40
- var generate = function generate(key) {
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
- setItem: function setItem(val) {
43
- return _setItem(key, val);
44
- },
45
- getItem: function getItem(defaultVal) {
46
- return _getItem(key, defaultVal);
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
- return {
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;
@@ -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
  };