Skip to content

Commit 67dc2fd

Browse files
committed
Match against encoded value when filling tokens
Forces the behavior to be consistent with path matching logic which does match against encoded paths. This is essentially the reverse. Closes pillarjs#58
1 parent c1cdfcc commit 67dc2fd

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

index.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ function tokensToFunction (tokens) {
132132
}
133133

134134
var value = data[token.name]
135+
var segment
135136

136137
if (value == null) {
137138
if (token.optional) {
@@ -143,7 +144,7 @@ function tokensToFunction (tokens) {
143144

144145
if (isarray(value)) {
145146
if (!token.repeat) {
146-
throw new TypeError('Expected "' + token.name + '" to not repeat')
147+
throw new TypeError('Expected "' + token.name + '" to not repeat, but received "' + value + '"')
147148
}
148149

149150
if (value.length === 0) {
@@ -155,21 +156,25 @@ function tokensToFunction (tokens) {
155156
}
156157

157158
for (var j = 0; j < value.length; j++) {
158-
if (!matches[i].test(value[j])) {
159-
throw new TypeError('Expected all "' + token.name + '" to match "' + token.pattern + '"')
159+
segment = encodeURIComponent(value[j])
160+
161+
if (!matches[i].test(segment)) {
162+
throw new TypeError('Expected all "' + token.name + '" to match "' + token.pattern + '", but received "' + segment + '"')
160163
}
161164

162-
path += (j === 0 ? token.prefix : token.delimiter) + encodeURIComponent(value[j])
165+
path += (j === 0 ? token.prefix : token.delimiter) + segment
163166
}
164167

165168
continue
166169
}
167170

168-
if (!matches[i].test(value)) {
169-
throw new TypeError('Expected "' + token.name + '" to match "' + token.pattern + '"')
171+
segment = encodeURIComponent(value)
172+
173+
if (!matches[i].test(segment)) {
174+
throw new TypeError('Expected "' + token.name + '" to match "' + token.pattern + '", but received "' + segment + '"')
170175
}
171176

172-
path += token.prefix + encodeURIComponent(value)
177+
path += token.prefix + segment
173178
}
174179

175180
return path

test.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,12 @@ var TESTS = [
384384
['/route', ['/route', 'route']],
385385
['/another', ['/another', 'another']],
386386
['/something/else', null],
387-
['/route.json', ['/route.json', 'route.json']]
387+
['/route.json', ['/route.json', 'route.json']],
388+
['/something%2Felse', ['/something%2Felse', 'something%2Felse']]
388389
],
389390
[
390-
[{ test: 'route' }, '/route']
391+
[{ test: 'route' }, '/route'],
392+
[{ test: 'something/else' }, '/something%2Felse']
391393
]
392394
],
393395
[

0 commit comments

Comments
 (0)