@whitesev/utils 1.5.2 → 1.5.4

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.
@@ -847,6 +847,7 @@ declare class Utils {
847
847
  * Utils.isNotNull("123");
848
848
  * > true
849
849
  */
850
+ isNotNull<T>(value: T | null): value is T;
850
851
  isNotNull(...args: any[]): boolean;
851
852
  /**
852
853
  * 判断对象或数据是否为空
@@ -893,6 +894,7 @@ declare class Utils {
893
894
  Utils.isNull(false,[123]);
894
895
  > false
895
896
  **/
897
+ isNull<T>(value: T | null): value is null;
896
898
  isNull(...args: any[]): boolean;
897
899
  /**
898
900
  * 判断浏览器主题是否是暗黑|深色模式
@@ -1732,11 +1734,31 @@ declare class Utils {
1732
1734
  /**
1733
1735
  * 将对象转换为FormData
1734
1736
  * @param data 待转换的对象
1735
- * @param isEncode 是否对值为string进行编码转换(encodeURIComponent)
1737
+ * @param isEncode 是否对值为string进行编码转换(encodeURIComponent),默认false
1738
+ * @param valueAutoParseToStr 是否对值强制使用JSON.stringify()转换,默认false
1739
+ * @example
1740
+ * Utils.toFormData({
1741
+ * test: "1",
1742
+ * 666: 666,
1743
+ * })
1736
1744
  */
1737
1745
  toFormData(data: {
1738
1746
  [key: string]: string | Blob | File | number;
1739
- }, isEncode?: boolean): FormData;
1747
+ }, isEncode?: boolean, valueAutoParseToStr?: boolean): FormData;
1748
+ /**
1749
+ * 将链接转为URL对象,自动补充URL的protocol或者origin
1750
+ * @param text 需要转换的链接字符串
1751
+ * @example
1752
+ * Utils.toUrl("//www.baidu.com/s?word=666");
1753
+ * Utils.toUrl("/s?word=666");
1754
+ */
1755
+ toUrl(text: string): URL;
1756
+ /**
1757
+ * 生成uuid
1758
+ * @example
1759
+ * Utils.generateUUID()
1760
+ */
1761
+ generateUUID(): string;
1740
1762
  }
1741
1763
  declare let utils: Utils;
1742
1764
  export { utils as Utils };
@@ -105,3 +105,6 @@ export declare interface Vue2Object extends AnyObject {
105
105
  deep?: boolean;
106
106
  }) => void;
107
107
  }
