Skip to content

Commit a961ac6

Browse files
committed
fixed TokenChars getting permanently set to hex if TokenHex is true
1 parent f199bee commit a961ac6

File tree

4 files changed

+38
-24
lines changed

4 files changed

+38
-24
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ go:
55
- 1.4
66
- 1.5
77
- 1.6
8+
- 1.7
89
- tip
910

1011
before_install:
11-
- go get github.com/axw/gocov/gocov
1212
- go get github.com/mattn/goveralls
13-
- if ! go get github.com/golang/tools/cmd/cover; then go get golang.org/x/tools/cmd/cover; fi
13+
- go get golang.org/x/tools/cmd/cover
1414

1515
script:
1616
- $HOME/gopath/bin/goveralls -service=travis-ci

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2015, Atrox
1+
Copyright (c) 2016, Atrox
22
All rights reserved.
33

44
Redistribution and use in source and binary forms, with or without

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
[![Build Status](https://img.shields.io/travis/Atrox/haikunatorgo.svg?style=flat-square)](https://travis-ci.org/Atrox/haikunatorgo)
44
[![Coverage Status](https://img.shields.io/coveralls/Atrox/haikunatorgo.svg?style=flat-square)](https://coveralls.io/r/Atrox/haikunatorgo)
5+
[![Go Report Card](https://goreportcard.com/badge/github.com/atrox/haikunatorgo?style=flat-square)](https://goreportcard.com/report/github.com/atrox/haikunatorgo)
56
[![GoDoc](https://img.shields.io/badge/godoc-reference-5272B4.svg?style=flat-square)](https://godoc.org/github.com/Atrox/haikunatorgo)
67

78
Generate Heroku-like random names to use in your go applications.
@@ -72,8 +73,8 @@ The following options are available:
7273

7374
```go
7475
Haikunator{
75-
Adjectives: []string{"..."},
76-
Nouns: []string{"..."},
76+
Adjectives: []string{"custom", "adjectives"},
77+
Nouns: []string{"custom", "nouns"},
7778
Delimiter: "-",
7879
TokenLength: 4,
7980
TokenHex: false,

haikunator.go

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"math/rand"
77
"strings"
88
"time"
9-
"unicode/utf8"
109
)
1110

1211
// A Haikunator represents all options needed to use haikunate()
@@ -53,6 +52,11 @@ var nouns = []string{
5352
"wood",
5453
}
5554

55+
const (
56+
numbers = "0123456789"
57+
hex = "0123456789abcdef"
58+
)
59+
5660
// NewHaikunator creates a new Haikunator with all default options
5761
func NewHaikunator() Haikunator {
5862
return Haikunator{
@@ -61,49 +65,58 @@ func NewHaikunator() Haikunator {
6165
Delimiter: "-",
6266
TokenLength: 4,
6367
TokenHex: false,
64-
TokenChars: "0123456789",
68+
TokenChars: numbers,
6569
Random: rand.New(rand.NewSource(time.Now().UnixNano())),
6670
}
6771
}
6872

6973
// Haikunate generates a random Heroku-like string
7074
func (h *Haikunator) Haikunate() string {
71-
if h.TokenHex {
72-
h.TokenChars = "0123456789abcdef"
73-
}
74-
7575
adjective := h.randomString(h.Adjectives)
7676
noun := h.randomString(h.Nouns)
77+
token := h.buildToken()
7778

78-
var token string
79+
sections := deleteEmpty([]string{adjective, noun, token})
80+
return strings.Join(sections, h.Delimiter)
81+
}
7982

80-
if len(h.TokenChars) > 0 {
81-
var buffer bytes.Buffer
83+
func (h *Haikunator) buildToken() string {
84+
var chars []rune
8285

83-
for i := 0; i < h.TokenLength; i++ {
84-
randomIndex := h.Random.Intn(utf8.RuneCountInString(h.TokenChars))
85-
buffer.WriteRune([]rune(h.TokenChars)[randomIndex])
86-
}
86+
if h.TokenHex {
87+
chars = []rune(hex)
88+
} else {
89+
chars = []rune(h.TokenChars)
90+
}
91+
92+
size := len(chars)
8793

88-
token = buffer.String()
94+
if size <= 0 {
95+
return ""
8996
}
9097

91-
sections := deleteEmpty([]string{adjective, noun, token})
92-
return strings.Join(sections, h.Delimiter)
98+
var buffer bytes.Buffer
99+
100+
for i := 0; i < h.TokenLength; i++ {
101+
index := h.Random.Intn(size)
102+
buffer.WriteRune(chars[index])
103+
}
104+
105+
return buffer.String()
93106
}
94107

95-
// Get random string from string array
108+
// Get random string from slice
96109
func (h *Haikunator) randomString(s []string) string {
97110
size := len(s)
98111

99-
if size <= 0 { // Random.Intn panics otherwise
112+
if size <= 0 {
100113
return ""
101114
}
102115

103116
return s[h.Random.Intn(size)]
104117
}
105118

106-
// Deletes empty strings from string array
119+
// Deletes empty strings from slice
107120
func deleteEmpty(s []string) []string {
108121
var r []string
109122
for _, str := range s {

0 commit comments

Comments
 (0)