mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-23 07:33:32 -06:00
cb2e9119aa
Signed-off-by: namgyalangmo <75657887+namgyalangmo@users.noreply.github.com>
30 lines
963 B
Go
30 lines
963 B
Go
// Copyright (c) The OpenTofu Authors
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package didyoumean
|
|
|
|
import (
|
|
"github.com/agext/levenshtein"
|
|
)
|
|
|
|
// NameSuggestion tries to find a name from the given slice of suggested names
|
|
// that is close to the given name and returns it if found. If no suggestion
|
|
// is close enough, returns the empty string.
|
|
//
|
|
// The suggestions are tried in order, so earlier suggestions take precedence
|
|
// if the given string is similar to two or more suggestions.
|
|
//
|
|
// This function is intended to be used with a relatively-small number of
|
|
// suggestions. It's not optimized for hundreds or thousands of them.
|
|
func NameSuggestion(given string, suggestions []string) string {
|
|
for _, suggestion := range suggestions {
|
|
dist := levenshtein.Distance(given, suggestion, nil)
|
|
if dist < 3 { // threshold determined experimentally
|
|
return suggestion
|
|
}
|
|
}
|
|
return ""
|
|
}
|