{"id":16906024,"url":"https://github.com/chocobo1/kmps","last_synced_at":"2025-04-12T14:33:15.609Z","repository":{"id":86429411,"uuid":"165392751","full_name":"Chocobo1/kmps","owner":"Chocobo1","description":"Knuth–Morris–Pratt algorithm that works with JS Array \u0026 TypedArray","archived":false,"fork":false,"pushed_at":"2022-12-11T13:33:26.000Z","size":53,"stargazers_count":3,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-26T09:11:08.775Z","etag":null,"topics":["javascript-library","knuth-morris-pratt","string-search"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Chocobo1.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"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}},"created_at":"2019-01-12T13:46:27.000Z","updated_at":"2022-12-28T08:38:52.000Z","dependencies_parsed_at":null,"dependency_job_id":"d13efe16-b152-4745-87c4-eb5c73f088d0","html_url":"https://github.com/Chocobo1/kmps","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Chocobo1%2Fkmps","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Chocobo1%2Fkmps/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Chocobo1%2Fkmps/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Chocobo1%2Fkmps/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Chocobo1","download_url":"https://codeload.github.com/Chocobo1/kmps/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248581361,"owners_count":21128155,"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","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":["javascript-library","knuth-morris-pratt","string-search"],"created_at":"2024-10-13T18:40:35.320Z","updated_at":"2025-04-12T14:33:15.589Z","avatar_url":"https://github.com/Chocobo1.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Knuth–Morris–Pratt Search\n\nAn naive implementation of [Knuth–Morris–Pratt algorithm][wikipedia_link] that works with [Array][mdn_array_link] \u0026 [TypedArray][mdn_typed_array_link]\n\n[wikipedia_link]: https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm\n[mdn_array_link]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array\n[mdn_typed_array_link]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray\n\n### Installation\n```shell\nnpm install git+https://github.com/Chocobo1/kmps.git\n```\n\n### Usage example\n```javascript\nconst Kmp = require('kmps');  // import this module, CommonJS style\n//import * as Kmp from 'kmps';  // import this module, ES module style\n\n// working with TypedArray\n{\n    const pattern = Uint32Array.from([0xFFFF, 0x3000]);\n    const corpus = Uint32Array.from([0xFFFF, 0xFFFF, 0x3000, 0x1000]);\n\n    // setup `kmp` for later reuse\n    const kmp = Kmp.KnuthMorrisPratt(pattern);\n    // returns the first index of the exact match in `corpus`; -1 if not found\n    const idx = kmp.match(corpus);\n    if (idx !== 1)\n        throw (new Error('Please file an issue'));\n}\n\n// also working with String\n{\n    const pattern = \"pattern\";\n    const corpus = \"some pattern !@#$%\";\n\n    const kmp = Kmp.KnuthMorrisPratt(pattern);\n    const idx = kmp.match(corpus);\n    if (idx !== corpus.indexOf(pattern))\n        throw (new Error('Please file an issue'));\n}\n\n// you can specify offset!\n{\n    const pattern = \"123\";\n    const corpus = \"123abc123\";\n    const corpusOffset = 1;\n\n    const idx = Kmp.KnuthMorrisPratt(pattern).match(corpus, corpusOffset);\n    if (idx !== corpus.indexOf(pattern, corpusOffset))\n        throw (new Error('Please file an issue'));\n}\n```\n\n### Run tests\n```shell\nnpm install -D  # install dev dependencies\nnpm test        # run tests\n```\n\n### References\n* https://www.inf.hs-flensburg.de/lang/algorithmen/pattern/kmpen.htm\n* https://people.ok.ubc.ca/ylucet/DS/KnuthMorrisPratt.html\n\n### See also\nYou might be interested to [Boyer–Moore–Horspool algorithm][bmh_link]\n\n[bmh_link]: https://github.com/Chocobo1/bmhs\n\n### License\nSee [LICENSE](./LICENSE) file\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchocobo1%2Fkmps","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchocobo1%2Fkmps","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchocobo1%2Fkmps/lists"}