Compare commits
6 commits
55038ea295
...
c17f23595c
| Author | SHA1 | Date | |
|---|---|---|---|
| c17f23595c | |||
| f20e737f2b | |||
| 1abfaa44d1 | |||
| d7b14f804c | |||
| fee2e3cc2f | |||
| f121b7dbce |
8 changed files with 129 additions and 68 deletions
78
CHANGELOG.md
78
CHANGELOG.md
|
|
@ -1,84 +1,96 @@
|
||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
### [0.4.0] - 2025-03-15
|
### [0.5.0] - 2025-03-19 - ✨ Different types of UUIDs
|
||||||
|
|
||||||
#### Features
|
#### Features
|
||||||
|
|
||||||
- Add Random Generator
|
- ✨ Allow for different types of UUIDs in the UUID `Generator`
|
||||||
- Add Make method to Generator
|
- ✨ `Config.AddOptions` method
|
||||||
- Add MultiGeneratorRandomOrder
|
- ✨ `Generator.MakeWithConfig` method
|
||||||
|
|
||||||
#### Changes
|
|
||||||
|
|
||||||
- Replace HashType with Hasher: This supports all crypto.Hash
|
|
||||||
|
|
||||||
#### Support
|
#### Support
|
||||||
|
|
||||||
- Add some missing doc comments
|
- 📝 Some better examples`
|
||||||
|
|
||||||
### [0.3.0] - 2025-03-14
|
### [0.4.0] - 2025-03-15 - ✨ More Generators, and `Generator.Make` method
|
||||||
|
|
||||||
#### Features
|
#### Features
|
||||||
|
|
||||||
- Simplified multiple Generator functions to single function with options
|
- ✨ Add Random Generator
|
||||||
- Added a lot of examples for docs
|
- ✨ Add Make method to Generator
|
||||||
- Can add extra Options to Make
|
- ✨ Add MultiGeneratorRandomOrder
|
||||||
|
|
||||||
|
#### Changes
|
||||||
|
|
||||||
|
- 💔 Breaking changes: Replace HashType with Hasher: This supports all crypto.Hash
|
||||||
|
|
||||||
|
#### Support
|
||||||
|
|
||||||
|
- 📝 Add some missing doc comments
|
||||||
|
|
||||||
|
### [0.3.0] - 2025-03-14 - ♻️ Refactor multiple Generators into one
|
||||||
|
|
||||||
|
#### Features
|
||||||
|
|
||||||
|
- ♻️ Simplified multiple `Generator` functions to single function with options
|
||||||
|
- 📝 Added a lot of examples for docs
|
||||||
|
- ✨ Can add extra `Option`s to `Make`
|
||||||
|
|
||||||
Multiple breaking changes around Generators.
|
Multiple breaking changes around Generators.
|
||||||
|
|
||||||
#### Bugs
|
#### Bugs
|
||||||
|
|
||||||
- Fixed date formats
|
- 🐛 Fixed date formats
|
||||||
|
|
||||||
### [0.2.1] - 2025-03-14
|
### [0.2.1] - 2025-03-14 - ✨ New Hash Generator
|
||||||
|
|
||||||
#### Features
|
#### Features
|
||||||
|
|
||||||
- Add Hash Generator
|
- ✨ Add Hash Generator
|
||||||
|
|
||||||
#### Dev Tooling
|
#### Dev Tooling
|
||||||
|
|
||||||
- Added a task to serve docs
|
- 🛠 Added a task to serve docs
|
||||||
- Added tasts to check code complexity
|
- 🛠 Added tasts to check code complexity
|
||||||
|
|
||||||
#### Miscellaneous
|
#### Miscellaneous
|
||||||
|
|
||||||
- Fixed some `go vet` complaints
|
- 💚 Fixed some `go vet` complaints
|
||||||
|
|
||||||
### [0.2.0] - 2025-03-14
|
### [0.2.0] - 2025-03-14 - ✨ New `Generator`s
|
||||||
|
|
||||||
#### Features
|
#### Features
|
||||||
|
|
||||||
- Add `IncrementalFormat`* Generators
|
- ✨ Add `IncrementalFormat`* Generators
|
||||||
- Add `Slug`* Generators
|
- ✨ Add `Slug`* Generators
|
||||||
- Add `WithOriginalSlug`* Options
|
- ✨ Add `WithOriginalSlug`* Options
|
||||||
- Change signature of `Generator` function
|
- 💔 Change signature of `Generator` function
|
||||||
|
|
||||||
Note that this last change **is** a breaking change from 0.0.3, but only for custom Generators.
|
Note that this last change **is** a breaking change from 0.0.3, but only for custom Generators.
|
||||||
|
|
||||||
## [0.0.3] - 2025-03-11
|
## [0.0.3] - 2025-03-11 - ✨ `WithSeparator`
|
||||||
|
|
||||||
### Features
|
### Features
|
||||||
|
|
||||||
- Added `WithSeparator` to allow for different separators between the parts of the generated filename.
|
- ✨ Added `WithSeparator` to allow for different separators between the parts of the generated filename.
|
||||||
|
|
||||||
## [0.0.2] - 2025-03-11
|
## [0.0.2] - 2025-03-11 - 🐛 Bugfix
|
||||||
|
|
||||||
Bugfix release
|
Bugfix release
|
||||||
|
|
||||||
### Fixes
|
### Fixes
|
||||||
|
|
||||||
- Extension being ignored. Original included twice.
|
- 🐛 Extension being ignored. Original included twice.
|
||||||
|
|
||||||
## [0.0.1] - 2025-03-10
|
## [0.0.1] - 2025-03-10 - 🚀 Initial Release
|
||||||
|
|
||||||
Initial Release! Hope you like it!
|
Initial Release! Hope you like it!
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
- nomino.Make
|
- ✨ `nomino.Make``
|
||||||
- nomino.Config
|
- ✨ `nomino.Config`
|
||||||
- nomino.Generator
|
- ✨ `nomino.Generator`
|
||||||
+ We needs more of these until I'm ready
|
+ We needs more of these until I'm ready
|
||||||
- Lots of tests!
|
- ✅ Lots of tests!
|
||||||
|
|
||||||
|
|
|
||||||
10
config.go
10
config.go
|
|
@ -14,10 +14,18 @@ func NewConfig(options ...Option) Config {
|
||||||
conf := Config{
|
conf := Config{
|
||||||
extension: ".txt",
|
extension: ".txt",
|
||||||
separator: "_",
|
separator: "_",
|
||||||
generator: uuidGen,
|
generator: UUID(nil),
|
||||||
}
|
}
|
||||||
for _, opt := range options {
|
for _, opt := range options {
|
||||||
opt(&conf)
|
opt(&conf)
|
||||||
}
|
}
|
||||||
return conf
|
return conf
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddOptions creates a new Config with options added.
|
||||||
|
func (c Config) AddOptions(options ...Option) Config {
|
||||||
|
for _, opt := range options {
|
||||||
|
opt(&c)
|
||||||
|
}
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,3 +20,12 @@ func TestNewConfWithOpts(t *testing.T) {
|
||||||
assert.Equal(t, "", c.extension)
|
assert.Equal(t, "", c.extension)
|
||||||
assert.Equal(t, "foobar", c.prefix)
|
assert.Equal(t, "foobar", c.prefix)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConfAddOpts(t *testing.T) {
|
||||||
|
c := Config{original: "hi"}
|
||||||
|
c2 := c.AddOptions(WithOriginalSlug("Hello, my dear"), WithPrefix("yo"))
|
||||||
|
assert.Equal(t, "", c.prefix)
|
||||||
|
assert.Equal(t, "hi", c.original)
|
||||||
|
assert.Equal(t, "hello-my-dear", c2.original)
|
||||||
|
assert.Equal(t, "yo", c2.prefix)
|
||||||
|
}
|
||||||
|
|
|
||||||
43
gen_rand.go
43
gen_rand.go
|
|
@ -8,17 +8,44 @@ import (
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
func uuidGen(*Config) (string, error) {
|
// UUIDer is an interface for generating UUIDs.
|
||||||
u, err := uuid.NewRandom()
|
// It is recommended that you use either the UUIDv4 or UUIDv7 variables.
|
||||||
|
type UUIDer interface {
|
||||||
|
UUID() (uuid.UUID, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UUIDFunc is a function that generates a UUID.
|
||||||
|
type UUIDFunc func() (uuid.UUID, error)
|
||||||
|
|
||||||
|
// UUID allows UUIDFunc to be used as a UUIDer.
|
||||||
|
func (u UUIDFunc) UUID() (uuid.UUID, error) {
|
||||||
|
return u()
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
// UUIDv1. You probably don't want to use this. It is included for completeness sake.
|
||||||
|
UUIDv1 = UUIDFunc(uuid.NewUUID)
|
||||||
|
// UUIDv4 is the default.
|
||||||
|
UUIDv4 = UUIDFunc(uuid.NewRandom)
|
||||||
|
// UUIDv6 is primarily a replacement for UUIDv1. You probably should use 4 or 7.
|
||||||
|
UUIDv6 = UUIDFunc(uuid.NewV6)
|
||||||
|
// UUIDv7 should be used if you want it sortable by time.
|
||||||
|
UUIDv7 = UUIDFunc(uuid.NewV7)
|
||||||
|
)
|
||||||
|
|
||||||
|
// UUID generates a UUID. If nil.is passed as an argument,
|
||||||
|
// a UUIDv4 is generated.
|
||||||
|
func UUID(u UUIDer) Generator {
|
||||||
|
if u == nil {
|
||||||
|
u = UUIDv4
|
||||||
|
}
|
||||||
|
return func(*Config) (string, error) {
|
||||||
|
uu, err := u.UUID()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return u.String(), nil
|
return uu.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UUID generates a UUIDv4.
|
|
||||||
func UUID() Generator {
|
|
||||||
return uuidGen
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type randConf struct {
|
type randConf struct {
|
||||||
|
|
|
||||||
|
|
@ -7,30 +7,34 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExampleUUID() {
|
func ExampleUUID() {
|
||||||
option := nomino.WithGenerator(nomino.UUID())
|
gen := nomino.UUID(nil)
|
||||||
|
|
||||||
str, _ := nomino.Make(nomino.NewConfig(option))
|
str, _ := gen.Make()
|
||||||
fmt.Println(str)
|
fmt.Println(str)
|
||||||
|
|
||||||
str, _ = nomino.Make(nomino.NewConfig(option))
|
str, _ = gen.Make()
|
||||||
|
fmt.Println(str)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleUUID_v7() {
|
||||||
|
gen := nomino.UUID(nomino.UUIDv7)
|
||||||
|
|
||||||
|
str, _ := gen.Make()
|
||||||
fmt.Println(str)
|
fmt.Println(str)
|
||||||
|
|
||||||
str, _ = nomino.Make(nomino.NewConfig(option))
|
str, _ = gen.Make()
|
||||||
|
fmt.Println(str)
|
||||||
|
|
||||||
|
str, _ = gen.Make()
|
||||||
fmt.Println(str)
|
fmt.Println(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleRandom() {
|
func ExampleRandom() {
|
||||||
option := nomino.WithGenerator(nomino.Random())
|
str, _ := nomino.Random().Make()
|
||||||
|
|
||||||
str, _ := nomino.Make(nomino.NewConfig(option))
|
|
||||||
fmt.Println(str)
|
fmt.Println(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleRandomLength() {
|
func ExampleRandomLength() {
|
||||||
option := nomino.WithGenerator(nomino.Random(
|
str, _ := nomino.Random(nomino.RandomLength(32)).Make()
|
||||||
nomino.RandomLength(32),
|
|
||||||
))
|
|
||||||
|
|
||||||
str, _ := nomino.Make(nomino.NewConfig(option))
|
|
||||||
fmt.Println(str)
|
fmt.Println(str)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUUID(t *testing.T) {
|
func TestUUID(t *testing.T) {
|
||||||
st, err := UUID()(nil)
|
st, err := UUID(nil)(nil)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
_, parseErr := uuid.Parse(st)
|
_, parseErr := uuid.Parse(st)
|
||||||
assert.NoError(t, parseErr)
|
assert.NoError(t, parseErr)
|
||||||
|
|
@ -25,7 +25,7 @@ func TestUUIDFail(t *testing.T) {
|
||||||
uuid.SetRand(badRead{})
|
uuid.SetRand(badRead{})
|
||||||
defer uuid.SetRand(nil)
|
defer uuid.SetRand(nil)
|
||||||
|
|
||||||
_, err := UUID()(nil)
|
_, err := UUID(nil)(nil)
|
||||||
assert.Equal(t, errors.New("sorry"), err)
|
assert.Equal(t, errors.New("sorry"), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,7 @@ import (
|
||||||
|
|
||||||
func ExampleTimestamp() {
|
func ExampleTimestamp() {
|
||||||
gen := nomino.Timestamp()
|
gen := nomino.Timestamp()
|
||||||
conf := nomino.NewConfig(nomino.WithGenerator(gen))
|
s, _ := gen.Make()
|
||||||
s, _ := nomino.Make(conf)
|
|
||||||
fmt.Println(s)
|
fmt.Println(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -18,8 +17,7 @@ func ExampleTimestampTime() {
|
||||||
tz, _ := time.LoadLocation("America/New_York")
|
tz, _ := time.LoadLocation("America/New_York")
|
||||||
ts := time.Date(2009, time.January, 20, 12, 5, 0, 0, tz)
|
ts := time.Date(2009, time.January, 20, 12, 5, 0, 0, tz)
|
||||||
gen := nomino.Timestamp(nomino.TimestampTime(ts))
|
gen := nomino.Timestamp(nomino.TimestampTime(ts))
|
||||||
conf := nomino.NewConfig(nomino.WithGenerator(gen))
|
s, _ := gen.Make()
|
||||||
s, _ := nomino.Make(conf)
|
|
||||||
fmt.Println(s)
|
fmt.Println(s)
|
||||||
// Output: 2009-01-20T12-05-00-0500.txt
|
// Output: 2009-01-20T12-05-00-0500.txt
|
||||||
}
|
}
|
||||||
|
|
@ -28,8 +26,7 @@ func ExampleTimestampFormat() {
|
||||||
tz, _ := time.LoadLocation("America/New_York")
|
tz, _ := time.LoadLocation("America/New_York")
|
||||||
ts := time.Date(2009, time.January, 20, 12, 5, 0, 0, tz)
|
ts := time.Date(2009, time.January, 20, 12, 5, 0, 0, tz)
|
||||||
gen := nomino.Timestamp(nomino.TimestampTime(ts), nomino.TimestampFormat("2006#01#02<>15|04|05-0700"))
|
gen := nomino.Timestamp(nomino.TimestampTime(ts), nomino.TimestampFormat("2006#01#02<>15|04|05-0700"))
|
||||||
conf := nomino.NewConfig(nomino.WithGenerator(gen))
|
s, _ := gen.Make()
|
||||||
s, _ := nomino.Make(conf)
|
|
||||||
fmt.Println(s)
|
fmt.Println(s)
|
||||||
// Output: 2009#01#20<>12|05|00-0500.txt
|
// Output: 2009#01#20<>12|05|00-0500.txt
|
||||||
}
|
}
|
||||||
|
|
@ -38,8 +35,7 @@ func ExampleTimestampUTC() {
|
||||||
tz, _ := time.LoadLocation("America/New_York")
|
tz, _ := time.LoadLocation("America/New_York")
|
||||||
ts := time.Date(2009, time.January, 20, 12, 5, 0, 0, tz)
|
ts := time.Date(2009, time.January, 20, 12, 5, 0, 0, tz)
|
||||||
gen := nomino.Timestamp(nomino.TimestampTime(ts), nomino.TimestampUTC())
|
gen := nomino.Timestamp(nomino.TimestampTime(ts), nomino.TimestampUTC())
|
||||||
conf := nomino.NewConfig(nomino.WithGenerator(gen))
|
s, _ := gen.Make()
|
||||||
s, _ := nomino.Make(conf)
|
|
||||||
fmt.Println(s)
|
fmt.Println(s)
|
||||||
// Output: 2009-01-20T17-05-00.txt
|
// Output: 2009-01-20T17-05-00.txt
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,10 +10,15 @@ import (
|
||||||
// for example.
|
// for example.
|
||||||
type Generator func(conf *Config) (string, error)
|
type Generator func(conf *Config) (string, error)
|
||||||
|
|
||||||
// Make allows you to generate a new string directly from a generator.
|
// Make allows you to generate a new string directly from a Generator.
|
||||||
func (g Generator) Make(opts ...Option) (string, error) {
|
func (g Generator) Make(opts ...Option) (string, error) {
|
||||||
opts = append(opts, WithGenerator(g))
|
return g.MakeWithConfig(NewConfig(opts...))
|
||||||
return Make(NewConfig(opts...))
|
}
|
||||||
|
|
||||||
|
// MakeWithConfig allows you to generate a new string directly from a Generator
|
||||||
|
// with a pre-existing Config.
|
||||||
|
func (g Generator) MakeWithConfig(c Config) (string, error) {
|
||||||
|
return Make(c.AddOptions(WithGenerator(g)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithGenerator sets the specified generator
|
// WithGenerator sets the specified generator
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue