@cclr/utils 0.0.9 → 0.1.0

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";const t=new RegExp(["^(https?:)//","(([^:/?#]*)(?::([0-9]+))?)","(/{0,1}[^?#]*)","(\\?[^#]*|)","(#.*|)$"].join(""));exports.urlParse=function(o){const r=o.match(t);return r?{href:o,protocol:r[1],host:r[2],hostname:r[3],port:r[4],pathname:r[5],search:r[6],hash:r[7]}:{}},exports.urlREG=t;
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("")};
package/lib/esm/index.js CHANGED
@@ -1 +1 @@
1
- const t=new RegExp(["^(https?:)//","(([^:/?#]*)(?::([0-9]+))?)","(/{0,1}[^?#]*)","(\\?[^#]*|)","(#.*|)$"].join(""));function o(o){const h=o.match(t);return h?{href:o,protocol:h[1],host:h[2],hostname:h[3],port:h[4],pathname:h[5],search:h[6],hash:h[7]}:{}}export{o as urlParse,t as urlREG};
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,27 +1,56 @@
1
+ import { TPlainObject } from '@cclr/lang';
2
+
3
+ declare const enumToArray: (e: TPlainObject) => string[][];
4
+
5
+ declare const URL_REGEX: RegExp;
6
+ declare const QUERY_REGEX: RegExp;
7
+ declare const HASH_REGEX: RegExp;
8
+
9
+ interface IUrl {
10
+ /** 完整的 URL 字符串,例如 https://example.com:8080/path?query#fragment */
11
+ href?: string | null | undefined;
12
+ /** URL 的协议部分,例如 http: 或 https: */
13
+ protocol?: string | null | undefined;
14
+ /** 指示 URL 是否包含双斜杠(//),例如 http:// */
15
+ slashes?: boolean | null | undefined;
16
+ /** 通常以 用户名:密码 的形式出现在 URL 中;例如: http://username:password@example.com */
17
+ auth?: string | null | undefined;
18
+ /** 主机名和端口的组合,例如 example.com:8080 */
19
+ host?: string | null | undefined;
20
+ /** 主机名,不包括端口,例如 example.com */
21
+ hostname?: string | null | undefined;
22
+ /** 端口号,例如 8080 */
23
+ port?: string | number | null | undefined;
24
+ /** URL 的路径部分,例如 /path/to/resource */
25
+ pathname?: string | null | undefined;
26
+ /** URL 的查询字符串部分,通常以 ? 开头,例如 ?query=string */
27
+ search?: string | null | undefined;
28
+ /** 查询参数,通常是键值对的组合,例如 key=value */
29
+ query?: string | null | undefined;
30
+ /** URL 的片段标识符,通常以 # 开头,例如 #section1 */
31
+ hash?: string | null | undefined;
32
+ }
33
+
1
34
  /**
2
35
  * 解析url
36
+ * 通过正则解析
3
37
  * @param url
38
+ * @returns
4
39
  */
5
- declare function urlParse(url: string): {
6
- href: string;
7
- protocol: string;
8
- host: string;
9
- hostname: string;
10
- port: string;
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;
40
+ declare function parseUrl(url: string): IUrl;
41
+
42
+ declare const uri: {
43
+ parseUrl: typeof parseUrl;
44
+ getQuery: (url: string) => string;
45
+ getQueryObj: (url: string) => {};
23
46
  };
24
47
 
25
- declare const urlREG: RegExp;
48
+ /**
49
+ * A function that returns a universally unique identifier (uuid).
50
+ * @example
51
+ * 1b83fd69-abe7-468c-bea1-306a8aa1c81d
52
+ * @returns `string` : 32 character uuid (see example)
53
+ */
54
+ declare const uuid: () => string;
26
55
 
27
- export { urlParse, urlREG };
56
+ export { HASH_REGEX, QUERY_REGEX, URL_REGEX, enumToArray, uri, uuid };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cclr/utils",
3
- "version": "0.0.9",
3
+ "version": "0.1.0",
4
4
  "description": "> TODO: description",
5
5
  "author": "cclr <18843152354@163.com>",
6
6
  "homepage": "",
@@ -8,6 +8,7 @@
8
8
  "main": "lib/cjs/index.js",
9
9
  "module": "lib/esm/index.js",
10
10
  "types": "lib/type/index.d.ts",
11
+ "type": "module",
11
12
  "directories": {
12
13
  "lib": "lib",
13
14
  "test": "__tests__"
@@ -24,7 +25,11 @@
24
25
  "url": "git@gitee.com:cclr/toolkit.git"
25
26
  },
26
27
  "scripts": {
27
- "test": "node ./__tests__/utils.test.js"
28
+ "test": "vitest",
29
+ "relese": "ccf build && npm publish"
28
30
  },
29
- "gitHead": "ad2e0a50411f99a529d84eb5901748d9c3cf5f3a"
31
+ "gitHead": "e2f8a819dfe7c5953186303d603fb8f6395d4042",
32
+ "dependencies": {
33
+ "@cclr/lang": "0.1.0"
34
+ }
30
35
  }