Маршрутизатор для Node.js на основе префиксного дерева

e22m4u 597aa8a86c chore: updates README.md 1 year ago
.husky 8a94327bc6 chore: initial commit 1 year ago
src 8a94327bc6 chore: initial commit 1 year ago
.c8rc 8a94327bc6 chore: initial commit 1 year ago
.commitlintrc 8a94327bc6 chore: initial commit 1 year ago
.editorconfig 8a94327bc6 chore: initial commit 1 year ago
.gitignore 8a94327bc6 chore: initial commit 1 year ago
.mocharc.cjs 8a94327bc6 chore: initial commit 1 year ago
.prettierrc 8a94327bc6 chore: initial commit 1 year ago
LICENSE 8a94327bc6 chore: initial commit 1 year ago
README.md 597aa8a86c chore: updates README.md 1 year ago
eslint.config.js 8a94327bc6 chore: initial commit 1 year ago
package.json 8a94327bc6 chore: initial commit 1 year ago
tsconfig.json 8a94327bc6 chore: initial commit 1 year ago

README.md

@e22m4u/js-path-trie

ES-module of the path trie routing.

Installation

npm install @e22m4u/js-path-trie

Example

  • add(pathTemplate: string, value: unknown)
    - adds a value by the path template
  • match(path: string)
    - value lookup by the given path
const trie = new PathTrie();

// add values to the trie
trie.add('/foo/bar', yourValue1);
trie.add('/foo/:p1/bar/:p2', yourValue2);

// path matching
trie.match('/foo/bar');
// {
//   value: yourValue1,
//   params: {}
// }

// path matching (with parameters)
trie.match('/foo/10/bar/20');
// {
//   value: yourValue2,
//   params: {p1: 10, p2: 20}
// }

// if not matched
trie.match('/foo/bar/baz');
// undefined

Debug

Set environment variable DEBUG=jsPathTrie* before start.

DEBUG=jsPathTrie* npm run test

Tests

npm run test

License

MIT