{"id":16039951,"url":"https://github.com/sonofmagic/modern-ahocorasick","last_synced_at":"2025-07-22T05:33:47.266Z","repository":{"id":187642650,"uuid":"677260454","full_name":"sonofmagic/modern-ahocorasick","owner":"sonofmagic","description":"modern-ahocorasick","archived":false,"fork":false,"pushed_at":"2025-02-08T04:48:07.000Z","size":273,"stargazers_count":8,"open_issues_count":2,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-07-01T23:04:29.769Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sonofmagic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":["sonofmagic"],"custom":["https://github.com/sonofmagic/sponsors"]}},"created_at":"2023-08-11T06:06:55.000Z","updated_at":"2025-06-30T05:49:18.000Z","dependencies_parsed_at":null,"dependency_job_id":"7f4e177b-15ad-4908-9cef-84415fc36c69","html_url":"https://github.com/sonofmagic/modern-ahocorasick","commit_stats":null,"previous_names":["sonofmagic/modern-ahocorasick"],"tags_count":10,"template":false,"template_full_name":"sonofmagic/npm-lib-template","purl":"pkg:github/sonofmagic/modern-ahocorasick","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sonofmagic%2Fmodern-ahocorasick","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sonofmagic%2Fmodern-ahocorasick/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sonofmagic%2Fmodern-ahocorasick/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sonofmagic%2Fmodern-ahocorasick/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sonofmagic","download_url":"https://codeload.github.com/sonofmagic/modern-ahocorasick/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sonofmagic%2Fmodern-ahocorasick/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266433705,"owners_count":23927800,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-08T23:09:12.760Z","updated_at":"2025-07-22T05:33:47.246Z","avatar_url":"https://github.com/sonofmagic.png","language":"TypeScript","funding_links":["https://github.com/sponsors/sonofmagic","https://github.com/sonofmagic/sponsors"],"categories":[],"sub_categories":[],"readme":"# modern-ahocorasick\n\n\u003e Forked from `https://github.com/BrunoRB/ahocorasick` and make it modern! Thanks to the author(`BrunoRB`) of `ahocorasick`\n\nImplementation of the Aho-Corasick string searching algorithm, as described in the paper \"Efficient string matching: an aid to bibliographic search\".\n\nthis pkg has `cjs` and `esm` format, and have `.d.ts` file.\n\n## Install\n\n```sh\nnpm i modern-ahocorasick\nyarn add modern-ahocorasick\npnpm i modern-ahocorasick\n```\n\n## Usage\n\n```ts\n// esm\nimport AhoCorasick from 'modern-ahocorasick'\n// cjs\nconst AhoCorasick = require('modern-ahocorasick')\n\nconst ac = new AhoCorasick(['keyword1', 'keyword2', 'etc'])\nconst results = ac.search('should find keyword1 at position 19 and keyword2 at position 47.')\n\n// [ [ 19, [ 'keyword1' ] ], [ 47, [ 'keyword2' ] ] ]\n```\n\n## Visualization\n\nSee \u003chttps://brunorb.github.io/ahocorasick/visualization.html\u003e for an interactive visualization of the algorithm.\n\n## API\n\n### Constructor\n\n#### `constructor(keywords: string[])`\n\nInitializes the Aho-Corasick state machine with the provided `keywords`.\n\n**Parameters**:\n\n- `keywords`: An array of strings representing the keywords to search for.\n\n**Example**:\n\n```typescript\nconst keywords = ['he', 'she', 'his', 'hers']\nconst ac = new AhoCorasick(keywords)\n```\n\n---\n\n### Methods\n\n#### `search(str: string): [number, string[]][]`\n\nSearches the input string `str` for occurrences of any of the keywords and returns a list of matches.\n\n**Parameters**:\n\n- `str`: The input string to search.\n\n**Returns**:\n\n- An array of tuples. Each tuple contains:\n  - The ending index of the match in the input string.\n  - An array of matched keywords at that position.\n\n**Example**:\n\n```typescript\nconst ac = new AhoCorasick(['keyword1', 'keyword2', 'etc'])\nconst results = ac.search('should find keyword1 at position 19 and keyword2 at position 47.')\n\n// [ [ 19, [ 'keyword1' ] ], [ 47, [ 'keyword2' ] ] ]\n```\n\n---\n\n#### `match(str: string): boolean`\n\nChecks if any keyword exists in the input string `str`.\n\n**Parameters**:\n\n- `str`: The input string to search.\n\n**Returns**:\n\n- `true` if any keyword is found.\n- `false` otherwise.\n\n**Example**:\n\n```typescript\nconst ac = new AhoCorasick(['he', 'she', 'his', 'hers'])\nconsole.log(ac.match('ushers')) // Output: true\nconsole.log(ac.match('xyz')) // Output: false\n```\n\n---\n\n### Internal Functionality\n\nWhile the `_buildTables` method is not part of the public API, it is responsible for building the transition (`gotoFn`), output, and failure functions used by the Aho-Corasick algorithm.\n\n---\n\n## Examples\n\n### Example 1: Basic Search\n\n```typescript\nconst keywords = ['cat', 'bat', 'rat']\nconst ac = new AhoCorasick(keywords)\n\nconst text = 'the cat chased the rat while a bat flew by'\nconst matches = ac.search(text)\n```\n\n### Example 2: Check Match Presence\n\n```typescript\nconst ac = new AhoCorasick(['abc', '123'])\n\nconsole.log(ac.match('hello abc world')) // Output: true\nconsole.log(ac.match('hello world')) // Output: false\n```\n\n---\n\n### Example: Case-Insensitive Search\n\n```typescript\nconst keywords = ['hello', 'world']\nconst ac = new AhoCorasick(keywords.map(k =\u003e k.toLowerCase()))\n\nconst text = 'Hello World'\nconst matches = ac.search(text.toLowerCase())\nconsole.log(matches)\n// Output: [\n//   [4, [\"hello\"]],\n//   [10, [\"world\"]]\n// ]\n```\n\nThis document serves as a complete guide for using the `AhoCorasick` class for multi-pattern string matching.\n\n## License\n\n[The MIT License](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsonofmagic%2Fmodern-ahocorasick","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsonofmagic%2Fmodern-ahocorasick","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsonofmagic%2Fmodern-ahocorasick/lists"}