Skip to content

Commit e6eecee

Browse files
committed
docs: add fname package comments
1 parent f3850b8 commit e6eecee

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

dictionary.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// package fname contains functions for generating random, human-friendly names.
12
package fname
23

34
import (
@@ -16,13 +17,15 @@ const (
1617
//go:embed data/*
1718
var dataFS embed.FS
1819

20+
// Dictionary is a collection of words.
1921
type Dictionary struct {
2022
adectives []string
2123
adverbs []string
2224
nouns []string
2325
verbs []string
2426
}
2527

28+
// NewDictionary creates a new dictionary.
2629
func 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.
5357
func (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.
6065
func (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.
6773
func (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.
7481
func (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.
8189
func (d *Dictionary) LengthAdjective() int {
8290
return len(d.adectives)
8391
}
8492

93+
// LengthAdverb returns the number of adverbs in the dictionary.
8594
func (d *Dictionary) LengthAdverb() int {
8695
return len(d.adverbs)
8796
}
8897

98+
// LengthNoun returns the number of nouns in the dictionary.
8999
func (d *Dictionary) LengthNoun() int {
90100
return len(d.nouns)
91101
}
92102

103+
// LengthVerb returns the number of verbs in the dictionary.
93104
func (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.
97109
func loadFile(path string) ([]string, error) {
98110
f, err := dataFS.Open(path)
99111
if err != nil {

generator.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// package fname contains functions for generating random, human-friendly names.
12
package fname
23

34
import (
@@ -7,33 +8,39 @@ import (
78
"time"
89
)
910

11+
// Generator is a random name generator.
1012
type Generator struct {
1113
dict *Dictionary
1214
delimiter string
1315
seed int64
1416
size uint
1517
}
1618

19+
// GeneratorOption is a function that configures a Generator.
1720
type GeneratorOption func(*Generator)
1821

22+
// WithDelimiter sets the delimiter used to join words.
1923
func WithDelimiter(delimiter string) GeneratorOption {
2024
return func(r *Generator) {
2125
r.delimiter = delimiter
2226
}
2327
}
2428

29+
// WithSeed sets the seed used to generate random numbers.
2530
func WithSeed(seed int64) GeneratorOption {
2631
return func(r *Generator) {
2732
r.seed = seed
2833
}
2934
}
3035

36+
// WithSize sets the number of words in the generated name.
3137
func WithSize(size uint) GeneratorOption {
3238
return func(r *Generator) {
3339
r.size = size
3440
}
3541
}
3642

43+
// NewGenerator creates a new Generator.
3744
func NewGenerator(opts ...GeneratorOption) *Generator {
3845
r := &Generator{
3946
dict: NewDictionary(),
@@ -48,6 +55,7 @@ func NewGenerator(opts ...GeneratorOption) *Generator {
4855
return r
4956
}
5057

58+
// Generate generates a random name.
5159
func (r *Generator) Generate() (string, error) {
5260
// TODO: address case where adjective and noun are the same, such as "orange-orange" or "sound-sound"
5361
adjective, err := r.dict.Adjective(rand.Intn(r.dict.LengthAdjective()))

0 commit comments

Comments
 (0)