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