@whitesev/utils 1.3.6 → 1.3.8

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.
@@ -1804,6 +1804,14 @@ declare class Utils {
1804
1804
  * @returns
1805
1805
  */
1806
1806
  createUtils(option?: UtilsCoreOption): Utils;
1807
+ /**
1808
+ * 将对象转换为FormData
1809
+ * @param data 待转换的对象
1810
+ * @param isEncode 是否对值为string进行编码转换(encodeURIComponent)
1811
+ */
1812
+ toFormData(data: {
1813
+ [key: string]: string | Blob | File | number;
1814
+ }, isEncode?: boolean): FormData;
1807
1815
  }
1808
1816
  declare let utils: Utils;
1809
1817
  export { utils as Utils };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whitesev/utils",
3
- "version": "1.3.6",
3
+ "version": "1.3.8",
4
4
  "description": "一个常用的工具库",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/node/index.esm.js",
package/src/Utils.ts CHANGED
@@ -4895,6 +4895,33 @@ class Utils {
4895
4895
  createUtils(option?: UtilsCoreOption) {
4896
4896
  return new Utils(option);
4897
4897
  }
4898
+
4899
+ /**
4900
+ * 将对象转换为FormData
4901
+ * @param data 待转换的对象
4902
+ * @param isEncode 是否对值为string进行编码转换(encodeURIComponent)
4903
+ */
4904
+ toFormData(
4905
+ data: { [key: string]: string | Blob | File | number },
4906
+ isEncode: boolean = false
4907
+ ) {
4908
+ const formData = new FormData();
4909
+ Object.keys(data).forEach((key) => {
4910
+ let value = data[key];
4911
+ if (typeof value === "number") {
4912
+ value = value.toString();
4913
+ }
4914
+ if (isEncode && typeof value === "string") {
4915
+ value = encodeURIComponent(value);
4916
+ }
4917
+ if (value instanceof File) {
4918
+ formData.append(key, value, value.name);
4919
+ } else {
4920
+ formData.append(key, value);
4921
+ }
4922
+ });
4923
+ return formData;
4924
+ }
4898
4925
  }
4899
4926
 
4900
4927
  let utils = new Utils();