![PyPI Now Supports iOS and Android Wheels for Mobile Python Development](https://cdn.sanity.io/images/cgdhsj6q/production/96416c872705517a6a65ad9646ce3e7caef623a0-1024x1024.webp?w=400&fit=max&auto=format)
Security News
PyPI Now Supports iOS and Android Wheels for Mobile Python Development
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
sheet-router
Advanced tools
sheet-router is a fast, modular client-side router. It enables view composition
and is tuned for performance by statically declaring routes in a
radix-trie. Weighs 1.5KB
minified and gzipped.
$ npm install sheet-router
<a href="">
linkssheet-router tries to make routing understandable and pleasant to work with. It does so by using a lisp-like structure which is internally compiled to an efficient data structure. Here each route takes either an array of children or a callback, which are then translated to paths that take callbacks
const sheetRouter = require('sheet-router')
const html = require('bel')
// default to `/404` if no path matches
const router = sheetRouter({ default: '/404' }, [
['/', (params) => html`<div>Welcome to router land!</div>`],
['/:username', (params) => html`<div>${params.username}</div>`, [
['/orgs', (params) => html`<div>${params.username}'s orgs!</div>`]
]],
['/404', (params) => html`<div>Oh no, path not found!</div>`],
])
router('/hughsk/orgs')
Interacting with the browser history is a common action, sheet-router
supports this out of the box. When the forwards
or backwards
buttons in the
browser are clicked, or history.back
/ history.go
are called sheet-router
will update accordingly.
const history = require('sheet-router/history')
history(function (href) {
router(href)
console.log('history changed: ' + href)
})
Interacting with hash changes is often a common fallback scenario for those who
don't have support for browser history. Whenever a hashchange
event is
triggered, sheet-router will trigger an update as seen below. However in order
to match hash prefixed routes, the hash-match
module can be used to normalize
routes (ex: #/foo
becomes /foo
).
const hash = require('sheet-router/hash')
const match = require('hash-match')
hash(function (href) {
router(match(href))
console.log('hash location changed: ' + href)
})
In HTML links are represented with <a href="">
style tags. Sheet-router can
be smart about these and handle them globally. This way there's no need to
attach specific listeners to each link and static HTML templates can be
upgraded seemlessly to include single-page routing.
const href = require('sheet-router/href')
href(function (href) {
router(href)
console.log('link was clicked: ' + href)
})
You can ignore specific links that you do not want to process through routing
by adding the data-no-routing
attribute.
<a href="/my-external-link" data-no-routing>Non routed link</a>
<a href="/another-external-link" data-no-routing="true">Not routed either</a>
Also, if you pass an optional root
node reference as a second argument to href
, it will never intercept clicks outside that node. This is useful when your app is confined to a widget in a larger document.
href(function (href) {
router(href)
console.log('link was clicked: ' + href)
}, document.getElementById("app-root"))
Sometimes query strings must be decoded. In order to do this, the
./qs.js
file is included.
const qs = require('./qs')
qs('https://www.npmjs.com/search?q=query+string')
// => { q: 'query+string' }
Sometimes it's necessary to walk the trie
to apply transformations. In order
to access the raw callback and prevent unnecessary function calls we need to
disable the default thunking mechanism by passing { thunk: false }
:
const sheetRouter = require('sheet-router')
const walk = require('sheet-router/walk')
const router = sheetRouter({ thunk: false }, [
['/multiply', (x, y) => x * y],
['/divide', (x, y) => x / y]
])
walk(router, (route, cb) => {
const y = 2
return function (params, x) {
return cb(x, y)
}
})
router('/multiply', 4)
// => 8
router('/divide', 8)
// => 4
We could change our transformed function to be thunked by changing walk
to
return a function, and setting { thunk: 'match' }
so only the match
function thunks. This is pretty advanced stuff, so don't sweat it too much -
but it's super useful to create performant frameworks!
const router = sheetRouter({ thunk: 'match' }, [
['/foo', (x, y) => x * y],
['/bar', (x, y) => x / y]
])
walk(router, (route, cb) => {
const y = 2
return function (params) {
return function (x) {
return cb(x, y)
}
}
})
router('/multiply', 4)
// => 8
router('/multiply', 4)
// => 8 (but this time around this is computed faster)
router('/divide', 8)
// => 4
Sometimes you want to mirror the browser location API inside an object to use
inside a framework. The hard part is to compute the new href
from a set of
changes. create-location
provides an API to do just that:
const createLocation = require('sheet-router/create-location')
document.location = '/foo/bar#hey?beep=boop'
var location = createLocation()
// => {
// pathname: '/',
// hash: '#hey',
// search: { beep: 'boop' },
// href: '/foo/bar#hey?beep=boop'
// }
const hashPatch = { hash: '#oh-no' }
var location = createLocation(location, hashPatch)
// => {
// pathname: '/',
// hash: '#oh-no',
// search: { beep: 'boop' },
// href: '/foo/bar#oh-no?beep=boop'
// }
const uriPatch = '/hey/hello'
var location = createLocation(location, uriPatch)
// => {
// pathname: '/hey/hello',
// hash: '',
// search: { },
// href: '/hey/hello'
// }
const render = require('virtual-dom/create-element')
const sheetRouter = require('sheet-router')
const h = require('virtual-dom/h')
const hyperx = require('hyperx')
const html = hyperx(h)
const router = sheetRouter([
['/foo/bar', (params, h, state) => html`<div>hello world!</div>`]
])
const node = render(router('/foo/bar', h, { name: 'Jane' }))
document.body.appendChild(node)
<body>
<div>hello world</div>
</body>
const sheetRouter = require('sheet-router')
const render = require('react-dom')
const hyperx = require('hyperx')
const react = require('react')
const html = hyperx(react.createElement)
const router = sheetRouter([
['/foo/bar', (params, h, state) => html`<div>hello world!</div>`]
])
render(router('/foo', react.createElement, { name: 'Jane' }), document.body)
<body>
<div>hello world</div>
</body>
Create a new router from a nested array. Takes an optional options object as the first argument. Options are:
'/404'
, default path to use if no paths matchtrue
. Toggle if callbacks should be thunked or
not. Can be set to 'match'
to only have the returned router.match()
function expect thunks to exist. Useful to write a custom walk
function
that creates a different signatureMatch a route on the router. Takes a path and an arbitrary list of arguments that are then passed to the matched routes. Cleans urls to only match the pathname.
Call a callback to handle html5 pushsState history.
Call a callback to handle <a href="#">
clicks.
FAQs
Fast, modular client router
The npm package sheet-router receives a total of 28 weekly downloads. As such, sheet-router popularity was classified as not popular.
We found that sheet-router demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.