Skip to content

Commit 02f06cd

Browse files
committed
add functions for different word types, add tests
1 parent ec0e1f4 commit 02f06cd

File tree

5 files changed

+75
-38
lines changed

5 files changed

+75
-38
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/symmetric-project/ngen
22

33
go 1.17
4+
5+
require github.com/iancoleman/strcase v0.2.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0=
2+
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=

main.go

Lines changed: 0 additions & 38 deletions
This file was deleted.

ngen.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package ngen
2+
3+
import (
4+
"math"
5+
"math/rand"
6+
"os"
7+
"time"
8+
9+
"github.com/iancoleman/strcase"
10+
)
11+
12+
var adjectives []string
13+
var adverbs []string
14+
var nouns []string
15+
var verbs []string
16+
17+
func init() {
18+
rand.Seed(time.Now().UnixNano())
19+
var err error
20+
adjectives, err = readLines("./words/adjectives.txt")
21+
if err != nil {
22+
os.Exit(1)
23+
}
24+
adverbs, err = readLines("./words/adverbs.txt")
25+
if err != nil {
26+
os.Exit(1)
27+
}
28+
nouns, err = readLines("./words/nouns.txt")
29+
if err != nil {
30+
os.Exit(1)
31+
}
32+
verbs, err = readLines("./words/verbs.txt")
33+
if err != nil {
34+
os.Exit(1)
35+
}
36+
}
37+
38+
func getRandomStringFromStringArray(arr []string) string {
39+
randomRawIndex := rand.Float64() * float64(len(arr)-1)
40+
randomIndex := int(math.Round(randomRawIndex))
41+
return arr[randomIndex]
42+
}
43+
44+
func adjective() string {
45+
return getRandomStringFromStringArray(adjectives)
46+
}
47+
48+
func adverb() string {
49+
return getRandomStringFromStringArray(adverbs)
50+
}
51+
52+
func noun() string {
53+
return getRandomStringFromStringArray(nouns)
54+
}
55+
56+
func verb() string {
57+
return getRandomStringFromStringArray(verbs)
58+
}
59+
60+
func Generate() string {
61+
return strcase.ToCamel(adjective() + "-" + noun())
62+
}

ngen_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package ngen
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestPrintName(t *testing.T) {
8+
t.Log(Generate())
9+
}

0 commit comments

Comments
 (0)