shawnbot/hashable — pull request #8 by shawnbot (2014-05-23 01:05:31+00:00)
Title: add hashable.router()

The hashable.router object is useful for defining multiple valid URL formats, and can either be used on its own (via the `format()` and `parse()` methods, or in conjunction with hashable.hash:

```js
var router = hashable.router()
.add("widgets/{widget}")
.add("widgets/{widget}/{size}")
.add("widgets/{widget}/{size}/detail", {detail: true});
router.parse("widgets/foo"); // {widget: "foo"}
router.parse("widgets/foo/small"); // {widget: "foo", size: "small"}
router.parse("widgets/foo/small/detail"); // {widget: "foo", size: "small", detail: true}
router.format({widget: "foo"}); // "widgets/foo"
router.format({widget: "foo", size: "medium"}); // "widgets/foo/medium"
router.format({widget: "foo", size: "medium", detail: true}); // "widgets/foo/medium/detail"
```

This is experimental, and has some side effects in hashable.format(), namely that the introduction of a "strict" mode means that a formatter will return `null` if any of the data keys in its parsed format is missing (null, undefined, or empty) from the provided data:

```js
hashable.format("foo/{bar}")({}) === null
hashable.format("foo/{bar}")({bar: null}) === null
hashable.format("foo/{bar}")({bar: ""}) === null
```

Strict mode might be settable later, but I don't see any reason why you'd want to format data with undefined, null or empty keys.