Skip to content

Commit 1044013

Browse files
committed
Add more README examples for compilation of path
1 parent 67dc2fd commit 1044013

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

Readme.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,20 @@ Path-To-RegExp exposes a compile function for transforming an express path into
167167
```js
168168
var toPath = pathToRegexp.compile('/user/:id')
169169

170-
var result = toPath({ id: 123 })
170+
toPath({ id: 123 }) //=> "/user/123"
171+
toPath({ id: 'café' }) //=> "/user/caf%C3%A9"
172+
toPath({ id: '/' }) //=> "%2F"
171173

172-
console.log(result)
173-
//=> "/user/123"
174+
var toPathRepeated = pathToRegexp.compile('/:segment+')
175+
176+
toPathRepeated({ segment: 'foo' }) //=> "/foo"
177+
toPathRepeated({ segment: ['a', 'b', 'c'] }) //=> "/a/b/c"
178+
179+
var toPathRegexp = pathToRegexp.compile('/user/:id(\\d+)')
180+
181+
toPathRegexp({ id: 123 }) //=> "/user/123"
182+
toPathRegexp({ id: '123' }) //=> "/user/123"
183+
toPathRegexp({ id: 'abc' }) //=> throws TypeError
174184
```
175185

176186
**Note:** The generated function will throw on any invalid input. It will execute all necessary checks to ensure the generated path is valid. This method only works with strings.

0 commit comments

Comments
 (0)