@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.
Files changed (174) hide show
  1. package/README.md +21 -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 +15 -17
@@ -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;
package/es/parseObject.js CHANGED
@@ -1,12 +1,13 @@
1
1
  /**
2
2
  * 返回对象
3
+ * parseObject('a.b.c', 1) => { a: { b: { c: 1 } } }
3
4
  */
4
5
  export function parseObject(path, value) {
5
- var result = {};
6
- var segments = path ? path.split('.') : [];
7
- segments.reduce(function (res, key, index, arr) {
8
- res[key] = index === arr.length - 1 ? value : {};
9
- return res[key];
10
- }, result);
11
- return result;
12
- }
6
+ const result = {};
7
+ const segments = path ? path.split('.') : [];
8
+ segments.reduce((res, key, index, arr) => {
9
+ res[key] = index === arr.length - 1 ? value : {};
10
+ return res[key];
11
+ }, result);
12
+ return result;
13
+ }
package/es/parsePath.js CHANGED
@@ -2,10 +2,10 @@
2
2
  * 根据路径解析值
3
3
  */
4
4
  export function parsePath(obj, path) {
5
- var segments = path ? path.split('.') : [];
6
- segments.forEach(function (key) {
7
- // eslint-disable-next-line no-param-reassign
8
- obj = obj[key];
9
- });
10
- return obj;
11
- }
5
+ const segments = path ? path.split('.') : [];
6
+ segments.forEach((key) => {
7
+ // eslint-disable-next-line no-param-reassign
8
+ obj = obj[key];
9
+ });
10
+ return obj;
11
+ }
@@ -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/es/parseQuery.js CHANGED
@@ -1,5 +1,3 @@
1
- import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
- import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
3
1
  import { isJSONString } from './isJSONString';
4
2
  /**
5
3
  * 解析url参数
@@ -7,26 +5,17 @@ import { isJSONString } from './isJSONString';
7
5
  * @param k 键名
8
6
  */
9
7
  export function parseQuery(v, k) {
10
- var url = new URL(v);
11
- var searchStr = v ? url.search : window.location.search;
12
- var query = new URLSearchParams(decodeURIComponent(searchStr));
13
- // 单个直接返回
14
- if (k) return query.get(k);
15
- var res = {};
16
- // eslint-disable-next-line no-restricted-syntax
17
- var _iterator = _createForOfIteratorHelper(query.entries()),
18
- _step;
19
- try {
20
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
21
- var _step$value = _slicedToArray(_step.value, 2),
22
- key = _step$value[0],
23
- value = _step$value[1];
24
- res[key] = isJSONString(value) ? JSON.parse(value) : value;
8
+ const url = new URL(v !== null && v !== void 0 ? v : '');
9
+ const searchStr = v ? url.search : window.location.search;
10
+ const query = new URLSearchParams(decodeURIComponent(searchStr));
11
+ // 单个直接返回
12
+ if (k) {
13
+ return query.get(k);
25
14
  }
26
- } catch (err) {
27
- _iterator.e(err);
28
- } finally {
29
- _iterator.f();
30
- }
31
- return res;
32
- }
15
+ const res = {};
16
+ // eslint-disable-next-line no-restricted-syntax
17
+ for (const [key, value] of query.entries()) {
18
+ res[key] = isJSONString(value) ? JSON.parse(value) : value;
19
+ }
20
+ return res;
21
+ }
package/es/pick.js CHANGED
@@ -1,11 +1,10 @@
1
1
  /**
2
2
  * 挑选对象里的某些属性,新对象返回
3
3
  */
4
- export function pick(obj) {
5
- var fields = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
6
- var clone = {};
7
- fields.forEach(function (key) {
8
- clone[key] = obj[key];
9
- });
10
- return clone;
11
- }
4
+ export function pick(obj, fields = []) {
5
+ const clone = {};
6
+ fields.forEach((key) => {
7
+ clone[key] = obj[key];
8
+ });
9
+ return clone;
10
+ }
package/es/replaceCrlf.js CHANGED
@@ -2,5 +2,5 @@
2
2
  * 替换字符串里面的回车换行
