github.com/evanw/esbuild@v0.21.4/internal/helpers/typos.go (about) 1 package helpers 2 3 import "unicode/utf8" 4 5 type TypoDetector struct { 6 oneCharTypos map[string]string 7 } 8 9 func MakeTypoDetector(valid []string) TypoDetector { 10 detector := TypoDetector{oneCharTypos: make(map[string]string)} 11 12 // Add all combinations of each valid word with one character missing 13 for _, correct := range valid { 14 if len(correct) > 3 { 15 for i, ch := range correct { 16 detector.oneCharTypos[correct[:i]+correct[i+utf8.RuneLen(ch):]] = correct 17 } 18 } 19 } 20 21 return detector 22 } 23 24 func (detector TypoDetector) MaybeCorrectTypo(typo string) (string, bool) { 25 // Check for a single deleted character 26 if corrected, ok := detector.oneCharTypos[typo]; ok { 27 return corrected, true 28 } 29 30 // Check for a single misplaced character 31 for i, ch := range typo { 32 if corrected, ok := detector.oneCharTypos[typo[:i]+typo[i+utf8.RuneLen(ch):]]; ok { 33 return corrected, true 34 } 35 } 36 37 return "", false 38 }