-
Notifications
You must be signed in to change notification settings - Fork 0
/
rword.go
110 lines (82 loc) · 1.78 KB
/
rword.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package rword
import (
"encoding/json"
"github.com/cxnky/rword-go/lib"
"io/ioutil"
"strconv"
"strings"
)
/**
* Created by cxnky on 08/05/2019 at 13:47
* rword_go
* https://github.com/cxnky/
**/
type WordList struct {
Words []string
}
type GenerateOptions struct {
Length string
Capitalise string
}
type RWord struct {
globalPool []string
Options GenerateOptions
}
var (
words = make([]string, 0)
)
func (r *RWord) Generate(count int) []string {
pool := make([]string, 0)
length, err := strconv.Atoi(r.Options.Length)
if err != nil {
panic(err)
}
capitalise := strings.ToLower(r.Options.Capitalise)
if capitalise != "all" && capitalise != "first" && capitalise != "none" {
panic("invalid capitalise option, valid options are all/first/none")
}
// As Go does not have any lambda functions; we will have to iterate over the array manually
for _, w := range words {
if len(w) == length {
pool = append(pool, w)
}
}
if len(pool) == 0 {
return nil
}
indexes := lib.GenerateIndexes(len(pool), count)
temp := make([]string, 0)
for _, i := range indexes {
temp = append(temp, pool[i])
}
pool = temp
if capitalise == "all" {
for i, w := range pool {
pool[i] = strings.ToUpper(w)
}
} else if capitalise == "first" {
for i, w := range pool {
pool[i] = strings.ToUpper(string(w[0])) + w[1:]
}
}
return pool
}
func (r *RWord) Shuffle() {
lib.ShuffleWords(words)
}
func (r *RWord) Load(wordList string) {
if wordList != "big" && wordList != "small" {
panic("invalid word list. valid options are big/small")
}
bytes, err := ioutil.ReadFile("words/" + wordList + ".json")
if err != nil {
panic(err)
}
var wordsList []string
err = json.Unmarshal(bytes, &wordsList)
if err != nil {
panic(err)
}
words = wordsList
r.Shuffle()
}