@fluixi/router 1.0.0-alpha.107 → 1.0.0-alpha.109

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.
@@ -0,0 +1,80 @@
1
+ import {
2
+ matchRoutes
3
+ } from "./chunk-JKWUHPUA.mjs";
4
+
5
+ // src/lib/router.ts
6
+ function parseQuery(search) {
7
+ const q = {};
8
+ if (search) {
9
+ const usp = new URLSearchParams(
10
+ search.startsWith("?") ? search.slice(1) : search
11
+ );
12
+ usp.forEach((v, k) => q[k] = v);
13
+ }
14
+ return q;
15
+ }
16
+ function stripBase(pathname, base) {
17
+ if (!base) return pathname || "/";
18
+ const b = base.replace(/\/$/, "");
19
+ if (pathname === b) return "/";
20
+ if (pathname.startsWith(b + "/")) return pathname.slice(b.length) || "/";
21
+ return pathname || "/";
22
+ }
23
+ function withBase(to, base) {
24
+ if (!base) return to;
25
+ if (/^https?:\/\//.test(to)) return to;
26
+ const b = base.replace(/\/$/, "");
27
+ return `${b}${to.startsWith("/") ? to : "/" + to}`;
28
+ }
29
+ function createRouter(config) {
30
+ const { routes, history, base } = config;
31
+ const listeners = /* @__PURE__ */ new Set();
32
+ const readLocation = () => {
33
+ const raw = history.get();
34
+ return {
35
+ pathname: raw.pathname,
36
+ search: raw.search,
37
+ hash: raw.hash,
38
+ state: raw.state,
39
+ query: parseQuery(raw.search),
40
+ ...raw.key !== void 0 ? { key: raw.key } : {},
41
+ ...raw.action !== void 0 ? { action: raw.action } : {}
42
+ };
43
+ };
44
+ const computeMatch = (pathname) => matchRoutes(routes, stripBase(pathname, base));
45
+ let current = (() => {
46
+ const location = readLocation();
47
+ return { location, match: computeMatch(location.pathname) };
48
+ })();
49
+ const recompute = () => {
50
+ const location = readLocation();
51
+ current = { location, match: computeMatch(location.pathname) };
52
+ listeners.forEach((l) => l(current));
53
+ };
54
+ const unsubscribeHistory = history.subscribe(recompute);
55
+ return {
56
+ state() {
57
+ return current;
58
+ },
59
+ subscribe(listener) {
60
+ listeners.add(listener);
61
+ return () => listeners.delete(listener);
62
+ },
63
+ navigate(to, options = {}) {
64
+ const target = withBase(to, base);
65
+ if (options.replace) history.replace(target, options.state ?? null);
66
+ else history.push(target, options.state ?? null);
67
+ },
68
+ match(pathname) {
69
+ return computeMatch(pathname);
70
+ },
71
+ destroy() {
72
+ unsubscribeHistory();
73
+ listeners.clear();
74
+ }
75
+ };
76
+ }
77
+
78
+ export {
79
+ createRouter
80
+ };
@@ -0,0 +1,102 @@
1
+ // src/lib/match.ts
2
+ function normalizePath(p) {
3
+ return p.startsWith("//") ? p.replace(/^\/+/, "/") : p;
4
+ }
5
+ function matchPath(pattern, path, exact = false) {
6
+ const keys = [];
7
+ if (pattern === path) return { params: {}, path };
8
+ if (!exact && pattern === "/" && path.startsWith("/"))
9
+ return { params: {}, path: "/" };
10
+ let regexStr = pattern.replace(/:([a-zA-Z0-9_]+)/g, (_, key) => {
11
+ keys.push(key);
12
+ return "([^/]+)";
13
+ }).replace(/\*([a-zA-Z0-9_]*)/g, (_, name) => {
14
+ if (name) keys.push(name);
15
+ return "(.*)";
16
+ });
17
+ if (!regexStr.startsWith("/")) regexStr = "/" + regexStr;
18
+ if (regexStr === "") regexStr = "/";
19
+ const regex = new RegExp(`^${regexStr}${exact ? "$" : "(?=/|$)"}`);
20
+ const match = path.match(regex);
21
+ if (!match) return null;
22
+ const params = {};
23
+ keys.forEach((key, i) => {
24
+ params[key] = match[i + 1];
25
+ });
26
+ return { params, path: match[0] };
27
+ }
28
+ function scoreRoute(def) {
29
+ const segments = def.path.split("/").filter(Boolean);
30
+ if (segments.length === 0) return 5;
31
+ let score = 0;
32
+ for (const seg of segments) {
33
+ if (seg.startsWith("*")) score += 1;
34
+ else if (seg.startsWith(":")) score += 10;
35
+ else score += 100;
36
+ }
37
+ return score;
38
+ }
39
+ function rankRoutes(defs) {
40
+ return [...defs].sort((a, b) => scoreRoute(b) - scoreRoute(a));
41
+ }
42
+ function matchRoutes(routes, pathname) {
43
+ const path = pathname.length > 1 ? pathname.replace(/\/+$/, "") || "/" : pathname;
44
+ const result = matchInto(routes, path);
45
+ if (!result) return null;
46
+ const matched = [];
47
+ let cumulative = "";
48
+ const params = {};
49
+ for (const layer of result.layers) {
50
+ cumulative = normalizePath(joinMatched(cumulative, layer.consumed));
51
+ const layerParams = {};
52
+ for (const [k, v] of Object.entries(layer.params)) {
53
+ if (!(k in params)) layerParams[k] = v;
54
+ params[k] = v;
55
+ }
56
+ matched.push({
57
+ route: layer.route,
58
+ component: layer.route.component,
59
+ params: layerParams,
60
+ pathname: cumulative || "/"
61
+ });
62
+ }
63
+ return { matched, params, pathname };
64
+ }
65
+ function matchInto(defs, currentPath) {
66
+ if (!defs || defs.length === 0) return null;
67
+ const ranked = rankRoutes(defs);
68
+ for (const def of ranked) {
69
+ const hasChildren = !!def.children && def.children.length > 0;
70
+ const seg = matchPath(def.path, currentPath, !hasChildren);
71
+ if (!seg) continue;
72
+ if (hasChildren) {
73
+ const childMatch = matchInto(def.children, currentPath);
74
+ if (childMatch) {
75
+ return {
76
+ layers: [
77
+ { route: def, params: seg.params, consumed: seg.path },
78
+ ...childMatch.layers
79
+ ]
80
+ };
81
+ }
82
+ continue;
83
+ }
84
+ return { layers: [{ route: def, params: seg.params, consumed: seg.path }] };
85
+ }
86
+ return null;
87
+ }
88
+ function joinMatched(cumulative, consumed) {
89
+ if (!cumulative) return consumed;
90
+ if (consumed === cumulative) return cumulative;
91
+ if (consumed.startsWith(cumulative)) return consumed;
92
+ const a = cumulative.replace(/\/$/, "");
93
+ const b = consumed.replace(/^\//, "");
94
+ return `${a}/${b}`;
95
+ }
96
+
97
+ export {
98
+ matchPath,
99
+ scoreRoute,
100
+ rankRoutes,
101
+ matchRoutes
102
+ };
@@ -0,0 +1,102 @@
1
+ // src/lib/history.ts
2
+ var keys = 0;
3
+ var nextKey = () => `${Date.now().toString(36)}-${(keys++).toString(36)}`;
4
+ function splitPath(to) {
5
+ let rest = to;
6
+ let hash = "";
7
+ const hashIdx = rest.indexOf("#");
8
+ if (hashIdx !== -1) {
9
+ hash = rest.slice(hashIdx);
10
+ rest = rest.slice(0, hashIdx);
11
+ }
12
+ let search = "";
13
+ const qIdx = rest.indexOf("?");
14
+ if (qIdx !== -1) {
15
+ search = rest.slice(qIdx);
16
+ rest = rest.slice(0, qIdx);
17
+ }
18
+ return { pathname: rest || "/", search, hash };
19
+ }
20
+ function createMemoryHistory(initial = "/") {
21
+ let entries = [
22
+ { path: initial, state: null, key: nextKey() }
23
+ ];
24
+ let index = 0;
25
+ let action = "pop";
26
+ const listeners = /* @__PURE__ */ new Set();
27
+ const notify = () => listeners.forEach((l) => l());
28
+ return {
29
+ get() {
30
+ const { path, state, key } = entries[index];
31
+ return { ...splitPath(path), state, key, action };
32
+ },
33
+ push(to, state = null) {
34
+ entries = entries.slice(0, index + 1);
35
+ entries.push({ path: to, state, key: nextKey() });
36
+ index = entries.length - 1;
37
+ action = "push";
38
+ notify();
39
+ },
40
+ replace(to, state = null) {
41
+ entries[index] = { path: to, state, key: nextKey() };
42
+ action = "replace";
43
+ notify();
44
+ },
45
+ /** Back (`-1`) or forward (`1`), as the browser's buttons would. */
46
+ go(delta) {
47
+ const next = index + delta;
48
+ if (next < 0 || next >= entries.length) return;
49
+ index = next;
50
+ action = "pop";
51
+ notify();
52
+ },
53
+ subscribe(onChange) {
54
+ listeners.add(onChange);
55
+ return () => listeners.delete(onChange);
56
+ }
57
+ };
58
+ }
59
+ function createBrowserHistory() {
60
+ const w = typeof window !== "undefined" ? window : void 0;
61
+ const listeners = /* @__PURE__ */ new Set();
62
+ let action = "pop";
63
+ const notify = () => listeners.forEach((l) => l());
64
+ if (w) w.addEventListener("popstate", () => (action = "pop", notify()));
65
+ const wrapped = (value) => typeof value === "object" && value !== null && typeof value.fx === "string";
66
+ const entry = () => {
67
+ const raw = w.history.state;
68
+ if (wrapped(raw)) return raw;
69
+ const keyed = { fx: nextKey(), usr: raw ?? null };
70
+ w.history.replaceState(keyed, "");
71
+ return keyed;
72
+ };
73
+ return {
74
+ window: w !== void 0,
75
+ get() {
76
+ if (!w) return { pathname: "/", search: "", hash: "", state: null };
77
+ const { pathname, search, hash } = w.location;
78
+ const { fx, usr } = entry();
79
+ return { pathname, search, hash, state: usr, key: fx, action };
80
+ },
81
+ push(to, state = null) {
82
+ w?.history.pushState({ fx: nextKey(), usr: state }, "", to);
83
+ action = "push";
84
+ notify();
85
+ },
86
+ replace(to, state = null) {
87
+ w?.history.replaceState({ fx: nextKey(), usr: state }, "", to);
88
+ action = "replace";
89
+ notify();
90
+ },
91
+ subscribe(onChange) {
92
+ listeners.add(onChange);
93
+ return () => listeners.delete(onChange);
94
+ }
95
+ };
96
+ }
97
+
98
+ export {
99
+ splitPath,
100
+ createMemoryHistory,
101
+ createBrowserHistory
102
+ };
package/dist/index.cjs CHANGED
@@ -1,4 +1,4 @@
1
- /*! @fluixi/router v1.0.0-alpha.107 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
1
+ /*! @fluixi/router v1.0.0-alpha.109 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
2
2
  "use strict";
3
3
  var __defProp = Object.defineProperty;
4
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
package/dist/index.mjs CHANGED
@@ -1,270 +1,18 @@
1
- /*! @fluixi/router v1.0.0-alpha.107 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
2
-
3
- // src/lib/match.ts
4
- function normalizePath(p) {
5
- return p.startsWith("//") ? p.replace(/^\/+/, "/") : p;
6
- }
7
- function matchPath(pattern, path, exact = false) {
8
- const keys2 = [];
9
- if (pattern === path) return { params: {}, path };
10
- if (!exact && pattern === "/" && path.startsWith("/"))
11
- return { params: {}, path: "/" };
12
- let regexStr = pattern.replace(/:([a-zA-Z0-9_]+)/g, (_, key) => {
13
- keys2.push(key);
14
- return "([^/]+)";
15
- }).replace(/\*([a-zA-Z0-9_]*)/g, (_, name) => {
16
- if (name) keys2.push(name);
17
- return "(.*)";
18
- });
19
- if (!regexStr.startsWith("/")) regexStr = "/" + regexStr;
20
- if (regexStr === "") regexStr = "/";
21
- const regex = new RegExp(`^${regexStr}${exact ? "$" : "(?=/|$)"}`);
22
- const match = path.match(regex);
23
- if (!match) return null;
24
- const params = {};
25
- keys2.forEach((key, i) => {
26
- params[key] = match[i + 1];
27
- });
28
- return { params, path: match[0] };
29
- }
30
- function scoreRoute(def) {
31
- const segments = def.path.split("/").filter(Boolean);
32
- if (segments.length === 0) return 5;
33
- let score = 0;
34
- for (const seg of segments) {
35
- if (seg.startsWith("*")) score += 1;
36
- else if (seg.startsWith(":")) score += 10;
37
- else score += 100;
38
- }
39
- return score;
40
- }
41
- function rankRoutes(defs) {
42
- return [...defs].sort((a, b) => scoreRoute(b) - scoreRoute(a));
43
- }
44
- function matchRoutes(routes, pathname) {
45
- const path = pathname.length > 1 ? pathname.replace(/\/+$/, "") || "/" : pathname;
46
- const result = matchInto(routes, path);
47
- if (!result) return null;
48
- const matched = [];
49
- let cumulative = "";
50
- const params = {};
51
- for (const layer of result.layers) {
52
- cumulative = normalizePath(joinMatched(cumulative, layer.consumed));
53
- const layerParams = {};
54
- for (const [k, v] of Object.entries(layer.params)) {
55
- if (!(k in params)) layerParams[k] = v;
56
- params[k] = v;
57
- }
58
- matched.push({
59
- route: layer.route,
60
- component: layer.route.component,
61
- params: layerParams,
62
- pathname: cumulative || "/"
63
- });
64
- }
65
- return { matched, params, pathname };
66
- }
67
- function matchInto(defs, currentPath) {
68
- if (!defs || defs.length === 0) return null;
69
- const ranked = rankRoutes(defs);
70
- for (const def of ranked) {
71
- const hasChildren = !!def.children && def.children.length > 0;
72
- const seg = matchPath(def.path, currentPath, !hasChildren);
73
- if (!seg) continue;
74
- if (hasChildren) {
75
- const childMatch = matchInto(def.children, currentPath);
76
- if (childMatch) {
77
- return {
78
- layers: [
79
- { route: def, params: seg.params, consumed: seg.path },
80
- ...childMatch.layers
81
- ]
82
- };
83
- }
84
- continue;
85
- }
86
- return { layers: [{ route: def, params: seg.params, consumed: seg.path }] };
87
- }
88
- return null;
89
- }
90
- function joinMatched(cumulative, consumed) {
91
- if (!cumulative) return consumed;
92
- if (consumed === cumulative) return cumulative;
93
- if (consumed.startsWith(cumulative)) return consumed;
94
- const a = cumulative.replace(/\/$/, "");
95
- const b = consumed.replace(/^\//, "");
96
- return `${a}/${b}`;
97
- }
98
-
99
- // src/lib/history.ts
100
- var keys = 0;
101
- var nextKey = () => `${Date.now().toString(36)}-${(keys++).toString(36)}`;
102
- function splitPath(to) {
103
- let rest = to;
104
- let hash = "";
105
- const hashIdx = rest.indexOf("#");
106
- if (hashIdx !== -1) {
107
- hash = rest.slice(hashIdx);
108
- rest = rest.slice(0, hashIdx);
109
- }
110
- let search = "";
111
- const qIdx = rest.indexOf("?");
112
- if (qIdx !== -1) {
113
- search = rest.slice(qIdx);
114
- rest = rest.slice(0, qIdx);
115
- }
116
- return { pathname: rest || "/", search, hash };
117
- }
118
- function createMemoryHistory(initial = "/") {
119
- let entries = [
120
- { path: initial, state: null, key: nextKey() }
121
- ];
122
- let index = 0;
123
- let action = "pop";
124
- const listeners = /* @__PURE__ */ new Set();
125
- const notify = () => listeners.forEach((l) => l());
126
- return {
127
- get() {
128
- const { path, state, key } = entries[index];
129
- return { ...splitPath(path), state, key, action };
130
- },
131
- push(to, state = null) {
132
- entries = entries.slice(0, index + 1);
133
- entries.push({ path: to, state, key: nextKey() });
134
- index = entries.length - 1;
135
- action = "push";
136
- notify();
137
- },
138
- replace(to, state = null) {
139
- entries[index] = { path: to, state, key: nextKey() };
140
- action = "replace";
141
- notify();
142
- },
143
- /** Back (`-1`) or forward (`1`), as the browser's buttons would. */
144
- go(delta) {
145
- const next = index + delta;
146
- if (next < 0 || next >= entries.length) return;
147
- index = next;
148
- action = "pop";
149
- notify();
150
- },
151
- subscribe(onChange) {
152
- listeners.add(onChange);
153
- return () => listeners.delete(onChange);
154
- }
155
- };
156
- }
157
- function createBrowserHistory() {
158
- const w = typeof window !== "undefined" ? window : void 0;
159
- const listeners = /* @__PURE__ */ new Set();
160
- let action = "pop";
161
- const notify = () => listeners.forEach((l) => l());
162
- if (w) w.addEventListener("popstate", () => (action = "pop", notify()));
163
- const wrapped = (value) => typeof value === "object" && value !== null && typeof value.fx === "string";
164
- const entry = () => {
165
- const raw = w.history.state;
166
- if (wrapped(raw)) return raw;
167
- const keyed = { fx: nextKey(), usr: raw ?? null };
168
- w.history.replaceState(keyed, "");
169
- return keyed;
170
- };
171
- return {
172
- window: w !== void 0,
173
- get() {
174
- if (!w) return { pathname: "/", search: "", hash: "", state: null };
175
- const { pathname, search, hash } = w.location;
176
- const { fx, usr } = entry();
177
- return { pathname, search, hash, state: usr, key: fx, action };
178
- },
179
- push(to, state = null) {
180
- w?.history.pushState({ fx: nextKey(), usr: state }, "", to);
181
- action = "push";
182
- notify();
183
- },
184
- replace(to, state = null) {
185
- w?.history.replaceState({ fx: nextKey(), usr: state }, "", to);
186
- action = "replace";
187
- notify();
188
- },
189
- subscribe(onChange) {
190
- listeners.add(onChange);
191
- return () => listeners.delete(onChange);
192
- }
193
- };
194
- }
195
-
196
- // src/lib/router.ts
197
- function parseQuery(search) {
198
- const q = {};
199
- if (search) {
200
- const usp = new URLSearchParams(
201
- search.startsWith("?") ? search.slice(1) : search
202
- );
203
- usp.forEach((v, k) => q[k] = v);
204
- }
205
- return q;
206
- }
207
- function stripBase(pathname, base) {
208
- if (!base) return pathname || "/";
209
- const b = base.replace(/\/$/, "");
210
- if (pathname === b) return "/";
211
- if (pathname.startsWith(b + "/")) return pathname.slice(b.length) || "/";
212
- return pathname || "/";
213
- }
214
- function withBase(to, base) {
215
- if (!base) return to;
216
- if (/^https?:\/\//.test(to)) return to;
217
- const b = base.replace(/\/$/, "");
218
- return `${b}${to.startsWith("/") ? to : "/" + to}`;
219
- }
220
- function createRouter(config) {
221
- const { routes, history, base } = config;
222
- const listeners = /* @__PURE__ */ new Set();
223
- const readLocation = () => {
224
- const raw = history.get();
225
- return {
226
- pathname: raw.pathname,
227
- search: raw.search,
228
- hash: raw.hash,
229
- state: raw.state,
230
- query: parseQuery(raw.search),
231
- ...raw.key !== void 0 ? { key: raw.key } : {},
232
- ...raw.action !== void 0 ? { action: raw.action } : {}
233
- };
234
- };
235
- const computeMatch = (pathname) => matchRoutes(routes, stripBase(pathname, base));
236
- let current = (() => {
237
- const location = readLocation();
238
- return { location, match: computeMatch(location.pathname) };
239
- })();
240
- const recompute = () => {
241
- const location = readLocation();
242
- current = { location, match: computeMatch(location.pathname) };
243
- listeners.forEach((l) => l(current));
244
- };
245
- const unsubscribeHistory = history.subscribe(recompute);
246
- return {
247
- state() {
248
- return current;
249
- },
250
- subscribe(listener) {
251
- listeners.add(listener);
252
- return () => listeners.delete(listener);
253
- },
254
- navigate(to, options = {}) {
255
- const target = withBase(to, base);
256
- if (options.replace) history.replace(target, options.state ?? null);
257
- else history.push(target, options.state ?? null);
258
- },
259
- match(pathname) {
260
- return computeMatch(pathname);
261
- },
262
- destroy() {
263
- unsubscribeHistory();
264
- listeners.clear();
265
- }
266
- };
267
- }
1
+ /*! @fluixi/router v1.0.0-alpha.109 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
2
+ import {
3
+ createRouter
4
+ } from "./chunk-IGTPZ7EV.mjs";
5
+ import {
6
+ createBrowserHistory,
7
+ createMemoryHistory,
8
+ splitPath
9
+ } from "./chunk-PZG5SWXI.mjs";
10
+ import {
11
+ matchPath,
12
+ matchRoutes,
13
+ rankRoutes,
14
+ scoreRoute
15
+ } from "./chunk-JKWUHPUA.mjs";
268
16
  export {
269
17
  createBrowserHistory,
270
18
  createMemoryHistory,
@@ -1,99 +1,8 @@
1
- // src/lib/history.ts
2
- var keys = 0;
3
- var nextKey = () => `${Date.now().toString(36)}-${(keys++).toString(36)}`;
4
- function splitPath(to) {
5
- let rest = to;
6
- let hash = "";
7
- const hashIdx = rest.indexOf("#");
8
- if (hashIdx !== -1) {
9
- hash = rest.slice(hashIdx);
10
- rest = rest.slice(0, hashIdx);
11
- }
12
- let search = "";
13
- const qIdx = rest.indexOf("?");
14
- if (qIdx !== -1) {
15
- search = rest.slice(qIdx);
16
- rest = rest.slice(0, qIdx);
17
- }
18
- return { pathname: rest || "/", search, hash };
19
- }
20
- function createMemoryHistory(initial = "/") {
21
- let entries = [
22
- { path: initial, state: null, key: nextKey() }
23
- ];
24
- let index = 0;
25
- let action = "pop";
26
- const listeners = /* @__PURE__ */ new Set();
27
- const notify = () => listeners.forEach((l) => l());
28
- return {
29
- get() {
30
- const { path, state, key } = entries[index];
31
- return { ...splitPath(path), state, key, action };
32
- },
33
- push(to, state = null) {
34
- entries = entries.slice(0, index + 1);
35
- entries.push({ path: to, state, key: nextKey() });
36
- index = entries.length - 1;
37
- action = "push";
38
- notify();
39
- },
40
- replace(to, state = null) {
41
- entries[index] = { path: to, state, key: nextKey() };
42
- action = "replace";
43
- notify();
44
- },
45
- /** Back (`-1`) or forward (`1`), as the browser's buttons would. */
46
- go(delta) {
47
- const next = index + delta;
48
- if (next < 0 || next >= entries.length) return;
49
- index = next;
50
- action = "pop";
51
- notify();
52
- },
53
- subscribe(onChange) {
54
- listeners.add(onChange);
55
- return () => listeners.delete(onChange);
56
- }
57
- };
58
- }
59
- function createBrowserHistory() {
60
- const w = typeof window !== "undefined" ? window : void 0;
61
- const listeners = /* @__PURE__ */ new Set();
62
- let action = "pop";
63
- const notify = () => listeners.forEach((l) => l());
64
- if (w) w.addEventListener("popstate", () => (action = "pop", notify()));
65
- const wrapped = (value) => typeof value === "object" && value !== null && typeof value.fx === "string";
66
- const entry = () => {
67
- const raw = w.history.state;
68
- if (wrapped(raw)) return raw;
69
- const keyed = { fx: nextKey(), usr: raw ?? null };
70
- w.history.replaceState(keyed, "");
71
- return keyed;
72
- };
73
- return {
74
- window: w !== void 0,
75
- get() {
76
- if (!w) return { pathname: "/", search: "", hash: "", state: null };
77
- const { pathname, search, hash } = w.location;
78
- const { fx, usr } = entry();
79
- return { pathname, search, hash, state: usr, key: fx, action };
80
- },
81
- push(to, state = null) {
82
- w?.history.pushState({ fx: nextKey(), usr: state }, "", to);
83
- action = "push";
84
- notify();
85
- },
86
- replace(to, state = null) {
87
- w?.history.replaceState({ fx: nextKey(), usr: state }, "", to);
88
- action = "replace";
89
- notify();
90
- },
91
- subscribe(onChange) {
92
- listeners.add(onChange);
93
- return () => listeners.delete(onChange);
94
- }
95
- };
96
- }
1
+ import {
2
+ createBrowserHistory,
3
+ createMemoryHistory,
4
+ splitPath
5
+ } from "../chunk-PZG5SWXI.mjs";
97
6
  export {
98
7
  createBrowserHistory,
99
8
  createMemoryHistory,
@@ -1,98 +1,9 @@
1
- // src/lib/match.ts
2
- function normalizePath(p) {
3
- return p.startsWith("//") ? p.replace(/^\/+/, "/") : p;
4
- }
5
- function matchPath(pattern, path, exact = false) {
6
- const keys = [];
7
- if (pattern === path) return { params: {}, path };
8
- if (!exact && pattern === "/" && path.startsWith("/"))
9
- return { params: {}, path: "/" };
10
- let regexStr = pattern.replace(/:([a-zA-Z0-9_]+)/g, (_, key) => {
11
- keys.push(key);
12
- return "([^/]+)";
13
- }).replace(/\*([a-zA-Z0-9_]*)/g, (_, name) => {
14
- if (name) keys.push(name);
15
- return "(.*)";
16
- });
17
- if (!regexStr.startsWith("/")) regexStr = "/" + regexStr;
18
- if (regexStr === "") regexStr = "/";
19
- const regex = new RegExp(`^${regexStr}${exact ? "$" : "(?=/|$)"}`);
20
- const match = path.match(regex);
21
- if (!match) return null;
22
- const params = {};
23
- keys.forEach((key, i) => {
24
- params[key] = match[i + 1];
25
- });
26
- return { params, path: match[0] };
27
- }
28
- function scoreRoute(def) {
29
- const segments = def.path.split("/").filter(Boolean);
30
- if (segments.length === 0) return 5;
31
- let score = 0;
32
- for (const seg of segments) {
33
- if (seg.startsWith("*")) score += 1;
34
- else if (seg.startsWith(":")) score += 10;
35
- else score += 100;
36
- }
37
- return score;
38
- }
39
- function rankRoutes(defs) {
40
- return [...defs].sort((a, b) => scoreRoute(b) - scoreRoute(a));
41
- }
42
- function matchRoutes(routes, pathname) {
43
- const path = pathname.length > 1 ? pathname.replace(/\/+$/, "") || "/" : pathname;
44
- const result = matchInto(routes, path);
45
- if (!result) return null;
46
- const matched = [];
47
- let cumulative = "";
48
- const params = {};
49
- for (const layer of result.layers) {
50
- cumulative = normalizePath(joinMatched(cumulative, layer.consumed));
51
- const layerParams = {};
52
- for (const [k, v] of Object.entries(layer.params)) {
53
- if (!(k in params)) layerParams[k] = v;
54
- params[k] = v;
55
- }
56
- matched.push({
57
- route: layer.route,
58
- component: layer.route.component,
59
- params: layerParams,
60
- pathname: cumulative || "/"
61
- });
62
- }
63
- return { matched, params, pathname };
64
- }
65
- function matchInto(defs, currentPath) {
66
- if (!defs || defs.length === 0) return null;
67
- const ranked = rankRoutes(defs);
68
- for (const def of ranked) {
69
- const hasChildren = !!def.children && def.children.length > 0;
70
- const seg = matchPath(def.path, currentPath, !hasChildren);
71
- if (!seg) continue;
72
- if (hasChildren) {
73
- const childMatch = matchInto(def.children, currentPath);
74
- if (childMatch) {
75
- return {
76
- layers: [
77
- { route: def, params: seg.params, consumed: seg.path },
78
- ...childMatch.layers
79
- ]
80
- };
81
- }
82
- continue;
83
- }
84
- return { layers: [{ route: def, params: seg.params, consumed: seg.path }] };
85
- }
86
- return null;
87
- }
88
- function joinMatched(cumulative, consumed) {
89
- if (!cumulative) return consumed;
90
- if (consumed === cumulative) return cumulative;
91
- if (consumed.startsWith(cumulative)) return consumed;
92
- const a = cumulative.replace(/\/$/, "");
93
- const b = consumed.replace(/^\//, "");
94
- return `${a}/${b}`;
95
- }
1
+ import {
2
+ matchPath,
3
+ matchRoutes,
4
+ rankRoutes,
5
+ scoreRoute
6
+ } from "../chunk-JKWUHPUA.mjs";
96
7
  export {
97
8
  matchPath,
98
9
  matchRoutes,
@@ -1,189 +1,10 @@
1
- // src/lib/match.ts
2
- function normalizePath(p) {
3
- return p.startsWith("//") ? p.replace(/^\/+/, "/") : p;
4
- }
5
- function matchPath(pattern, path, exact = false) {
6
- const keys = [];
7
- if (pattern === path) return { params: {}, path };
8
- if (!exact && pattern === "/" && path.startsWith("/"))
9
- return { params: {}, path: "/" };
10
- let regexStr = pattern.replace(/:([a-zA-Z0-9_]+)/g, (_, key) => {
11
- keys.push(key);
12
- return "([^/]+)";
13
- }).replace(/\*([a-zA-Z0-9_]*)/g, (_, name) => {
14
- if (name) keys.push(name);
15
- return "(.*)";
16
- });
17
- if (!regexStr.startsWith("/")) regexStr = "/" + regexStr;
18
- if (regexStr === "") regexStr = "/";
19
- const regex = new RegExp(`^${regexStr}${exact ? "$" : "(?=/|$)"}`);
20
- const match = path.match(regex);
21
- if (!match) return null;
22
- const params = {};
23
- keys.forEach((key, i) => {
24
- params[key] = match[i + 1];
25
- });
26
- return { params, path: match[0] };
27
- }
28
- function scoreRoute(def) {
29
- const segments = def.path.split("/").filter(Boolean);
30
- if (segments.length === 0) return 5;
31
- let score = 0;
32
- for (const seg of segments) {
33
- if (seg.startsWith("*")) score += 1;
34
- else if (seg.startsWith(":")) score += 10;
35
- else score += 100;
36
- }
37
- return score;
38
- }
39
- function rankRoutes(defs) {
40
- return [...defs].sort((a, b) => scoreRoute(b) - scoreRoute(a));
41
- }
42
- function matchRoutes(routes, pathname) {
43
- const path = pathname.length > 1 ? pathname.replace(/\/+$/, "") || "/" : pathname;
44
- const result = matchInto(routes, path);
45
- if (!result) return null;
46
- const matched = [];
47
- let cumulative = "";
48
- const params = {};
49
- for (const layer of result.layers) {
50
- cumulative = normalizePath(joinMatched(cumulative, layer.consumed));
51
- const layerParams = {};
52
- for (const [k, v] of Object.entries(layer.params)) {
53
- if (!(k in params)) layerParams[k] = v;
54
- params[k] = v;
55
- }
56
- matched.push({
57
- route: layer.route,
58
- component: layer.route.component,
59
- params: layerParams,
60
- pathname: cumulative || "/"
61
- });
62
- }
63
- return { matched, params, pathname };
64
- }
65
- function matchInto(defs, currentPath) {
66
- if (!defs || defs.length === 0) return null;
67
- const ranked = rankRoutes(defs);
68
- for (const def of ranked) {
69
- const hasChildren = !!def.children && def.children.length > 0;
70
- const seg = matchPath(def.path, currentPath, !hasChildren);
71
- if (!seg) continue;
72
- if (hasChildren) {
73
- const childMatch = matchInto(def.children, currentPath);
74
- if (childMatch) {
75
- return {
76
- layers: [
77
- { route: def, params: seg.params, consumed: seg.path },
78
- ...childMatch.layers
79
- ]
80
- };
81
- }
82
- continue;
83
- }
84
- return { layers: [{ route: def, params: seg.params, consumed: seg.path }] };
85
- }
86
- return null;
87
- }
88
- function joinMatched(cumulative, consumed) {
89
- if (!cumulative) return consumed;
90
- if (consumed === cumulative) return cumulative;
91
- if (consumed.startsWith(cumulative)) return consumed;
92
- const a = cumulative.replace(/\/$/, "");
93
- const b = consumed.replace(/^\//, "");
94
- return `${a}/${b}`;
95
- }
96
-
97
- // src/lib/history.ts
98
- function splitPath(to) {
99
- let rest = to;
100
- let hash = "";
101
- const hashIdx = rest.indexOf("#");
102
- if (hashIdx !== -1) {
103
- hash = rest.slice(hashIdx);
104
- rest = rest.slice(0, hashIdx);
105
- }
106
- let search = "";
107
- const qIdx = rest.indexOf("?");
108
- if (qIdx !== -1) {
109
- search = rest.slice(qIdx);
110
- rest = rest.slice(0, qIdx);
111
- }
112
- return { pathname: rest || "/", search, hash };
113
- }
114
-
115
- // src/lib/router.ts
116
- function parseQuery(search) {
117
- const q = {};
118
- if (search) {
119
- const usp = new URLSearchParams(
120
- search.startsWith("?") ? search.slice(1) : search
121
- );
122
- usp.forEach((v, k) => q[k] = v);
123
- }
124
- return q;
125
- }
126
- function stripBase(pathname, base) {
127
- if (!base) return pathname || "/";
128
- const b = base.replace(/\/$/, "");
129
- if (pathname === b) return "/";
130
- if (pathname.startsWith(b + "/")) return pathname.slice(b.length) || "/";
131
- return pathname || "/";
132
- }
133
- function withBase(to, base) {
134
- if (!base) return to;
135
- if (/^https?:\/\//.test(to)) return to;
136
- const b = base.replace(/\/$/, "");
137
- return `${b}${to.startsWith("/") ? to : "/" + to}`;
138
- }
139
- function createRouter(config) {
140
- const { routes, history, base } = config;
141
- const listeners = /* @__PURE__ */ new Set();
142
- const readLocation = () => {
143
- const raw = history.get();
144
- return {
145
- pathname: raw.pathname,
146
- search: raw.search,
147
- hash: raw.hash,
148
- state: raw.state,
149
- query: parseQuery(raw.search),
150
- ...raw.key !== void 0 ? { key: raw.key } : {},
151
- ...raw.action !== void 0 ? { action: raw.action } : {}
152
- };
153
- };
154
- const computeMatch = (pathname) => matchRoutes(routes, stripBase(pathname, base));
155
- let current = (() => {
156
- const location = readLocation();
157
- return { location, match: computeMatch(location.pathname) };
158
- })();
159
- const recompute = () => {
160
- const location = readLocation();
161
- current = { location, match: computeMatch(location.pathname) };
162
- listeners.forEach((l) => l(current));
163
- };
164
- const unsubscribeHistory = history.subscribe(recompute);
165
- return {
166
- state() {
167
- return current;
168
- },
169
- subscribe(listener) {
170
- listeners.add(listener);
171
- return () => listeners.delete(listener);
172
- },
173
- navigate(to, options = {}) {
174
- const target = withBase(to, base);
175
- if (options.replace) history.replace(target, options.state ?? null);
176
- else history.push(target, options.state ?? null);
177
- },
178
- match(pathname) {
179
- return computeMatch(pathname);
180
- },
181
- destroy() {
182
- unsubscribeHistory();
183
- listeners.clear();
184
- }
185
- };
186
- }
1
+ import {
2
+ createRouter
3
+ } from "../chunk-IGTPZ7EV.mjs";
4
+ import {
5
+ splitPath
6
+ } from "../chunk-PZG5SWXI.mjs";
7
+ import "../chunk-JKWUHPUA.mjs";
187
8
  export {
188
9
  createRouter,
189
10
  splitPath
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fluixi/router",
3
- "version": "1.0.0-alpha.107",
3
+ "version": "1.0.0-alpha.109",
4
4
  "description": "Framework-agnostic router core for Fluixi — URL to a resolved match tree + navigation.",
5
5
  "license": "MIT",
6
6
  "author": "Ibrahima Touré",
@@ -33,7 +33,7 @@
33
33
  }
34
34
  },
35
35
  "devDependencies": {
36
- "@bambiste/mix-builder": "^0.4.0",
36
+ "@bambiste/mix-builder": "^0.4.3",
37
37
  "cross-env": "^7.0.3"
38
38
  },
39
39
  "scripts": {