♻️ Refactor Incremental Generator to single function with options
This commit is contained in:
parent
63d538d889
commit
586fe4f1de
4 changed files with 176 additions and 182 deletions
55
gen_int.go
Normal file
55
gen_int.go
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
package nomino
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type incConf struct {
|
||||||
|
start int
|
||||||
|
step int
|
||||||
|
cb func(int) string
|
||||||
|
}
|
||||||
|
|
||||||
|
// IncrementalOption sets an option for the Incremental Generator
|
||||||
|
type IncrementalOption func(c *incConf)
|
||||||
|
|
||||||
|
// Incremental generates a name that is a series of integers.
|
||||||
|
// By default it begins at 0 and increments by 1 each time.
|
||||||
|
func Incremental(opts ...IncrementalOption) Generator {
|
||||||
|
c := incConf{step: 1, cb: strconv.Itoa}
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(&c)
|
||||||
|
}
|
||||||
|
|
||||||
|
next := c.start
|
||||||
|
return func(*Config) (string, error) {
|
||||||
|
out := c.cb(next)
|
||||||
|
next += c.step
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// IncrementalStart sets the starting integer for Incremental
|
||||||
|
func IncrementalStart(start int) IncrementalOption {
|
||||||
|
return func(c *incConf) {
|
||||||
|
c.start = start
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// IncrementalStepsets the step by which Incremental increases with each invocation.
|
||||||
|
func IncrementalStep(step int) IncrementalOption {
|
||||||
|
return func(c *incConf) {
|
||||||
|
c.step = step
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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"
|
||||||
|
func IncrementalFormat(format string) IncrementalOption {
|
||||||
|
return func(c *incConf) {
|
||||||
|
c.cb = func(i int) string {
|
||||||
|
return fmt.Sprintf(format, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
121
gen_int_examples_test.go
Normal file
121
gen_int_examples_test.go
Normal file
|
|
@ -0,0 +1,121 @@
|
||||||
|
package nomino_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"codeberg.org/danjones000/nomino"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExampleIncremental() {
|
||||||
|
conf := nomino.NewConfig(
|
||||||
|
nomino.WithPrefix("foo"),
|
||||||
|
nomino.WithGenerator(nomino.Incremental()),
|
||||||
|
)
|
||||||
|
|
||||||
|
str, _ := nomino.Make(conf)
|
||||||
|
fmt.Println(str)
|
||||||
|
|
||||||
|
str, _ = nomino.Make(conf)
|
||||||
|
fmt.Println(str)
|
||||||
|
|
||||||
|
str, _ = nomino.Make(conf)
|
||||||
|
fmt.Println(str)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// foo_0.txt
|
||||||
|
// foo_1.txt
|
||||||
|
// foo_2.txt
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleIncrementalStart() {
|
||||||
|
conf := nomino.NewConfig(
|
||||||
|
nomino.WithPrefix("foo"),
|
||||||
|
nomino.WithGenerator(nomino.Incremental(
|
||||||
|
nomino.IncrementalStart(42),
|
||||||
|
)),
|
||||||
|
)
|
||||||
|
|
||||||
|
str, _ := nomino.Make(conf)
|
||||||
|
fmt.Println(str)
|
||||||
|
|
||||||
|
str, _ = nomino.Make(conf)
|
||||||
|
fmt.Println(str)
|
||||||
|
|
||||||
|
str, _ = nomino.Make(conf)
|
||||||
|
fmt.Println(str)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// foo_42.txt
|
||||||
|
// foo_43.txt
|
||||||
|
// foo_44.txt
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleIncrementalStep() {
|
||||||
|
conf := nomino.NewConfig(
|
||||||
|
nomino.WithPrefix("foo"),
|
||||||
|
nomino.WithGenerator(nomino.Incremental(
|
||||||
|
nomino.IncrementalStep(2),
|
||||||
|
)),
|
||||||
|
)
|
||||||
|
|
||||||
|
str, _ := nomino.Make(conf)
|
||||||
|
fmt.Println(str)
|
||||||
|
|
||||||
|
str, _ = nomino.Make(conf)
|
||||||
|
fmt.Println(str)
|
||||||
|
|
||||||
|
str, _ = nomino.Make(conf)
|
||||||
|
fmt.Println(str)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// foo_0.txt
|
||||||
|
// foo_2.txt
|
||||||
|
// foo_4.txt
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleIncremental_withStartAndStep() {
|
||||||
|
conf := nomino.NewConfig(
|
||||||
|
nomino.WithPrefix("foo"),
|
||||||
|
nomino.WithGenerator(nomino.Incremental(
|
||||||
|
nomino.IncrementalStart(42),
|
||||||
|
nomino.IncrementalStep(2),
|
||||||
|
)),
|
||||||
|
)
|
||||||
|
|
||||||
|
str, _ := nomino.Make(conf)
|
||||||
|
fmt.Println(str)
|
||||||
|
|
||||||
|
str, _ = nomino.Make(conf)
|
||||||
|
fmt.Println(str)
|
||||||
|
|
||||||
|
str, _ = nomino.Make(conf)
|
||||||
|
fmt.Println(str)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// foo_42.txt
|
||||||
|
// foo_44.txt
|
||||||
|
// foo_46.txt
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleIncrementalFormat() {
|
||||||
|
conf := nomino.NewConfig(
|
||||||
|
nomino.WithPrefix("foo"),
|
||||||
|
nomino.WithGenerator(nomino.Incremental(
|
||||||
|
nomino.IncrementalFormat("%03d"),
|
||||||
|
)),
|
||||||
|
)
|
||||||
|
|
||||||
|
str, _ := nomino.Make(conf)
|
||||||
|
fmt.Println(str)
|
||||||
|
|
||||||
|
str, _ = nomino.Make(conf)
|
||||||
|
fmt.Println(str)
|
||||||
|
|
||||||
|
str, _ = nomino.Make(conf)
|
||||||
|
fmt.Println(str)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// foo_000.txt
|
||||||
|
// foo_001.txt
|
||||||
|
// foo_002.txt
|
||||||
|
}
|
||||||
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"hash"
|
"hash"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/gosimple/slug"
|
"github.com/gosimple/slug"
|
||||||
|
|
@ -64,60 +63,6 @@ func UUID() Generator {
|
||||||
return uuidGen
|
return uuidGen
|
||||||
}
|
}
|
||||||
|
|
||||||
// Incremental generates a name that is a series of integers starting at 0
|
|
||||||
func Incremental() Generator {
|
|
||||||
return IncrementalWithStartAndStep(0, 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// IncrementalFormat generates a name that is a series of integers starting at 0, formatted with Printf
|
|
||||||
// This is mostly likely useful with a format like "%02d"
|
|
||||||
func IncrementalFormat(format string) Generator {
|
|
||||||
return IncrementalFormatWithStartAndStep(0, 1, format)
|
|
||||||
}
|
|
||||||
|
|
||||||
// IncrementalWithStart generates a name that is a series of integers starting at the specified number
|
|
||||||
func IncrementalWithStart(start int) Generator {
|
|
||||||
return IncrementalWithStartAndStep(start, 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// IncrementalFormatWithStart generates a name that is a series of integers starting at the specified number, formatted with Printf
|
|
||||||
func IncrementalFormatWithStart(start int, format string) Generator {
|
|
||||||
return IncrementalFormatWithStartAndStep(start, 1, format)
|
|
||||||
}
|
|
||||||
|
|
||||||
// IncrementalWithStep generates a name that is a series of integers, starting at 0, and increasing the specified number each time
|
|
||||||
func IncrementalWithStep(step int) Generator {
|
|
||||||
return IncrementalWithStartAndStep(0, step)
|
|
||||||
}
|
|
||||||
|
|
||||||
// IncrementalFormatWithStep generates a name that is a series of integers, starting at 0, and increasing the specified number each time,
|
|
||||||
// formatted with Printf
|
|
||||||
func IncrementalFormatWithStep(step int, format string) Generator {
|
|
||||||
return IncrementalFormatWithStartAndStep(0, step, format)
|
|
||||||
}
|
|
||||||
func incrementalHelper(start, step int, cb func(int) string) Generator {
|
|
||||||
next := start
|
|
||||||
return func(*Config) (string, error) {
|
|
||||||
out := cb(next)
|
|
||||||
next += step
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// InrementalWithStartAndStep generates a name that is a series of integers, starting at the specified number,
|
|
||||||
// and increasing the specified step each time
|
|
||||||
func IncrementalWithStartAndStep(start, step int) Generator {
|
|
||||||
return incrementalHelper(start, step, strconv.Itoa)
|
|
||||||
}
|
|
||||||
|
|
||||||
// IncrementalFormatWithStartAndStep generates a name that is a series of integers, starting at the specified number,
|
|
||||||
// and increasing the specified step each time, formatted with Printf
|
|
||||||
func IncrementalFormatWithStartAndStep(start, step int, format string) Generator {
|
|
||||||
return incrementalHelper(start, step, func(i int) string {
|
|
||||||
return fmt.Sprintf(format, i)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,133 +19,6 @@ func ExampleWithGenerator_custom_generator() {
|
||||||
// hello
|
// hello
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleIncremental() {
|
|
||||||
conf := NewConfig(WithPrefix("foo"), WithGenerator(Incremental()))
|
|
||||||
|
|
||||||
str, _ := Make(conf)
|
|
||||||
fmt.Println(str)
|
|
||||||
|
|
||||||
str, _ = Make(conf)
|
|
||||||
fmt.Println(str)
|
|
||||||
|
|
||||||
str, _ = Make(conf)
|
|
||||||
fmt.Println(str)
|
|
||||||
|
|
||||||
// Output:
|
|
||||||
// foo_0.txt
|
|
||||||
// foo_1.txt
|
|
||||||
// foo_2.txt
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleIncrementalWithStart() {
|
|
||||||
conf := NewConfig(WithPrefix("foo"), WithGenerator(IncrementalWithStart(42)))
|
|
||||||
|
|
||||||
str, _ := Make(conf)
|
|
||||||
fmt.Println(str)
|
|
||||||
|
|
||||||
str, _ = Make(conf)
|
|
||||||
fmt.Println(str)
|
|
||||||
|
|
||||||
str, _ = Make(conf)
|
|
||||||
fmt.Println(str)
|
|
||||||
|
|
||||||
// Output:
|
|
||||||
// foo_42.txt
|
|
||||||
// foo_43.txt
|
|
||||||
// foo_44.txt
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleIncrementalWithStep() {
|
|
||||||
conf := NewConfig(WithPrefix("foo"), WithGenerator(IncrementalWithStep(2)))
|
|
||||||
|
|
||||||
str, _ := Make(conf)
|
|
||||||
fmt.Println(str)
|
|
||||||
|
|
||||||
str, _ = Make(conf)
|
|
||||||
fmt.Println(str)
|
|
||||||
|
|
||||||
str, _ = Make(conf)
|
|
||||||
fmt.Println(str)
|
|
||||||
|
|
||||||
// Output:
|
|
||||||
// foo_0.txt
|
|
||||||
// foo_2.txt
|
|
||||||
// foo_4.txt
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleIncrementalWithStartAndStep() {
|
|
||||||
conf := NewConfig(WithPrefix("foo"), WithGenerator(IncrementalWithStartAndStep(42, 2)))
|
|
||||||
|
|
||||||
str, _ := Make(conf)
|
|
||||||
fmt.Println(str)
|
|
||||||
|
|
||||||
str, _ = Make(conf)
|
|
||||||
fmt.Println(str)
|
|
||||||
|
|
||||||
str, _ = Make(conf)
|
|
||||||
fmt.Println(str)
|
|
||||||
|
|
||||||
// Output:
|
|
||||||
// foo_42.txt
|
|
||||||
// foo_44.txt
|
|
||||||
// foo_46.txt
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleIncrementalFormat() {
|
|
||||||
conf := NewConfig(
|
|
||||||
WithPrefix("foo"),
|
|
||||||
WithGenerator(IncrementalFormat("%03d")),
|
|
||||||
)
|
|
||||||
|
|
||||||
str, _ := Make(conf)
|
|
||||||
fmt.Println(str)
|
|
||||||
|
|
||||||
str, _ = Make(conf)
|
|
||||||
fmt.Println(str)
|
|
||||||
|
|
||||||
str, _ = Make(conf)
|
|
||||||
fmt.Println(str)
|
|
||||||
|
|
||||||
// Output:
|
|
||||||
// foo_000.txt
|
|
||||||
// foo_001.txt
|
|
||||||
// foo_002.txt
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleIncrementalFormatWithStart() {
|
|
||||||
conf := NewConfig(
|
|
||||||
WithPrefix("foo"),
|
|
||||||
WithGenerator(IncrementalFormatWithStart(9, "%02d")),
|
|
||||||
)
|
|
||||||
|
|
||||||
str, _ := Make(conf)
|
|
||||||
fmt.Println(str)
|
|
||||||
|
|
||||||
str, _ = Make(conf)
|
|
||||||
fmt.Println(str)
|
|
||||||
|
|
||||||
// Output:
|
|
||||||
// foo_09.txt
|
|
||||||
// foo_10.txt
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleIncrementalFormatWithStep() {
|
|
||||||
conf := NewConfig(
|
|
||||||
WithPrefix("foo"),
|
|
||||||
WithGenerator(IncrementalFormatWithStep(10, "%02d")),
|
|
||||||
)
|
|
||||||
|
|
||||||
str, _ := Make(conf)
|
|
||||||
fmt.Println(str)
|
|
||||||
|
|
||||||
str, _ = Make(conf)
|
|
||||||
fmt.Println(str)
|
|
||||||
|
|
||||||
// Output:
|
|
||||||
// foo_00.txt
|
|
||||||
// foo_10.txt
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleSlug() {
|
func ExampleSlug() {
|
||||||
conf := NewConfig(WithGenerator(Slug()), WithOriginal("My name is Jimmy"))
|
conf := NewConfig(WithGenerator(Slug()), WithOriginal("My name is Jimmy"))
|
||||||
str, _ := Make(conf)
|
str, _ := Make(conf)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue