Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

canals

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

canals

URL-source agnostic routing

  • 0.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Canals: pure URL routing

Things Canals does:

  • Maps URL fragments to params and arbitrary JS objects
  • Parses query params

Things Canals doesn't do:

  • Listen for URL changes
  • Construct URLs
  • Know anything about pushState

The limited scope of its responsibilities make it useful on the server and client! It also makes it really nice to write tests against, because it's all pure functions.

If type signatures appeal to you, Canals.compile works roughly like so:

Canals.compile :: {route: A, ...} -> (url) -> ({args: [arg1, arg2, ...],
                                                query: {queryParam: queryVal, ...},
                                                value: A})

Usage:

var route = Canals.compile({
    "posts/:postId": function(postId) { ... }
    "posts/:postId/comments/:commentId": function(postId, commendId) { ...  },
    "download/*path": function(path) { ... },
    "edit(/:tab)": function(tab) { ... }
    "about": "about",
    "": "home"
});

// Note that Canals doesn't *execute* the callback. That's up to you to do, 
// if you wish.

route("");
// {args: [], query: {}, value: "home"}

route("about");
// {args: [], query: {}, value: "about"}

route("about?lang=fr&v=2");
// {args: [], query: {"lang": "fr", "v": "2"}, value: "about"}

route("posts/1");
// {args: ["1"], query: {}, value: function(postId) { ... }}

route("posts/1/comments/3");
// {args: ["1", "3"], query: {}, value: function(postId, commentId) { ... }}

route("download/foo/bar/baz?lang=fr");
// {args: ["foo/bar/baz"], query: {"lang": "fr"},
//  value: function(postId, commentId) { ...  }}

route("edit");
// {args: [null], query: {}, value: function(tab) { ... }}

route("edit/preview");
// {args: ["preview"], query: {}, value: function(tab) { ... }}

route("upload");  // No route defined for "upload"
// null

The syntax for specifying routes was ripped directly from Backbone.Router.

Keywords

FAQs

Package last updated on 19 Oct 2015

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc