Skip to content

Commit 4b7f304

Browse files
committed
feat(urlMatcher): add support for optional spaces
- Adds support for optional spaces in url param typing, i.e. `url: 'foo/{bar: int}'`
1 parent 9d2e57f commit 4b7f304

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/urlMatcherFactory.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ function UrlMatcher(pattern, config, parentMatcher) {
7676
// The regular expression is somewhat complicated due to the need to allow curly braces
7777
// inside the regular expression. The placeholder regexp breaks down as follows:
7878
// ([:*])([\w\[\]]+) - classic placeholder ($1 / $2) (search version has - for snake-case)
79-
// \{([\w\[\]]+)(?:\:( ... ))?\} - curly brace placeholder ($3) with optional regexp/type ... ($4) (search version has - for snake-case
79+
// \{([\w\[\]]+)(?:\:\s*( ... ))?\} - curly brace placeholder ($3) with optional regexp/type ... ($4) (search version has - for snake-case
8080
// (?: ... | ... | ... )+ - the regexp consists of any number of atoms, an atom being either
8181
// [^{}\\]+ - anything other than curly braces or backslash
8282
// \\. - a backslash escape
8383
// \{(?:[^{}\\]+|\\.)*\} - a matched set of curly braces containing other atoms
84-
var placeholder = /([:*])([\w\[\]]+)|\{([\w\[\]]+)(?:\:((?:[^{}\\]+|\\.|\{(?:[^{}\\]+|\\.)*\})+))?\}/g,
85-
searchPlaceholder = /([:]?)([\w\[\]-]+)|\{([\w\[\]-]+)(?:\:((?:[^{}\\]+|\\.|\{(?:[^{}\\]+|\\.)*\})+))?\}/g,
84+
var placeholder = /([:*])([\w\[\]]+)|\{([\w\[\]]+)(?:\:\s*((?:[^{}\\]+|\\.|\{(?:[^{}\\]+|\\.)*\})+))?\}/g,
85+
searchPlaceholder = /([:]?)([\w\[\]-]+)|\{([\w\[\]-]+)(?:\:\s*((?:[^{}\\]+|\\.|\{(?:[^{}\\]+|\\.)*\})+))?\}/g,
8686
compiled = '^', last = 0, m,
8787
segments = this.segments = [],
8888
parentParams = parentMatcher ? parentMatcher.params : {},

test/urlMatcherFactorySpec.js

+6
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,12 @@ describe("urlMatcherFactory", function () {
520520
expect(m.format({ foo: 5, flag: true })).toBe("/5/1");
521521
});
522522

523+
it("should match built-in types with spaces", function () {
524+
var m = new UrlMatcher("/{foo: int}/{flag: bool}");
525+
expect(m.exec("/1138/1")).toEqual({ foo: 1138, flag: true });
526+
expect(m.format({ foo: 5, flag: true })).toBe("/5/1");
527+
});
528+
523529
it("should match types named only in params", function () {
524530
var m = new UrlMatcher("/{foo}/{flag}", {
525531
params: {

0 commit comments

Comments
 (0)