@ezuikit/utils-tools 1.1.0 → 2.0.0-beta.1
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.js +82 -7
- package/dist/index.mjs +92 -7
- package/dist/index.umd.js +82 -7
- package/dist/types/isMobile.d.ts +4 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,7 +1,82 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var getQuery = function(url) {
|
|
4
|
+
var query = url.split("?")[1], obj = {};
|
|
5
|
+
if (query) for (var vars = query.split("&"), i = 0; i < vars.length; i++) {
|
|
6
|
+
var pair = vars[i].split("=");
|
|
7
|
+
obj[pair[0]] = decodeURIComponent(pair[1]);
|
|
8
|
+
}
|
|
9
|
+
return obj;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
function parseEzopenUrl(url) {
|
|
13
|
+
var obj = {};
|
|
14
|
+
if (/^ezopen:\/\//.test(url)) {
|
|
15
|
+
if (url.includes("@")) {
|
|
16
|
+
var endIndex = url.indexOf("@"), startIndex = url.indexOf("//");
|
|
17
|
+
obj.validateCode = url.substring(startIndex + 2, endIndex), url = url.replace(obj.validateCode + "@", "");
|
|
18
|
+
}
|
|
19
|
+
obj.protocol = url.split("//")[0], obj.search = url.split("?")[1] ? "?" + url.split("?")[1] : "",
|
|
20
|
+
obj.hostname = url.split("/")[2], obj.origin = obj.protocol + "//" + obj.hostname,
|
|
21
|
+
obj.pathname = url.split("?")[0].replace(obj.origin, ""), obj.href = url;
|
|
22
|
+
var pathnames = obj.pathname.split("/"), lastPath = pathnames[2].split(".");
|
|
23
|
+
return obj.deviceSerial = pathnames[1], obj.channelNo = lastPath[0], obj.type = lastPath[lastPath.length - 1],
|
|
24
|
+
"live" === obj.type && (obj.definition = lastPath.length > 2 ? lastPath[1] : "sd"),
|
|
25
|
+
"rec" === obj.type && pathnames[2] && (obj.recType = pathnames[2].includes(".cloud.") ? "cloud" : ""),
|
|
26
|
+
obj.searchParams = getQuery(url), obj;
|
|
27
|
+
}
|
|
28
|
+
return {};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
var codec = {
|
|
32
|
+
h264: 1,
|
|
33
|
+
h265: 2,
|
|
34
|
+
h266: 4,
|
|
35
|
+
vp8: 8,
|
|
36
|
+
vp9: 16,
|
|
37
|
+
av1: 32
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
exports.addVc = function(url, codecs) {
|
|
41
|
+
void 0 === codecs && (codecs = [ "h264" ]);
|
|
42
|
+
var vc = (codecs || [ "h264" ]).reduce(function(acc, key) {
|
|
43
|
+
return key = key.toLowerCase(), acc + (codec[key] || 0);
|
|
44
|
+
}, 0), host = url.split("?")[0], query = url.split("?")[1];
|
|
45
|
+
return query = (query || "").split("&").filter(function(item) {
|
|
46
|
+
return !/^vc=\d?/.test(item);
|
|
47
|
+
}).join("&"), host + "?" + (query += (query ? "&" : "") + "vc=" + vc);
|
|
48
|
+
}, exports.getQuery = getQuery, exports.getStaticPath = function(path, base) {
|
|
49
|
+
void 0 === base && (base = "");
|
|
50
|
+
var protocolReg = /^(https?:\/\/|^\/\/)/;
|
|
51
|
+
if ("" === path || void 0 === path) return "";
|
|
52
|
+
if (protocolReg.test(path)) return path;
|
|
53
|
+
path = path.replace(/\/+/g, "/"), base = protocolReg.test(base || "") ? base : window.location.href;
|
|
54
|
+
try {
|
|
55
|
+
return new URL(path, base).href;
|
|
56
|
+
} catch (error) {
|
|
57
|
+
return path;
|
|
58
|
+
}
|
|
59
|
+
}, exports.isHttp = function(url) {
|
|
60
|
+
return /^http[s]?:\/\//.test(url);
|
|
61
|
+
}, exports.isMobile = function(agent) {
|
|
62
|
+
var _navigator, _navigator1, _navigator2, _window;
|
|
63
|
+
return /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone|Opera Mini)/i.test(null != agent ? agent : null == (_navigator = navigator) ? void 0 : _navigator.userAgent) || /Macintosh/i.test(null == (_navigator1 = navigator) ? void 0 : _navigator1.userAgent) && (null == (_navigator2 = navigator) ? void 0 : _navigator2.maxTouchPoints) > 1 || (null == (_window = window) ? void 0 : _window.__IS_MOBILE_SIMULATOR__);
|
|
64
|
+
}, exports.parseEzopenUrl = parseEzopenUrl, exports.pick = function(obj, keys) {
|
|
65
|
+
return Object.entries(obj).reduce(function(acc, param) {
|
|
66
|
+
var key = param[0], value = param[1];
|
|
67
|
+
return keys.includes(key) && (acc[key] = value), acc;
|
|
68
|
+
}, {});
|
|
69
|
+
}, exports.switchEzopenUrl = function(url, options) {
|
|
70
|
+
var _ref = null != options ? options : {}, type = _ref.type, recType = _ref.recType, search = _ref.search, definition = _ref.definition, channelNo = _ref.channelNo, deviceSerial = _ref.deviceSerial, validateCode = _ref.validateCode, obj = parseEzopenUrl(url);
|
|
71
|
+
if (search) {
|
|
72
|
+
var ns = Object.assign({}, obj.searchParams, getQuery("?" + search.replace("?", "")));
|
|
73
|
+
search = Object.entries(ns).reduce(function(acc, param) {
|
|
74
|
+
var key = param[0], value = param[1];
|
|
75
|
+
return "" === value ? acc : acc += key + "=" + encodeURIComponent(value) + "&";
|
|
76
|
+
}, "?").replace(/&$/, "");
|
|
77
|
+
} else search = obj.search;
|
|
78
|
+
return type = type || obj.type, recType = null != recType ? recType : obj.recType,
|
|
79
|
+
definition = null != definition ? definition : obj.definition, channelNo = channelNo || obj.channelNo,
|
|
80
|
+
deviceSerial = deviceSerial || obj.deviceSerial, validateCode = null != validateCode ? validateCode : obj.validateCode,
|
|
81
|
+
obj.protocol + "//" + (validateCode ? validateCode + "@" : "") + obj.hostname + "/" + deviceSerial + "/" + channelNo + ("rec" === type && recType ? "." + recType : "") + ("live" === type && "hd" === definition ? "." + definition : "") + "." + type + search;
|
|
82
|
+
};
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,92 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
var getQuery = function(url) {
|
|
2
|
+
var query = url.split("?")[1], obj = {};
|
|
3
|
+
if (query) for (var vars = query.split("&"), i = 0; i < vars.length; i++) {
|
|
4
|
+
var pair = vars[i].split("=");
|
|
5
|
+
obj[pair[0]] = decodeURIComponent(pair[1]);
|
|
6
|
+
}
|
|
7
|
+
return obj;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
function parseEzopenUrl(url) {
|
|
11
|
+
var obj = {};
|
|
12
|
+
if (/^ezopen:\/\//.test(url)) {
|
|
13
|
+
if (url.includes("@")) {
|
|
14
|
+
var endIndex = url.indexOf("@"), startIndex = url.indexOf("//");
|
|
15
|
+
obj.validateCode = url.substring(startIndex + 2, endIndex), url = url.replace(obj.validateCode + "@", "");
|
|
16
|
+
}
|
|
17
|
+
obj.protocol = url.split("//")[0], obj.search = url.split("?")[1] ? "?" + url.split("?")[1] : "",
|
|
18
|
+
obj.hostname = url.split("/")[2], obj.origin = obj.protocol + "//" + obj.hostname,
|
|
19
|
+
obj.pathname = url.split("?")[0].replace(obj.origin, ""), obj.href = url;
|
|
20
|
+
var pathnames = obj.pathname.split("/"), lastPath = pathnames[2].split(".");
|
|
21
|
+
return obj.deviceSerial = pathnames[1], obj.channelNo = lastPath[0], obj.type = lastPath[lastPath.length - 1],
|
|
22
|
+
"live" === obj.type && (obj.definition = lastPath.length > 2 ? lastPath[1] : "sd"),
|
|
23
|
+
"rec" === obj.type && pathnames[2] && (obj.recType = pathnames[2].includes(".cloud.") ? "cloud" : ""),
|
|
24
|
+
obj.searchParams = getQuery(url), obj;
|
|
25
|
+
}
|
|
26
|
+
return {};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function switchEzopenUrl(url, options) {
|
|
30
|
+
var _ref = null != options ? options : {}, type = _ref.type, recType = _ref.recType, search = _ref.search, definition = _ref.definition, channelNo = _ref.channelNo, deviceSerial = _ref.deviceSerial, validateCode = _ref.validateCode, obj = parseEzopenUrl(url);
|
|
31
|
+
if (search) {
|
|
32
|
+
var ns = Object.assign({}, obj.searchParams, getQuery("?" + search.replace("?", "")));
|
|
33
|
+
search = Object.entries(ns).reduce(function(acc, param) {
|
|
34
|
+
var key = param[0], value = param[1];
|
|
35
|
+
return "" === value ? acc : acc += key + "=" + encodeURIComponent(value) + "&";
|
|
36
|
+
}, "?").replace(/&$/, "");
|
|
37
|
+
} else search = obj.search;
|
|
38
|
+
return type = type || obj.type, recType = null != recType ? recType : obj.recType,
|
|
39
|
+
definition = null != definition ? definition : obj.definition, channelNo = channelNo || obj.channelNo,
|
|
40
|
+
deviceSerial = deviceSerial || obj.deviceSerial, validateCode = null != validateCode ? validateCode : obj.validateCode,
|
|
41
|
+
obj.protocol + "//" + (validateCode ? validateCode + "@" : "") + obj.hostname + "/" + deviceSerial + "/" + channelNo + ("rec" === type && recType ? "." + recType : "") + ("live" === type && "hd" === definition ? "." + definition : "") + "." + type + search;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function isHttp(url) {
|
|
45
|
+
return /^http[s]?:\/\//.test(url);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function isMobile(agent) {
|
|
49
|
+
var _navigator, _navigator1, _navigator2, _window;
|
|
50
|
+
return /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone|Opera Mini)/i.test(null != agent ? agent : null == (_navigator = navigator) ? void 0 : _navigator.userAgent) || /Macintosh/i.test(null == (_navigator1 = navigator) ? void 0 : _navigator1.userAgent) && (null == (_navigator2 = navigator) ? void 0 : _navigator2.maxTouchPoints) > 1 || (null == (_window = window) ? void 0 : _window.__IS_MOBILE_SIMULATOR__);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function pick(obj, keys) {
|
|
54
|
+
return Object.entries(obj).reduce(function(acc, param) {
|
|
55
|
+
var key = param[0], value = param[1];
|
|
56
|
+
return keys.includes(key) && (acc[key] = value), acc;
|
|
57
|
+
}, {});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function getStaticPath(path, base) {
|
|
61
|
+
void 0 === base && (base = "");
|
|
62
|
+
var protocolReg = /^(https?:\/\/|^\/\/)/;
|
|
63
|
+
if ("" === path || void 0 === path) return "";
|
|
64
|
+
if (protocolReg.test(path)) return path;
|
|
65
|
+
path = path.replace(/\/+/g, "/"), base = protocolReg.test(base || "") ? base : window.location.href;
|
|
66
|
+
try {
|
|
67
|
+
return new URL(path, base).href;
|
|
68
|
+
} catch (error) {
|
|
69
|
+
return path;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
var codec = {
|
|
74
|
+
h264: 1,
|
|
75
|
+
h265: 2,
|
|
76
|
+
h266: 4,
|
|
77
|
+
vp8: 8,
|
|
78
|
+
vp9: 16,
|
|
79
|
+
av1: 32
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
function addVc(url, codecs) {
|
|
83
|
+
void 0 === codecs && (codecs = [ "h264" ]);
|
|
84
|
+
var vc = (codecs || [ "h264" ]).reduce(function(acc, key) {
|
|
85
|
+
return key = key.toLowerCase(), acc + (codec[key] || 0);
|
|
86
|
+
}, 0), host = url.split("?")[0], query = url.split("?")[1];
|
|
87
|
+
return query = (query || "").split("&").filter(function(item) {
|
|
88
|
+
return !/^vc=\d?/.test(item);
|
|
89
|
+
}).join("&"), host + "?" + (query += (query ? "&" : "") + "vc=" + vc);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export { addVc, getQuery, getStaticPath, isHttp, isMobile, parseEzopenUrl, pick, switchEzopenUrl };
|
package/dist/index.umd.js
CHANGED
|
@@ -1,7 +1,82 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
!function(global, factory) {
|
|
2
|
+
"object" == typeof exports && "undefined" != typeof module ? factory(exports) : "function" == typeof define && define.amd ? define([ "exports" ], factory) : factory((global = "undefined" != typeof globalThis ? globalThis : global || self).Tools = {});
|
|
3
|
+
}(this, function(exports) {
|
|
4
|
+
"use strict";
|
|
5
|
+
var getQuery = function(url) {
|
|
6
|
+
var query = url.split("?")[1], obj = {};
|
|
7
|
+
if (query) for (var vars = query.split("&"), i = 0; i < vars.length; i++) {
|
|
8
|
+
var pair = vars[i].split("=");
|
|
9
|
+
obj[pair[0]] = decodeURIComponent(pair[1]);
|
|
10
|
+
}
|
|
11
|
+
return obj;
|
|
12
|
+
};
|
|
13
|
+
function parseEzopenUrl(url) {
|
|
14
|
+
var obj = {};
|
|
15
|
+
if (/^ezopen:\/\//.test(url)) {
|
|
16
|
+
if (url.includes("@")) {
|
|
17
|
+
var endIndex = url.indexOf("@"), startIndex = url.indexOf("//");
|
|
18
|
+
obj.validateCode = url.substring(startIndex + 2, endIndex), url = url.replace(obj.validateCode + "@", "");
|
|
19
|
+
}
|
|
20
|
+
obj.protocol = url.split("//")[0], obj.search = url.split("?")[1] ? "?" + url.split("?")[1] : "",
|
|
21
|
+
obj.hostname = url.split("/")[2], obj.origin = obj.protocol + "//" + obj.hostname,
|
|
22
|
+
obj.pathname = url.split("?")[0].replace(obj.origin, ""), obj.href = url;
|
|
23
|
+
var pathnames = obj.pathname.split("/"), lastPath = pathnames[2].split(".");
|
|
24
|
+
return obj.deviceSerial = pathnames[1], obj.channelNo = lastPath[0], obj.type = lastPath[lastPath.length - 1],
|
|
25
|
+
"live" === obj.type && (obj.definition = lastPath.length > 2 ? lastPath[1] : "sd"),
|
|
26
|
+
"rec" === obj.type && pathnames[2] && (obj.recType = pathnames[2].includes(".cloud.") ? "cloud" : ""),
|
|
27
|
+
obj.searchParams = getQuery(url), obj;
|
|
28
|
+
}
|
|
29
|
+
return {};
|
|
30
|
+
}
|
|
31
|
+
var codec = {
|
|
32
|
+
h264: 1,
|
|
33
|
+
h265: 2,
|
|
34
|
+
h266: 4,
|
|
35
|
+
vp8: 8,
|
|
36
|
+
vp9: 16,
|
|
37
|
+
av1: 32
|
|
38
|
+
};
|
|
39
|
+
exports.addVc = function(url, codecs) {
|
|
40
|
+
void 0 === codecs && (codecs = [ "h264" ]);
|
|
41
|
+
var vc = (codecs || [ "h264" ]).reduce(function(acc, key) {
|
|
42
|
+
return key = key.toLowerCase(), acc + (codec[key] || 0);
|
|
43
|
+
}, 0), host = url.split("?")[0], query = url.split("?")[1];
|
|
44
|
+
return query = (query || "").split("&").filter(function(item) {
|
|
45
|
+
return !/^vc=\d?/.test(item);
|
|
46
|
+
}).join("&"), host + "?" + (query += (query ? "&" : "") + "vc=" + vc);
|
|
47
|
+
}, exports.getQuery = getQuery, exports.getStaticPath = function(path, base) {
|
|
48
|
+
void 0 === base && (base = "");
|
|
49
|
+
var protocolReg = /^(https?:\/\/|^\/\/)/;
|
|
50
|
+
if ("" === path || void 0 === path) return "";
|
|
51
|
+
if (protocolReg.test(path)) return path;
|
|
52
|
+
path = path.replace(/\/+/g, "/"), base = protocolReg.test(base || "") ? base : window.location.href;
|
|
53
|
+
try {
|
|
54
|
+
return new URL(path, base).href;
|
|
55
|
+
} catch (error) {
|
|
56
|
+
return path;
|
|
57
|
+
}
|
|
58
|
+
}, exports.isHttp = function(url) {
|
|
59
|
+
return /^http[s]?:\/\//.test(url);
|
|
60
|
+
}, exports.isMobile = function(agent) {
|
|
61
|
+
var _navigator, _navigator1, _navigator2, _window;
|
|
62
|
+
return /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone|Opera Mini)/i.test(null != agent ? agent : null == (_navigator = navigator) ? void 0 : _navigator.userAgent) || /Macintosh/i.test(null == (_navigator1 = navigator) ? void 0 : _navigator1.userAgent) && (null == (_navigator2 = navigator) ? void 0 : _navigator2.maxTouchPoints) > 1 || (null == (_window = window) ? void 0 : _window.__IS_MOBILE_SIMULATOR__);
|
|
63
|
+
}, exports.parseEzopenUrl = parseEzopenUrl, exports.pick = function(obj, keys) {
|
|
64
|
+
return Object.entries(obj).reduce(function(acc, param) {
|
|
65
|
+
var key = param[0], value = param[1];
|
|
66
|
+
return keys.includes(key) && (acc[key] = value), acc;
|
|
67
|
+
}, {});
|
|
68
|
+
}, exports.switchEzopenUrl = function(url, options) {
|
|
69
|
+
var _ref = null != options ? options : {}, type = _ref.type, recType = _ref.recType, search = _ref.search, definition = _ref.definition, channelNo = _ref.channelNo, deviceSerial = _ref.deviceSerial, validateCode = _ref.validateCode, obj = parseEzopenUrl(url);
|
|
70
|
+
if (search) {
|
|
71
|
+
var ns = Object.assign({}, obj.searchParams, getQuery("?" + search.replace("?", "")));
|
|
72
|
+
search = Object.entries(ns).reduce(function(acc, param) {
|
|
73
|
+
var key = param[0], value = param[1];
|
|
74
|
+
return "" === value ? acc : acc += key + "=" + encodeURIComponent(value) + "&";
|
|
75
|
+
}, "?").replace(/&$/, "");
|
|
76
|
+
} else search = obj.search;
|
|
77
|
+
return type = type || obj.type, recType = null != recType ? recType : obj.recType,
|
|
78
|
+
definition = null != definition ? definition : obj.definition, channelNo = channelNo || obj.channelNo,
|
|
79
|
+
deviceSerial = deviceSerial || obj.deviceSerial, validateCode = null != validateCode ? validateCode : obj.validateCode,
|
|
80
|
+
obj.protocol + "//" + (validateCode ? validateCode + "@" : "") + obj.hostname + "/" + deviceSerial + "/" + channelNo + ("rec" === type && recType ? "." + recType : "") + ("live" === type && "hd" === definition ? "." + definition : "") + "." + type + search;
|
|
81
|
+
};
|
|
82
|
+
});
|
package/dist/types/isMobile.d.ts
CHANGED