@whitesev/utils 1.5.3 → 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
@@ -6219,6 +6219,33 @@
6219
6219
  });
6220
6220
  return formData;
6221
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
+ }
6222
6249
  /**
6223
6250
  * 生成uuid
6224
6251
  * @example