@cclr/utils 0.1.2 → 0.1.4
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 -1
- package/lib/esm/index.js +1 -1
- package/lib/type/index.d.ts +25 -1
- package/package.json +4 -3
package/lib/cjs/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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("")),
|
|
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("")),n=/#(?<hash>.*)/,r=/^(?:[a-zA-Z][a-zA-Zd+-.]*:)?(?:\/\/(?:[^@/?#]*@)?[^/?#]*)?(?<pathname>[^?#]*)/,o=e=>{const{protocol:t,auth:n,host:r,hostname:o,port:s,pathname:u,query:l,search:a,hash:c}=e;return`${t?`${t}//`:""}${n?`${n}@`:""}${r||`${o||""}${s?`:${s}`:""}`}${u}${l?`?${l}`:a||""}${c?`#${c}`:""}`},s=e=>{if(!e)return"";const t=e.split("?")[1];return t?t.replace(n,""):""},u=e=>{if(!e)return{};return s(e).split("&").reduce(((e,t)=>{const n=t.indexOf("=");let r=[];r=-1!==n?[t.substring(0,n),t.substring(n+1)]:[t];const o=r.map((e=>decodeURIComponent(e.replace("+"," "))));return e[o[0]]=o[1],e}),{})};function l(n){const r=n.match(t),{protocol:o,auth:s,host:u,pathname:l,query:a,hash:c}=e.get(r,"groups",{});let h=null,p=null;if(u){const[e,t]=u.split(":");h=e,p=t||null}return{href:n,protocol:o||null,slashes:n.includes("//")||null,auth:s?s.slice(0,-1):null,host:u||null,hostname:h||null,port:p||null,pathname:l||null,search:a?`?${a}`:null,query:a||null,hash:c||null}}const a=t=>{const n=[];for(const r in t){const o=t[r];e.isUndefinedOrNull(o)?n.push(encodeURIComponent(r)+"="):n.push(encodeURIComponent(r)+"="+encodeURIComponent(o))}return n.join("&")},c=(e,t)=>{const{queryObj:n,...r}=t,s={...l(e),...r},c={...u(s.search||""),...n};return o({...s,search:"",query:a(c)})};function h(e){const t=e.match(r);if(t)return t.groups?.pathname||""}const p={parseUrl:l,getPathArr:function(t){const n=h(t);if(!e.isUndefined(n))return n.split("/").filter(Boolean)},getPathStr:h,getQuery:s,getQueryObj:u,toQueryString:a,change:c,addQuery:(e,t)=>c(e,{queryObj:t}),combine:o};exports.HASH_REGEX=n,exports.PATHNAME_REGEX=r,exports.QUERY_REGEX=/\?(?<query>[^#]*)/,exports.URL_REGEX=t,exports.deepClone=e=>JSON.parse(JSON.stringify(e)),exports.enumToArray=t=>e.isPlainObject(t)?[Object.keys(t)]:[],exports.uri=p,exports.uuid=()=>{const e=["a","b","c","d","e","f","0","1","2","3","4","5","6","7","8","9"],t=[];for(let n=0;n<36;n++)t[n]=8===n||13===n||18===n||23===n?"-":e[Math.ceil(Math.random()*e.length-1)];return t.join("")};
|
package/lib/esm/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{isPlainObject as t,get as e}from"@cclr/lang";const
|
|
1
|
+
import{isPlainObject as t,get as n,isUndefinedOrNull as e,isUndefined as r}from"@cclr/lang";const o=t=>JSON.parse(JSON.stringify(t)),s=n=>t(n)?[Object.keys(n)]:[],u=new RegExp(["^(?<protocol>[a-zA-Z][a-zA-Zd+-.]*:)?","(?:\\/\\/(?<auth>[^@/?#]*@)?(?<host>[^/?#]*))?","(?<pathname>[^?#]*)","(?:\\?(?<query>[^#]*))?","(?:#(?<hash>.*))?"].join("")),a=/\?(?<query>[^#]*)/,l=/#(?<hash>.*)/,c=/^(?:[a-zA-Z][a-zA-Zd+-.]*:)?(?:\/\/(?:[^@/?#]*@)?[^/?#]*)?(?<pathname>[^?#]*)/,h=t=>{const{protocol:n,auth:e,host:r,hostname:o,port:s,pathname:u,query:a,search:l,hash:c}=t;return`${n?`${n}//`:""}${e?`${e}@`:""}${r||`${o||""}${s?`:${s}`:""}`}${u}${a?`?${a}`:l||""}${c?`#${c}`:""}`},p=t=>{if(!t)return"";const n=t.split("?")[1];return n?n.replace(l,""):""},i=t=>{if(!t)return{};return p(t).split("&").reduce(((t,n)=>{const e=n.indexOf("=");let r=[];r=-1!==e?[n.substring(0,e),n.substring(e+1)]:[n];const o=r.map((t=>decodeURIComponent(t.replace("+"," "))));return t[o[0]]=o[1],t}),{})};function m(t){const e=t.match(u),{protocol:r,auth:o,host:s,pathname:a,query:l,hash:c}=n(e,"groups",{});let h=null,p=null;if(s){const[t,n]=s.split(":");h=t,p=n||null}return{href:t,protocol:r||null,slashes:t.includes("//")||null,auth:o?o.slice(0,-1):null,host:s||null,hostname:h||null,port:p||null,pathname:a||null,search:l?`?${l}`:null,query:l||null,hash:c||null}}const f=t=>{const n=[];for(const r in t){const o=t[r];e(o)?n.push(encodeURIComponent(r)+"="):n.push(encodeURIComponent(r)+"="+encodeURIComponent(o))}return n.join("&")},d=(t,n)=>{const{queryObj:e,...r}=n,o={...m(t),...r},s={...i(o.search||""),...e};return h({...o,search:"",query:f(s)})};function g(t){const n=t.match(c);if(n)return n.groups?.pathname||""}const y={parseUrl:m,getPathArr:function(t){const n=g(t);if(!r(n))return n.split("/").filter(Boolean)},getPathStr:g,getQuery:p,getQueryObj:i,toQueryString:f,change:d,addQuery:(t,n)=>d(t,{queryObj:n}),combine:h},$=()=>{const t=["a","b","c","d","e","f","0","1","2","3","4","5","6","7","8","9"],n=[];for(let e=0;e<36;e++)n[e]=8===e||13===e||18===e||23===e?"-":t[Math.ceil(Math.random()*t.length-1)];return n.join("")};export{l as HASH_REGEX,c as PATHNAME_REGEX,a as QUERY_REGEX,u as URL_REGEX,o as deepClone,s as enumToArray,y as uri,$ as uuid};
|
package/lib/type/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as _cclr_lang from '@cclr/lang';
|
|
1
2
|
import { TAny, TPlainObject } from '@cclr/lang';
|
|
2
3
|
|
|
3
4
|
/**
|
|
@@ -17,6 +18,7 @@ declare const enumToArray: (e: TPlainObject) => string[][];
|
|
|
17
18
|
declare const URL_REGEX: RegExp;
|
|
18
19
|
declare const QUERY_REGEX: RegExp;
|
|
19
20
|
declare const HASH_REGEX: RegExp;
|
|
21
|
+
declare const PATHNAME_REGEX: RegExp;
|
|
20
22
|
|
|
21
23
|
interface IUrl {
|
|
22
24
|
/** 完整的 URL 字符串,例如 https://example.com:8080/path?query#fragment */
|
|
@@ -43,6 +45,20 @@ interface IUrl {
|
|
|
43
45
|
hash?: string | null | undefined;
|
|
44
46
|
}
|
|
45
47
|
|
|
48
|
+
/**
|
|
49
|
+
* 获取 URL 路径数组
|
|
50
|
+
* @param url
|
|
51
|
+
* @returns
|
|
52
|
+
*/
|
|
53
|
+
declare function getPathArr(url: string): string[] | undefined;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 获取 URL 路径字符串
|
|
57
|
+
* @param url
|
|
58
|
+
* @returns
|
|
59
|
+
*/
|
|
60
|
+
declare function getPathStr(url: string): string | undefined;
|
|
61
|
+
|
|
46
62
|
/**
|
|
47
63
|
* 解析url
|
|
48
64
|
* 通过正则解析
|
|
@@ -53,8 +69,16 @@ declare function parseUrl(url: string): IUrl;
|
|
|
53
69
|
|
|
54
70
|
declare const uri: {
|
|
55
71
|
parseUrl: typeof parseUrl;
|
|
72
|
+
getPathArr: typeof getPathArr;
|
|
73
|
+
getPathStr: typeof getPathStr;
|
|
56
74
|
getQuery: (url: string) => string;
|
|
57
75
|
getQueryObj: (url: string) => {};
|
|
76
|
+
toQueryString: (params: _cclr_lang.TPlainObject) => string;
|
|
77
|
+
change: (url: string, urlObj: Omit<IUrl, "search" | "query"> & {
|
|
78
|
+
queryObj?: _cclr_lang.TPlainObject;
|
|
79
|
+
}) => string;
|
|
80
|
+
addQuery: (url: string, queryObj: _cclr_lang.TPlainObject) => string;
|
|
81
|
+
combine: (urlParams: IUrl) => string;
|
|
58
82
|
};
|
|
59
83
|
|
|
60
84
|
/**
|
|
@@ -65,4 +89,4 @@ declare const uri: {
|
|
|
65
89
|
*/
|
|
66
90
|
declare const uuid: () => string;
|
|
67
91
|
|
|
68
|
-
export { HASH_REGEX, QUERY_REGEX, URL_REGEX, deepClone, enumToArray, uri, uuid };
|
|
92
|
+
export { HASH_REGEX, type IUrl, PATHNAME_REGEX, QUERY_REGEX, URL_REGEX, deepClone, enumToArray, uri, uuid };
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cclr/utils",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "> TODO: description",
|
|
5
5
|
"author": "cclr <18843152354@163.com>",
|
|
6
6
|
"homepage": "",
|
|
7
7
|
"license": "MIT",
|
|
8
|
+
"type": "module",
|
|
8
9
|
"main": "lib/cjs/index.js",
|
|
9
10
|
"module": "lib/esm/index.js",
|
|
10
11
|
"types": "lib/type/index.d.ts",
|
|
@@ -30,8 +31,8 @@
|
|
|
30
31
|
"g:test": "vitest run",
|
|
31
32
|
"g:build": "ccf build"
|
|
32
33
|
},
|
|
33
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "060e2cfbf8d6f933f431d54b528332f08af20b60",
|
|
34
35
|
"dependencies": {
|
|
35
|
-
"@cclr/lang": "
|
|
36
|
+
"@cclr/lang": "0.1.4"
|
|
36
37
|
}
|
|
37
38
|
}
|