Compare commits
3 commits
cdf12a767c
...
81e04ea319
| Author | SHA1 | Date | |
|---|---|---|---|
| 81e04ea319 | |||
| b6994e73a8 | |||
| c5a9f09166 |
8 changed files with 38 additions and 34 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
package nomino
|
package nomino
|
||||||
|
|
||||||
|
// Config controls how the generatred filename is created.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
original string
|
original string
|
||||||
prefix string
|
prefix string
|
||||||
|
|
@ -9,7 +10,9 @@ type Config struct {
|
||||||
generator Generator
|
generator Generator
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewConfig returns a new Config with the specified Options.
|
// NewConfig returns a new [Config] with the specified [Option]s.
|
||||||
|
// With no Options, the Config uses an extension of .txt, a separator
|
||||||
|
// of _, and the [UUID] [Generator].
|
||||||
func NewConfig(options ...Option) Config {
|
func NewConfig(options ...Option) Config {
|
||||||
conf := Config{
|
conf := Config{
|
||||||
extension: ".txt",
|
extension: ".txt",
|
||||||
|
|
@ -22,7 +25,7 @@ func NewConfig(options ...Option) Config {
|
||||||
return conf
|
return conf
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddOptions creates a new Config with options added.
|
// AddOptions creates a new [Config] with the given [Option]s added.
|
||||||
func (c Config) AddOptions(options ...Option) Config {
|
func (c Config) AddOptions(options ...Option) Config {
|
||||||
for _, opt := range options {
|
for _, opt := range options {
|
||||||
opt(&c)
|
opt(&c)
|
||||||
|
|
|
||||||
12
gen_file.go
12
gen_file.go
|
|
@ -9,7 +9,7 @@ import (
|
||||||
"github.com/gosimple/slug"
|
"github.com/gosimple/slug"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrMissingOriginal is the error returned by Slug if there is no filename.
|
// ErrMissingOriginal is the error returned by [Slug] if there is no filename.
|
||||||
var ErrMissingOriginal = errors.New("missing original filename")
|
var ErrMissingOriginal = errors.New("missing original filename")
|
||||||
|
|
||||||
func getOriginal(c *Config) (string, error) {
|
func getOriginal(c *Config) (string, error) {
|
||||||
|
|
@ -37,21 +37,21 @@ func Slug(lang ...string) Generator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// HashingFunc is a function that generates a hash.Hash.
|
// HashingFunc is a function that generates a [hash.Hash].
|
||||||
type HashingFunc func() hash.Hash
|
type HashingFunc func() hash.Hash
|
||||||
|
|
||||||
// New allows HashingFunc to be used as a Hasher.
|
// New allows [HashingFunc] to be used as a [Hasher].
|
||||||
func (hf HashingFunc) New() hash.Hash {
|
func (hf HashingFunc) New() hash.Hash {
|
||||||
return hf()
|
return hf()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hasher is a type returns a hash.Hash.
|
// Hasher is a type returns a [hash.Hash].
|
||||||
// All crypto.Hash may be used.
|
// All [crypto.Hash] may be used.
|
||||||
type Hasher interface {
|
type Hasher interface {
|
||||||
New() hash.Hash
|
New() hash.Hash
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrInvalidHash is returned by the Hash generator when an invalid HashType is passed.
|
// ErrInvalidHash is returned by the [Hash] [Generator] when an invalid [Hasher] is passed.
|
||||||
var ErrInvalidHash = errors.New("invalid hash type")
|
var ErrInvalidHash = errors.New("invalid hash type")
|
||||||
|
|
||||||
// Hash generates a name from a hash of the filename.
|
// Hash generates a name from a hash of the filename.
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ type incConf struct {
|
||||||
cb func(int) string
|
cb func(int) string
|
||||||
}
|
}
|
||||||
|
|
||||||
// IncrementalOption sets an option for the Incremental Generator.
|
// IncrementalOption sets an option for the [Incremental] [Generator].
|
||||||
type IncrementalOption func(c *incConf)
|
type IncrementalOption func(c *incConf)
|
||||||
|
|
||||||
// Incremental generates a name that is a series of integers.
|
// Incremental generates a name that is a series of integers.
|
||||||
|
|
@ -30,21 +30,21 @@ func Incremental(opts ...IncrementalOption) Generator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// IncrementalStart sets the starting integer for Incremental.
|
// IncrementalStart sets the starting integer for [Incremental].
|
||||||
func IncrementalStart(start int) IncrementalOption {
|
func IncrementalStart(start int) IncrementalOption {
|
||||||
return func(c *incConf) {
|
return func(c *incConf) {
|
||||||
c.start = start
|
c.start = start
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// IncrementalStepsets the step by which Incremental increases with each invocation.
|
// IncrementalStepsets the step by which [Incremental] increases with each invocation.
|
||||||
func IncrementalStep(step int) IncrementalOption {
|
func IncrementalStep(step int) IncrementalOption {
|
||||||
return func(c *incConf) {
|
return func(c *incConf) {
|
||||||
c.step = step
|
c.step = step
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// IncrementalFormatsets the format for the number generated by Incremental.
|
// IncrementalFormatsets the format for the number generated by [Incremental].
|
||||||
// It will be formatted with Printf. This is mostly likely useful with a format like "%02d".
|
// It will be formatted with Printf. This is mostly likely useful with a format like "%02d".
|
||||||
func IncrementalFormat(format string) IncrementalOption {
|
func IncrementalFormat(format string) IncrementalOption {
|
||||||
return func(c *incConf) {
|
return func(c *incConf) {
|
||||||
|
|
|
||||||
12
gen_rand.go
12
gen_rand.go
|
|
@ -9,7 +9,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// UUIDer is an interface for generating UUIDs.
|
// UUIDer is an interface for generating UUIDs.
|
||||||
// It is recommended that you use either the UUIDv4 or UUIDv7 variables.
|
// It is recommended that you use either the [UUIDv4] or [UUIDv7] variables.
|
||||||
type UUIDer interface {
|
type UUIDer interface {
|
||||||
UUID() (uuid.UUID, error)
|
UUID() (uuid.UUID, error)
|
||||||
}
|
}
|
||||||
|
|
@ -17,7 +17,7 @@ type UUIDer interface {
|
||||||
// UUIDFunc is a function that generates a UUID.
|
// UUIDFunc is a function that generates a UUID.
|
||||||
type UUIDFunc func() (uuid.UUID, error)
|
type UUIDFunc func() (uuid.UUID, error)
|
||||||
|
|
||||||
// UUID allows UUIDFunc to be used as a UUIDer.
|
// UUID allows [UUIDFunc] to be used as a [UUIDer].
|
||||||
func (u UUIDFunc) UUID() (uuid.UUID, error) {
|
func (u UUIDFunc) UUID() (uuid.UUID, error) {
|
||||||
return u()
|
return u()
|
||||||
}
|
}
|
||||||
|
|
@ -33,7 +33,7 @@ var (
|
||||||
UUIDv7 = UUIDFunc(uuid.NewV7)
|
UUIDv7 = UUIDFunc(uuid.NewV7)
|
||||||
)
|
)
|
||||||
|
|
||||||
// UUID generates a UUID. If nil.is passed as an argument,
|
// UUID generates a UUID. If nil is passed as an argument,
|
||||||
// a UUIDv4 is generated.
|
// a UUIDv4 is generated.
|
||||||
func UUID(u UUIDer) Generator {
|
func UUID(u UUIDer) Generator {
|
||||||
if u == nil {
|
if u == nil {
|
||||||
|
|
@ -52,10 +52,10 @@ type randConf struct {
|
||||||
length int
|
length int
|
||||||
}
|
}
|
||||||
|
|
||||||
// RandomOption is an option for the Random Generator.
|
// RandomOption is an option for the [Random] [Generator].
|
||||||
type RandomOption func(*randConf)
|
type RandomOption func(*randConf)
|
||||||
|
|
||||||
// RandomLength controls the length of the string generated by Random.
|
// RandomLength controls the length of the string generated by [Random].
|
||||||
func RandomLength(length int) RandomOption {
|
func RandomLength(length int) RandomOption {
|
||||||
return func(c *randConf) {
|
return func(c *randConf) {
|
||||||
c.length = length
|
c.length = length
|
||||||
|
|
@ -75,7 +75,7 @@ func fillBuffer(buff *strings.Builder, length int) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Random generates a random string containing the characters [A-Za-z0-9].
|
// Random generates a random string containing the characters A-Za-z0-9.
|
||||||
// By default, it will be eight characters long.
|
// By default, it will be eight characters long.
|
||||||
func Random(opts ...RandomOption) Generator {
|
func Random(opts ...RandomOption) Generator {
|
||||||
c := randConf{8}
|
c := randConf{8}
|
||||||
|
|
|
||||||
10
gen_ts.go
10
gen_ts.go
|
|
@ -2,10 +2,10 @@ package nomino
|
||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
// FileTimestamp is the default format for WithTimestamp and WithTime.
|
// FileTimestamp is the default format for [Timestamp].
|
||||||
const FileTimestamp string = "2006-01-02T15-04-05-0700"
|
const FileTimestamp string = "2006-01-02T15-04-05-0700"
|
||||||
|
|
||||||
// FileTimestampNoTZ is the default format for WithTimestampUTC and WithTimeUTC.
|
// FileTimestampNoTZ is the default format when using the [TimestampUTC] [TimestampOption].
|
||||||
const FileTimestampNoTZ string = "2006-01-02T15-04-05"
|
const FileTimestampNoTZ string = "2006-01-02T15-04-05"
|
||||||
|
|
||||||
type timestampConf struct {
|
type timestampConf struct {
|
||||||
|
|
@ -14,7 +14,7 @@ type timestampConf struct {
|
||||||
utc bool
|
utc bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// TimestampOption provides options for the Timestamp Generator.
|
// TimestampOption provides options for the [Timestamp] [Generator].
|
||||||
type TimestampOption func(c *timestampConf)
|
type TimestampOption func(c *timestampConf)
|
||||||
|
|
||||||
// Timestamp generates a a date and time. By default, it uses the current time, and will.
|
// Timestamp generates a a date and time. By default, it uses the current time, and will.
|
||||||
|
|
@ -35,7 +35,7 @@ func Timestamp(opts ...TimestampOption) Generator {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TimestampFormat sets the format for the generated name.
|
// TimestampFormat sets the format for the generated name.
|
||||||
// Consult time.Time.Format for details on the format.
|
// Consult [time.Time.Format] for details on the format.
|
||||||
func TimestampFormat(format string) TimestampOption {
|
func TimestampFormat(format string) TimestampOption {
|
||||||
return func(c *timestampConf) {
|
return func(c *timestampConf) {
|
||||||
c.format = format
|
c.format = format
|
||||||
|
|
@ -43,7 +43,7 @@ func TimestampFormat(format string) TimestampOption {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TimestampTime sets the time for the generated name.
|
// TimestampTime sets the time for the generated name.
|
||||||
// By default, it uses the current time.
|
// By default, [Timestamp] uses the current time.
|
||||||
func TimestampTime(t time.Time) TimestampOption {
|
func TimestampTime(t time.Time) TimestampOption {
|
||||||
return func(c *timestampConf) {
|
return func(c *timestampConf) {
|
||||||
c.ts = t
|
c.ts = t
|
||||||
|
|
|
||||||
|
|
@ -10,25 +10,25 @@ 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) {
|
||||||
return g.MakeWithConfig(NewConfig(opts...))
|
return g.MakeWithConfig(NewConfig(opts...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// MakeWithConfig allows you to generate a new string directly from a Generator
|
// MakeWithConfig allows you to generate a new string directly from a [Generator]
|
||||||
// with a pre-existing Config.
|
// with a pre-existing Config.
|
||||||
func (g Generator) MakeWithConfig(c Config) (string, error) {
|
func (g Generator) MakeWithConfig(c Config) (string, error) {
|
||||||
return Make(c.AddOptions(WithGenerator(g)))
|
return Make(c.AddOptions(WithGenerator(g)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithGenerator sets the specified generator.
|
// WithGenerator sets the specified [Generator].
|
||||||
func WithGenerator(g Generator) Option {
|
func WithGenerator(g Generator) Option {
|
||||||
return func(c *Config) {
|
return func(c *Config) {
|
||||||
c.generator = g
|
c.generator = g
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrMissingGenerators is returned by a multi-generator if no generators are supplied.
|
// ErrMissingGenerators is returned by a multi-generator if no [Generator]s are supplied.
|
||||||
var ErrMissingGenerators = errors.New("no generators supplied")
|
var ErrMissingGenerators = errors.New("no generators supplied")
|
||||||
|
|
||||||
func missingGen(*Config) (string, error) {
|
func missingGen(*Config) (string, error) {
|
||||||
|
|
@ -36,7 +36,7 @@ func missingGen(*Config) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// MultiGeneratorInOrder allows the use of multiple generators. Each new invokation will use the next generator in turn.
|
// MultiGeneratorInOrder allows the use of multiple generators. Each new invokation will use the next generator in turn.
|
||||||
// If none are passed, the generator will always return ErrMissingGenerators.
|
// If none are passed, the generator will always return [ErrMissingGenerators].
|
||||||
func MultiGeneratorInOrder(gens ...Generator) Generator {
|
func MultiGeneratorInOrder(gens ...Generator) Generator {
|
||||||
if len(gens) == 0 {
|
if len(gens) == 0 {
|
||||||
return missingGen
|
return missingGen
|
||||||
|
|
@ -55,7 +55,7 @@ func MultiGeneratorInOrder(gens ...Generator) Generator {
|
||||||
}
|
}
|
||||||
|
|
||||||
// MultiGeneratorRandomOrder allows the use of multiple generators. Each new invokation will use one of the generators randomly.
|
// MultiGeneratorRandomOrder allows the use of multiple generators. Each new invokation will use one of the generators randomly.
|
||||||
// If none are passed, the generator will always return ErrMissingGenerators.
|
// If none are passed, the generator will always return [ErrMissingGenerators].
|
||||||
func MultiGeneratorRandomOrder(gens ...Generator) Generator {
|
func MultiGeneratorRandomOrder(gens ...Generator) Generator {
|
||||||
if len(gens) == 0 {
|
if len(gens) == 0 {
|
||||||
return missingGen
|
return missingGen
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
// Package nomino is a utility that allows us to generate random filenames.
|
// Package nomino is a utility that allows us to generate random filenames.
|
||||||
//
|
//
|
||||||
// There are two main methods of using nomino.
|
// There are two main methods of using nomino.
|
||||||
// 1. Using the `nomini.Make` function.
|
//
|
||||||
// 2. Creating a generator, and using its `Make` method.
|
// 1. Using the [Make] function.
|
||||||
|
// 2. Creating a generator, and using its [Generator.Make] method.
|
||||||
package nomino
|
package nomino
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"github.com/gosimple/slug"
|
"github.com/gosimple/slug"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Option sets configuration parameters for Config.
|
// Option sets configuration parameters for [Config].
|
||||||
type Option func(c *Config)
|
type Option func(c *Config)
|
||||||
|
|
||||||
// WithOriginal sets the original filename.
|
// WithOriginal sets the original filename.
|
||||||
|
|
@ -18,7 +18,7 @@ func WithOriginal(o string) Option {
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithOriginal sets the original filename as a slug.
|
// WithOriginal sets the original filename as a slug.
|
||||||
// This should not be used with the Slug Generator (as it would be redundant).
|
// This should not be used with the [Slug] [Generator] (as it would be redundant).
|
||||||
func WithOriginalSlug(o string) Option {
|
func WithOriginalSlug(o string) Option {
|
||||||
return func(c *Config) {
|
return func(c *Config) {
|
||||||
c.original = slug.Make(o)
|
c.original = slug.Make(o)
|
||||||
|
|
@ -26,7 +26,7 @@ func WithOriginalSlug(o string) Option {
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithOriginal sets the original filename as a slug, taking the language into account.
|
// WithOriginal sets the original filename as a slug, taking the language into account.
|
||||||
// This should not be used with the Slug Generator (as it would be redundant).
|
// This should not be used with the [Slug] [Generator] (as it would be redundant).
|
||||||
func WithOriginalSlugLang(o, lang string) Option {
|
func WithOriginalSlugLang(o, lang string) Option {
|
||||||
return func(c *Config) {
|
return func(c *Config) {
|
||||||
c.original = slug.MakeLang(o, lang)
|
c.original = slug.MakeLang(o, lang)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue