@lijuhong1981/jsurl 1.0.0 → 1.0.2

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 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 extractQueryString from "./src/extractQueryString.js";
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
- extractQueryString,
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,6 +1,6 @@
1
1
  {
2
2
  "name": "@lijuhong1981/jsurl",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "module": "index.js",
@@ -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 - 1);
9
+ url = url.substring(0, index);
10
10
 
11
11
  index = url.lastIndexOf('.');
12
12
  if (index !== -1)
13
- url = url.substring(0, index - 1);
13
+ url = url.substring(0, index);
14
14
 
15
15
  index = url.lastIndexOf('/');
16
16
  if (index === -1)
@@ -6,7 +6,7 @@
6
6
  function extractBaseUrl(url) {
7
7
  const index = url.lastIndexOf('/');
8
8
  if (index === -1) return './';
9
- return url.substring(0, index);
9
+ return url.substring(0, index + 1);
10
10
  }
11
11
 
12
12
  export default extractBaseUrl;
@@ -1,17 +1,18 @@
1
1
  /**
2
2
  * 从url中提取文件扩展名
3
3
  * @param {String} url 传入的url
4
+ * @param {Boolean} containsPoint 是否包含'.'符号,默认true
4
5
  * @returns {String} 提取的扩展名
5
6
  */
6
- function extractExtName(url) {
7
+ function extractExtName(url, containsPoint = true) {
7
8
  let index = url.lastIndexOf('?');
8
9
  if (index !== -1)
9
- url = url.substring(0, index - 1);
10
+ url = url.substring(0, index);
10
11
 
11
12
  index = url.lastIndexOf('.');
12
13
  if (index === -1)
13
14
  return '';
14
- return url.substring(index);
15
+ return url.substring(containsPoint ? index : index + 1);
15
16
  };
16
17
 
17
18
  export default extractExtName;
@@ -6,7 +6,7 @@
6
6
  function extractFileName(url) {
7
7
  let index = url.lastIndexOf('?');
8
8
  if (index !== -1)
9
- url = url.substring(0, index - 1);
9
+ url = url.substring(0, index);
10
10
 
11
11
  index = url.lastIndexOf('/');
12
12
  if (index === -1)
@@ -1,6 +1,6 @@
1
1
  import hasProtocolPrefix from "./hasProtocolPrefix.js";
2
2
 
3
- function resolveUrl(base, relative) {
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 resolveUrl;
16
+ export default mergeUrl;
@@ -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) : '';
27
+ return result;
28
+ };
29
+
30
+ export default parsePath;
@@ -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;
@@ -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;
@@ -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;