Fixing docs a bit
This commit is contained in:
parent
6b3e2ac47e
commit
8268934635
32
config.go
32
config.go
@ -20,7 +20,7 @@ const (
|
||||
defaultSize = 4
|
||||
)
|
||||
|
||||
// NewTinDConfig starts off a factory pattern to build a TinD
|
||||
// New starts off a factory pattern to build a TinD
|
||||
// with either the default values, or setting custom values
|
||||
// using the `With` functions
|
||||
// Example:
|
||||
@ -39,25 +39,24 @@ const (
|
||||
// Example:
|
||||
//
|
||||
// myNumberOnlyConfig := New().WithSize(8).WithRuneset("0123456789")
|
||||
func NewTinDConfig() *Config {
|
||||
return &Config{
|
||||
func New() *Config {
|
||||
c := &Config{
|
||||
size: defaultSize,
|
||||
runes: []rune(defaultRunes),
|
||||
runesize: len(defaultRunes),
|
||||
mod: byte(len(defaultRunes) - 1),
|
||||
}
|
||||
c.calcMod()
|
||||
return c
|
||||
}
|
||||
|
||||
// NewConfigFrom will build a configuration from a byte slice. Because this is a
|
||||
// NewFrom will build a configuration from a byte slice. Because this is a
|
||||
// byte slice, the provided input doesn't matter, it's more about the length,
|
||||
// not how you use it 😉
|
||||
func NewConfigFrom(in []byte) *Config {
|
||||
return &Config{
|
||||
size: len(in),
|
||||
runes: []rune(defaultRunes),
|
||||
runesize: len(defaultRunes),
|
||||
mod: byte(len(defaultRunes) - 1),
|
||||
}
|
||||
func NewFrom(in []byte) *Config {
|
||||
c := New()
|
||||
c.size = len(in)
|
||||
c.calcMod()
|
||||
return c
|
||||
}
|
||||
|
||||
// WithRuneset requires a string as input, where each character must be unique.
|
||||
@ -73,10 +72,17 @@ func NewConfigFrom(in []byte) *Config {
|
||||
func (c *Config) WithRuneset(r []rune) *Config {
|
||||
c.runes = ensureUniqueRunes(r)
|
||||
c.runesize = len(c.runes)
|
||||
c.mod = byte(c.runesize - 1)
|
||||
c.calcMod()
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Config) calcMod() {
|
||||
c.mod = byte(c.runesize - 1)
|
||||
}
|
||||
|
||||
// ensureUniqueRunes loops through the provided runeset to make sure only unique
|
||||
// runes are included. This ensures that, given the same runeset in the same
|
||||
// order, a TinD can be decoded from a string.
|
||||
func ensureUniqueRunes(r []rune) []rune {
|
||||
unq := make(map[rune]int)
|
||||
var index int
|
||||
|
11
tind.go
11
tind.go
@ -1,3 +1,4 @@
|
||||
// Package tind provides a simple random identifier as a byte slice
|
||||
package tind
|
||||
|
||||
// TinD: Because a UUID is too big for a lot of people
|
||||
@ -9,8 +10,8 @@ package tind
|
||||
// whatever unicode characters you want, as the alphabet it uses is just a slice
|
||||
// of runes
|
||||
|
||||
// defaults are just a call to NewConfig()
|
||||
var defaults = NewTinDConfig()
|
||||
// defaults are just a call to New()
|
||||
var defaults = New()
|
||||
|
||||
// TinD represents a very simple `size` byte unique ID that is generated
|
||||
// at random. TinD stands for Tiny iDentifier, and is pronouced "tind"
|
||||
@ -43,3 +44,9 @@ func (t TinD) Bytes() []byte {
|
||||
return t.bytes
|
||||
}
|
||||
|
||||
// Config allows access to the underlying configuration. When used with the
|
||||
// default TinD (ie. by calling the package level Gen()), allows the user to
|
||||
// modify the default configuration.
|
||||
func (t TinD) Config() *Config {
|
||||
return t.config
|
||||
}
|
||||
|
14
tind_test.go
14
tind_test.go
@ -34,7 +34,7 @@ func Test_GenTinD_With_VaryingSizes(t *testing.T) {
|
||||
},
|
||||
}
|
||||
for _, v := range tests {
|
||||
tt := NewTinDConfig().WithRuneset(v.runes).WithSize(v.size).Gen()
|
||||
tt := New().WithRuneset(v.runes).WithSize(v.size).Gen()
|
||||
if len(tt.Runes()) != v.size {
|
||||
t.Errorf("config set size to %d, but generated an id of size %d. id: %s", v.size, len([]rune(tt.String())), tt.String())
|
||||
}
|
||||
@ -47,7 +47,7 @@ func Test_GenTinD_With_VaryingSizes(t *testing.T) {
|
||||
func checkCollisions(tindSize int) {
|
||||
ids := make(map[string]struct{})
|
||||
collided := false
|
||||
tc := NewTinDConfig().WithSize(tindSize)
|
||||
tc := New().WithSize(tindSize)
|
||||
var iters uint64
|
||||
start := time.Now()
|
||||
|
||||
@ -84,16 +84,16 @@ func Test_GenerateConfigFromByteSlice(t *testing.T) {
|
||||
runeset: []rune(defaultRunes),
|
||||
},
|
||||
{
|
||||
name: "Custom runeset of 8 runes with size of 14",
|
||||
bytes: []byte("01234567890123"),
|
||||
name: "Custom runeset of 8 runes with size of 14",
|
||||
bytes: []byte("01234567890123"),
|
||||
expectedSize: 14,
|
||||
expectedMod: 7,
|
||||
runeset: []rune("aBcDeFgH"),
|
||||
expectedMod: 7,
|
||||
runeset: []rune("aBcDeFgH"),
|
||||
},
|
||||
}
|
||||
for _, v := range tests {
|
||||
t.Log(v.name)
|
||||
tt := NewConfigFrom(v.bytes).WithRuneset(v.runeset)
|
||||
tt := NewFrom(v.bytes).WithRuneset(v.runeset)
|
||||
if tt.mod != v.expectedMod {
|
||||
t.Errorf("expected mod %d, but got %d", v.expectedMod, tt.mod)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user