DEPRECATION WARNING
This module have been renamed to patterns and you should use that instead!
match-patterns
Match a string against a list of patterns.
Installation
npm install match-patterns
Example usage
Even though this module is a generic pattern matcher, it's well suited for HTTP route matching:
var http = ;var patterns = ; patterns;patterns;patterns; http;
API
patterns.add(pattern[, value])
Arguments:
pattern
- The pattern as either a string or a RegExp objectvalue
(optional) - Returned as part of the match object when calling the.match()
method. Can be of any type
If the pattern
is a string it will be matched using the
murl module. If the pattern
is a
RegExp object it will be matched as is.
patterns.match(target)
Arguments:
target
- The string that should be matched against each pattern
Runs through each pattern in order and returns a match object for the
first match. If no pattern matches null
is returned.
Example match object
pattern: 'GET /foo/{id}' // the matched pattern (1st argument to `.add()` function) target: 'GET /foo/bar' // the target string params: // the named parameters from the pattern id: '...' value: value // the value (2nd argument to `.add()` function) {...} // function to skip this match and continue
Note that if the pattern is a RegExp object, params
will be the result
of the native <str>.match(<ptn>)
function (an array).
The next-function
The match.next
function can be used to skip the found match and
continue matching the string against the patterns:
patterns;patterns;patterns; var values = ; var match = patterns;while match values; match = matchnext; console; // [1, 3]
License
MIT