Fixing docs a bit

This commit is contained in:
Matthew Stobbs 2024-02-24 13:16:04 -07:00
parent 6b3e2ac47e
commit 8268934635
3 changed files with 35 additions and 22 deletions

View File

@ -20,7 +20,7 @@ const (
defaultSize = 4 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 // with either the default values, or setting custom values
// using the `With` functions // using the `With` functions
// Example: // Example:
@ -39,25 +39,24 @@ const (
// Example: // Example:
// //
// myNumberOnlyConfig := New().WithSize(8).WithRuneset("0123456789") // myNumberOnlyConfig := New().WithSize(8).WithRuneset("0123456789")
func NewTinDConfig() *Config { func New() *Config {
return &Config{ c := &Config{
size: defaultSize, size: defaultSize,
runes: []rune(defaultRunes), runes: []rune(defaultRunes),
runesize: len(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, // byte slice, the provided input doesn't matter, it's more about the length,
// not how you use it 😉
func NewConfigFrom(in []byte) *Config { func NewConfigFrom(in []byte) *Config {
return &Config{ func NewFrom(in []byte) *Config {
size: len(in), c := New()
runes: []rune(defaultRunes), c.size = len(in)
runesize: len(defaultRunes), c.calcMod()
mod: byte(len(defaultRunes) - 1), return c
}
} }
// WithRuneset requires a string as input, where each character must be unique. // 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 { func (c *Config) WithRuneset(r []rune) *Config {
c.runes = ensureUniqueRunes(r) c.runes = ensureUniqueRunes(r)
c.runesize = len(c.runes) c.runesize = len(c.runes)
c.mod = byte(c.runesize - 1) c.calcMod()
return c 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 { func ensureUniqueRunes(r []rune) []rune {
unq := make(map[rune]int) unq := make(map[rune]int)
var index int var index int

11
tind.go
View File

@ -1,3 +1,4 @@
// Package tind provides a simple random identifier as a byte slice
package tind package tind
// TinD: Because a UUID is too big for a lot of people // 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 // whatever unicode characters you want, as the alphabet it uses is just a slice
// of runes // of runes
// defaults are just a call to NewConfig() // defaults are just a call to New()
var defaults = NewTinDConfig() var defaults = New()
// TinD represents a very simple `size` byte unique ID that is generated // TinD represents a very simple `size` byte unique ID that is generated
// at random. TinD stands for Tiny iDentifier, and is pronouced "tind" // at random. TinD stands for Tiny iDentifier, and is pronouced "tind"
@ -43,3 +44,9 @@ func (t TinD) Bytes() []byte {
return t.bytes 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
}

View File

@ -34,7 +34,7 @@ func Test_GenTinD_With_VaryingSizes(t *testing.T) {
}, },
} }
for _, v := range tests { 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 { 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()) 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) { func checkCollisions(tindSize int) {
ids := make(map[string]struct{}) ids := make(map[string]struct{})
collided := false collided := false
tc := NewTinDConfig().WithSize(tindSize) tc := New().WithSize(tindSize)
var iters uint64 var iters uint64
start := time.Now() start := time.Now()
@ -84,16 +84,16 @@ func Test_GenerateConfigFromByteSlice(t *testing.T) {
runeset: []rune(defaultRunes), runeset: []rune(defaultRunes),
}, },
{ {
name: "Custom runeset of 8 runes with size of 14", name: "Custom runeset of 8 runes with size of 14",
bytes: []byte("01234567890123"), bytes: []byte("01234567890123"),
expectedSize: 14, expectedSize: 14,
expectedMod: 7, expectedMod: 7,
runeset: []rune("aBcDeFgH"), runeset: []rune("aBcDeFgH"),
}, },
} }
for _, v := range tests { for _, v := range tests {
t.Log(v.name) t.Log(v.name)
tt := NewConfigFrom(v.bytes).WithRuneset(v.runeset) tt := NewFrom(v.bytes).WithRuneset(v.runeset)
if tt.mod != v.expectedMod { if tt.mod != v.expectedMod {
t.Errorf("expected mod %d, but got %d", v.expectedMod, tt.mod) t.Errorf("expected mod %d, but got %d", v.expectedMod, tt.mod)
} }