@cclr/utils 0.1.1 → 0.1.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/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("")),r=/#(?<hash>.*)/,n=e=>{if(!e)return"";const t=e.split("?")[1];return t?t.replace(r,""):""};const l={parseUrl:function(r){const n=r.match(t),{protocol:l,auth:s,host:u,pathname:o,query:a,hash:c}=e.get(n,"groups",{});let h=null,p=null;if(u){const[e,t]=u.split(":");h=e,p=t||null}return{href:r,protocol:l||null,slashes:r.includes("//")||null,auth:s?s.slice(0,-1):null,host:u||null,hostname:h||null,port:p||null,pathname:o||null,search:a?`?${a}`:null,query:a||null,hash:c||null}},getQuery:n,getQueryObj:e=>{if(!e)return{};return(n(e)||e).split("&").reduce(((e,t)=>{const r=t.indexOf("=");let n=[];n=-1!==r?[t.substring(0,r),t.substring(r+1)]:[t];const l=n.map((e=>decodeURIComponent(e.replace("+"," "))));return e[l[0]]=l[1],e}),{})}};exports.HASH_REGEX=r,exports.QUERY_REGEX=/\?(?<query>[^#]*)/,exports.URL_REGEX=t,exports.enumToArray=t=>e.isPlainObject(t)?[Object.keys(t)]:[],exports.uri=l,exports.uuid=()=>{const e=["a","b","c","d","e","f","0","1","2","3","4","5","6","7","8","9"],t=[];for(let r=0;r<36;r++)t[r]=8===r||13===r||18===r||23===r?"-":e[Math.ceil(Math.random()*e.length-1)];return t.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}${a||(l?`?${l}`:"")}${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 n=e=>t(e)?[Object.keys(e)]:[],l=new RegExp(["^(?<protocol>[a-zA-Z][a-zA-Zd+-.]*:)?","(?:\\/\\/(?<auth>[^@/?#]*@)?(?<host>[^/?#]*))?","(?<pathname>[^?#]*)","(?:\\?(?<query>[^#]*))?","(?:#(?<hash>.*))?"].join("")),r=/\?(?<query>[^#]*)/,u=/#(?<hash>.*)/,o=t=>{if(!t)return"";const e=t.split("?")[1];return e?e.replace(u,""):""};const s={parseUrl:function(t){const n=t.match(l),{protocol:r,auth:u,host:o,pathname:s,query:a,hash:c}=e(n,"groups",{});let h=null,p=null;if(o){const[t,e]=o.split(":");h=t,p=e||null}return{href:t,protocol:r||null,slashes:t.includes("//")||null,auth:u?u.slice(0,-1):null,host:o||null,hostname:h||null,port:p||null,pathname:s||null,search:a?`?${a}`:null,query:a||null,hash:c||null}},getQuery:o,getQueryObj:t=>{if(!t)return{};return(o(t)||t).split("&").reduce(((t,e)=>{const n=e.indexOf("=");let l=[];l=-1!==n?[e.substring(0,n),e.substring(n+1)]:[e];const r=l.map((t=>decodeURIComponent(t.replace("+"," "))));return t[r[0]]=r[1],t}),{})}},a=()=>{const t=["a","b","c","d","e","f","0","1","2","3","4","5","6","7","8","9"],e=[];for(let n=0;n<36;n++)e[n]=8===n||13===n||18===n||23===n?"-":t[Math.ceil(Math.random()*t.length-1)];return e.join("")};export{u as HASH_REGEX,r as QUERY_REGEX,l as URL_REGEX,n as enumToArray,s as uri,a as uuid};
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}${l||(a?`?${a}`:"")}${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};
@@ -1,10 +1,24 @@
1
- import { TPlainObject } from '@cclr/lang';
1
+ import * as _cclr_lang from '@cclr/lang';
2
+ import { TAny, TPlainObject } from '@cclr/lang';
2
3
 
4
+ /**
5
+ * 深拷贝 JSON 方式
6
+ * @param Obj
7
+ * @returns
8
+ */
9
+ declare const deepClone: <T extends object | TAny[]>(Obj: T) => T;
10
+
11
+ /**
12
+ * 枚举key变成数组
13
+ * @param e
14
+ * @returns
15
+ */
3
16
  declare const enumToArray: (e: TPlainObject) => string[][];
4
17
 
5
18
  declare const URL_REGEX: RegExp;
6
19
  declare const QUERY_REGEX: RegExp;
7
20
  declare const HASH_REGEX: RegExp;
21
+ declare const PATHNAME_REGEX: RegExp;
8
22
 
9
23
  interface IUrl {
10
24
  /** 完整的 URL 字符串,例如 https://example.com:8080/path?query#fragment */
@@ -31,6 +45,20 @@ interface IUrl {
31
45
  hash?: string | null | undefined;
32
46
  }
33
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
+
34
62
  /**
35
63
  * 解析url
36
64
  * 通过正则解析
@@ -41,8 +69,16 @@ declare function parseUrl(url: string): IUrl;
41
69
 
42
70
  declare const uri: {
43
71
  parseUrl: typeof parseUrl;
72
+ getPathArr: typeof getPathArr;
73
+ getPathStr: typeof getPathStr;
44
74
  getQuery: (url: string) => string;
45
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;
46
82
  };
47
83
 
48
84
  /**
@@ -53,4 +89,4 @@ declare const uri: {
53
89
  */
54
90
  declare const uuid: () => string;
55
91
 
56
- export { HASH_REGEX, QUERY_REGEX, URL_REGEX, enumToArray, uri, uuid };
92
+ export { HASH_REGEX, type IUrl, PATHNAME_REGEX, QUERY_REGEX, URL_REGEX, deepClone, enumToArray, uri, uuid };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cclr/utils",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "> TODO: description",
5
5
  "author": "cclr <18843152354@163.com>",
6
6
  "homepage": "",
@@ -25,10 +25,13 @@
25
25
  },
26
26
  "scripts": {
27
27
  "test": "vitest",
28
- "relese": "ccf build && npm publish"
28
+ "relese": "npm run test && npm publish",
29
+ "build": "ccf build",
30
+ "g:test": "vitest run",
31
+ "g:build": "ccf build"
29
32
  },
30
- "gitHead": "03fc59004998fb54a36ee04d641d6547bc9abe28",
33
+ "gitHead": "756346e96b198cbc70acc052058123826f59f5ed",
31
34
  "dependencies": {
32
- "@cclr/lang": "0.1.1"
35
+ "@cclr/lang": "^0.1.3"
33
36
  }
34
37
  }