@whitesev/utils 1.5.2 → 1.5.3

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,23 @@ 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
+ * 生成uuid
1750
+ * @example
1751
+ * Utils.generateUUID()
1752
+ */
1753
+ generateUUID(): string;
1740
1754
  }
1741
1755
  declare let utils: Utils;
1742
1756
  export { utils as Utils };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whitesev/utils",
3
- "version": "1.5.2",
3
+ "version": "1.5.3",
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,26 @@ class Utils {
4857
4869
  });
4858
4870
  return formData;
4859
4871
  }
4872
+ /**
4873
+ * 生成uuid
4874
+ * @example
4875
+ * Utils.generateUUID()
4876
+ */
4877
+ generateUUID() {
4878
+ if (typeof globalThis?.crypto?.randomUUID === "function") {
4879
+ return globalThis.crypto.randomUUID();
4880
+ } else {
4881
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
4882
+ /[xy]/g,
4883
+ function (charStr) {
4884
+ var randomValue = (Math.random() * 16) | 0,
4885
+ randomCharValue =
4886
+ charStr === "x" ? randomValue : (randomValue & 0x3) | 0x8;
4887
+ return randomCharValue.toString(16);
4888
+ }
4889
+ );
4890
+ }
4891
+ }
4860
4892
  }
4861
4893
 
4862
4894
  let utils = new Utils();