@lijuhong1981/jsurl 1.0.1 → 1.0.3
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/README.md +11 -0
- package/index.js +10 -4
- package/package.json +10 -13
- package/src/extractBaseName.js +2 -2
- package/src/extractBaseUrl.js +1 -1
- package/src/extractExtName.js +1 -1
- package/src/extractFileName.js +1 -1
- package/src/{resolveUrl.js → mergeUrl.js} +2 -2
- package/src/parsePath.js +30 -0
- package/src/parseUrl.js +47 -0
- package/src/splitPath.js +14 -0
- package/src/splitUrl.js +13 -0
- package/src/extractQueryString.js +0 -12
package/README.md
ADDED
package/index.js
CHANGED
|
@@ -2,24 +2,30 @@ import extractBaseUrl from "./src/extractBaseUrl.js";
|
|
|
2
2
|
import extractFileName from "./src/extractFileName.js";
|
|
3
3
|
import extractBaseName from "./src/extractBaseName.js";
|
|
4
4
|
import extractExtName from "./src/extractExtName.js";
|
|
5
|
-
import
|
|
6
|
-
import resolveUrl from "./src/resolveUrl.js";
|
|
5
|
+
import mergeUrl from "./src/mergeUrl.js";
|
|
7
6
|
import hasProtocolPrefix from "./src/hasProtocolPrefix";
|
|
8
7
|
import isBlobProtocol from "./src/isBlobProtocol.js";
|
|
9
8
|
import isDataProtocol from "./src/isDataProtocol.js";
|
|
10
9
|
import isFileProtocol from "./src/isFileProtocol.js";
|
|
11
10
|
import isHttpProtocol from "./src/isHttpProtocol.js";
|
|
11
|
+
import splitPath from "./src/splitPath.js";
|
|
12
|
+
import splitUrl from "./src/splitUrl.js";
|
|
13
|
+
import parsePath from "./src/parsePath.js";
|
|
14
|
+
import parseUrl from "./src/parseUrl.js";
|
|
12
15
|
|
|
13
16
|
export {
|
|
14
17
|
extractBaseUrl,
|
|
15
18
|
extractFileName,
|
|
16
19
|
extractBaseName,
|
|
17
20
|
extractExtName,
|
|
18
|
-
|
|
19
|
-
resolveUrl,
|
|
21
|
+
mergeUrl,
|
|
20
22
|
hasProtocolPrefix,
|
|
21
23
|
isBlobProtocol,
|
|
22
24
|
isDataProtocol,
|
|
23
25
|
isFileProtocol,
|
|
24
26
|
isHttpProtocol,
|
|
27
|
+
splitPath,
|
|
28
|
+
splitUrl,
|
|
29
|
+
parsePath,
|
|
30
|
+
parseUrl,
|
|
25
31
|
};
|
package/package.json
CHANGED
|
@@ -1,21 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lijuhong1981/jsurl",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "",
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"description": "> TODO: description",
|
|
5
|
+
"author": "lijuhong1981",
|
|
6
|
+
"homepage": "https://github.com/lijuhong1981/jslib#readme",
|
|
7
|
+
"license": "ISC",
|
|
8
|
+
"type": "module",
|
|
5
9
|
"main": "index.js",
|
|
6
10
|
"module": "index.js",
|
|
7
|
-
"type": "module",
|
|
8
|
-
"scripts": {
|
|
9
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
10
|
-
},
|
|
11
11
|
"repository": {
|
|
12
12
|
"type": "git",
|
|
13
|
-
"url": "git+https://github.com/lijuhong1981/
|
|
13
|
+
"url": "git+https://github.com/lijuhong1981/jslib.git"
|
|
14
14
|
},
|
|
15
|
-
"author": "lijuhong1981",
|
|
16
|
-
"license": "ISC",
|
|
17
15
|
"bugs": {
|
|
18
|
-
"url": "https://github.com/lijuhong1981/
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
}
|
|
16
|
+
"url": "https://github.com/lijuhong1981/jslib/issues"
|
|
17
|
+
}
|
|
18
|
+
}
|
package/src/extractBaseName.js
CHANGED
|
@@ -6,11 +6,11 @@
|
|
|
6
6
|
function extractBaseName(url) {
|
|
7
7
|
let index = url.lastIndexOf('?');
|
|
8
8
|
if (index !== -1)
|
|
9
|
-
url = url.substring(0, index
|
|
9
|
+
url = url.substring(0, index);
|
|
10
10
|
|
|
11
11
|
index = url.lastIndexOf('.');
|
|
12
12
|
if (index !== -1)
|
|
13
|
-
url = url.substring(0, index
|
|
13
|
+
url = url.substring(0, index);
|
|
14
14
|
|
|
15
15
|
index = url.lastIndexOf('/');
|
|
16
16
|
if (index === -1)
|
package/src/extractBaseUrl.js
CHANGED
package/src/extractExtName.js
CHANGED
package/src/extractFileName.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import hasProtocolPrefix from "./hasProtocolPrefix.js";
|
|
2
2
|
|
|
3
|
-
function
|
|
3
|
+
function mergeUrl(base, relative) {
|
|
4
4
|
if (!base || base.length === 0 || hasProtocolPrefix(relative))
|
|
5
5
|
return relative;
|
|
6
6
|
const hasEndSeparator = base.endsWith('/');
|
|
@@ -13,4 +13,4 @@ function resolveUrl(base, relative) {
|
|
|
13
13
|
return base + relative.substring(1);
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
-
export default
|
|
16
|
+
export default mergeUrl;
|
package/src/parsePath.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import splitPath from "./splitPath.js";
|
|
2
|
+
|
|
3
|
+
const names = ['source', 'directory', 'root', 'filename', 'basename', 'extname'];
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 解析path,输出结果如下:
|
|
7
|
+
* {
|
|
8
|
+
* source: 'c:/xxx/xxx/test.png',
|
|
9
|
+
* directory: 'c:/xxx/xxx/',
|
|
10
|
+
* root: 'c:/',
|
|
11
|
+
* filename: 'test.png',
|
|
12
|
+
* basename: 'test',
|
|
13
|
+
* extname: '.png',
|
|
14
|
+
* format: 'png',
|
|
15
|
+
* }
|
|
16
|
+
* @param {String} path 传入的path
|
|
17
|
+
* @param {Object} result 传入的结果对象,可不填
|
|
18
|
+
* @returns {Object} 输出的结果对象
|
|
19
|
+
*/
|
|
20
|
+
function parsePath(path, result = {}) {
|
|
21
|
+
const allParts = splitPath(path);
|
|
22
|
+
for (let i = 0; i < names.length; i++) {
|
|
23
|
+
result[names[i]] = allParts[i];
|
|
24
|
+
}
|
|
25
|
+
if (result.extname)
|
|
26
|
+
result['format'] = result.extname.startsWith('.') ? result.extname.substring(1) : result.extname;
|
|
27
|
+
return result;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export default parsePath;
|
package/src/parseUrl.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import splitUrl from "./splitUrl";
|
|
2
|
+
|
|
3
|
+
const names = ["source", "protocol", "authority", "userInfo", "user", "password", "host", "port", "relative", "path", "directory", "file", "query", "anchor"];
|
|
4
|
+
const parseQuery = /(?:^|&)([^&=]*)=?([^&]*)/g;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 解析url,输出结果如下:
|
|
8
|
+
* {
|
|
9
|
+
* source: '',
|
|
10
|
+
* protocol: '',
|
|
11
|
+
* authority: '',
|
|
12
|
+
* userInfo: '',
|
|
13
|
+
* user: '',
|
|
14
|
+
* password: '',
|
|
15
|
+
* host: '',
|
|
16
|
+
* port: '',
|
|
17
|
+
* relative: '',
|
|
18
|
+
* path: '',
|
|
19
|
+
* directory: '',
|
|
20
|
+
* file: '',
|
|
21
|
+
* query: '',
|
|
22
|
+
* queryParams: {
|
|
23
|
+
* key1: value1,
|
|
24
|
+
* key2: value2,
|
|
25
|
+
* ...
|
|
26
|
+
* },
|
|
27
|
+
* anchor: '',
|
|
28
|
+
* }
|
|
29
|
+
* @param {String} url 传入的url
|
|
30
|
+
* @param {Object} result 传入的结果对象,可不填
|
|
31
|
+
* @returns {Object} 输出的结果对象
|
|
32
|
+
*/
|
|
33
|
+
function parseUrl(url, result = {}) {
|
|
34
|
+
const allParts = splitUrl(url);
|
|
35
|
+
for (let i = 0; i < names.length; i++) {
|
|
36
|
+
result[names[i]] = allParts[i];
|
|
37
|
+
}
|
|
38
|
+
result.queryParams = {};
|
|
39
|
+
if (result.query)
|
|
40
|
+
result.query.replace(parseQuery, function (match, key, value) {
|
|
41
|
+
if (key)
|
|
42
|
+
result.queryParams[key] = value;
|
|
43
|
+
});
|
|
44
|
+
return result;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export default parseUrl;
|
package/src/splitPath.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Regex to split a path into into [path, dir, root, fullname, basename, ext]
|
|
2
|
+
const splitPattern =
|
|
3
|
+
/^(((?:[a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/]+[^\\\/]+)?[\\\/]?)(?:[^\\\/]*[\\\/])*)((\.{1,2}|[^\\\/]+?|)(\.[^.\/\\]*|))[\\\/]*$/;;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 使用正则表达式把一个path解析为[path, directory, root, fullname, basename, extname]数组
|
|
7
|
+
* @param {String} path 传入的path
|
|
8
|
+
* @returns {Array} 解析结果数组
|
|
9
|
+
*/
|
|
10
|
+
function splitPath(path) {
|
|
11
|
+
return splitPattern.exec(path);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export default splitPath;
|
package/src/splitUrl.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// const strict = /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/;
|
|
2
|
+
const loose = /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/;
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 使用正则表达式把一个url解析为["source", "protocol", "authority", "userInfo", "user", "password", "host", "port", "relative", "path", "directory", "file", "query", "anchor"]数组
|
|
6
|
+
* @param {String} url 传入的url
|
|
7
|
+
* @returns {Array} 解析结果数组
|
|
8
|
+
*/
|
|
9
|
+
function splitUrl(url) {
|
|
10
|
+
return loose.exec(url);
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export default splitUrl;
|
|
@@ -1,12 +0,0 @@
|
|
|
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;
|