3
3
  */
4
4
  export function replaceCrlf(val, replaceVal) {
5
- return val ? val.replace(/\\r|\\n/gi, replaceVal) : '';
6
- }
5
+ return val ? val.replace(/\\r|\\n/gi, replaceVal) : '';
6
+ }
package/es/reserve.js CHANGED
@@ -6,5 +6,5 @@ import { isEmpty } from './isEmpty';
6
6
  * reserve(0, '--') => 0
7
7
  */
8
8
  export function reserve(val, defaultVal) {
9
- return isEmpty(val) ? defaultVal : val;
10
- }
9
+ return isEmpty(val) ? defaultVal : val;
10
+ }
package/es/round.js CHANGED
@@ -4,45 +4,44 @@
4
4
  * @param fractionDigits 需要保留的小数点位数
5
5
  */
6
6
  export function round(val, fractionDigits) {
7
- // todo
8
- var number = val;
9
- if (number >= Math.pow(10, 21)) {
10
- return number.toString();
11
- }
12
- if (typeof fractionDigits === 'undefined' || fractionDigits === 0) {
13
- return Math.round(number).toString();
14
- }
15
- var result = number.toString();
16
- var arr = result.split('.');
17
- // 整数的情况
18
- if (arr.length < 2) {
19
- result += '.';
20
- for (var i = 0; i < fractionDigits; i += 1) {
21
- result += '0';
7
+ const number = val;
8
+ if (number >= Math.pow(10, 21)) {
9
+ return number.toString();
22
10
  }
23
- return result;
24
- }
25
- var integer = arr[0];
26
- var decimal = arr[1];
27
- if (decimal.length === fractionDigits) {
28
- return result;
29
- }
30
- if (decimal.length < fractionDigits) {
31
- for (var _i = 0; _i < fractionDigits - decimal.length; _i += 1) {
32
- result += '0';
11
+ if (typeof fractionDigits === 'undefined' || fractionDigits === 0) {
12
+ return Math.round(number).toString();
13
+ }
14
+ let result = number.toString();
15
+ const arr = result.split('.');
16
+ // 整数的情况
17
+ if (arr.length < 2) {
18
+ result += '.';
19
+ for (let i = 0; i < fractionDigits; i += 1) {
20
+ result += '0';
21
+ }
22
+ return result;
23
+ }
24
+ const integer = arr[0];
25
+ const decimal = arr[1];
26
+ if (decimal.length === fractionDigits) {
27
+ return result;
28
+ }
29
+ if (decimal.length < fractionDigits) {
30
+ for (let i = 0; i < fractionDigits - decimal.length; i += 1) {
31
+ result += '0';
32
+ }
33
+ return result;
34
+ }
35
+ result = `${integer}.${decimal.substr(0, fractionDigits)}`;
36
+ let last = decimal.substr(fractionDigits, 1);
37
+ if (/^\d(9){5,}[0-9]$/.test(decimal.substr(fractionDigits))) {
38
+ last = (+last + 1).toString();
39
+ }
40
+ // 四舍五入,转换为整数再处理,避免浮点数精度的损失
41
+ if (parseInt(last, 10) >= 5) {
42
+ const x = Math.pow(10, fractionDigits);
43
+ result = (Math.round(parseFloat(result) * x) + 1) / x;
44
+ result = round(result, fractionDigits);
33
45
  }
34
46
  return result;
35
- }
36
- result = "".concat(integer, ".").concat(decimal.substr(0, fractionDigits));
37
- var last = decimal.substr(fractionDigits, 1);
38
- if (/^\d(9){5,}[0-9]$/.test(decimal.substr(fractionDigits))) {
39
- last = (+last + 1).toString();
40
- }
41
- // 四舍五入,转换为整数再处理,避免浮点数精度的损失
42
- if (parseInt(last, 10) >= 5) {
43
- var x = Math.pow(10, fractionDigits);
44
- result = (Math.round(parseFloat(result) * x) + 1) / x;
45
- result = round(result, fractionDigits);
46
- }
47
- return result;
48
- }
47
+ }
package/es/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/es/sleep.js CHANGED
@@ -1,9 +1,10 @@
1
1
  /**
2
- * 说明
2
+ * 休眠方法
3
+ * @param ms 休眠时长 毫秒
4
+ * @param isSuccess 默认执行成功
3
5
  */
4
- export function sleep(timeout) {
5
- var isSuccess = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
6
- return new Promise(function (resolve, reject) {
7
- setTimeout(isSuccess ? resolve : reject, timeout);
8
- });
9
- }
6
+ export function sleep(ms, isSuccess = true) {
7
+ return new Promise((resolve, reject) => {
8
+ setTimeout(isSuccess ? resolve : reject, ms);
9
+ });
10
+ }
@@ -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,57 +1,47 @@
1
- import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
1
  export function factory(type) {
3
- var fn = window[type];
4
- // 设置
5
- var _setItem = function setItem(key, val) {
6
- fn.setItem(key, JSON.stringify(val));
7
- return val;
8
- };
9
- // 获取
10
- var _getItem = function getItem(key) {
11
- var defaultVal = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
12
- var val = fn.getItem(key);
13
- // 如果为 null 取默认值
14
- if (val === null) {
15
- return defaultVal;
16
- }
17
- return JSON.parse(val);
18
- };
19
- // 删除
20
- var _removeItem = function removeItem(key) {
21
- fn.removeItem(key);
22
- };
23
- // 更新
24
- var _updateItem = function updateItem(key, val) {
25
- var prev = _getItem(key);
26
- return _setItem(key, _objectSpread(_objectSpread({}, prev), val));
27
- };
28
- // 清除
29
- var clear = function clear() {
30
- fn.clear();
31
- };
32
- // 生成
33
- var generate = function generate(key) {
2
+ const fn = window[type];
3
+ // 设置
4
+ const setItem = (key, val) => {
5
+ fn.setItem(key, JSON.stringify(val));
6
+ return val;
7
+ };
8
+ // 获取
9
+ const getItem = (key, defaultVal = null) => {
10
+ const val = fn.getItem(key);
11
+ // 如果为 null 取默认值
12
+ if (val === null) {
13
+ return defaultVal;
14
+ }
15
+ return JSON.parse(val);
16
+ };
17
+ // 删除
18
+ const removeItem = (key) => {
19
+ fn.removeItem(key);
20
+ };
21
+ // 更新
22
+ const updateItem = (key, val) => {
23
+ const prev = getItem(key);
24
+ return setItem(key, Object.assign(Object.assign({}, prev), val));
25
+ };
26
+ // 清除
27
+ const clear = () => {
28
+ fn.clear();
29
+ };
30
+ // 生成
31
+ const generate = (key) => {
32
+ return {
33
+ setItem: (val) => setItem(key, val),
34
+ getItem: (defaultVal) => getItem(key, defaultVal),
35
+ removeItem: () => removeItem(key),
36
+ updateItem: (val) => updateItem(key, val),
37
+ };
38
+ };
34
39
  return {
35
- setItem: function setItem(val) {
36
- return _setItem(key, val);
37
- },
38
- getItem: function getItem(defaultVal) {
39
- return _getItem(key, defaultVal);
40
- },
41
- removeItem: function removeItem() {
42
- return _removeItem(key);
43
- },
44
- updateItem: function updateItem(val) {
45
- return _updateItem(key, val);
46
- }
40
+ setItem,
41
+ getItem,
42
+ removeItem,
43
+ updateItem,
44
+ clear,
45
+ generate,
47
46
  };
48
- };
49
- return {
50
- setItem: _setItem,
51
- getItem: _getItem,
52
- removeItem: _removeItem,
53
- updateItem: _updateItem,
54
- clear: clear,
55
- generate: generate
56
- };
57
- }
47
+ }
@@ -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
  };
