@lijuhong1981/jsurl 1.0.0

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/index.js ADDED
@@ -0,0 +1,25 @@
1
+ import extractBaseUrl from "./src/extractBaseUrl.js";
2
+ import extractFileName from "./src/extractFileName.js";
3
+ import extractBaseName from "./src/extractBaseName.js";
4
+ import extractExtName from "./src/extractExtName.js";
5
+ import extractQueryString from "./src/extractQueryString.js";
6
+ import resolveUrl from "./src/resolveUrl.js";
7
+ import hasProtocolPrefix from "./src/hasProtocolPrefix";
8
+ import isBlobProtocol from "./src/isBlobProtocol.js";
9
+ import isDataProtocol from "./src/isDataProtocol.js";
10
+ import isFileProtocol from "./src/isFileProtocol.js";
11
+ import isHttpProtocol from "./src/isHttpProtocol.js";
12
+
13
+ export {
14
+ extractBaseUrl,
15
+ extractFileName,
16
+ extractBaseName,
17
+ extractExtName,
18
+ extractQueryString,
19
+ resolveUrl,
20
+ hasProtocolPrefix,
21
+ isBlobProtocol,
22
+ isDataProtocol,
23
+ isFileProtocol,
24
+ isHttpProtocol,
25
+ };
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "@lijuhong1981/jsurl",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "module": "index.js",
7
+ "type": "module",
8
+ "scripts": {
9
+ "test": "echo \"Error: no test specified\" && exit 1"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/lijuhong1981/jsurl.git"
14
+ },
15
+ "author": "lijuhong1981",
16
+ "license": "ISC",
17
+ "bugs": {
18
+ "url": "https://github.com/lijuhong1981/jsurl/issues"
19
+ },
20
+ "homepage": "https://github.com/lijuhong1981/jsurl#readme"
21
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * 从url中提取不包含扩展名的文件名
3
+ * @param {String} url 传入的url
4
+ * @returns {String} 提取的文件名
5
+ */
6
+ function extractBaseName(url) {
7
+ let index = url.lastIndexOf('?');
8
+ if (index !== -1)
9
+ url = url.substring(0, index - 1);
10
+
11
+ index = url.lastIndexOf('.');
12
+ if (index !== -1)
13
+ url = url.substring(0, index - 1);
14
+
15
+ index = url.lastIndexOf('/');
16
+ if (index === -1)
17
+ return url;
18
+ return url.substring(index + 1);
19
+ };
20
+
21
+ export default extractBaseName;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * 从url中提取baseUrl
3
+ * @param {String} url 传入的url
4
+ * @returns {String} 提取的baseUrl
5
+ */
6
+ function extractBaseUrl(url) {
7
+ const index = url.lastIndexOf('/');
8
+ if (index === -1) return './';
9
+ return url.substring(0, index);
10
+ }
11
+
12
+ export default extractBaseUrl;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * 从url中提取文件扩展名
3
+ * @param {String} url 传入的url
4
+ * @returns {String} 提取的扩展名
5
+ */
6
+ function extractExtName(url) {
7
+ let index = url.lastIndexOf('?');
8
+ if (index !== -1)
9
+ url = url.substring(0, index - 1);
10
+
11
+ index = url.lastIndexOf('.');
12
+ if (index === -1)
13
+ return '';
14
+ return url.substring(index);
15
+ };
16
+
17
+ export default extractExtName;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * 从url中提取文件名
3
+ * @param {String} url 传入的url
4
+ * @returns {String} 提取的文件名
5
+ */
6
+ function extractFileName(url) {
7
+ let index = url.lastIndexOf('?');
8
+ if (index !== -1)
9
+ url = url.substring(0, index - 1);
10
+
11
+ index = url.lastIndexOf('/');
12
+ if (index === -1)
13
+ return url;
14
+ return url.substring(index + 1);
15
+ };
16
+
17
+ export default extractFileName;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * 从url中提取query字符串
3
+ * @param {String} url 传入的url
4
+ * @returns {String} 提取的query字符串
5
+ */
6
+ function extractQueryString(url) {
7
+ const index = url.lastIndexOf('?');
8
+ if (index !== -1)
9
+ return url.substring(index + 1);
10
+ };
11
+
12
+ export default extractQueryString;
@@ -0,0 +1,7 @@
1
+ const pattern = /^(https?|ftp|file|data|blob):/i;
2
+
3
+ function hasProtocolPrefix(url) {
4
+ return pattern.test(url);
5
+ };
6
+
7
+ export default hasProtocolPrefix;
@@ -0,0 +1,7 @@
1
+ const pattern = /^blob:/i;
2
+
3
+ function isBlobProtocol(url) {
4
+ return pattern.test(url);
5
+ };
6
+
7
+ export default isBlobProtocol;
@@ -0,0 +1,7 @@
1
+ const pattern = /^data:/i;
2
+
3
+ function isDataProtocol(url) {
4
+ return pattern.test(url);
5
+ };
6
+
7
+ export default isDataProtocol;
@@ -0,0 +1,7 @@
1
+ const pattern = /^file:\/\//i;
2
+
3
+ function isFileProtocol(url) {
4
+ return pattern.test(url);
5
+ };
6
+
7
+ export default isFileProtocol;
@@ -0,0 +1,7 @@
1
+ const pattern = /^(https?:\/\/)?\.?\/?([\da-z])/i;
2
+
3
+ function isHttpProtocol(url) {
4
+ return pattern.test(url);
5
+ };
6
+
7
+ export default isHttpProtocol;
@@ -0,0 +1,16 @@
1
+ import hasProtocolPrefix from "./hasProtocolPrefix.js";
2
+
3
+ function resolveUrl(base, relative) {
4
+ if (!base || base.length === 0 || hasProtocolPrefix(relative))
5
+ return relative;
6
+ const hasEndSeparator = base.endsWith('/');
7
+ const hasStartSeparator = relative.startsWith('/');
8
+ if (!hasEndSeparator && !hasStartSeparator)
9
+ return base + '/' + relative;
10
+ else if ((hasEndSeparator && !hasStartSeparator) || (!hasEndSeparator && hasStartSeparator))
11
+ return base + relative;
12
+ else
13
+ return base + relative.substring(1);
14
+ };
15
+
16
+ export default resolveUrl;