Skip to content

Commit

Permalink
feat(urlMatcher): add support for optional spaces
Browse files Browse the repository at this point in the history
- Adds support for optional spaces in url param typing, i.e. `url:
  'foo/{bar: int}'`
  • Loading branch information
wesleycho committed Dec 1, 2015
1 parent 9d2e57f commit 4b7f304
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/urlMatcherFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ function UrlMatcher(pattern, config, parentMatcher) {
// The regular expression is somewhat complicated due to the need to allow curly braces
// inside the regular expression. The placeholder regexp breaks down as follows:
// ([:*])([\w\[\]]+) - classic placeholder ($1 / $2) (search version has - for snake-case)
// \{([\w\[\]]+)(?:\:( ... ))?\} - curly brace placeholder ($3) with optional regexp/type ... ($4) (search version has - for snake-case
// \{([\w\[\]]+)(?:\:\s*( ... ))?\} - curly brace placeholder ($3) with optional regexp/type ... ($4) (search version has - for snake-case
// (?: ... | ... | ... )+ - the regexp consists of any number of atoms, an atom being either
// [^{}\\]+ - anything other than curly braces or backslash
// \\. - a backslash escape
// \{(?:[^{}\\]+|\\.)*\} - a matched set of curly braces containing other atoms
var placeholder = /([:*])([\w\[\]]+)|\{([\w\[\]]+)(?:\:((?:[^{}\\]+|\\.|\{(?:[^{}\\]+|\\.)*\})+))?\}/g,
searchPlaceholder = /([:]?)([\w\[\]-]+)|\{([\w\[\]-]+)(?:\:((?:[^{}\\]+|\\.|\{(?:[^{}\\]+|\\.)*\})+))?\}/g,
var placeholder = /([:*])([\w\[\]]+)|\{([\w\[\]]+)(?:\:\s*((?:[^{}\\]+|\\.|\{(?:[^{}\\]+|\\.)*\})+))?\}/g,
searchPlaceholder = /([:]?)([\w\[\]-]+)|\{([\w\[\]-]+)(?:\:\s*((?:[^{}\\]+|\\.|\{(?:[^{}\\]+|\\.)*\})+))?\}/g,
compiled = '^', last = 0, m,
segments = this.segments = [],
parentParams = parentMatcher ? parentMatcher.params : {},
Expand Down
6 changes: 6 additions & 0 deletions test/urlMatcherFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,12 @@ describe("urlMatcherFactory", function () {
expect(m.format({ foo: 5, flag: true })).toBe("/5/1");
});

it("should match built-in types with spaces", function () {
var m = new UrlMatcher("/{foo: int}/{flag: bool}");
expect(m.exec("/1138/1")).toEqual({ foo: 1138, flag: true });
expect(m.format({ foo: 5, flag: true })).toBe("/5/1");
});

it("should match types named only in params", function () {
var m = new UrlMatcher("/{foo}/{flag}", {
params: {
Expand Down

0 comments on commit 4b7f304

Please sign in to comment.