crossroad 2.0.3 → 3.0.1

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/index.min.js CHANGED
@@ -1 +1 @@
1
- import e,{createContext as t,useState as r,useRef as n,useCallback as o,useEffect as i,useContext as a}from"react";var s=t();const u=e=>{if("string"!=typeof e)return e;const t={},r=new URL(e,"http://localhost:3000/");t.path=(r.pathname.replace(/\/$/,"")||"/").replaceAll("%3C","<").replaceAll("%3E",">"),t.query={};for(const[e]of r.searchParams)t.query[e]=(n=r.searchParams.getAll(e)).length>1?n:n[0];var n;return r.hash&&(t.hash=r.hash.replace(/^#/,"")),t},c=e=>{if("string"==typeof e)return e;const{path:t,query:r={},hash:n}=e||{};let o=t||"/";const i=new URLSearchParams(Object.entries(r).map(([e,t])=>(Array.isArray(t)?t:[t]).map(t=>[e,t])).flat().filter(([e,t])=>t)).toString();return i&&(o+="?"+i),n&&(o+="#"+n),o};var l=()=>"undefined"==typeof window;function p(e,t,r={}){if(e=JSON.parse(JSON.stringify(u(e))),(t=JSON.parse(JSON.stringify(u(t)))).path=t.path.replace(/\/$/,"")||"/",e.path=e.path.replace(/\/$/,"")||"/",e.path.endsWith("*")){e.path=e.path.replace(/\/?\*/,"")||"/";const r=e.path.split("/").filter(Boolean).length;t.path="/"+t.path.slice(1).split("/").slice(0,r).join("/")}if(Object.entries(e.query).length)for(let r in e.query){if(!(r in t.query))return!1;if(e.query[r]&&e.query[r]!==t.query[r])return!1}if(!e.path.includes(":"))return e.path===t.path&&r;if(e.path.split("/").length!==t.path.split("/").length)return!1;const n={},o=e.path.split("/").every((e,o)=>{const i=t.path.split("/")[o];if(e.startsWith(":")){let t=e.slice(1),o="string";t.includes("<")&&([t,o]=t.split("<"),o=o.slice(0,-1));const a=decodeURIComponent(i);return n[t]="number"===o?Number(a):"date"===o?new Date(/^\d+$/.test(a)?Number(a):a):"boolean"===o?"true"===a:a,r}return i===e});return o&&Object.assign(r,n),o&&r}var h=()=>{const e=a(s);if(!e)throw new Error("Wrap your App with <Router>");return e},f=({path:t="*",scrollUp:r,component:n,render:o,children:i})=>{const a=h(),u=p(t,a[0]);if(!u)return null;if(r&&window.scrollTo(0,0),n){const t=n;i=e.createElement(t,u)}else if(o)i=o(u);else if(!i)throw new Error("Route needs prop `component`, `render` or `children`");return e.createElement(s.Provider,{value:[{...a[0],params:u},...a.slice(1)]},i)};var y=({redirect:e,children:t})=>{const[r,n]=h(),o=(e=>(Array.isArray(e)||(e=[e]),e.filter(e=>e&&e.props)))(t).find(e=>p(e.props.path||"*",r))||null;return i(()=>{e&&(o||("function"==typeof e&&(e=e(r)),n(c(e))))},[e,o]),o},d=()=>{const[e,t]=h(),r=o((e,r)=>{t(t=>("function"==typeof e&&(e=e(t.path)),"string"!=typeof e&&(e="/"),{...t,path:e}),r)},[]);return[e.path,r]};const m=e=>c({query:e});var w=e=>e?(e=>{const[t,r]=h(),n=o((t,n)=>{r(r=>{const n=r.query[e];if((t="function"==typeof t?t(n):t)===n)return r;if(t)return{...r,query:{...r.query,[e]:t}};{const{[e]:t,...n}=r.query;return{...r,query:n}}},n)},[]);return[t.query[e],n]})(e):(()=>{const[e,t]=h(),r=o((e,r)=>{t(t=>("string"==typeof(e="function"==typeof e?e(t.query):e)&&(e=u("/?"+e.replace(/^\?/,"")).query),e=u(m(e)).query,m(e)===m(t.query)?t:{...t,query:e}),r)},[]);return[e.query,r]})(),q=()=>{const[e,t]=h(),r=o((e,r)=>{t(t=>("function"==typeof e&&(e=e(t.hash)),"string"!=typeof e&&(e=""),e=e.replace(/^#/,""),{...t,hash:e}),r)},[]);return[e.hash,r]},g=e=>{const[{params:t}]=a(s);return e?e in t?t[e]:"":t||{}};export default({scrollUp:t,url:a,children:p})=>{const h=a||(l()?"/":window.location.href),[f,y]=r(()=>u(h)),d=n(f),m=o((e,{mode:r="push"}={})=>{if(!history[r+"State"])throw new Error(`Invalid mode "${r}"`);y(t=>(e="function"==typeof e?e(t):e,c(t)===c(e)?t:u(e)));const n=d.current,o="function"==typeof e?e(n):e;if(c(n)!==c(o)){const e=u(o);d.current=e,history[r+"State"]({},null,c(e)),t&&window.scrollTo(0,0)}},[]);return i(()=>{d.current=f},[f]),i(()=>{if(l())return;const e=()=>y(u(window.location.href)),t=e=>{const t=(e=>{if(!e)return null;const t=e.getAttribute("href");if(!t)return null;const r=t.trim();return/^[a-zA-Z][a-zA-Z0-9+\-.]*:/.test(r)||null!==e.getAttribute("target")?null:r})(e.target.closest("a"));if(!t)return!1;e.preventDefault();const[r,n]=t.split("#");r?m(t):n&&(window.location.hash="#"+n)};return window.addEventListener("popstate",e),document.addEventListener("click",t),()=>{window.removeEventListener("popstate",e),document.removeEventListener("click",t)}},[m]),e.createElement(s.Provider,{value:[f,m]},p)};export{s as Context,f as Route,y as Switch,q as useHash,g as useParams,d as usePath,w as useQuery,h as useUrl};
1
+ import t,{createContext as e,useState as r,useRef as n,useCallback as o,useEffect as i,useContext as s}from"react";var a=e(void 0);const u=t=>t.length>1?t:t[0],c=t=>{if("string"!=typeof t)return t;const e={query:{}},r=new URL(t,"http://localhost:3000/");e.path=(r.pathname.replace(/\/$/,"")||"/").replaceAll("%3C","<").replaceAll("%3E",">"),e.query={};for(const[t]of r.searchParams)e.query[t]=u(r.searchParams.getAll(t));return r.hash&&(e.hash=r.hash.replace(/^#/,"")),e},l=t=>{if("string"==typeof t)return t;const{path:e,query:r={},hash:n}=t||{};let o=e||"/";const i=new URLSearchParams(Object.entries(r).map(([t,e])=>(Array.isArray(e)?e:[e]).map(e=>[t,e])).flat().filter(([t,e])=>e)).toString();return i&&(o+="?"+i),n&&(o+="#"+n),o};var p=()=>"undefined"==typeof window,h=({scrollUp:e,url:s,children:u})=>{const h=s||(p()?"/":window.location.href),[f,y]=r(()=>c(h)),d=n(f),m=o((t,{mode:r="push"}={})=>{if(!history[r+"State"])throw new Error(`Invalid mode "${r}"`);y(e=>{const r="function"==typeof t?t(e):t;return l(e)===l(r)?e:c(r)});const n=d.current,o="function"==typeof t?t(n):t;if(l(n)!==l(o)){const t=c(o);d.current=t,history[r+"State"]({},null,l(t)),e&&window.scrollTo(0,0)}},[]);return i(()=>{d.current=f},[f]),i(()=>{if(p())return;const t=()=>y(c(window.location.href)),e=t=>{const e=t.target,r=(t=>{if(!t)return null;const e=t.getAttribute("href");if(!e)return null;const r=e.trim();return/^[a-zA-Z][a-zA-Z0-9+\-.]*:/.test(r)||null!==t.getAttribute("target")?null:r})(e?.closest("a")??null);if(!r)return!1;t.preventDefault();const[n,o]=r.split("#");n?m(r):o&&(window.location.hash="#"+o)};return window.addEventListener("popstate",t),document.addEventListener("click",e),()=>{window.removeEventListener("popstate",t),document.removeEventListener("click",e)}},[m]),t.createElement(a.Provider,{value:[f,m]},u)};function f(t,e,r={}){let n=JSON.parse(JSON.stringify(c(t))),o=JSON.parse(JSON.stringify(c(e)));if(o.path=o.path.replace(/\/$/,"")||"/",n.path=n.path.replace(/\/$/,"")||"/",n.path.endsWith("*")){n.path=n.path.replace(/\/?\*/,"")||"/";const t=n.path.split("/").filter(Boolean).length;o.path="/"+o.path.slice(1).split("/").slice(0,t).join("/")}if(Object.entries(n.query).length)for(const t in n.query){if(!(t in o.query))return!1;if(n.query[t]&&n.query[t]!==o.query[t])return!1}if(!n.path.includes(":"))return n.path===o.path&&r;if(n.path.split("/").length!==o.path.split("/").length)return!1;const i={},s=n.path.split("/").every((t,e)=>{const n=o.path.split("/")[e];if(t.startsWith(":")){let e=t.slice(1),o="string";e.includes("<")&&([e,o]=e.split("<"),o=o.slice(0,-1));const s=decodeURIComponent(n);return i[e]="number"===o?Number(s):"date"===o?new Date(/^\d+$/.test(s)?Number(s):s):"boolean"===o?"true"===s:s,r}return n===t});return s&&Object.assign(r,i),s&&r}var y=()=>{const t=s(a);if(!t)throw new Error("Wrap your App with <Router>");return t},d=({path:e="*",scrollUp:r,component:n,render:o,children:i})=>{const s=y(),u=f(e,s[0]);if(!u)return null;if(r&&window.scrollTo(0,0),n){const e=n;i=t.createElement(e,u)}else if(o)i=o(u);else if(!i)throw new Error("Route needs prop `component`, `render` or `children`");return t.createElement(a.Provider,{value:[{...s[0],params:u},s[1]]},i)};var m=({redirect:t,children:e})=>{const[r,n]=y(),o=(t=>(Array.isArray(t)?t:[t]).filter(t=>null!=t&&"object"==typeof t&&"props"in t))(e).find(t=>f(t.props.path||"*",r))||null;return i(()=>{if(!t)return;if(o)return;const e="function"==typeof t?t(r):t;n(l(e))},[t,o]),o},w=()=>{const[t,e]=y(),r=o((t,r)=>{e(e=>{const r="function"==typeof t?t(e.path):t;return{...e,path:"string"==typeof r?r:"/"}},r)},[]);return[t.path,r]};const q=t=>l({query:t});function g(t){return t?(t=>{const[e,r]=y(),n=o((e,n)=>{r(r=>{const n=r.query[t],o="function"==typeof e?e(n):e;if(o===n)return r;if(o)return{...r,query:{...r.query,[t]:o}};{const{[t]:e,...n}=r.query;return{...r,query:n}}},n)},[]);return[e.query[t],n]})(t):(()=>{const[t,e]=y(),r=o((t,r)=>{e(e=>{let r="function"==typeof t?t(e.query):t;"string"==typeof r&&(r=c("/?"+r.replace(/^\?/,"")).query);const n=c(q(r)).query;return q(n)===q(e.query)?e:{...e,query:n}},r)},[]);return[t.query,r]})()}var v=()=>{const[t,e]=y(),r=o((t,r)=>{e(e=>{const r="function"==typeof t?t(e.hash):t,n="string"!=typeof r?"":r.replace(/^#/,"");return{...e,hash:n}},r)},[]);return[t.hash,r]};function A(t){const[{params:e}]=s(a);return t?t in e?e[t]:"":e||{}}export{a as Context,d as Route,m as Switch,h as default,v as useHash,A as useParams,w as usePath,g as useQuery,y as useUrl};
package/package.json CHANGED
@@ -1,18 +1,28 @@
1
1
  {
2
2
  "name": "crossroad",
3
- "version": "2.0.3",
3
+ "version": "3.0.1",
4
4
  "description": "A React library to handle navigation easily in your WebApp",
5
5
  "homepage": "https://crossroad.page/",
6
6
  "repository": "https://github.com/franciscop/crossroad.git",
7
7
  "bugs": "https://github.com/franciscop/crossroad/issues",
8
8
  "funding": "https://www.paypal.me/franciscopresencia/19",
9
9
  "author": "Francisco Presencia <public@francisco.io> (https://francisco.io/)",
10
+ "documentation": {
11
+ "home": "assets/home.html",
12
+ "menu": {
13
+ "Documentation": "/documentation",
14
+ "Donate": "https://www.paypal.me/franciscopresencia/19",
15
+ "Author": "https://francisco.io/",
16
+ "Github": "https://github.com/franciscop/crossroad"
17
+ }
18
+ },
10
19
  "license": "MIT",
11
20
  "scripts": {
12
21
  "build": "rollup -c",
22
+ "typecheck": "tsc",
13
23
  "size": "echo $(gzip -c index.min.js | wc -c) bytes",
14
24
  "start": "jest --watch",
15
- "test": "jest --coverage && npx check-dts"
25
+ "test": "jest --coverage"
16
26
  },
17
27
  "keywords": [
18
28
  "react",
@@ -23,26 +33,28 @@
23
33
  ],
24
34
  "main": "index.min.js",
25
35
  "type": "module",
26
- "types": "src/index.d.ts",
36
+ "types": "index.d.ts",
27
37
  "files": [
28
- "src/index.d.ts"
38
+ "index.d.ts"
29
39
  ],
30
40
  "devDependencies": {
31
41
  "@babel/core": "^7.15.0",
32
42
  "@babel/preset-env": "^7.15.0",
33
43
  "@babel/preset-react": "^7.14.5",
44
+ "@babel/preset-typescript": "^7.28.5",
45
+ "@rollup/plugin-terser": "^1.0.0",
46
+ "@types/node": "^20.19.39",
34
47
  "@types/react": "^18.3.4",
35
48
  "babel-loader": "^8.2.2",
36
49
  "babel-polyfill": "^6.26.0",
37
- "check-dts": "^0.8.2",
38
50
  "jest": "^29.7.0",
39
51
  "jest-environment-jsdom": "^29.7.0",
40
52
  "react": "^18.3.1",
41
53
  "react-dom": "^18.3.1",
42
54
  "react-test": "^0.22.1",
43
- "rollup": "^1.32.1",
55
+ "rollup": "^4.60.1",
44
56
  "rollup-plugin-babel": "^4.4.0",
45
- "rollup-plugin-terser": "^5.2.0"
57
+ "typescript": "^5.0.0"
46
58
  },
47
59
  "peerDependencies": {
48
60
  "react": ">=16.8.0"
@@ -50,7 +62,8 @@
50
62
  "babel": {
51
63
  "presets": [
52
64
  "@babel/preset-env",
53
- "@babel/preset-react"
65
+ "@babel/preset-react",
66
+ "@babel/preset-typescript"
54
67
  ]
55
68
  },
56
69
  "jest": {
File without changes