1.0.3 • Published 1 month ago
@jswork/path-dual-parser
Licence
MIT
Version
1.0.3
Deps
1
Size
50 kB
Vulns
0
Weekly
0
path-dual-parser
Path matcher supporting both colon and brace URI template syntax.
installation
pnpm add @jswork/path-dual-parser
usage
match
Match a path against a template, supports both :param and {param} syntax:
import { match } from '@jswork/path-dual-parser';
const m1 = match('/users/:id');
const m2 = match('/users/{id}');
m1('/users/123'); // { path: '/users/123', params: { id: '123' } }
m2('/users/123'); // { path: '/users/123', params: { id: '123' } }
m1('/other'); // false
compile
Compile a template into a URL generator:
import { compile } from '@jswork/path-dual-parser';
const c1 = compile('/users/:id');
const c2 = compile('/users/{id}');
c1({ id: '123' }); // '/users/123'
c2({ id: '123' }); // '/users/123'
parse
One-step parse template with data to URL (simpler API):
import { parse } from '@jswork/path-dual-parser';
parse('/users/:id', { id: '123' }); // '/users/123'
parse('/users/{id}', { id: '123' }); // '/users/123'
parse('/users/:userId/posts/{postId}', { userId: '1', postId: '2' }); // '/users/1/posts/2'
params
Extract parameter names from a template:
import { params } from '@jswork/path-dual-parser';
params('/users/:id/posts/{postId}') // ['id', 'postId']
params('/users') // []
isMatch
Check whether a path matches a template (boolean result):
import { isMatch } from '@jswork/path-dual-parser';
isMatch('/users/:id', '/users/123') // true
isMatch('/users/:id', '/posts/1') // false
license
Code released under the MIT license.