-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllms.txt
More file actions
29 lines (23 loc) · 1.41 KB
/
Copy pathllms.txt
File metadata and controls
29 lines (23 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# @ristellise/tru.js
This library is a Tiny Router Utility (tru.mjs) for vanilla JavaScript Single Page Applications (SPAs).
## Key Philosophies
1. Zero dependencies.
2. ES Modules ONLY (`.mjs`). There is no CommonJS/legacy build.
3. Uses the native browser `URLPattern` API for route matching instead of custom Regular Expressions.
4. Uses a global click interceptor on `<a>` tags to handle routing without requiring custom link components.
## How the router works
It exports a factory function `createRouter()`.
Routes are defined using the `.add(path, callback)` method.
The `path` accepts standard URLPattern syntax, including named parameters like `/users/:id` and wildcards like `*`.
The `callback` receives an object containing the extracted named parameters.
The router safely intercepts link clicks. It checks if the `href` matches a registered route via `URLPattern.test()`. If it does, it prevents default browser navigation and uses `history.pushState()`. If it does not, or if the link has `target="_blank"`, `download`, or `data-bypass` attributes, the router ignores the click and allows native browser behavior.
## Example usage for AI context
```javascript
import { createRouter } from '@ristellise/tru.js';
const router = createRouter();
router
.add('/', () => console.log('Home'))
.add('/posts/:id', (params) => console.log('Post ID:', params.id))
.add('*', () => console.log('404 Not Found'))
.start();
```