Skip to content

Commit 4bfd405

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 4398690 commit 4bfd405

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/url/urlMatcher.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,13 @@ export default class UrlMatcher {
106106
// The regular expression is somewhat complicated due to the need to allow curly braces
107107
// inside the regular expression. The placeholder regexp breaks down as follows:
108108
// ([:*])([\w\[\]]+) - classic placeholder ($1 / $2) (search version has - for snake-case)
109-
// \{([\w\[\]]+)(?:\:( ... ))?\} - curly brace placeholder ($3) with optional regexp/type ... ($4) (search version has - for snake-case
109+
// \{([\w\[\]]+)(?:\:\s*( ... ))?\} - curly brace placeholder ($3) with optional regexp/type ... ($4) (search version has - for snake-case
110110
// (?: ... | ... | ... )+ - the regexp consists of any number of atoms, an atom being either
111111
// [^{}\\]+ - anything other than curly braces or backslash
112112
// \\. - a backslash escape
113113
// \{(?:[^{}\\]+|\\.)*\} - a matched set of curly braces containing other atoms
114-
var placeholder = /([:*])([\w\[\]]+)|\{([\w\[\]]+)(?:\:((?:[^{}\\]+|\\.|\{(?:[^{}\\]+|\\.)*\})+))?\}/g,
115-
searchPlaceholder = /([:]?)([\w\[\]-]+)|\{([\w\[\]-]+)(?:\:((?:[^{}\\]+|\\.|\{(?:[^{}\\]+|\\.)*\})+))?\}/g,
114+
var placeholder = /([:*])([\w\[\]]+)|\{([\w\[\]]+)(?:\:\s*((?:[^{}\\]+|\\.|\{(?:[^{}\\]+|\\.)*\})+))?\}/g,
115+
searchPlaceholder = /([:]?)([\w\[\]-]+)|\{([\w\[\]-]+)(?:\:\s*((?:[^{}\\]+|\\.|\{(?:[^{}\\]+|\\.)*\})+))?\}/g,
116116
last = 0, m, patterns = [];
117117

118118
const checkParamErrors = (id) => {

test/urlMatcherFactorySpec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,12 @@ describe("urlMatcherFactory", function () {
546546
expect(m.format({ foo: 5, flag: true })).toBe("/5/1");
547547
});
548548

549+
it("should match built-in types with spaces", function () {
550+
var m = new UrlMatcher("/{foo: int}/{flag: bool}");
551+
expect(m.exec("/1138/1")).toEqual({ foo: 1138, flag: true });
552+
expect(m.format({ foo: 5, flag: true })).toBe("/5/1");
553+
});
554+
549555
it("should match types named only in params", function () {
550556
var m = new UrlMatcher("/{foo}/{flag}", {
551557
params: {

0 commit comments

Comments
 (0)