108
+ export declare interface HTMLVue2DivElement extends HTMLDivElement {
109
+ __vue__: Vue2Object;
110
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whitesev/utils",
3
- "version": "1.5.2",
3
+ "version": "1.5.4",
4
4
  "description": "一个常用的工具库",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/node/index.esm.js",
package/src/Utils.ts CHANGED
@@ -1323,7 +1323,7 @@ class Utils {
1323
1323
  ];
1324
1324
  let androidVersion = UtilsContext.getRandomValue(12, 14);
1325
1325
  let randomMobile = UtilsContext.getRandomValue(mobileNameList);
1326
- let chromeVersion1 = UtilsContext.getRandomValue(115, 125);
1326
+ let chromeVersion1 = UtilsContext.getRandomValue(115, 127);
1327
1327
  let chromeVersion2 = UtilsContext.getRandomValue(0, 0);
1328
1328
  let chromeVersion3 = UtilsContext.getRandomValue(2272, 6099);
1329
1329
  let chromeVersion4 = UtilsContext.getRandomValue(1, 218);
@@ -1417,7 +1417,7 @@ class Utils {
1417
1417
  **/
1418
1418
  getRandomPCUA(): string {
1419
1419
  let UtilsContext = this;
1420
- let chromeVersion1 = UtilsContext.getRandomValue(115, 126);
1420
+ let chromeVersion1 = UtilsContext.getRandomValue(115, 127);
1421
1421
  let chromeVersion2 = UtilsContext.getRandomValue(0, 0);
1422
1422
  let chromeVersion3 = UtilsContext.getRandomValue(2272, 6099);
1423
1423
  let chromeVersion4 = UtilsContext.getRandomValue(1, 218);
@@ -2019,6 +2019,7 @@ class Utils {
2019
2019
  * Utils.isNotNull("123");
2020
2020
  * > true
2021
2021
  */
2022
+ isNotNull<T>(value: T | null): value is T;
2022
2023
  isNotNull(...args: any[]): boolean;
2023
2024
  isNotNull(...args: any[]): boolean {
2024
2025
  let UtilsContext = this;
@@ -2069,6 +2070,7 @@ class Utils {
2069
2070
  Utils.isNull(false,[123]);
2070
2071
  > false
2071
2072
  **/
2073
+ isNull<T>(value: T | null): value is null;
2072
2074
  isNull(...args: any[]): boolean;
2073
2075
  isNull(...args: any[]): boolean {
2074
2076
  let result = true;
@@ -4834,15 +4836,25 @@ class Utils {
4834
4836
  /**
4835
4837
  * 将对象转换为FormData
4836
4838
  * @param data 待转换的对象
4837
- * @param isEncode 是否对值为string进行编码转换(encodeURIComponent)
4839
+ * @param isEncode 是否对值为string进行编码转换(encodeURIComponent),默认false
4840
+ * @param valueAutoParseToStr 是否对值强制使用JSON.stringify()转换,默认false
4841
+ * @example
4842
+ * Utils.toFormData({
4843
+ * test: "1",
4844
+ * 666: 666,
4845
+ * })
4838
4846
  */
4839
4847
  toFormData(
4840
4848
  data: { [key: string]: string | Blob | File | number },
4841
- isEncode: boolean = false
4849
+ isEncode: boolean = false,
4850
+ valueAutoParseToStr: boolean = false
4842
4851
  ) {
4843
4852
  const formData = new FormData();
4844
4853
  Object.keys(data).forEach((key) => {
4845
4854
  let value = data[key];
4855
+ if (valueAutoParseToStr) {
4856
+ value = JSON.stringify(value);
4857
+ }
4846
4858
  if (typeof value === "number") {
4847
4859
  value = value.toString();
4848
4860
  }
@@ -4857,6 +4869,52 @@ class Utils {
4857
4869
  });
4858
4870
  return formData;
4859
4871
  }
4872
+ /**
4873
+ * 将链接转为URL对象,自动补充URL的protocol或者origin
4874
+ * @param text 需要转换的链接字符串
4875
+ * @example
4876
+ * Utils.toUrl("//www.baidu.com/s?word=666");
4877
+ * Utils.toUrl("/s?word=666");
4878
+ */
4879
+ toUrl(text: string) {
4880
+ if (typeof text !== "string") {
4881
+ throw new TypeError("toUrl: text must be string");
4882
+ }
4883
+ text = text.trim();
4884
+ if (text === "") {
4885
+ throw new TypeError("toUrl: text must not be empty");
4886
+ }
4887
+ if (text.startsWith("//")) {
4888
+ /* //www.baidu.com/xxxxxxx */
4889
+ /* 没有protocol,加上 */
4890
+ text = UtilsCore.globalThis.location.protocol + text;
4891
+ } else if (text.startsWith("/")) {
4892
+ /* /xxx/info?xxx=xxx */
4893
+ /* 没有Origin,加上 */
4894
+ text = UtilsCore.globalThis.location.origin + text;
4895
+ }
4896
+ return new URL(text);
4897
+ }
4898
+ /**
4899
+ * 生成uuid
4900
+ * @example
4901
+ * Utils.generateUUID()
4902
+ */
4903
+ generateUUID() {
4904
+ if (typeof globalThis?.crypto?.randomUUID === "function") {
4905
+ return globalThis.crypto.randomUUID();
4906
+ } else {
4907
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
4908
+ /[xy]/g,
4909
+ function (charStr) {
4910
+ var randomValue = (Math.random() * 16) | 0,
4911
+ randomCharValue =
4912
+ charStr === "x" ? randomValue : (randomValue & 0x3) | 0x8;
4913
+ return randomCharValue.toString(16);
4914
+ }
4915
+ );
4916
+ }
4917
+ }
4860
4918
  }
4861
4919
 
4862
4920
  let utils = new Utils();
package/src/VueObject.ts CHANGED
@@ -122,3 +122,7 @@ export declare interface Vue2Object extends AnyObject {
122
122
  }
123
123
  ) => void;
124
124
  }
125
+
126
+ export declare interface HTMLVue2DivElement extends HTMLDivElement {
127
+ __vue__: Vue2Object;
128
+ }