@argon-router/paths 0.11.0 → 0.12.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/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";function e(e){return r=>{const n=r.split("/").map(e=>e.trim()).filter(e=>""!==e);let t=null;function a(e,r){t||(t={}),t[e]=r}if(0===e.length)return 0===n.length?{path:r,params:null}:null;for(let i=0;i<e.length;i++){const r=e[i];switch(r.type){case"const":if(r.name!==n.shift())return null;continue;case"parameter":{const{arrayProps:t,genericProps:o,required:s}=r.payload;if(t){const s=[];let l;for(;l=n.shift(),l;){switch(null==o?void 0:o.type){case"number":if(isNaN(+l))return null;s.push(+l);break;case"union":if(!o.items.includes(l))return null;s.push(l);break;default:s.push(l)}if(s.length>=(t.max??1/0))break}if(s.length<(t.min??0))return null;if(n.length>0&&!e[i+1])return null;a(r.name,s);break}const l=n.shift();if(s&&!l)return null;if(!l){a(r.name,void 0);continue}switch(null==o?void 0:o.type){case"number":if(isNaN(+l))return null;a(r.name,+l);break;case"union":if(!o.items.includes(l))return null;a(r.name,l);break;default:a(r.name,l)}}}}return n.length>0?null:{path:r,params:t}}}function r(e){if(!e)return null;const r=e[1];let n,t,a;for(const i of e.slice(2))i&&(i.includes("<")?n=i.replaceAll(/\s/g,"").replace("<","").replace(">",""):(i.includes("{")&&(t=i.replace("{","").replace("}","").split(",").map(e=>parseInt(e))),["*","?","+"].includes(i)&&(a=i)));return{name:r,genericProps:n,arrayProps:t,modificator:a}}function n(e){return r=>{const n=[];if(0===e.length)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 e of r[t.name])n.push(e.toString());else n.push(r[t.name].toString())}return`/${n.join("/")}`}}Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"}),exports.compile=function(t){const a=[],i=/:(\w+)(<[\s?\w|]+>)?({\d+\,\d+})?([+*?])?/,o=t.split("/").filter(Boolean);for(let e=0;e<o.length;e++){const n=o[e];if(!n)continue;const s=r(n.match(i));if(!s){a.push({type:"const",name:n,payload:void 0});continue}const{arrayProps:l,genericProps:u,modificator:c,name:p}=s;if(!p)throw new Error(`Invalid path: "${t}". Name for argument must be provided`);const f={type:"parameter",name:p,payload:{required:!0}};switch(u&&"number"===u&&(f.payload.genericProps={type:"number"}),u&&u.includes("|")&&(f.payload.genericProps={type:"union",items:u.split("|")}),c){case"*":f.payload.arrayProps={};break;case"+":f.payload.arrayProps={min:1};break;case"?":f.payload.required=!1}l&&(f.payload.arrayProps={...f.payload.arrayProps,min:l[0],max:l[1]}),a.push(f)}return{parse:e(a),build:n(a)}};
1
+ "use strict";function e(e){return r=>{const n=r.split("/").map(e=>e.trim()).filter(e=>""!==e);let t=null;function a(e,r){t||(t={}),t[e]=r}if(0===e.length)return 0===n.length?{path:r,params:null}:null;for(let i=0;i<e.length;i++){const r=e[i];switch(r.type){case"const":if(r.name!==n.shift())return null;continue;case"parameter":{const{arrayProps:t,genericProps:o,required:s}=r.payload;if(t){const s=[];let l;for(;l=n.shift(),l;){switch(null==o?void 0:o.type){case"number":if(isNaN(+l))return null;s.push(+l);break;case"union":if(!o.items.includes(l))return null;s.push(l);break;default:s.push(l)}if(s.length>=(t.max??1/0))break}if(s.length<(t.min??0))return null;if(n.length>0&&!e[i+1])return null;a(r.name,s);break}const l=n.shift();if(s&&!l)return null;if(!l){a(r.name,void 0);continue}switch(null==o?void 0:o.type){case"number":if(isNaN(+l))return null;a(r.name,+l);break;case"union":if(!o.items.includes(l))return null;a(r.name,l);break;default:a(r.name,l)}}}}return n.length>0?null:{path:r,params:t}}}function r(e){if(!e)return null;const r=e[1];let n,t,a;for(const i of e.slice(2))i&&(i.includes("<")?n=i.replaceAll(/\s/g,"").replace("<","").replace(">",""):(i.includes("{")&&(t=i.replace("{","").replace("}","").split(",").map(e=>parseInt(e))),["*","?","+"].includes(i)&&(a=i)));return{name:r,genericProps:n,arrayProps:t,modificator:a}}function n(e){return r=>{const n=[];if(0===e.length)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 e of r[t.name])n.push(e.toString());else n.push(r[t.name].toString())}return`/${n.join("/")}`}}Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=[[/:id<.+>/g,":id"],[/:id\+/g,"*id"],[/:id\*/g,"*id"],[/:id\{.+\}/g,"*id"],[/([a-zA-Z0-9:/_\.]+)\/([\*:])id\?/g,"$1{/$2id}"],[/([\*:])id\?/g,"{$1id}"]];exports.compile=function(t){const a=[],i=/:(\w+)(<[\s?\w|]+>)?({\d+\,\d+})?([+*?])?/,o=t.split("/").filter(Boolean);for(let e=0;e<o.length;e++){const n=o[e];if(!n)continue;const s=r(n.match(i));if(!s){a.push({type:"const",name:n,payload:void 0});continue}const{arrayProps:l,genericProps:u,modificator:c,name:p}=s;if(!p)throw new Error(`Invalid path: "${t}". Name for argument must be provided`);const f={type:"parameter",name:p,payload:{required:!0}};switch(u&&"number"===u&&(f.payload.genericProps={type:"number"}),u&&u.includes("|")&&(f.payload.genericProps={type:"union",items:u.split("|")}),c){case"*":f.payload.arrayProps={};break;case"+":f.payload.arrayProps={min:1};break;case"?":f.payload.required=!1}l&&(f.payload.arrayProps={...f.payload.arrayProps,min:l[0],max:l[1]}),a.push(f)}return{parse:e(a),build:n(a)}},exports.convertPath=function(e,r){if("express"===r){let r=e;for(const[e,n]of t){r.match(e)&&(r=r.replace(e,n))}return r}};
package/dist/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  export declare type Builder<T> = (params: T) => string;
2
2
 
