Skip to content

Commit

Permalink
add default generate
Browse files Browse the repository at this point in the history
  • Loading branch information
RyosukeCla committed Nov 19, 2024
1 parent 9f3c4da commit 3f01fef
Showing 1 changed file with 1 addition and 20 deletions.
21 changes: 1 addition & 20 deletions sleekid.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@ package sleekid

import (
"bytes"
"errors"
"fmt"
"time"

"crypto/rand"
)

var generator Generator
var generator Generator = NewGenerator(GeneratorInit{})

// Setup initializes the generator.
//
// It must be called before using New or Validate.
//
// sleekid.Setup(sleekid.GeneratorInit{
// ChecksumToken: 12345,
// RandomDigitsLength: 12,
// })
func Setup(init GeneratorInit) {
generator = NewGenerator(init)
Expand All @@ -28,49 +24,34 @@ func Setup(init GeneratorInit) {
// id, err := sleekid.New("usr")
// id, err := sleekid.New("usr", sleekid.WithRandomBytes(16))
func New(prefix string, options ...*GenerateOption) (SleekId, error) {
if generator == nil {
return nil, errors.New("must be initialized by sleekid.Setup")
}
return generator.New(prefix, options...)
}

// Prefix returns the prefix of the given id.
//
// sleekid.Prefix(id)
func Prefix(id SleekId) string {
if generator == nil {
return ""
}
return generator.Prefix(id)
}

// Timestamp returns the unix time of the timestamp part.
//
// sleekid.Timestamp(id)
func Timestamp(id SleekId) time.Time {
if generator == nil {
return time.Time{}
}
return generator.Timestamp(id)
}

// Validate checks if the given the timestamp and random part is valid.
//
// sleekid.Validate(id)
func Validate(id SleekId) bool {
if generator == nil {
return false
}
return generator.Validate(id)
}

// ValidateWithPrefix checks if the given id is valid with the specific prefix..
//
// sleekid.ValidateWithPrefix("usr", id)
func ValidateWithPrefix(prefix string, id SleekId) bool {
if generator == nil {
return false
}
return generator.ValidateWithPrefix(prefix, id)
}

Expand Down

0 comments on commit 3f01fef

Please sign in to comment.