{"id":13475298,"url":"https://github.com/choojs/wayfarer","last_synced_at":"2025-03-26T23:30:42.360Z","repository":{"id":17859398,"uuid":"20784957","full_name":"choojs/wayfarer","owner":"choojs","description":":eyeglasses: composable trie based router","archived":false,"fork":false,"pushed_at":"2020-03-21T13:18:22.000Z","size":126,"stargazers_count":332,"open_issues_count":2,"forks_count":31,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-10-30T08:51:35.438Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/choojs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-06-12T22:31:00.000Z","updated_at":"2024-06-03T18:20:10.000Z","dependencies_parsed_at":"2022-09-24T19:20:23.088Z","dependency_job_id":null,"html_url":"https://github.com/choojs/wayfarer","commit_stats":null,"previous_names":[],"tags_count":50,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/choojs%2Fwayfarer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/choojs%2Fwayfarer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/choojs%2Fwayfarer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/choojs%2Fwayfarer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/choojs","download_url":"https://codeload.github.com/choojs/wayfarer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245753836,"owners_count":20666822,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T16:01:19.180Z","updated_at":"2025-03-26T23:30:42.022Z","avatar_url":"https://github.com/choojs.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# wayfarer [![stability][0]][1]\n[![npm version][2]][3] [![build status][4]][5] [![test coverage][6]][7]\n[![downloads][8]][9] [![js-standard-style][10]][11]\n\nComposable [trie based](https://en.wikipedia.org/wiki/Trie) router.  It's\nfaster than traditional, linear, regular expression-matching routers, although\ninsignficantly, and scales with the number of routes.\n\nIf you're looking for a client-side router check out\n[sheet-router](https://github.com/yoshuawuyts/sheet-router). If you're looking\nfor a server router check out\n[server-router](https://github.com/yoshuawuyts/server-router).\n\n### features\n- works with any framework\n- built for speed\n- minimal dependencies\n- extensible\n\n## Installation\n```sh\n$ npm install wayfarer\n```\n\n## Usage\n```js\nvar wayfarer = require('wayfarer')\n\nvar router = wayfarer('/404')\n\nrouter.on('/', () =\u003e console.log('/'))\nrouter.on('/404', () =\u003e console.log('404 not found'))\nrouter.on('/:user', (params) =\u003e console.log('user is %s', params.user))\nrouter.on('/wildcard/*', (params) =\u003e console.log('wildcard path is %s', params.wildcard))\n\nrouter('tobi')\n// =\u003e 'user is tobi'\n\nrouter('/uh/oh')\n// =\u003e '404 not found'\n\nrouter('/wildcard/example/path')\n// =\u003e 'wildcard path is example/path'\n```\n\n## Subrouting\nRouters can be infinitely nested, allowing routing to be scoped per view.\nMatched params are passed into subrouters. Nested routes will call their\nparent's default handler if no path matches.\n```js\nvar r1 = wayfarer()\nvar r2 = wayfarer()\n\nr2.on('/child', () =\u003e console.log('subrouter trix!'))\nr1.on('/:parent', r2)\n\nr1('/dada/child')\n// =\u003e 'subrouter trix!'\n```\n\n## Walk\nSometimes it's necessary to walk the `trie` to apply transformations.\n```js\nvar walk = require('wayfarer/walk')\nvar wayfarer = require('wayfarer')\n\nvar router = wayfarer()\nrouter.on('/multiply', (x, y) =\u003e x * y)\nrouter.on('/divide', (x, y) =\u003e x / y)\n\nwalk(router, (route, cb) =\u003e {\n  var y = 2\n  return function (params, x) {\n    return cb(x, y)\n  }\n})\n\nrouter('/multiply', 4)\n// =\u003e 8\nrouter('/divide', 8)\n// =\u003e 4\n```\n\n## API\n### router = wayfarer(default)\nInitialize a router with a default route. Doesn't ignore querystrings and\nhashes.\n\n### router.on(route, cb(params, [arg1, ...]))\nRegister a new route. The order in which routes are registered does not matter.\nRoutes can register multiple callbacks. The callback can return values when\ncalled.\n\n### matchedRoute = router.match(route)\nMatches a route and returns an object. The returned object contains the properties `{cb, params, route}`. This method does not invoke the callback of a route. If no route matches, the default route will be returned. If no default route matches, an error will be thrown.\n\n### val = router(route, [arg1, ...])\nMatch a route and execute the corresponding callback. Alias: `router.emit()`.\nReturns a values from the matched route (e.g. useful for streams). Any\nadditional values will be passed to the matched route.\n\n## Internals\nWayfarer is built on an internal trie structure. If you want to build a router\nusing this trie structure it can be accessed through\n`require('wayfarer/trie')`. It exposes the following methods:\n- `trie = Trie()` - create a new trie\n- `node = trie.create(route)` - create a node at a path, and return a node\n- `node = trie.match(route)` - match a route on the the trie and return a node\n- `trie.mount(path, trie)` - mount a trie onto a node at route\n\n## Known issues\n### multiple nested partials don't match\nE.g. `/:foo/:bar/:baz` won't work. This is due `Trie.mount()` overriding child\npartial paths when mounted. I'll get around to fixing this at some point in the\nfuture, but if anyone wants to contribute a patch it'd most appreciated.\n\n## FAQ\n### Why did you build this?\nRouters like [react-router](https://github.com/rackt/react-router) are\ncomplicated solutions for a simple problem. In reality all that's needed is a\n[methodless](http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html) router\nthat can define + match paths and can mount other routers to delegate requests.\n\n### Why isn't my route matching?\nWayfarer only compares strings. Before you pass in an url you probably want to\nstrip it of querystrings and hashes using the\n[pathname-match](https://github.com/yoshuawuyts/pathname-match) module.\n\n## See Also\n- [sheet-router](https://github.com/yoshuawuyts/sheet-router) - fast, modular\n  client-side router\n- [server-router](https://github.com/yoshuawuyts/server-router) - server router\n- [hash-match](https://github.com/sethvincent/hash-match) - easy\n  `window.location.hash` matching\n- [pathname-match](https://github.com/yoshuawuyts/pathname-match) - strip\n  querystrings and hashes from a url\n- [wayfarer-to-server](https://github.com/yoshuawuyts/wayfarer-to-server) -\n  Wrap wayfarer to provide HTTP method matching and `req, res` delegation\n\n## License\n[MIT](https://tldrlegal.com/license/mit-license)\n\n[0]: https://img.shields.io/badge/stability-2%20stable-brightgreen.svg?style=flat-square\n[1]: https://nodejs.org/api/documentation.html#documentation_stability_index\n[2]: https://img.shields.io/npm/v/wayfarer.svg?style=flat-square\n[3]: https://npmjs.org/package/wayfarer\n[4]: https://img.shields.io/travis/choojs/wayfarer/master.svg?style=flat-square\n[5]: https://travis-ci.org/choojs/wayfarer\n[6]: https://img.shields.io/codecov/c/github/choojs/wayfarer/master.svg?style=flat-square\n[7]: https://codecov.io/github/choojs/wayfarer\n[8]: http://img.shields.io/npm/dm/wayfarer.svg?style=flat-square\n[9]: https://npmjs.org/package/wayfarer\n[10]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square\n[11]: https://github.com/feross/standard\n[12]: http://github.com/raynos/mercury\n[13]: http://github.com/raynos/virtual-dom\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchoojs%2Fwayfarer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchoojs%2Fwayfarer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchoojs%2Fwayfarer/lists"}