@cclr/utils 0.0.8 → 0.0.10
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/lib/cjs/index.js +1 -32
- package/lib/esm/index.js +1 -29
- package/lib/type/index.d.ts +37 -20
- package/package.json +10 -3
package/lib/cjs/index.js
CHANGED
|
@@ -1,32 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const urlREG = new RegExp([
|
|
4
|
-
"^(https?:)//", // protocol
|
|
5
|
-
"(([^:/?#]*)(?::([0-9]+))?)", // host (hostname and port)
|
|
6
|
-
"(/{0,1}[^?#]*)", // pathname
|
|
7
|
-
"(\\?[^#]*|)", // search
|
|
8
|
-
"(#.*|)$", // hash
|
|
9
|
-
].join(""));
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* 解析url
|
|
13
|
-
* @param url
|
|
14
|
-
*/
|
|
15
|
-
function urlParse(url) {
|
|
16
|
-
const match = url.match(urlREG);
|
|
17
|
-
return match
|
|
18
|
-
? {
|
|
19
|
-
href: url,
|
|
20
|
-
protocol: match[1],
|
|
21
|
-
host: match[2],
|
|
22
|
-
hostname: match[3],
|
|
23
|
-
port: match[4],
|
|
24
|
-
pathname: match[5],
|
|
25
|
-
search: match[6],
|
|
26
|
-
hash: match[7],
|
|
27
|
-
}
|
|
28
|
-
: {};
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
exports.urlParse = urlParse;
|
|
32
|
-
exports.urlREG = urlREG;
|
|
1
|
+
"use strict";var e=require("@cclr/lang");const t=new RegExp(["^(?<protocol>[a-zA-Z][a-zA-Zd+-.]*:)?","(?:\\/\\/(?<auth>[^@/?#]*@)?(?<host>[^/?#]*))?","(?<pathname>[^?#]*)","(?:\\?(?<query>[^#]*))?","(?:#(?<hash>.*))?"].join("")),l=/#(?<hash>.*)/,r=e=>{if(!e)return"";const t=e.split("?")[1];return t?t.replace(l,""):""};const n={parseUrl:function(l){const r=l.match(t),{protocol:n,auth:u,host:s,pathname:o,query:a,hash:c}=e.get(r,"groups",{});let h=null,p=null;if(s){const[e,t]=s.split(":");h=e,p=t||null}return{href:l,protocol:n||null,slashes:l.includes("//")||null,auth:u?u.slice(0,-1):null,host:s||null,hostname:h||null,port:p||null,pathname:o||null,search:a?`?${a}`:null,query:a||null,hash:c||null}},getQuery:r,getQueryObj:e=>{if(!e)return{};return(r(e)||e).split("&").reduce(((e,t)=>{const l=t.indexOf("=");let r=[];r=-1!==l?[t.substring(0,l),t.substring(l+1)]:[t];const n=r.map((e=>decodeURIComponent(e.replace("+"," "))));return e[n[0]]=n[1],e}),{})}};exports.HASH_REGEX=l,exports.QUERY_REGEX=/\?(?<query>[^#]*)/,exports.URL_REGEX=t,exports.uri=n;
|
package/lib/esm/index.js
CHANGED
|
@@ -1,29 +1 @@
|
|
|
1
|
-
const
|
|
2
|
-
"^(https?:)//", // protocol
|
|
3
|
-
"(([^:/?#]*)(?::([0-9]+))?)", // host (hostname and port)
|
|
4
|
-
"(/{0,1}[^?#]*)", // pathname
|
|
5
|
-
"(\\?[^#]*|)", // search
|
|
6
|
-
"(#.*|)$", // hash
|
|
7
|
-
].join(""));
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* 解析url
|
|
11
|
-
* @param url
|
|
12
|
-
*/
|
|
13
|
-
function urlParse(url) {
|
|
14
|
-
const match = url.match(urlREG);
|
|
15
|
-
return match
|
|
16
|
-
? {
|
|
17
|
-
href: url,
|
|
18
|
-
protocol: match[1],
|
|
19
|
-
host: match[2],
|
|
20
|
-
hostname: match[3],
|
|
21
|
-
port: match[4],
|
|
22
|
-
pathname: match[5],
|
|
23
|
-
search: match[6],
|
|
24
|
-
hash: match[7],
|
|
25
|
-
}
|
|
26
|
-
: {};
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export { urlParse, urlREG };
|
|
1
|
+
import{get as l}from"@cclr/lang";const n=new RegExp(["^(?<protocol>[a-zA-Z][a-zA-Zd+-.]*:)?","(?:\\/\\/(?<auth>[^@/?#]*@)?(?<host>[^/?#]*))?","(?<pathname>[^?#]*)","(?:\\?(?<query>[^#]*))?","(?:#(?<hash>.*))?"].join("")),t=/\?(?<query>[^#]*)/,e=/#(?<hash>.*)/,r=l=>{if(!l)return"";const n=l.split("?")[1];return n?n.replace(e,""):""};const u={parseUrl:function(t){const e=t.match(n),{protocol:r,auth:u,host:o,pathname:s,query:a,hash:h}=l(e,"groups",{});let c=null,p=null;if(o){const[l,n]=o.split(":");c=l,p=n||null}return{href:t,protocol:r||null,slashes:t.includes("//")||null,auth:u?u.slice(0,-1):null,host:o||null,hostname:c||null,port:p||null,pathname:s||null,search:a?`?${a}`:null,query:a||null,hash:h||null}},getQuery:r,getQueryObj:l=>{if(!l)return{};return(r(l)||l).split("&").reduce(((l,n)=>{const t=n.indexOf("=");let e=[];e=-1!==t?[n.substring(0,t),n.substring(t+1)]:[n];const r=e.map((l=>decodeURIComponent(l.replace("+"," "))));return l[r[0]]=r[1],l}),{})}};export{e as HASH_REGEX,t as QUERY_REGEX,n as URL_REGEX,u as uri};
|
package/lib/type/index.d.ts
CHANGED
|
@@ -1,27 +1,44 @@
|
|
|
1
|
+
interface IUrl {
|
|
2
|
+
/** 完整的 URL 字符串,例如 https://example.com:8080/path?query#fragment */
|
|
3
|
+
href?: string | null | undefined;
|
|
4
|
+
/** URL 的协议部分,例如 http: 或 https: */
|
|
5
|
+
protocol?: string | null | undefined;
|
|
6
|
+
/** 指示 URL 是否包含双斜杠(//),例如 http:// */
|
|
7
|
+
slashes?: boolean | null | undefined;
|
|
8
|
+
/** 通常以 用户名:密码 的形式出现在 URL 中;例如: http://username:password@example.com */
|
|
9
|
+
auth?: string | null | undefined;
|
|
10
|
+
/** 主机名和端口的组合,例如 example.com:8080 */
|
|
11
|
+
host?: string | null | undefined;
|
|
12
|
+
/** 主机名,不包括端口,例如 example.com */
|
|
13
|
+
hostname?: string | null | undefined;
|
|
14
|
+
/** 端口号,例如 8080 */
|
|
15
|
+
port?: string | number | null | undefined;
|
|
16
|
+
/** URL 的路径部分,例如 /path/to/resource */
|
|
17
|
+
pathname?: string | null | undefined;
|
|
18
|
+
/** URL 的查询字符串部分,通常以 ? 开头,例如 ?query=string */
|
|
19
|
+
search?: string | null | undefined;
|
|
20
|
+
/** 查询参数,通常是键值对的组合,例如 key=value */
|
|
21
|
+
query?: string | null | undefined;
|
|
22
|
+
/** URL 的片段标识符,通常以 # 开头,例如 #section1 */
|
|
23
|
+
hash?: string | null | undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
1
26
|
/**
|
|
2
27
|
* 解析url
|
|
28
|
+
* 通过正则解析
|
|
3
29
|
* @param url
|
|
30
|
+
* @returns
|
|
4
31
|
*/
|
|
5
|
-
declare function
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
pathname: string;
|
|
12
|
-
search: string;
|
|
13
|
-
hash: string;
|
|
14
|
-
} | {
|
|
15
|
-
href?: undefined;
|
|
16
|
-
protocol?: undefined;
|
|
17
|
-
host?: undefined;
|
|
18
|
-
hostname?: undefined;
|
|
19
|
-
port?: undefined;
|
|
20
|
-
pathname?: undefined;
|
|
21
|
-
search?: undefined;
|
|
22
|
-
hash?: undefined;
|
|
32
|
+
declare function parseUrl(url: string): IUrl;
|
|
33
|
+
|
|
34
|
+
declare const uri: {
|
|
35
|
+
parseUrl: typeof parseUrl;
|
|
36
|
+
getQuery: (url: string) => string;
|
|
37
|
+
getQueryObj: (url: string) => {};
|
|
23
38
|
};
|
|
24
39
|
|
|
25
|
-
declare const
|
|
40
|
+
declare const URL_REGEX: RegExp;
|
|
41
|
+
declare const QUERY_REGEX: RegExp;
|
|
42
|
+
declare const HASH_REGEX: RegExp;
|
|
26
43
|
|
|
27
|
-
export {
|
|
44
|
+
export { HASH_REGEX, QUERY_REGEX, URL_REGEX, uri };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cclr/utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.10",
|
|
4
4
|
"description": "> TODO: description",
|
|
5
5
|
"author": "cclr <18843152354@163.com>",
|
|
6
6
|
"homepage": "",
|
|
@@ -15,12 +15,19 @@
|
|
|
15
15
|
"files": [
|
|
16
16
|
"lib"
|
|
17
17
|
],
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public",
|
|
20
|
+
"registry": "https://registry.npmjs.org/"
|
|
21
|
+
},
|
|
18
22
|
"repository": {
|
|
19
23
|
"type": "git",
|
|
20
24
|
"url": "git@gitee.com:cclr/toolkit.git"
|
|
21
25
|
},
|
|
22
26
|
"scripts": {
|
|
23
|
-
"test": "
|
|
27
|
+
"test": "vitest"
|
|
24
28
|
},
|
|
25
|
-
"gitHead": "
|
|
29
|
+
"gitHead": "d58e3b15266c99cfe0938671a0de64bcc19e0b3f",
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@cclr/lang": "0.0.10"
|
|
32
|
+
}
|
|
26
33
|
}
|