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