1+ // package fname contains functions for generating random, human-friendly names.
12package fname
23
34import (
@@ -16,13 +17,15 @@ const (
1617//go:embed data/*
1718var dataFS embed.FS
1819
20+ // Dictionary is a collection of words.
1921type Dictionary struct {
2022 adectives []string
2123 adverbs []string
2224 nouns []string
2325 verbs []string
2426}
2527
28+ // NewDictionary creates a new dictionary.
2629func NewDictionary () * Dictionary {
2730 // TODO: allow for custom dictionary
2831 a , err := loadFile (adjectiveFilePath )
@@ -50,50 +53,59 @@ func NewDictionary() *Dictionary {
5053 }
5154}
5255
56+ // Adjective returns a random adjective.
5357func (d * Dictionary ) Adjective (idx int ) (string , error ) {
5458 if idx < 0 || idx >= len (d .adectives ) {
5559 return "" , fmt .Errorf ("index out of range: %d" , idx )
5660 }
5761 return d .adectives [idx ], nil
5862}
5963
64+ // Adverb returns a random adverb.
6065func (d * Dictionary ) Adverb (idx int ) (string , error ) {
6166 if idx < 0 || idx >= len (d .adverbs ) {
6267 return "" , fmt .Errorf ("index out of range: %d" , idx )
6368 }
6469 return d .adverbs [idx ], nil
6570}
6671
72+ // Noun returns a random noun.
6773func (d * Dictionary ) Noun (idx int ) (string , error ) {
6874 if idx < 0 || idx >= len (d .nouns ) {
6975 return "" , fmt .Errorf ("index out of range: %d" , idx )
7076 }
7177 return d .nouns [idx ], nil
7278}
7379
80+ // Verb returns a random verb.
7481func (d * Dictionary ) Verb (idx int ) (string , error ) {
7582 if idx < 0 || idx >= len (d .verbs ) {
7683 return "" , fmt .Errorf ("index out of range: %d" , idx )
7784 }
7885 return d .verbs [idx ], nil
7986}
8087
88+ // LengthAdjective returns the number of adjectives in the dictionary.
8189func (d * Dictionary ) LengthAdjective () int {
8290 return len (d .adectives )
8391}
8492
93+ // LengthAdverb returns the number of adverbs in the dictionary.
8594func (d * Dictionary ) LengthAdverb () int {
8695 return len (d .adverbs )
8796}
8897
98+ // LengthNoun returns the number of nouns in the dictionary.
8999func (d * Dictionary ) LengthNoun () int {
90100 return len (d .nouns )
91101}
92102
103+ // LengthVerb returns the number of verbs in the dictionary.
93104func (d * Dictionary ) LengthVerb () int {
94105 return len (d .verbs )
95106}
96107
108+ // loadFile loads a file from the embedded filesystem, and returns a slice of strings containing each line.
97109func loadFile (path string ) ([]string , error ) {
98110 f , err := dataFS .Open (path )
99111 if err != nil {
0 commit comments