@@ -1,12 +1,12 @@
1
1
  import { factory } from './factory';
2
- var getStorage = function getStorage(type) {
3
- var instance = null;
4
- return function () {
5
- if (!instance) {
6
- instance = factory(type);
7
- }
8
- return instance;
9
- };
2
+ const getStorage = (type) => {
3
+ let instance = null;
4
+ return () => {
5
+ if (!instance) {
6
+ instance = factory(type);
7
+ }
8
+ return instance;
9
+ };
10
10
  };
11
- export var getCache = getStorage('sessionStorage');
12
- export var getLocal = getStorage('localStorage');
11
+ export const getCache = getStorage('sessionStorage');
12
+ export const getLocal = getStorage('localStorage');
@@ -1,4 +1,6 @@
1
1
  /**
2
- *去除字符串空格
2
+ * 去除字符串空格
3
+ * @param str 字符串值
4
+ * @param type 方式 1-所有空格,2-前后空格,3-前空格,4-后空格
3
5
  */
4
6
  export declare function stringTrim(str: string, type?: 1 | 2 | 3 | 4): string;
package/es/stringTrim.js CHANGED
@@ -1,21 +1,19 @@
1
1
  /**
2
- *去除字符串空格
2
+ * 去除字符串空格
3
+ * @param str 字符串值
4
+ * @param type 方式 1-所有空格,2-前后空格,3-前空格,4-后空格
3
5
  */
4
- export function stringTrim(str, type) {
5
- var _type;
6
- // 1-所有空格,2-前后空格,3-前空格,4-后空格
7
- // eslint-disable-next-line no-param-reassign
8
- type = (_type = type) !== null && _type !== void 0 ? _type : 1;
9
- switch (type) {
10
- case 1:
11
- return str.replace(/\s+/g, '');
12
- case 2:
13
- return str.replace(/(^\s*)|(\s*$)/g, '');
14
- case 3:
15
- return str.replace(/(^\s*)/g, '');
16
- case 4:
17
- return str.replace(/(\s*$)/g, '');
18
- default:
19
- return str;
20
- }
21
- }
6
+ export function stringTrim(str, type = 1) {
7
+ switch (type) {
8
+ case 1:
9
+ return str.replace(/\s+/g, '');
10
+ case 2:
11
+ return str.replace(/(^\s*)|(\s*$)/g, '');
12
+ case 3:
13
+ return str.replace(/(^\s*)/g, '');
14
+ case 4:
15
+ return str.replace(/(\s*$)/g, '');
16
+ default:
17
+ return str;
18
+ }
19
+ }
@@ -1,4 +1,4 @@
1
1
  /**
2
2
  * 说明
3
3
  */
4
- export declare function stringifyQuery(v: object): string;
4
+ export declare function stringifyQuery(v: Record<string, any>): string;
@@ -1,13 +1,16 @@
1
1
  import { isEmptyObject } from './isEmptyObject';
2
+ import { isString } from './isString';
2
3
  /**
3
4
  * 说明
4
5
  */
5
6
  export function stringifyQuery(v) {
6
- if (isEmptyObject(v)) return '';
7
- var query = new URLSearchParams();
8
- Object.keys(v).forEach(function (key) {
9
- var value = typeof v[key] === 'string' ? v[key] : JSON.stringify(v[key]);
10
- query.append(key, value);
11
- });
12
- return query.toString();
13
- }
7
+ if (isEmptyObject(v)) {
8
+ return '';
9
+ }
10
+ const query = new URLSearchParams();
11
+ Object.keys(v).forEach((key) => {
12
+ const value = isString(v[key]) ? v[key] : JSON.stringify(v[key]);
13
+ query.append(key, value);
14
+ });
15
+ return query.toString();
16
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * 节流函数
3
+ * 减少事件执行次数,有规律的执行
4
+ */
5
+ export declare function throttle<T extends (...args: []) => any>(fn: T, ms: number): (this: unknown, ...args: Parameters<T>) => void;
package/es/throttle.js ADDED
@@ -0,0 +1,17 @@
1
+ /**
2
+ * 节流函数
3
+ * 减少事件执行次数,有规律的执行
4
+ */
5
+ export function throttle(fn, ms) {
6
+ let timer = null;
7
+ // eslint-disable-next-line func-names
8
+ return function (...args) {
9
+ if (timer) {
10
+ return;
11
+ }
12
+ timer = setTimeout(() => {
13
+ fn.apply(this, args);
14
+ timer = null;
15
+ }, ms);
16
+ };
17
+ }
package/es/typeOf.d.ts CHANGED
@@ -3,4 +3,4 @@
3
3
  * @param val 待判断数据
4
4
  * @returns 'undefined'|'null'|'boolean'|'string'|'number'|'object'|'array'|'function'|'symbol'|'map'|'weakmap'|'bigint'|'regexp'|'date'
5
5
  */
6
- export declare function typeOf<T>(val: T): val is T;
6
+ export declare function typeOf(val: unknown): string;
package/es/typeOf.js CHANGED
@@ -4,5 +4,5 @@
4
4
  * @returns 'undefined'|'null'|'boolean'|'string'|'number'|'object'|'array'|'function'|'symbol'|'map'|'weakmap'|'bigint'|'regexp'|'date'
5
5
  */
6
6
  export function typeOf(val) {
7
- return Object.prototype.toString.call(val).slice(8, -1).toLowerCase();
8
- }
7
+ return Object.prototype.toString.call(val).slice(8, -1).toLowerCase();
8
+ }
@@ -1,30 +1,21 @@
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.base64ToBlob = base64ToBlob;
8
- var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.base64ToBlob = void 0;
9
4
  /**
10
5
  * base64 转 blob
11
6
  * @param base64 base64字符串
12
7
  * @param contentType 文件类型
13
8
  */