3
+ declare type CompabilityMode = 'express';
4
+
3
5
  /**
4
6
  * @param path Route path
5
7
  * @description compiles route and give two functions: build (from params to string) & parse (validate from string and get params)
@@ -37,6 +39,8 @@ export declare type Builder<T> = (params: T) => string;
37
39
  build: Builder<Params>;
38
40
  };
39
41
 
42
+ export declare function convertPath(path: string, mode: CompabilityMode): string;
43
+
40
44
  declare type GenericType<T extends string> = ReplaceAll<T, ' ', ''> extends infer Trimmed ? Trimmed extends `number` ? number : Trimmed extends `${infer A}|${infer B}` ? Union<Trimmed> : Trimmed : never;
41
45
 
42
46
  declare type Join<T extends any[]> = T['length'] extends 0 ? never : T extends [infer F, ...infer Rest] ? Join<Rest> extends infer Tail ? [Tail] extends [never] ? `${F & string}` : `${F & string}/${Tail & string}` : never : never;
@@ -122,3 +126,11 @@ export declare type Builder<T> = (params: T) => string;
122
126
  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;
123
127
 
124
128
  export { }
129
+
130
+
131
+ declare module 'vitest' {
132
+ interface Assertion<T = any> extends CustomMatchers<T> {
133
+ }
134
+ interface AsymmetricMatchersContaining extends CustomMatchers {
135
+ }
136
+ }
package/dist/index.js CHANGED
@@ -254,6 +254,25 @@ function compile(path) {
254
254
  build: prepareBuilder(tokens)
255
255
  };
256
256
  }
257
+ const cases = {
258
+ express: [[/:id<.+>/g, ":id"], [/:id\+/g, "*id"], [/:id\*/g, "*id"], [/:id\{.+\}/g, "*id"], [/([a-zA-Z0-9:/_\.]+)\/([\*:])id\?/g, "$1{/$2id}"], [/([\*:])id\?/g, "{$1id}"]]
259
+ };
260
+ function convertPath(path, mode) {
261
+ switch (mode) {
262
+ case "express": {
263
+ let newPath = path;
264
+ for (const [regex, replacement] of cases.express) {
265
+ const match = newPath.match(regex);
266
+ if (!match) {
267
+ continue;
268
+ }
269
+ newPath = newPath.replace(regex, replacement);
270
+ }
271
+ return newPath;
272
+ }
273
+ }
274
+ }
257
275
  export {
258
- compile
276
+ compile,
277
+ convertPath
259
278
  };
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "registry": "https://registry.npmjs.org/"
6
6
  },
7
7
  "private": false,
8
- "version": "0.11.0",
8
+ "version": "0.12.0",
9
9
  "description": "Paths with power of effector",
10
10
  "keywords": [
11
11
  "paths",
@@ -41,5 +41,5 @@
41
41
  "bugs": {
42
42
  "url": "https://github.com/movpushmov/argon-router/issues"
43
43
  },
44
- "gitHead": "0180f75dbbc73a123e706ff48961f8e57d57ce65"
44
+ "gitHead": "9832d6cd282d6abd33c26ba4d03b9a1176110fc4"
45
45
  }
package/LICENSE.MD DELETED
@@ -1,21 +0,0 @@
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.