Skip to content

Commit 2a119a1

Browse files
authored
Merge pull request #13 from Splode/feat/codebase-improvements
Feat/codebase improvements
2 parents 99fb9a4 + 3d2cb4f commit 2a119a1

File tree

25 files changed

+824
-293
lines changed

25 files changed

+824
-293
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ jobs:
44
test:
55
strategy:
66
matrix:
7-
go-version: [~1.17, ^1]
7+
go-version: [~1.19, ^1]
88
os: [ubuntu-latest, macos-latest, windows-latest]
99
runs-on: ${{ matrix.os }}
10-
env:
11-
GO111MODULE: "on"
1210
steps:
1311
- name: Install Go
14-
uses: actions/setup-go@v2
12+
uses: actions/setup-go@v5
1513
with:
1614
go-version: ${{ matrix.go-version }}
1715
- name: Checkout code

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
**/*.bak
33

44
.vscode/
5+
.claude/
6+
.codex/
57

68
dist/
9+
fname

README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ pleasant-joy
108108
eligible-tenant
109109
```
110110

111+
Generate names as a JSON array (useful for scripting):
112+
113+
```sh
114+
$ fname --format json --quantity 3
115+
["influential-length","direct-ear","cultural-storage"]
116+
117+
$ fname -f json
118+
["extinct-green"]
119+
```
120+
111121
### Library
112122

113123
#### Install
@@ -147,13 +157,42 @@ import (
147157
)
148158

149159
func main() {
150-
rng := fname.NewGenerator(fname.WithDelimiter("__"), fname.WithSize(3))
160+
sizeOpt, err := fname.WithSize(3)
161+
if err != nil {
162+
panic(err)
163+
}
164+
rng := fname.NewGenerator(fname.WithDelimiter("__"), sizeOpt)
151165
phrase, err := rng.Generate()
152166
fmt.Println(phrase)
153167
// => "established__shark__destroyed"
154168
}
155169
```
156170

171+
#### Custom Dictionary
172+
173+
```go
174+
package main
175+
176+
import (
177+
"fmt"
178+
179+
"github.com/splode/fname"
180+
)
181+
182+
func main() {
183+
dict := fname.NewCustomDictionary(
184+
[]string{"blazing", "frozen"}, // adjectives
185+
nil, // adverbs (uses default)
186+
[]string{"comet", "nebula"}, // nouns
187+
nil, // verbs (uses default)
188+
)
189+
rng := fname.NewGenerator(fname.WithDictionary(dict))
190+
phrase, err := rng.Generate()
191+
fmt.Println(phrase)
192+
// => "blazing-nebula"
193+
}
194+
```
195+
157196
## Disclaimers
158197

159198
fname is not cryptographically secure, and should not be used for anything that requires a truly unique identifier. It is meant to be a fun, human-friendly alternative to UUIDs.

cmd/fname/fname.go

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
_ "embed"
5+
"encoding/json"
56
"fmt"
67
"log"
78
"os"
@@ -45,19 +46,20 @@ func main() {
4546
var (
4647
casing string = "lower"
4748
delimiter string = "-"
49+
format string = "plain"
4850
help bool
4951
ver bool
50-
quantity int = 1
51-
size uint = 2
52-
seed int64 = -1
53-
// TODO: add option to use custom dictionary
52+
quantity int = 1
53+
size uint = 2
54+
seed int64
5455
)
5556

5657
pflag.StringVarP(&casing, "casing", "c", casing, "set the casing of the generated name <title|upper|lower>")
5758
pflag.StringVarP(&delimiter, "delimiter", "d", delimiter, "set the delimiter used to join words")
59+
pflag.StringVarP(&format, "format", "f", format, "set the output format <plain|json>")
5860
pflag.IntVarP(&quantity, "quantity", "q", quantity, "set the number of names to generate")
5961
pflag.UintVarP(&size, "size", "z", size, "set the number of words in the generated name (minimum 2, maximum 4)")
60-
pflag.Int64VarP(&seed, "seed", "s", seed, "random generator seed")
62+
pflag.Int64VarP(&seed, "seed", "s", 0, "random generator seed")
6163
pflag.BoolVarP(&help, "help", "h", help, "show fname usage")
6264
pflag.BoolVarP(&ver, "version", "v", ver, "show fname version")
6365
pflag.Parse()
@@ -72,27 +74,49 @@ func main() {
7274
os.Exit(0)
7375
}
7476

75-
c, err := fname.CasingFromString(casing)
77+
if quantity <= 0 {
78+
log.Fatalf("error: quantity must be greater than 0, got %d", quantity)
79+
}
80+
81+
if format != "plain" && format != "json" {
82+
log.Fatalf("error: invalid format %q, must be plain or json", format)
83+
}
84+
85+
c, err := fname.ParseCasing(casing)
7686
handleError(err)
7787

7888
opts := []fname.GeneratorOption{
7989
fname.WithCasing(c),
8090
fname.WithDelimiter(delimiter),
8191
}
8292

83-
if seed != -1 {
93+
if pflag.Lookup("seed").Changed {
8494
opts = append(opts, fname.WithSeed(seed))
8595
}
8696
if size != 2 {
87-
opts = append(opts, fname.WithSize(size))
97+
sizeOpt, err := fname.WithSize(size)
98+
handleError(err)
99+
opts = append(opts, sizeOpt)
88100
}
89101

90102
rng := fname.NewGenerator(opts...)
91103

104+
names := make([]string, 0, quantity)
92105
for i := 0; i < quantity; i++ {
93106
name, err := rng.Generate()
94107
handleError(err)
95-
fmt.Println(name)
108+
names = append(names, name)
109+
}
110+
111+
switch format {
112+
case "json":
113+
out, err := json.Marshal(names)
114+
handleError(err)
115+
fmt.Println(string(out))
116+
default:
117+
for _, name := range names {
118+
fmt.Println(name)
119+
}
96120
}
97121
}
98122

data/adjective

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ certified
189189
cerulean
190190
challenging
191191
changeable
192-
characteristic
193192
charged
194193
charismatic
195194
charmed
@@ -198,10 +197,7 @@ chaste
198197
cheap
199198
cheerful
200199
cheery
201-
chemical
202-
cherry
203200
chic
204-
chief
205201
childish
206202
chilled
207203
chilling
@@ -216,7 +212,6 @@ chubby
216212
circular
217213
civic
218214
civil
219-
civilian
220215
civilized
221216
clammy
222217
clandestine
@@ -280,7 +275,6 @@ constant
280275
constitutional
281276
constructive
282277
contemplative
283-
content
284278
continental
285279
continuous
286280
contradictory
@@ -290,7 +284,6 @@ conventional
290284
convivial
291285
cool
292286
cooperative
293-
copper
294287
corporate
295288
correct
296289
cosmic
@@ -424,7 +417,6 @@ elliptical
424417
eloquent
425418
elusive
426419
elysian
427-
emerald
428420
emotional
429421
emphatic
430422
empirical
@@ -645,7 +637,6 @@ glowing
645637
glum
646638
glutinous
647639
gnomish
648-
gold
649640
golden
650641
good
651642
gooey
@@ -817,7 +808,6 @@ jealous
817808
jingoistic
818809
jittery
819810
jocular
820-
joint
821811
jolly
822812
jolting
823813
jovial
@@ -872,7 +862,6 @@ likely
872862
limited
873863
linear
874864
lined
875-
linen
876865
linguistic
877866
liquid
878867
literal
@@ -958,7 +947,6 @@ milky
958947
mindful
959948
minimum
960949
minor
961-
mint
962950
minty
963951
miraculous
964952
mirthful
@@ -1104,7 +1092,6 @@ passive
11041092
past
11051093
pastel
11061094
pastoral
1107-
patient
11081095
patriotic
11091096
peaceful
11101097
pearly
@@ -1153,8 +1140,6 @@ pithy
11531140
placid
11541141
plain
11551142
planetary
1156-
plastic
1157-
platinum
11581143
plausible
11591144
pleasant
11601145
plentiful
@@ -1219,7 +1204,6 @@ pyrrhic
12191204
quaint
12201205
qualified
12211206
qualitative
1222-
quality
12231207
quantitative
12241208
quantum
12251209
quick
@@ -1269,11 +1253,9 @@ relevant
12691253
reliable
12701254
religious
12711255
repetitious
1272-
representative
12731256
reptilian
12741257
requisite
12751258
reserved
1276-
resident
12771259
residential
12781260
resilient
12791261
resolute
@@ -1308,7 +1290,6 @@ rotten
13081290
rough
13091291
round
13101292
routine
1311-
ruby
13121293
ruddy
13131294
runic
13141295
rural
@@ -1380,7 +1361,6 @@ shy
13801361
sick
13811362
significant
13821363
silly
1383-
silver
13841364
similar
13851365
simplistic
13861366
sincere
@@ -1704,7 +1684,6 @@ weary
17041684
weekly
17051685
welcome
17061686
welcoming
1707-
well
17081687
wet
17091688
whimsical
17101689
whiskered

0 commit comments

Comments
 (0)