@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.
package/dist/index.cjs.js CHANGED
@@ -6059,6 +6059,30 @@ class Utils {
6059
6059
  createUtils(option) {
6060
6060
  return new Utils(option);
6061
6061
  }
6062
+ /**
6063
+ * 将对象转换为FormData
6064
+ * @param data 待转换的对象
6065
+ * @param isEncode 是否对值为string进行编码转换(encodeURIComponent)
6066
+ */
6067
+ toFormData(data, isEncode = false) {
6068
+ const formData = new FormData();
6069
+ Object.keys(data).forEach((key) => {
6070
+ let value = data[key];
6071
+ if (typeof value === "number") {
6072
+ value = value.toString();
6073
+ }
6074
+ if (isEncode && typeof value === "string") {
6075
+ value = encodeURIComponent(value);
6076
+ }
6077
+ if (value instanceof File) {
6078
+ formData.append(key, value, value.name);
6079
+ }
6080
+ else {
6081
+ formData.append(key, value);
6082
+ }
6083
+ });
6084
+ return formData;
6085
+ }
6062
6086
  }
6063
6087
  let utils = new Utils();
6064
6088