fuzzaldrin

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2022 License: Apache-2.0 Imports: 5 Imported by: 1

README

go-fuzzaldrin-plus - fuzzy filtering and string scoring

This is Go port of fuzzaldrin, a library used by Atom and so its focus is on scoring and filtering paths, methods, and other things common when writing code. It therefore specializes in handling common patterns in these types of strings such as characters like /, -, and _, and also handling of camel case text.

Usage

go get -u github.com/abc-inc/go-fuzzaldrin-plus
import "github.com/abc-inc/go-fuzzaldrin-plus"
Filter

Filter sorts and filters the given items by matching them against the query. It returns a slice of items sorted by best match against the query.

func ExampleFilter_string() {
	ss := []string{"Call", "Me", "Maybe"}
	res := fuzzaldrin.Filter(ss, "me", ident[string])
	fmt.Println(strings.Join(res, "\n"))
	// Output:
	// Me
	// Maybe
}
Match

Match returns the indices of any query matches in the given string.

func ExampleMatch() {
	fmt.Println(fuzzaldrin.Match("Call Me Maybe", "m m"))
	fmt.Println(fuzzaldrin.Match("Call Me Maybe", "eY"))
	// Output:
	// [5 8]
	// [6 10]
}
Score

Score calculates the score of the given string against the query.

func ExampleScore() {
	fmt.Println(fuzzaldrin.Score("Me", "me"))
	fmt.Println(fuzzaldrin.Score("Maybe", "me"))
	// Output:
	// 0.17099999999999999
	// 0.0693
}

Similar Projects

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var PathSeparator = os.PathSeparator

Functions

func Filter

func Filter[T any](items []T, query string, strFunc func(T) string) (res []T)

Filter sorts and filters the given items by matching them against the query. It returns a slice of items sorted by best match against the query.

Example (String)
ss := []string{"Call", "Me", "Maybe"}
res := fuzzaldrin.Filter(ss, "me", ident[string])
fmt.Println(strings.Join(res, "\n"))
Output:

Me
Maybe
Example (Struct)
es := []entry{
	{name: "Call", id: 1},
	{name: "Me", id: 2},
	{name: "Maybe", id: 3},
}
res := fuzzaldrin.Filter[entry](es, "me", func(t entry) string { return t.name })
for _, i := range res {
	fmt.Println(i)
}
Output:

{Me 2}
{Maybe 3}

func Match

func Match(str, query string) (indices []int)

Match returns the indices of any query matches in the given string.

Example
fmt.Println(fuzzaldrin.Match("Call Me Maybe", "m m"))
fmt.Println(fuzzaldrin.Match("Call Me Maybe", "eY"))
Output:

[5 8]
[6 10]

func Score

func Score(str, query string) float64

Score calculates the score of the given string against the query.

Example
fmt.Println(fuzzaldrin.Score("Me", "me"))
fmt.Println(fuzzaldrin.Score("Maybe", "me"))
Output:

0.17099999999999999
0.0693

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL