Skip to content

Commit 9d9f2f1

Browse files
authored
Merge pull request #1 from Splode/refactor/api-refactoring
Refactor API
2 parents a1ca725 + ee54e80 commit 9d9f2f1

4 files changed

Lines changed: 28 additions & 182 deletions

File tree

dictionary.go

Lines changed: 20 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,25 @@
22
package fname
33

44
import (
5-
"bufio"
6-
"embed"
7-
"fmt"
5+
_ "embed"
6+
"strings"
87
)
98

10-
const (
11-
adjectiveFilePath = "data/adjective"
12-
adverbFilePath = "data/adverb"
13-
nounFilePath = "data/noun"
14-
verbFilePath = "data/verb"
15-
)
9+
//go:embed data/adjective
10+
var _adjective string
11+
var adjective = strings.Split(_adjective, "\n")
12+
13+
//go:embed data/adverb
14+
var _adverb string
15+
var adverb = strings.Split(_adverb, "\n")
1616

17-
//go:embed data/*
18-
var dataFS embed.FS
17+
//go:embed data/noun
18+
var _noun string
19+
var noun = strings.Split(_noun, "\n")
20+
21+
//go:embed data/verb
22+
var _verb string
23+
var verb = strings.Split(_verb, "\n")
1924

2025
// Dictionary is a collection of words.
2126
type Dictionary struct {
@@ -28,61 +33,12 @@ type Dictionary struct {
2833
// NewDictionary creates a new dictionary.
2934
func NewDictionary() *Dictionary {
3035
// TODO: allow for custom dictionary
31-
a, err := loadFile(adjectiveFilePath)
32-
if err != nil {
33-
panic(err)
34-
}
35-
av, err := loadFile(adverbFilePath)
36-
if err != nil {
37-
panic(err)
38-
}
39-
n, err := loadFile(nounFilePath)
40-
if err != nil {
41-
panic(err)
42-
}
43-
v, err := loadFile(verbFilePath)
44-
if err != nil {
45-
panic(err)
46-
}
47-
4836
return &Dictionary{
49-
adectives: a,
50-
adverbs: av,
51-
nouns: n,
52-
verbs: v,
53-
}
54-
}
55-
56-
// Adjective returns a random adjective.
57-
func (d *Dictionary) Adjective(idx int) (string, error) {
58-
if idx < 0 || idx >= len(d.adectives) {
59-
return "", fmt.Errorf("index out of range: %d", idx)
60-
}
61-
return d.adectives[idx], nil
62-
}
63-
64-
// Adverb returns a random adverb.
65-
func (d *Dictionary) Adverb(idx int) (string, error) {
66-
if idx < 0 || idx >= len(d.adverbs) {
67-
return "", fmt.Errorf("index out of range: %d", idx)
37+
adectives: adjective,
38+
adverbs: adverb,
39+
nouns: noun,
40+
verbs: verb,
6841
}
69-
return d.adverbs[idx], nil
70-
}
71-
72-
// Noun returns a random noun.
73-
func (d *Dictionary) Noun(idx int) (string, error) {
74-
if idx < 0 || idx >= len(d.nouns) {
75-
return "", fmt.Errorf("index out of range: %d", idx)
76-
}
77-
return d.nouns[idx], nil
78-
}
79-
80-
// Verb returns a random verb.
81-
func (d *Dictionary) Verb(idx int) (string, error) {
82-
if idx < 0 || idx >= len(d.verbs) {
83-
return "", fmt.Errorf("index out of range: %d", idx)
84-
}
85-
return d.verbs[idx], nil
8642
}
8743

8844
// LengthAdjective returns the number of adjectives in the dictionary.
@@ -104,26 +60,3 @@ func (d *Dictionary) LengthNoun() int {
10460
func (d *Dictionary) LengthVerb() int {
10561
return len(d.verbs)
10662
}
107-
108-
// loadFile loads a file from the embedded filesystem, and returns a slice of strings containing each line.
109-
func loadFile(path string) ([]string, error) {
110-
f, err := dataFS.Open(path)
111-
if err != nil {
112-
return nil, err
113-
}
114-
defer f.Close()
115-
116-
var words []string
117-
scanner := bufio.NewScanner(f)
118-
for scanner.Scan() {
119-
w := scanner.Text()
120-
if w != "" {
121-
words = append(words, scanner.Text())
122-
}
123-
}
124-
if scanner.Err() != nil {
125-
return nil, scanner.Err()
126-
}
127-
128-
return words, nil
129-
}

dictionary_test.go

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -32,72 +32,6 @@ func TestNewDictionary(t *testing.T) {
3232
t.Error("\t\tShould be able to load the verb file.")
3333
}
3434

35-
adj, err := d.Adjective(0)
36-
if err != nil {
37-
t.Error("\t\tShould be able to get an adjective from the dictionary.")
38-
}
39-
t.Log("\t\tShould be able to get an adjective from the dictionary.")
40-
if adj != "able" {
41-
t.Error("\t\tShould be able to get the first adjective from the dictionary.")
42-
}
43-
t.Log("\t\tShould be able to get the first adjective from the dictionary.")
44-
45-
_, err = d.Adjective(-1)
46-
if err == nil {
47-
t.Error("\t\tShould not be able to get an adjective from the dictionary with a negative index.")
48-
}
49-
t.Log("\t\tShould not be able to get an adjective from the dictionary with a negative index.")
50-
51-
adv, err := d.Adverb(0)
52-
if err != nil {
53-
t.Error("\t\tShould be able to get an adverb from the dictionary.")
54-
}
55-
t.Log("\t\tShould be able to get an adverb from the dictionary.")
56-
if adv != "abnormally" {
57-
t.Error("\t\tShould be able to get the first adverb from the dictionary.")
58-
}
59-
t.Log("\t\tShould be able to get the first adverb from the dictionary.")
60-
61-
_, err = d.Adverb(-1)
62-
if err == nil {
63-
t.Error("\t\tShould not be able to get an adverb from the dictionary with a negative index.")
64-
}
65-
t.Log("\t\tShould not be able to get an adverb from the dictionary with a negative index.")
66-
67-
noun, err := d.Noun(0)
68-
if err != nil {
69-
t.Error("\t\tShould be able to get a noun from the dictionary.")
70-
}
71-
t.Log("\t\tShould be able to get a noun from the dictionary.")
72-
73-
if noun != "aardvark" {
74-
t.Error("\t\tShould be able to get the first noun from the dictionary.")
75-
}
76-
t.Log("\t\tShould be able to get the first noun from the dictionary.")
77-
78-
_, err = d.Noun(-1)
79-
if err == nil {
80-
t.Error("\t\tShould not be able to get a noun from the dictionary with a negative index.")
81-
}
82-
t.Log("\t\tShould not be able to get a noun from the dictionary with a negative index.")
83-
84-
verb, err := d.Verb(0)
85-
if err != nil {
86-
t.Error("\t\tShould be able to get a verb from the dictionary.")
87-
}
88-
t.Log("\t\tShould be able to get a verb from the dictionary.")
89-
90-
if verb != "abandoned" {
91-
t.Error("\t\tShould be able to get the first verb from the dictionary.")
92-
}
93-
t.Log("\t\tShould be able to get the first verb from the dictionary.")
94-
95-
_, err = d.Verb(-1)
96-
if err == nil {
97-
t.Error("\t\tShould not be able to get a verb from the dictionary with a negative index.")
98-
}
99-
t.Log("\t\tShould not be able to get a verb from the dictionary with a negative index.")
100-
10135
if len(d.adectives) != d.LengthAdjective() {
10236
t.Error("\t\tShould be able to get the length of the adjective file.")
10337
}

generator.go

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
type Generator struct {
1313
dict *Dictionary
1414
delimiter string
15-
seed int64
15+
rand *rand.Rand
1616
size uint
1717
}
1818

@@ -29,7 +29,7 @@ func WithDelimiter(delimiter string) GeneratorOption {
2929
// WithSeed sets the seed used to generate random numbers.
3030
func WithSeed(seed int64) GeneratorOption {
3131
return func(r *Generator) {
32-
r.seed = seed
32+
r.rand.Seed(seed)
3333
}
3434
}
3535

@@ -45,48 +45,32 @@ func NewGenerator(opts ...GeneratorOption) *Generator {
4545
r := &Generator{
4646
dict: NewDictionary(),
4747
delimiter: "-",
48-
seed: time.Now().UnixNano(),
48+
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
4949
size: 2,
5050
}
5151
for _, opt := range opts {
5252
opt(r)
5353
}
54-
rand.Seed(r.seed)
5554
return r
5655
}
5756

5857
// Generate generates a random name.
5958
func (r *Generator) Generate() (string, error) {
6059
// TODO: address case where adjective and noun are the same, such as "orange-orange" or "sound-sound"
61-
adjective, err := r.dict.Adjective(rand.Intn(r.dict.LengthAdjective()))
62-
if err != nil {
63-
return "", err
64-
}
65-
noun, err := r.dict.Noun(rand.Intn(r.dict.LengthNoun()))
66-
if err != nil {
67-
return "", err
68-
}
60+
adjective := r.dict.adectives[r.rand.Intn(r.dict.LengthAdjective())]
61+
noun := r.dict.nouns[r.rand.Intn(r.dict.LengthNoun())]
6962
words := []string{adjective, noun}
7063

7164
switch r.size {
7265
case 2:
7366
return strings.Join(words, r.delimiter), nil
7467
case 3:
75-
verb, err := r.dict.Verb(rand.Intn(r.dict.LengthVerb()))
76-
if err != nil {
77-
return "", err
78-
}
68+
verb := r.dict.verbs[r.rand.Intn(r.dict.LengthVerb())]
7969
words = append(words, verb)
8070
case 4:
81-
verb, err := r.dict.Verb(rand.Intn(r.dict.LengthVerb()))
82-
if err != nil {
83-
return "", err
84-
}
71+
verb := r.dict.verbs[r.rand.Intn(r.dict.LengthVerb())]
8572
words = append(words, verb)
86-
adverb, err := r.dict.Adverb(rand.Intn(r.dict.LengthAdverb()))
87-
if err != nil {
88-
return "", err
89-
}
73+
adverb := r.dict.adverbs[r.rand.Intn(r.dict.LengthAdverb())]
9074
words = append(words, adverb)
9175
default:
9276
return "", fmt.Errorf("invalid size: %d", r.size)

generator_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ func TestNewGenerator(t *testing.T) {
3939
t.Fatal("\t\tShould be able to set the delimiter of the phrase.")
4040
}
4141
t.Log("\t\tShould be able to set the delimiter of the phrase.")
42-
43-
if g.seed != 12345 {
44-
t.Fatal("\t\tShould be able to set the seed of the phrase.")
45-
}
46-
t.Log("\t\tShould be able to set the seed of the phrase.")
4742
}
4843
}
4944
}

0 commit comments

Comments
 (0)