Template string from regex

Spack

Let's say that I have the following URL path:

/api/resource/:id

Given the context object {id: 1} the path will be transformed to /api/resource/1.

Now I want to build a more complex path template:

/api/resource/:id(/relationships/:name)?

Here the relationships member is optional so that the both contexts can match:

  • {id: 1, name: 'rel1'} will build the string /api/resource/1/relationships/rel1.
  • {id: 1} should still build the string /api/resource/1.

I would like to be able to build such strings using a limited set of the regular expressions magic.

How to implement such a template engine?

Spack

Actually, I've found the path-to-regex that does exactly what I want.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related