@stryke/url 0.2.13 → 0.3.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/dist/helpers.cjs +11 -7
- package/dist/helpers.d.ts +7 -0
- package/dist/helpers.mjs +1 -1
- package/dist/storm-url.cjs +16 -15
- package/dist/storm-url.mjs +1 -1
- package/package.json +5 -5
package/dist/helpers.cjs
CHANGED
|
@@ -4,15 +4,19 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.formatLocalePath = formatLocalePath;
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
exports.isValidURL = isValidURL;
|
|
8
|
+
function formatLocalePath(e) {
|
|
9
|
+
let t = e;
|
|
9
10
|
if (t.includes("_") && (t = t.replace(/_/g, "-")), t.includes("-")) {
|
|
10
|
-
const
|
|
11
|
-
if (
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
const n = t.split("-");
|
|
12
|
+
if (n.length > 1) {
|
|
13
|
+
const i = n[0],
|
|
14
|
+
o = n[1];
|
|
15
|
+
i && o && (t = `${i}-${o}`);
|
|
15
16
|
}
|
|
16
17
|
}
|
|
17
18
|
return t.toLowerCase();
|
|
19
|
+
}
|
|
20
|
+
function isValidURL(e) {
|
|
21
|
+
return /(?:https?:\/\/.)?(?:www\.)?[-\w@:%.+~#=]{2,256}\.[a-z]{2,6}\b[-\w@:%+.~#?&/=]*/.test(e);
|
|
18
22
|
}
|
package/dist/helpers.d.ts
CHANGED
|
@@ -1 +1,8 @@
|
|
|
1
1
|
export declare function formatLocalePath(locale: string): string;
|
|
2
|
+
/**
|
|
3
|
+
* Check if a string has a valid URL format.
|
|
4
|
+
*
|
|
5
|
+
* @param str - The string to check.
|
|
6
|
+
* @returns `true` if the string is a valid URL, otherwise `false`.
|
|
7
|
+
*/
|
|
8
|
+
export declare function isValidURL(str: string): boolean;
|
package/dist/helpers.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export function formatLocalePath(
|
|
1
|
+
export function formatLocalePath(e){let t=e;if(t.includes("_")&&(t=t.replace(/_/g,"-")),t.includes("-")){const n=t.split("-");if(n.length>1){const i=n[0],o=n[1];i&&o&&(t=`${i}-${o}`)}}return t.toLowerCase()}export function isValidURL(e){return/(?:https?:\/\/.)?(?:www\.)?[-\w@:%.+~#=]{2,256}\.[a-z]{2,6}\b[-\w@:%+.~#?&/=]*/.test(e)}
|
package/dist/storm-url.cjs
CHANGED
|
@@ -15,8 +15,8 @@ class StormURL {
|
|
|
15
15
|
#a;
|
|
16
16
|
#t;
|
|
17
17
|
#s;
|
|
18
|
-
#i;
|
|
19
18
|
#r;
|
|
19
|
+
#i;
|
|
20
20
|
#o;
|
|
21
21
|
#e = [];
|
|
22
22
|
#h;
|
|
@@ -24,12 +24,13 @@ class StormURL {
|
|
|
24
24
|
constructor(t, s = {
|
|
25
25
|
decode: !0
|
|
26
26
|
}) {
|
|
27
|
+
if (t && !(0, _helpers.isValidURL)(t)) throw new TypeError(`Invalid URL: ${t}`);
|
|
27
28
|
this.#a = s;
|
|
28
|
-
const
|
|
29
|
-
a = (0, _ufo.parseAuth)(
|
|
29
|
+
const r = (0, _ufo.parseURL)(this.#a.decode ? (0, _ufo.decode)(t) : t),
|
|
30
|
+
a = (0, _ufo.parseAuth)(r.auth);
|
|
30
31
|
this.#t = a.username, this.#s = a.password;
|
|
31
|
-
const o = (0, _ufo.parseHost)(
|
|
32
|
-
this.#
|
|
32
|
+
const o = (0, _ufo.parseHost)(r.host);
|
|
33
|
+
this.#r = o.hostname, this.#i = o.port, this.#o = r.protocol, this.#h = r.hash || "", this.#n = (0, _ufo.parseQuery)(r.search), this.#e = r.pathname ? r.pathname.split("/").filter(Boolean) : [], s.locale && this.#e.unshift((0, _helpers.formatLocalePath)(((0, _isSetString.isSetString)(s.locale) ? s.locale : process.env.DEFAULT_LOCALE) || "en-us"));
|
|
33
34
|
}
|
|
34
35
|
set params(t) {
|
|
35
36
|
this.#n = t;
|
|
@@ -39,7 +40,7 @@ class StormURL {
|
|
|
39
40
|
}
|
|
40
41
|
get searchParams() {
|
|
41
42
|
const t = new URLSearchParams();
|
|
42
|
-
for (const [s,
|
|
43
|
+
for (const [s, r] of Object.entries(this.params)) t.append(s, r);
|
|
43
44
|
return t;
|
|
44
45
|
}
|
|
45
46
|
set searchParams(t) {
|
|
@@ -59,10 +60,10 @@ class StormURL {
|
|
|
59
60
|
return this.#h;
|
|
60
61
|
}
|
|
61
62
|
set port(t) {
|
|
62
|
-
this.#
|
|
63
|
+
this.#i = !(0, _isUndefined.isUndefined)(t) && (0, _isInteger.isInteger)(t) ? `${t}` : "";
|
|
63
64
|
}
|
|
64
65
|
get port() {
|
|
65
|
-
return this.#
|
|
66
|
+
return this.#i || "";
|
|
66
67
|
}
|
|
67
68
|
set username(t) {
|
|
68
69
|
this.auth = `${t}:${this.#s}`;
|
|
@@ -90,17 +91,17 @@ class StormURL {
|
|
|
90
91
|
return this.#o || this.#a.defaultProtocol || "https:";
|
|
91
92
|
}
|
|
92
93
|
set hostname(t) {
|
|
93
|
-
this.#
|
|
94
|
+
this.#r = t;
|
|
94
95
|
}
|
|
95
96
|
get hostname() {
|
|
96
|
-
return this.#
|
|
97
|
+
return this.#r;
|
|
97
98
|
}
|
|
98
99
|
set host(t) {
|
|
99
100
|
const s = (0, _ufo.parseHost)(t);
|
|
100
|
-
this.#
|
|
101
|
+
this.#r = s.hostname, this.#i = s.port;
|
|
101
102
|
}
|
|
102
103
|
get host() {
|
|
103
|
-
return this.#
|
|
104
|
+
return this.#r && this.#i ? `${this.#r}:${this.#i}` : this.#r ? this.#r : this.#i ? this.#i : "";
|
|
104
105
|
}
|
|
105
106
|
set paths(t) {
|
|
106
107
|
this.#e = t.filter(Boolean);
|
|
@@ -175,9 +176,9 @@ class StormURL {
|
|
|
175
176
|
}
|
|
176
177
|
exports.StormURL = StormURL;
|
|
177
178
|
_stormJson.StormJSON.instance.registerCustom({
|
|
178
|
-
isApplicable:
|
|
179
|
-
serialize:
|
|
180
|
-
deserialize:
|
|
179
|
+
isApplicable: i => i.__typename === "StormURL",
|
|
180
|
+
serialize: i => i.toEncoded(),
|
|
181
|
+
deserialize: i => new StormURL(i, {
|
|
181
182
|
decode: !0
|
|
182
183
|
})
|
|
183
184
|
}, "StormURL");
|
package/dist/storm-url.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{StormJSON as e}from"@stryke/json/storm-json";import{joinPaths as m}from"@stryke/path/join-paths";import{isInteger as g}from"@stryke/type-checks/is-integer";import{isSetString as l}from"@stryke/type-checks/is-set-string";import{isUndefined as b}from"@stryke/type-checks/is-undefined";import{cleanDoubleSlashes as d,decode as f,isEqual as S,isNonEmptyURL as R,isRelative as L,isSamePath as U,isScriptProtocol as
|
|
1
|
+
import{StormJSON as e}from"@stryke/json/storm-json";import{joinPaths as m}from"@stryke/path/join-paths";import{isInteger as g}from"@stryke/type-checks/is-integer";import{isSetString as l}from"@stryke/type-checks/is-set-string";import{isUndefined as b}from"@stryke/type-checks/is-undefined";import{cleanDoubleSlashes as d,decode as f,isEqual as S,isNonEmptyURL as R,isRelative as L,isSamePath as U,isScriptProtocol as y,normalizeURL as P,parseAuth as n,parseHost as p,parsePath as $,parseQuery as c,parseURL as u,stringifyParsedURL as h,stringifyQuery as E}from"ufo";import{formatLocalePath as w,isValidURL as O}from"./helpers";export class StormURL{#a;#t;#s;#r;#i;#o;#e=[];#h;#n;constructor(t,s={decode:!0}){if(t&&!O(t))throw new TypeError(`Invalid URL: ${t}`);this.#a=s;const r=u(this.#a.decode?f(t):t),a=n(r.auth);this.#t=a.username,this.#s=a.password;const o=p(r.host);this.#r=o.hostname,this.#i=o.port,this.#o=r.protocol,this.#h=r.hash||"",this.#n=c(r.search),this.#e=r.pathname?r.pathname.split("/").filter(Boolean):[],s.locale&&this.#e.unshift(w((l(s.locale)?s.locale:process.env.DEFAULT_LOCALE)||"en-us"))}set params(t){this.#n=t}get params(){return this.#n}get searchParams(){const t=new URLSearchParams;for(const[s,r]of Object.entries(this.params))t.append(s,r);return t}set searchParams(t){this.params=Object.fromEntries(t.entries())}set search(t){this.params=c(t)}get search(){const t=E(this.params);return t?t.startsWith("?")?t:`?${t}`:""}set hash(t){this.#h=t.startsWith("#")?t:`#${t}`}get hash(){return this.#h}set port(t){this.#i=!b(t)&&g(t)?`${t}`:""}get port(){return this.#i||""}set username(t){this.auth=`${t}:${this.#s}`}get username(){return this.#t||""}set password(t){this.auth=`${this.#t}:${t}`}get password(){return this.#s||""}set auth(t){const s=n(t);this.#t=s.username,this.#s=s.password}get auth(){return this.#t&&this.#s?`${this.#t}:${this.#s}`:this.#t?this.#t:this.#s?this.#s:""}set protocol(t){this.#o=t.endsWith(":")?t:t.endsWith("://")?t.slice(0,-2):`${t}:`}get protocol(){return this.#o||this.#a.defaultProtocol||"https:"}set hostname(t){this.#r=t}get hostname(){return this.#r}set host(t){const s=p(t);this.#r=s.hostname,this.#i=s.port}get host(){return this.#r&&this.#i?`${this.#r}:${this.#i}`:this.#r?this.#r:this.#i?this.#i:""}set paths(t){this.#e=t.filter(Boolean)}get paths(){return this.#e}set pathname(t){this.paths=d(t).split("/")}get pathname(){return`/${this.paths?m(...this.paths.map(t=>e.stringify(t))):""}`}set path(t){const s=$(t);this.pathname=s.pathname,this.search=s.search,this.#h=s.hash}get path(){return h({pathname:this.pathname,search:this.search,hash:this.hash})}get href(){return this.toString()}set href(t){const s=u(t);this.protocol=s.protocol||this.protocol,this.auth=s.auth||this.auth,this.host=s.host||this.host,this.pathname=s.pathname||this.pathname,this.search=s.search||this.search,this.hash=s.hash||this.hash}get origin(){return`${this.protocol}//${this.host}`}get isScriptProtocol(){return y(this.protocol)}get isRelative(){return L(this.toString())}get isNonEmptyURL(){return R(this.toString())}get __typename(){return"StormURL"}isSamePath(t){return U(this.path,t)}isEqual(t){return S(this.toString(),typeof t=="string"?t:t instanceof StormURL?t.toString():h(t))}toParsed(){return{protocol:this.protocol,auth:this.auth,host:this.host,pathname:this.pathname,search:this.search,hash:this.hash}}toString(){return h(this.toParsed())}toEncoded(){return P(this.toString())}toJSON(){return e.stringify(this.toParsed())}}e.instance.registerCustom({isApplicable:i=>i.__typename==="StormURL",serialize:i=>i.toEncoded(),deserialize:i=>new StormURL(i,{decode:!0})},"StormURL");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stryke/url",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A package containing the `StormURL` class, which is used to parse and manipulate URLs.",
|
|
6
6
|
"repository": {
|
|
@@ -11,9 +11,9 @@
|
|
|
11
11
|
"private": false,
|
|
12
12
|
"dependencies": {
|
|
13
13
|
"ufo": "1.5.4",
|
|
14
|
-
"@stryke/json": "^0.9.
|
|
15
|
-
"@stryke/path": "^0.21.
|
|
16
|
-
"@stryke/type-checks": "^0.
|
|
14
|
+
"@stryke/json": "^0.9.12",
|
|
15
|
+
"@stryke/path": "^0.21.3",
|
|
16
|
+
"@stryke/type-checks": "^0.4.0"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": { "@types/node": "^24.10.1" },
|
|
19
19
|
"publishConfig": { "access": "public" },
|
|
@@ -117,5 +117,5 @@
|
|
|
117
117
|
"main": "./dist/index.cjs",
|
|
118
118
|
"module": "./dist/index.mjs",
|
|
119
119
|
"types": "./dist/index.d.ts",
|
|
120
|
-
"gitHead": "
|
|
120
|
+
"gitHead": "cd16bb3da577f7adb6f5c4ca6f12b90b17fe7061"
|
|
121
121
|
}
|