{"id":19390014,"url":"https://github.com/deanishe/go-fuzzy","last_synced_at":"2025-04-24T00:31:02.348Z","repository":{"id":57536468,"uuid":"283999324","full_name":"deanishe/go-fuzzy","owner":"deanishe","description":"Fuzzy matching \u0026 sorting for Go","archived":false,"fork":false,"pushed_at":"2020-08-04T09:48:18.000Z","size":17,"stargazers_count":9,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-06-19T02:04:05.414Z","etag":null,"topics":["fuzzy","fuzzy-search","go","golang"],"latest_commit_sha":null,"homepage":"","language":"Go","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/deanishe.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-07-31T09:46:39.000Z","updated_at":"2024-06-02T19:52:19.000Z","dependencies_parsed_at":"2022-08-28T17:41:59.172Z","dependency_job_id":null,"html_url":"https://github.com/deanishe/go-fuzzy","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deanishe%2Fgo-fuzzy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deanishe%2Fgo-fuzzy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deanishe%2Fgo-fuzzy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deanishe%2Fgo-fuzzy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/deanishe","download_url":"https://codeload.github.com/deanishe/go-fuzzy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250539335,"owners_count":21447291,"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":["fuzzy","fuzzy-search","go","golang"],"created_at":"2024-11-10T10:18:37.116Z","updated_at":"2025-04-24T00:31:01.992Z","avatar_url":"https://github.com/deanishe.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"fuzzy\n=====\n\n[![CI status][github-status-icon]][github-status-link]\n[![Go Report Card][goreport-icon]][goreport-link]\n[![Codacy coverage][coverage-icon]][codacy-link]\n[![GitHub licence][licence-icon]][licence-link]\n[![GoDoc][godoc-icon]][godoc-link]\n\nPackage fuzzy implements fuzzy matching/sorting of string slices and custom types (via `fuzzy.Sortable` interface).\n\nImport path is `go.deanishe.net/fuzzy`\n\nThe fuzzy matching algorithm is based on [Forrest Smith's reverse engineering of Sublime Text's search][forrest].\n\n\nFeatures\n--------\n\n- Sublime Text-like fuzzy matching\n- Simple sorting of string slices via `fuzzy.SortStrings()`\n- Sorting of custom types via `fuzzy.Sortable` interface\n- Intelligent handling of diacritics\n\n\nUsage\n-----\n\nSee [Godoc][godoc] for full documentation.\n\n\n### Slice of strings ###\n\n```go\nactors := []string{\"Tommy Lee Jones\", \"James Earl Jones\", \"Keanu Reeves\"}\nfuzzy.SortStrings(actors, \"jej\")\nfmt.Println(actors[0])\n// -\u003e James Earl Jones\n```\n\n\n### Custom types ###\n\nTo sort a custom type, it must implement `fuzzy.Sortable`:\n\n```go\ntype Sortable interface {\n    sort.Interface\n    // Keywords returns the string to compare to the sort query\n    Keywords(i int) string\n}\n```\n\nThis is a superset of `sort.Interface` (i.e. your type must also implement `sort.Interface`). The string returned by `Keywords()` is compared to the search query using the fuzzy algorithm. The `Less()` function of `sort.Interface` is used as a tie-breaker if multiple items have the same fuzzy matching score.\n\n```go\n// Player is a very simple data model.\ntype Player struct {\n    Firstname string\n    Lastname  string\n}\n\n// Name returns the full name of the Player.\nfunc (p *Player) Name() string {\n    return strings.TrimSpace(p.Firstname + \" \" + p.Lastname)\n}\n\n// Team is a collection of Player items. This is where fuzzy.Sortable\n// must be implemented to enable fuzzy sorting.\ntype Team []*Player\n\n// Default sort.Interface methods\nfunc (t Team) Len() int      { return len(t) }\nfunc (t Team) Swap(i, j int) { t[i], t[j] = t[j], t[i] }\n\n// Less is used as a tie-breaker when fuzzy match score is the same.\nfunc (t Team) Less(i, j int) bool { return t[i].Name() \u003c t[j].Name() }\n\n// Keywords implements Sortable.\n// Comparisons are based on the the full name of the player.\nfunc (t Team) Keywords(i int) string { return t[i].Name() }\n\n// Fuzzy sort players by name.\nvar t = Team{\n    \u0026Player{\"Alisson\", \"Becker\"},\n    \u0026Player{Firstname: \"Adrián\"},\n    \u0026Player{\"Andy\", \"Lonergan\"},\n    \u0026Player{\"Caoimhín\", \"Kelleher\"},\n    \u0026Player{\"Virgil\", \"van Dijk\"},\n    \u0026Player{\"Joe\", \"Gomez\"},\n    \u0026Player{\"Andy\", \"Robertson\"},\n    \u0026Player{\"Joel\", \"Matip\"},\n    \u0026Player{\"Ki-Jana\", \"Hoever\"},\n    \u0026Player{\"Trent\", \"Alexander-Arnold\"},\n    \u0026Player{\"Sepp\", \"van den Berg\"},\n    \u0026Player{\"Neco\", \"Williams\"},\n    \u0026Player{Firstname: \"Fabinho\"},\n    \u0026Player{\"Georginio\", \"Wijnaldum\"},\n    \u0026Player{\"James\", \"Milner\"},\n    \u0026Player{\"Naby\", \"Keita\"},\n    \u0026Player{\"Jordan\", \"Henderson\"},\n    \u0026Player{\"Alex\", \"Oxlade-Chamberlain\"},\n    \u0026Player{\"Xherdan\", \"Shaqiri\"},\n    \u0026Player{\"Curtis\", \"Jones\"},\n    \u0026Player{\"Harvel\", \"Elliott\"},\n    \u0026Player{\"Roberto\", \"Firmino\"},\n    \u0026Player{\"Sadio\", \"Mané\"},\n    \u0026Player{\"Mohamed\", \"Salah\"},\n    \u0026Player{\"Takumi\", \"Minamino\"},\n    \u0026Player{\"Divock\", \"Origi\"},\n}\n// Unsorted\nfmt.Println(t[0].Name()) // --\u003e Alisson Becker\n\n// Initials\nSort(t, \"taa\")\nfmt.Println(t[0].Name()) // --\u003e Trent Alexander-Arnold\n\n// Initials beat start of string\nSort(t, \"al\")\nfmt.Println(t[0].Name()) // --\u003e Andy Lonergan\n\n// Start of word\nSort(t, \"ox\")\nfmt.Println(t[0].Name()) // --\u003e Alex Oxlade-Chamberlain\n\n// Earlier in string = better match\nSort(t, \"x\")\nfmt.Println(t[0].Name()) // --\u003e Xherdan Shaqiri\n\n// Diacritics are considered\nSort(t, \"ne\")\nfmt.Println(t[0].Name()) // --\u003e Neco Williams\nSort(t, \"né\")\nfmt.Println(t[0].Name()) // --\u003e Sadio Mané\n\n// But ignored if query is ASCII\nSort(t, \"mane\")\nfmt.Println(t[0].Name()) // --\u003e Sadio Mané\n```\n\n\nLicence\n-------\n\n`fuzzy` is released under the [MIT licence][mit].\n\n[godoc]: https://godoc.org/go.deanishe.net/fuzzy\n[forrest]: https://blog.forrestthewoods.com/reverse-engineering-sublime-text-s-fuzzy-match-4cffeed33fdb\n[mit]: ./LICENCE.txt\n[godoc-icon]: https://godoc.org/go.deanishe.net/fuzzy?status.svg\n[godoc-link]: https://godoc.org/go.deanishe.net/fuzzy\n[goreport-link]: https://goreportcard.com/report/github.com/deanishe/go-fuzzy\n[goreport-icon]: https://goreportcard.com/badge/github.com/deanishe/go-fuzzy\n[coverage-icon]: https://img.shields.io/codacy/coverage/9cdd179cb6ce4236979ef01915b9e6eb?color=brightgreen\n[codacy-link]: https://www.codacy.com/app/deanishe/go-fuzzy\n[licence-icon]: https://img.shields.io/github/license/deanishe/go-fuzzy\n[licence-link]: https://github.com/deanishe/go-fuzzy/blob/master/LICENCE.txt\n[github-status-icon]: https://github.com/deanishe/go-fuzzy/workflows/CI/badge.svg\n[github-status-link]: https://github.com/deanishe/go-fuzzy/actions?query=workflow%3ACI\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeanishe%2Fgo-fuzzy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdeanishe%2Fgo-fuzzy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeanishe%2Fgo-fuzzy/lists"}