Skip to content

Commit

Permalink
fixed TokenChars getting permanently set to hex if TokenHex is true
Browse files Browse the repository at this point in the history
  • Loading branch information
Atrox committed Aug 18, 2016
1 parent f199bee commit a961ac6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 24 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ go:
- 1.4
- 1.5
- 1.6
- 1.7
- tip

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

script:
- $HOME/gopath/bin/goveralls -service=travis-ci
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2015, Atrox
Copyright (c) 2016, Atrox
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

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

```go
Haikunator{
Adjectives: []string{"..."},
Nouns: []string{"..."},
Adjectives: []string{"custom", "adjectives"},
Nouns: []string{"custom", "nouns"},
Delimiter: "-",
TokenLength: 4,
TokenHex: false,
Expand Down
51 changes: 32 additions & 19 deletions haikunator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"math/rand"
"strings"
"time"
"unicode/utf8"
)

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

const (
numbers = "0123456789"
hex = "0123456789abcdef"
)

// NewHaikunator creates a new Haikunator with all default options
func NewHaikunator() Haikunator {
return Haikunator{
Expand All @@ -61,49 +65,58 @@ func NewHaikunator() Haikunator {
Delimiter: "-",
TokenLength: 4,
TokenHex: false,
TokenChars: "0123456789",
TokenChars: numbers,
Random: rand.New(rand.NewSource(time.Now().UnixNano())),
}
}

// Haikunate generates a random Heroku-like string
func (h *Haikunator) Haikunate() string {
if h.TokenHex {
h.TokenChars = "0123456789abcdef"
}

adjective := h.randomString(h.Adjectives)
noun := h.randomString(h.Nouns)
token := h.buildToken()

var token string
sections := deleteEmpty([]string{adjective, noun, token})
return strings.Join(sections, h.Delimiter)
}

if len(h.TokenChars) > 0 {
var buffer bytes.Buffer
func (h *Haikunator) buildToken() string {
var chars []rune

for i := 0; i < h.TokenLength; i++ {
randomIndex := h.Random.Intn(utf8.RuneCountInString(h.TokenChars))
buffer.WriteRune([]rune(h.TokenChars)[randomIndex])
}
if h.TokenHex {
chars = []rune(hex)
} else {
chars = []rune(h.TokenChars)
}

size := len(chars)

token = buffer.String()
if size <= 0 {
return ""
}

sections := deleteEmpty([]string{adjective, noun, token})
return strings.Join(sections, h.Delimiter)
var buffer bytes.Buffer

for i := 0; i < h.TokenLength; i++ {
index := h.Random.Intn(size)
buffer.WriteRune(chars[index])
}

return buffer.String()
}

// Get random string from string array
// Get random string from slice
func (h *Haikunator) randomString(s []string) string {
size := len(s)

if size <= 0 { // Random.Intn panics otherwise
if size <= 0 {
return ""
}

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

// Deletes empty strings from string array
// Deletes empty strings from slice
func deleteEmpty(s []string) []string {
var r []string
for _, str := range s {
Expand Down

0 comments on commit a961ac6

Please sign in to comment.