This is a tiny (~50 SLOC and and zero-dependency) hierarchical path (tree node) matcher.
npm install tiny-path-matcher
(Note: an ES2018+ESM-compatible environment is required.)
import { PathNode } from 'tiny-path-matcher';
const rootPathNode = new PathNode(),
usersPathNode = new PathNode('users');
userPathNode = new PathNode(/^user-(?<userId>\d+)$/, { description: 'User page' });
rootPathNode.push(usersPathNode);
usersPathNode.push(userPathNode);
let [ pathNode, groups ] = rootPathNode.match([ 'users', 'user-12' ]);
// assert.strictEqual(pathNode, userPathNode)
// assert.deepStrictEqual(grooups, { 'userId': '12' });
rootPathNode.match([ 'users', 'user-12' ]); // === null
See a full example here.
Alexander Kurakin <kuraga333@mail.ru>
https://github.com/kuraga/tiny-path-matcher/issues
MIT