@argon-router/paths 0.6.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/LICENSE.MD ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Edward Gigolaev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # ☄️ argon-router/paths
2
+
3
+ Exteremly customizable paths without a headache
4
+
5
+ > [!WARNING]
6
+ > argon-router is extermely unstable & maybe buggy. DO NOT USE IN PRODUCTION!
7
+
8
+ ## Documentation
9
+
10
+ For additional information, guides and api reference visit [documentation site](https://movpushmov.dev/argon-router/)
11
+
12
+ ## Packages
13
+
14
+ - [@argon-router/core](https://www.npmjs.com/package/@argon-router/core)
15
+ - [@argon-router/react](https://www.npmjs.com/package/@argon-router/react)
16
+
17
+ ## Installation
18
+
19
+ ```
20
+ npm i @argon-router/paths
21
+ ```
22
+
23
+ ## Supported types
24
+
25
+ - String parameter
26
+
27
+ ```ts
28
+ '/:id'; // same as '/something'
29
+ ```
30
+
31
+ - String parameters array
32
+
33
+ ```ts
34
+ '/:id+'; // same as '/something', '/something/went' and etc
35
+ ```
36
+
37
+ - String parameters array with possible zero length
38
+
39
+ ```ts
40
+ '/:id*'; // same as '/', '/something', '/something/went' and etc
41
+ ```
42
+
43
+ - Nullable parameter
44
+
45
+ ```ts
46
+ '/:id?'; // same as '/' and '/test'
47
+ ```
48
+
49
+ - Generic parameter (numbers)
50
+
51
+ ```ts
52
+ '/:id<number>'; // same as '/123' and not '/test'
53
+ ```
54
+
55
+ - Nullable generic parameter (numbers)
56
+
57
+ ```ts
58
+ '/:id<number>?'; // same as '/123', '/' and not '/test'
59
+ ```
60
+
61
+ - Generic parameter (string literals)
62
+
63
+ ```ts
64
+ '/:id<hello|world>'; // same as '/hello', '/world' and not '/test'
65
+ ```
66
+
67
+ - Length for parameter
68
+
69
+ ```ts
70
+ '/:id{2,3}'; // same as '/test/test', '/test/test/test' and not '/test'
71
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function b(e){return r=>{const n=r.split("/").filter(Boolean);let t=null;function a(l,i){t||(t={}),t[l]=i}if(e.length===0)return n.length===0?{path:r,params:null}:null;for(let l=0;l<e.length;l++){let i=n[l];const c=e[l];switch(c.type){case"const":{if(i!==c.name)return null;continue}case"parameter":{const{arrayProps:f,genericProps:o,required:d}=c.payload;if(f){var u;const p=[];for(;i&&p.length<((s=f.max)!==null&&s!==void 0?s:1/0);){var s;switch(o==null?void 0:o.type){case"number":{if(isNaN(+i))return null;p.push(+i);break}case"union":{if(!o.items.includes(i))return null;p.push(i);break}default:{p.push(i);break}}i=n[l+p.length]}if(p.length<((u=f.min)!==null&&u!==void 0?u:0)||n[l+p.length]&&!e[l+1])return null;a(c.name,p);break}if(d&&!i)return null;if(!i){a(c.name,void 0);continue}switch(o==null?void 0:o.type){case"number":{if(isNaN(+i))return null;a(c.name,+i);break}case"union":{if(!o.items.includes(i))return null;a(c.name,i);break}default:{a(c.name,i);break}}}}}return{path:r,params:t}}}function g(e){if(!e)return null;const r=e[1];let n,t,a;for(const u of e.slice(2))if(u){if(u.includes("<")){n=u.replace("<","").replace(">","");continue}if(u.includes("{")&&(t=u.replace("{","").replace("}","").split(",").map(s=>parseInt(s))),["*","?","+"].includes(u)){a=u;continue}}return{name:r,genericProps:n,arrayProps:t,modificator:a}}function h(e){return r=>{const n=[];if(e.length===0)return"/";for(const t of e)switch(t.type){case"const":{n.push(t.name);break}case"parameter":{if(!r[t.name])continue;if(Array.isArray(r[t.name]))for(const a of r[t.name])n.push(a.toString());else n.push(r[t.name].toString());break}}return"/".concat(n.join("/"))}}function m(e,r){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var t=Object.getOwnPropertySymbols(e);r&&(t=t.filter(function(a){return Object.getOwnPropertyDescriptor(e,a).enumerable})),n.push.apply(n,t)}return n}function y(e){for(var r=1;r<arguments.length;r++){var n=arguments[r]!=null?arguments[r]:{};r%2?m(Object(n),!0).forEach(function(t){v(e,t,n[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):m(Object(n)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))})}return e}function v(e,r,n){return(r=P(r))in e?Object.defineProperty(e,r,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[r]=n,e}function P(e){var r=w(e,"string");return typeof r=="symbol"?r:r+""}function w(e,r){if(typeof e!="object"||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var t=n.call(e,r);if(typeof t!="object")return t;throw new TypeError("@@toPrimitive must return a primitive value.")}return(r==="string"?String:Number)(e)}function O(e){const r=[],n=/:(\w+)(<[\w|]+>)?({\d+\,\d+})?([+*?])?/,t=e.split("/").filter(Boolean);for(let a=0;a<t.length;a++){const u=t[a];if(!u)continue;const s=g(u.match(n));if(!s){r.push({type:"const",name:u,payload:void 0});continue}const{arrayProps:l,genericProps:i,modificator:c,name:f}=s;if(!f)throw new Error('Invalid path: "'.concat(e,'". Name for argument must be provided'));const o={type:"parameter",name:f,payload:{required:!0}};switch(i&&i==="number"&&(o.payload.genericProps={type:"number"}),i&&i.includes("|")&&(o.payload.genericProps={type:"union",items:i.split("|")}),c){case"*":{o.payload.arrayProps={};break}case"+":{o.payload.arrayProps={min:1};break}case"?":{o.payload.required=!1;break}}l&&(o.payload.arrayProps=y(y({},o.payload.arrayProps),{},{min:l[0],max:l[1]})),r.push(o)}return{parse:b(r),build:h(r)}}exports.compile=O;
@@ -0,0 +1,77 @@
1
+ export declare type Builder<T> = (params: T) => string;
2
+
3
+ export declare function compile<T extends string, Params = ParseUrlParams<T>>(path: T): {
4
+ /**
5
+ * @param input Input path
6
+ * @returns `{ path: string; params: Params }` | `null`
7
+ */
8
+ parse: Parser<Params>;
9
+ /**
10
+ * @param params Route parameters
11
+ * @returns string
12
+ */
13
+ build: Builder<Params>;
14
+ };
15
+
16
+ declare type GenericType<T extends string> = T extends `number` ? number : T extends `${infer A}|${infer B}` ? Union<T> : T;
17
+
18
+ declare type Parameter<Name extends string, Payload> = {
19
+ [k in Name]: Payload;
20
+ };
21
+
22
+ export declare type Parser<T> = (path: string) => {
23
+ path: string;
24
+ params: T;
25
+ } | null;
26
+
27
+ /**
28
+ * @description Extracts the parameters from a URL string.
29
+ * @example
30
+ *
31
+ * type Params = ParseUrlParams<'/:id/:name'>;
32
+ * // ^----- { id: string, name: string }
33
+ *
34
+ * type Params = ParseUrlParams<'/:id+'>;
35
+ * // ^----- { id: string[] }
36
+ *
37
+ * type Params = ParseUrlParams<'/:id*'>;
38
+ * // ^----- { id: string[] }
39
+ *
40
+ * type Params = ParseUrlParams<'/:id?'>;
41
+ * // ^----- { id?: string }
42
+ *
43
+ * type Params = ParseUrlParams<'/:id<number>+'>;
44
+ * // ^----- { id: number[] }
45
+ *
46
+ * type Params = ParseUrlParams<'/:id<number>*'>;
47
+ * // ^----- { id: number[] }
48
+ *
49
+ * type Params = ParseUrlParams<'/:id<number>'>;
50
+ * // ^----- { id: number }
51
+ *
52
+ * type Params = ParseUrlParams<'/:id<hello|world>?'>;
53
+ * // ^----- { id?: 'hello' | 'world' }
54
+ *
55
+ * type Params = ParseUrlParams<'/:id<hello|world>+'>;
56
+ * // ^----- { id: ('hello' | 'world')[] }
57
+ *
58
+ * type Params = ParseUrlParams<'/'>;
59
+ * // ^----- void
60
+ */
61
+ export declare type ParseUrlParams<T extends string> = Unwrap<UrlParams<T>>;
62
+
63
+ declare type Union<T extends string, Result = void> = T extends `${infer Start}|${infer Type}` ? Union<Type, Result extends void ? Start : Result | Start> : Result extends void ? T : Result | T;
64
+
65
+ declare type Unwrap<Result extends UrlParams<any, void>> = {
66
+ [k in keyof Result]: Result[k];
67
+ };
68
+
69
+ declare type UrlParameter<T extends string> = T extends `:${infer Name}<${infer Type}>${infer Modificator}` ? Parameter<WithoutModificator<Name>, WithModificator<GenericType<Type>, T>> : T extends `:${infer Name}<${infer Type}>` ? Parameter<Name, GenericType<Type>> : T extends `:${infer Name}` ? Parameter<WithoutModificator<Name>, WithModificator<string, T>> : never;
70
+
71
+ declare type UrlParams<T extends string, Result = void> = T extends `/:${infer Parameter}/${infer Route}` ? Result extends void ? UrlParams<`/${Route}`, UrlParameter<`:${Parameter}`>> : UrlParams<`/${Route}`, Result & UrlParameter<`:${Parameter}`>> : T extends `/:${infer Parameter}` ? Result extends void ? UrlParameter<`:${Parameter}`> : Result & UrlParameter<`:${Parameter}`> : T extends `/${infer Static}/${infer Next}` ? UrlParams<`/${Next}`, Result> : Result;
72
+
73
+ declare type WithModificator<Type, T extends string> = T extends `${infer K}{${infer Start},${infer End}}+` ? Type[] : T extends `${infer K}{${infer Start},${infer End}}*` ? Type[] : T extends `${infer K}{${infer Start},${infer End}}?` ? Type[] | undefined : T extends `${infer K}{${infer Start},${infer End}}` ? Type[] : T extends `${infer K}+` ? Type[] : T extends `${infer K}*` ? Type[] : T extends `${infer K}?` ? Type | undefined : Type;
74
+
75
+ declare type WithoutModificator<T extends string> = T extends `${infer K}{${infer Start},${infer End}}${infer Modificator}` ? K : T extends `${infer K}{${infer Start},${infer End}}` ? K : T extends `${infer K}?` ? K : T extends `${infer K}*` ? K : T extends `${infer K}+` ? K : T;
76
+
77
+ export { }
package/dist/index.js ADDED
@@ -0,0 +1,248 @@
1
+ function b(e) {
2
+ return (r) => {
3
+ const n = r.split("/").filter(Boolean);
4
+ let t = null;
5
+ function a(l, i) {
6
+ t || (t = {}), t[l] = i;
7
+ }
8
+ if (e.length === 0)
9
+ return n.length === 0 ? {
10
+ path: r,
11
+ params: null
12
+ } : null;
13
+ for (let l = 0; l < e.length; l++) {
14
+ let i = n[l];
15
+ const c = e[l];
16
+ switch (c.type) {
17
+ case "const": {
18
+ if (i !== c.name)
19
+ return null;
20
+ continue;
21
+ }
22
+ case "parameter": {
23
+ const {
24
+ arrayProps: f,
25
+ genericProps: o,
26
+ required: d
27
+ } = c.payload;
28
+ if (f) {
29
+ var u;
30
+ const p = [];
31
+ for (; i && p.length < ((s = f.max) !== null && s !== void 0 ? s : 1 / 0); ) {
32
+ var s;
33
+ switch (o == null ? void 0 : o.type) {
34
+ case "number": {
35
+ if (isNaN(+i))
36
+ return null;
37
+ p.push(+i);
38
+ break;
39
+ }
40
+ case "union": {
41
+ if (!o.items.includes(i))
42
+ return null;
43
+ p.push(i);
44
+ break;
45
+ }
46
+ default: {
47
+ p.push(i);
48
+ break;
49
+ }
50
+ }
51
+ i = n[l + p.length];
52
+ }
53
+ if (p.length < ((u = f.min) !== null && u !== void 0 ? u : 0) || n[l + p.length] && !e[l + 1])
54
+ return null;
55
+ a(c.name, p);
56
+ break;
57
+ }
58
+ if (d && !i)
59
+ return null;
60
+ if (!i) {
61
+ a(c.name, void 0);
62
+ continue;
63
+ }
64
+ switch (o == null ? void 0 : o.type) {
65
+ case "number": {
66
+ if (isNaN(+i))
67
+ return null;
68
+ a(c.name, +i);
69
+ break;
70
+ }
71
+ case "union": {
72
+ if (!o.items.includes(i))
73
+ return null;
74
+ a(c.name, i);
75
+ break;
76
+ }
77
+ default: {
78
+ a(c.name, i);
79
+ break;
80
+ }
81
+ }
82
+ }
83
+ }
84
+ }
85
+ return {
86
+ path: r,
87
+ params: t
88
+ };
89
+ };
90
+ }
91
+ function g(e) {
92
+ if (!e)
93
+ return null;
94
+ const r = e[1];
95
+ let n, t, a;
96
+ for (const u of e.slice(2))
97
+ if (u) {
98
+ if (u.includes("<")) {
99
+ n = u.replace("<", "").replace(">", "");
100
+ continue;
101
+ }
102
+ if (u.includes("{") && (t = u.replace("{", "").replace("}", "").split(",").map((s) => parseInt(s))), ["*", "?", "+"].includes(u)) {
103
+ a = u;
104
+ continue;
105
+ }
106
+ }
107
+ return {
108
+ name: r,
109
+ genericProps: n,
110
+ arrayProps: t,
111
+ modificator: a
112
+ };
113
+ }
114
+ function h(e) {
115
+ return (r) => {
116
+ const n = [];
117
+ if (e.length === 0)
118
+ return "/";
119
+ for (const t of e)
120
+ switch (t.type) {
121
+ case "const": {
122
+ n.push(t.name);
123
+ break;
124
+ }
125
+ case "parameter": {
126
+ if (!r[t.name])
127
+ continue;
128
+ if (Array.isArray(r[t.name]))
129
+ for (const a of r[t.name])
130
+ n.push(a.toString());
131
+ else
132
+ n.push(r[t.name].toString());
133
+ break;
134
+ }
135
+ }
136
+ return "/".concat(n.join("/"));
137
+ };
138
+ }
139
+ function m(e, r) {
140
+ var n = Object.keys(e);
141
+ if (Object.getOwnPropertySymbols) {
142
+ var t = Object.getOwnPropertySymbols(e);
143
+ r && (t = t.filter(function(a) {
144
+ return Object.getOwnPropertyDescriptor(e, a).enumerable;
145
+ })), n.push.apply(n, t);
146
+ }
147
+ return n;
148
+ }
149
+ function y(e) {
150
+ for (var r = 1; r < arguments.length; r++) {
151
+ var n = arguments[r] != null ? arguments[r] : {};
152
+ r % 2 ? m(Object(n), !0).forEach(function(t) {
153
+ v(e, t, n[t]);
154
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(n)) : m(Object(n)).forEach(function(t) {
155
+ Object.defineProperty(e, t, Object.getOwnPropertyDescriptor(n, t));
156
+ });
157
+ }
158
+ return e;
159
+ }
160
+ function v(e, r, n) {
161
+ return (r = P(r)) in e ? Object.defineProperty(e, r, { value: n, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = n, e;
162
+ }
163
+ function P(e) {
164
+ var r = w(e, "string");
165
+ return typeof r == "symbol" ? r : r + "";
166
+ }
167
+ function w(e, r) {
168
+ if (typeof e != "object" || !e) return e;
169
+ var n = e[Symbol.toPrimitive];
170
+ if (n !== void 0) {
171
+ var t = n.call(e, r);
172
+ if (typeof t != "object") return t;
173
+ throw new TypeError("@@toPrimitive must return a primitive value.");
174
+ }
175
+ return (r === "string" ? String : Number)(e);
176
+ }
177
+ function k(e) {
178
+ const r = [], n = /:(\w+)(<[\w|]+>)?({\d+\,\d+})?([+*?])?/, t = e.split("/").filter(Boolean);
179
+ for (let a = 0; a < t.length; a++) {
180
+ const u = t[a];
181
+ if (!u)
182
+ continue;
183
+ const s = g(u.match(n));
184
+ if (!s) {
185
+ r.push({
186
+ type: "const",
187
+ name: u,
188
+ payload: void 0
189
+ });
190
+ continue;
191
+ }
192
+ const {
193
+ arrayProps: l,
194
+ genericProps: i,
195
+ modificator: c,
196
+ name: f
197
+ } = s;
198
+ if (!f)
199
+ throw new Error('Invalid path: "'.concat(e, '". Name for argument must be provided'));
200
+ const o = {
201
+ type: "parameter",
202
+ name: f,
203
+ payload: {
204
+ required: !0
205
+ }
206
+ };
207
+ switch (i && i === "number" && (o.payload.genericProps = {
208
+ type: "number"
209
+ }), i && i.includes("|") && (o.payload.genericProps = {
210
+ type: "union",
211
+ items: i.split("|")
212
+ }), c) {
213
+ case "*": {
214
+ o.payload.arrayProps = {};
215
+ break;
216
+ }
217
+ case "+": {
218
+ o.payload.arrayProps = {
219
+ min: 1
220
+ };
221
+ break;
222
+ }
223
+ case "?": {
224
+ o.payload.required = !1;
225
+ break;
226
+ }
227
+ }
228
+ l && (o.payload.arrayProps = y(y({}, o.payload.arrayProps), {}, {
229
+ min: l[0],
230
+ max: l[1]
231
+ })), r.push(o);
232
+ }
233
+ return {
234
+ /**
235
+ * @param input Input path
236
+ * @returns `{ path: string; params: Params }` | `null`
237
+ */
238
+ parse: b(r),
239
+ /**
240
+ * @param params Route parameters
241
+ * @returns string
242
+ */
243
+ build: h(r)
244
+ };
245
+ }
246
+ export {
247
+ k as compile
248
+ };
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@argon-router/paths",
3
+ "publishConfig": {
4
+ "access": "public",
5
+ "registry": "https://registry.npmjs.org/"
6
+ },
7
+ "private": false,
8
+ "version": "0.6.0",
9
+ "description": "Paths with power of effector",
10
+ "keywords": [
11
+ "paths",
12
+ "argon",
13
+ "argon-router"
14
+ ],
15
+ "author": "movpushmov",
16
+ "homepage": "https://github.com/movpushmov/argon-router#readme",
17
+ "license": "MIT",
18
+ "directories": {
19
+ "lib": "lib"
20
+ },
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "type": "module",
25
+ "module": "./dist/index.js",
26
+ "types": "./dist/index.d.ts",
27
+ "exports": {
28
+ ".": {
29
+ "import": "./dist/index.js",
30
+ "require": "./dist/index.cjs"
31
+ }
32
+ },
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "git+https://github.com/movpushmov/argon-router.git"
36
+ },
37
+ "scripts": {
38
+ "build": "vite build",
39
+ "test": "vitest run"
40
+ },
41
+ "bugs": {
42
+ "url": "https://github.com/movpushmov/argon-router/issues"
43
+ },
44
+ "gitHead": "02a833234ffa6ca8a6d4704634f73d2451e573d0"
45
+ }