fix(server): Improvements

This commit is contained in:
Myzel394 2025-03-29 18:53:20 +01:00
parent e4d7521a4c
commit 9f22689cac
No known key found for this signature in database
GPG Key ID: 3B955307C2FC2F11
2 changed files with 43 additions and 1 deletions

View File

@ -73,4 +73,3 @@ var APFSDocumentationEnums = []docvalues.EnumString{
"This option indicates that in the course of the mount system call, the kernel should not follow any symlinks that may be present in the provided mount-on directory. This is the same as the -k option.", "This option indicates that in the course of the mount system call, the kernel should not follow any symlinks that may be present in the provided mount-on directory. This is the same as the -k option.",
), ),
} }

View File

@ -0,0 +1,43 @@
package common
import (
"github.com/hbollon/go-edlib"
)
// Find items that are similar to the given input.
// This is used to find typos & suggest the correct item.
// Once an item is found that has a Damerau-Levenshtein distance of 1, it is immediately returned.
// If not, then the next 2 items of similarity 2, or 3 items of similarity 3 are returned.
// If no items with similarity <= 3 are found, then an empty slice is returned.
func FindSimilarItems[T ~string](
input T,
items []T,
) []T {
itemsPerSimilarity := map[uint8][]T{
2: make([]T, 0, 2),
3: make([]T, 0, 3),
}
for _, item := range items {
similarity := edlib.DamerauLevenshteinDistance(string(item), string(input))
switch similarity {
case 1:
return []T{item}
case 2:
itemsPerSimilarity[2] = append(itemsPerSimilarity[2], item)
if len(itemsPerSimilarity[2]) >= 2 {
return itemsPerSimilarity[2]
}
case 3:
itemsPerSimilarity[3] = append(itemsPerSimilarity[3], item)
if len(itemsPerSimilarity[3]) >= 3 {
return itemsPerSimilarity[3]
}
}
}
return append(itemsPerSimilarity[2], itemsPerSimilarity[3]...)
}