14
9
  function base64ToBlob(base64, contentType) {
15
- var _prefix$match;
16
- var _base64$split = base64.split(','),
17
- _base64$split2 = (0, _slicedToArray2["default"])(_base64$split, 2),
18
- prefix = _base64$split2[0],
19
- rest = _base64$split2[1];
20
- var type = contentType !== null && contentType !== void 0 ? contentType : (_prefix$match = prefix.match(/:(.*?);/)) === null || _prefix$match === void 0 ? void 0 : _prefix$match[1];
21
- var data = window.atob(rest);
22
- var len = data.length;
23
- var u8arr = new Uint8Array(len);
24
- while (len--) {
25
- u8arr[len] = data.charCodeAt(len);
26
- }
27
- return new Blob([u8arr], {
28
- type: type
29
- });
30
- }
10
+ var _a;
11
+ const [prefix, rest] = base64.split(',');
12
+ const type = contentType !== null && contentType !== void 0 ? contentType : (_a = prefix.match(/:(.*?);/)) === null || _a === void 0 ? void 0 : _a[1];
13
+ const data = window.atob(rest);
14
+ let len = data.length;
15
+ const u8arr = new Uint8Array(len);
16
+ while (len--) {
17
+ u8arr[len] = data.charCodeAt(len);
18
+ }
19
+ return new Blob([u8arr], { type });
20
+ }
21
+ exports.base64ToBlob = base64ToBlob;
@@ -1,20 +1,18 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.blobToBase64 = blobToBase64;
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.blobToBase64 = void 0;
7
4
  /**
8
5
  * blob 转 base64
9
6
  */
10
7
  function blobToBase64(blob) {
11
- return new Promise(function (resolve, reject) {
12
- var fileReader = new FileReader();
13
- fileReader.onload = function (res) {
14
- var _res$target$result, _res$target;
15
- resolve((_res$target$result = (_res$target = res.target) === null || _res$target === void 0 ? void 0 : _res$target.result) !== null && _res$target$result !== void 0 ? _res$target$result : '');
16
- };
17
- fileReader.onerror = reject;
18
- fileReader.readAsDataURL(blob);
19
- });
20
- }
8
+ return new Promise((resolve, reject) => {
9
+ const fileReader = new FileReader();
10
+ fileReader.onload = (res) => {
11
+ var _a, _b;
12
+ resolve((_b = (_a = res.target) === null || _a === void 0 ? void 0 : _a.result) !== null && _b !== void 0 ? _b : '');
13
+ };
14
+ fileReader.onerror = reject;
15
+ fileReader.readAsDataURL(blob);
16
+ });
17
+ }
18
+ exports.blobToBase64 = blobToBase64;