github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/spellcheck/userdictionary/userdictionary.go (about)

     1  package userdictionary
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/lmorg/murex/app"
     9  	"github.com/lmorg/murex/lang/types"
    10  )
    11  
    12  var dictionary = []string{
    13  	app.Name,
    14  }
    15  
    16  // IsInDictionary checks if word is in user dictionary
    17  func IsInDictionary(word string) bool {
    18  	s := strings.ToLower(word)
    19  	for i := range dictionary {
    20  		if s == strings.ToLower(dictionary[i]) {
    21  			return true
    22  		}
    23  	}
    24  
    25  	return false
    26  }
    27  
    28  // Get returns a copy of the slice dictionary
    29  func Get() []string {
    30  	a := make([]string, len(dictionary))
    31  	copy(a, dictionary)
    32  	return a
    33  }
    34  
    35  // Read returns an interface{} of the user dictionary.
    36  // This is only intended to be used by `config.Properties.GoFunc.Read()`
    37  func Read() (interface{}, error) {
    38  	return Get(), nil
    39  }
    40  
    41  // Write takes a JSON-encoded string and writes it to the dictionary slice.
    42  // This is only intended to be used by `config.Properties.GoFunc.Write()`
    43  func Write(v interface{}) error {
    44  	switch v := v.(type) {
    45  	case string:
    46  		return json.Unmarshal([]byte(v), &dictionary)
    47  
    48  	default:
    49  		return fmt.Errorf("invalid data-type. Expecting a %s encoded string", types.Json)
    50  	}
    51  }