@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.
package/dist/index.amd.js CHANGED
@@ -4043,7 +4043,7 @@ define((function () { 'use strict';
4043
4043
  ];
4044
4044
  let androidVersion = UtilsContext.getRandomValue(12, 14);
4045
4045
  let randomMobile = UtilsContext.getRandomValue(mobileNameList);
4046
- let chromeVersion1 = UtilsContext.getRandomValue(115, 125);
4046
+ let chromeVersion1 = UtilsContext.getRandomValue(115, 127);
4047
4047
  let chromeVersion2 = UtilsContext.getRandomValue(0, 0);
4048
4048
  let chromeVersion3 = UtilsContext.getRandomValue(2272, 6099);
4049
4049
  let chromeVersion4 = UtilsContext.getRandomValue(1, 218);
@@ -4098,7 +4098,7 @@ define((function () { 'use strict';
4098
4098
  **/
4099
4099
  getRandomPCUA() {
4100
4100
  let UtilsContext = this;
4101
- let chromeVersion1 = UtilsContext.getRandomValue(115, 126);
4101
+ let chromeVersion1 = UtilsContext.getRandomValue(115, 127);
4102
4102
  let chromeVersion2 = UtilsContext.getRandomValue(0, 0);
4103
4103
  let chromeVersion3 = UtilsContext.getRandomValue(2272, 6099);
4104
4104
  let chromeVersion4 = UtilsContext.getRandomValue(1, 218);
@@ -6185,12 +6185,21 @@ define((function () { 'use strict';
6185
6185
  /**
6186
6186
  * 将对象转换为FormData
6187
6187
  * @param data 待转换的对象
6188
- * @param isEncode 是否对值为string进行编码转换(encodeURIComponent)
6188
+ * @param isEncode 是否对值为string进行编码转换(encodeURIComponent),默认false
6189
+ * @param valueAutoParseToStr 是否对值强制使用JSON.stringify()转换,默认false
6190
+ * @example
6191
+ * Utils.toFormData({
6192
+ * test: "1",
6193
+ * 666: 666,
6194
+ * })
6189
6195
  */
6190
- toFormData(data, isEncode = false) {
6196
+ toFormData(data, isEncode = false, valueAutoParseToStr = false) {
6191
6197
  const formData = new FormData();
6192
6198
  Object.keys(data).forEach((key) => {
6193
6199
  let value = data[key];
6200
+ if (valueAutoParseToStr) {
6201
+ value = JSON.stringify(value);
6202
+ }
6194
6203
  if (typeof value === "number") {
6195
6204
  value = value.toString();
6196
6205
  }
@@ -6206,6 +6215,49 @@ define((function () { 'use strict';
6206
6215
  });
6207
6216
  return formData;
6208
6217
  }
6218
+ /**
6219
+ * 将链接转为URL对象,自动补充URL的protocol或者origin
6220
+ * @param text 需要转换的链接字符串
6221
+ * @example
6222
+ * Utils.toUrl("//www.baidu.com/s?word=666");
6223
+ * Utils.toUrl("/s?word=666");
6224
+ */
6225
+ toUrl(text) {
6226
+ if (typeof text !== "string") {
6227
+ throw new TypeError("toUrl: text must be string");
6228
+ }
6229
+ text = text.trim();
6230
+ if (text === "") {
6231
+ throw new TypeError("toUrl: text must not be empty");
6232
+ }
6233
+ if (text.startsWith("//")) {
6234
+ /* //www.baidu.com/xxxxxxx */
6235
+ /* 没有protocol,加上 */
6236
+ text = UtilsCore.globalThis.location.protocol + text;
6237
+ }
6238
+ else if (text.startsWith("/")) {
6239
+ /* /xxx/info?xxx=xxx */
6240
+ /* 没有Origin,加上 */
6241
+ text = UtilsCore.globalThis.location.origin + text;
6242
+ }
6243
+ return new URL(text);
6244
+ }
6245
+ /**
6246
+ * 生成uuid
6247
+ * @example
6248
+ * Utils.generateUUID()
6249
+ */
6250
+ generateUUID() {
6251
+ if (typeof globalThis?.crypto?.randomUUID === "function") {
6252
+ return globalThis.crypto.randomUUID();
6253
+ }
6254
+ else {
6255
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (charStr) {
6256
+ var randomValue = (Math.random() * 16) | 0, randomCharValue = charStr === "x" ? randomValue : (randomValue & 0x3) | 0x8;
6257
+ return randomCharValue.toString(16);
6258
+ });
6259
+ }
6260
+ }
6209
6261
  }
6210
6262
  let utils = new Utils();
6211
6263