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