Compare commits
No commits in common. "28803e6a28d1b277811822b33545740899d648a0" and "84994d3b73070cf58669b945a95a365d6573552c" have entirely different histories.
28803e6a28
...
84994d3b73
9 changed files with 8 additions and 72 deletions
|
|
@ -1,11 +1,5 @@
|
||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
## [0.0.3] - 2025-03-11
|
|
||||||
|
|
||||||
### Features
|
|
||||||
|
|
||||||
- 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 release
|
Bugfix release
|
||||||
|
|
|
||||||
|
|
@ -5,14 +5,12 @@ type Config struct {
|
||||||
prefix string
|
prefix string
|
||||||
suffix string
|
suffix string
|
||||||
extension string
|
extension string
|
||||||
separator string
|
|
||||||
generator Generator
|
generator Generator
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConfig(options ...Option) Config {
|
func NewConfig(options ...Option) Config {
|
||||||
conf := Config{
|
conf := Config{
|
||||||
extension: ".txt",
|
extension: ".txt",
|
||||||
separator: "_",
|
|
||||||
generator: uuidGen,
|
generator: uuidGen,
|
||||||
}
|
}
|
||||||
for _, opt := range options {
|
for _, opt := range options {
|
||||||
|
|
|
||||||
|
|
@ -18,5 +18,5 @@ func TestNewConf(t *testing.T) {
|
||||||
func TestNewConfWithOpts(t *testing.T) {
|
func TestNewConfWithOpts(t *testing.T) {
|
||||||
c := NewConfig(WithoutExtension(), WithPrefix("foobar"))
|
c := NewConfig(WithoutExtension(), WithPrefix("foobar"))
|
||||||
assert.Equal(t, "", c.extension)
|
assert.Equal(t, "", c.extension)
|
||||||
assert.Equal(t, "foobar", c.prefix)
|
assert.Equal(t, "foobar_", c.prefix)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,20 +0,0 @@
|
||||||
package nomino
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
func ExampleWithGenerator_custom_generator() {
|
|
||||||
gen := func() (string, error) {
|
|
||||||
return "hello", nil
|
|
||||||
}
|
|
||||||
option := WithGenerator(gen)
|
|
||||||
|
|
||||||
str, _ := Make(NewConfig(option))
|
|
||||||
fmt.Println(str)
|
|
||||||
|
|
||||||
str, _ = Make(NewConfig(option, WithoutExtension()))
|
|
||||||
fmt.Println(str)
|
|
||||||
|
|
||||||
// Output:
|
|
||||||
// hello.txt
|
|
||||||
// hello
|
|
||||||
}
|
|
||||||
11
make.go
11
make.go
|
|
@ -11,16 +11,5 @@ func Make(conf Config) (string, error) {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if conf.prefix != "" {
|
|
||||||
conf.prefix = conf.prefix + conf.separator
|
|
||||||
}
|
|
||||||
if conf.original != "" {
|
|
||||||
conf.original = conf.separator + conf.original
|
|
||||||
}
|
|
||||||
if conf.suffix != "" {
|
|
||||||
conf.suffix = conf.separator + conf.suffix
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprintf("%s%s%s%s%s", conf.prefix, name, conf.original, conf.suffix, conf.extension), nil
|
return fmt.Sprintf("%s%s%s%s%s", conf.prefix, name, conf.original, conf.suffix, conf.extension), nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +0,0 @@
|
||||||
package nomino
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
func ExampleMake_basic() {
|
|
||||||
// Use default config
|
|
||||||
out, _ := Make(NewConfig())
|
|
||||||
fmt.Println(out)
|
|
||||||
}
|
|
||||||
|
|
@ -20,7 +20,6 @@ func TestMake(t *testing.T) {
|
||||||
{"with original", []Option{WithOriginal("file")}, "abc_file.txt"},
|
{"with original", []Option{WithOriginal("file")}, "abc_file.txt"},
|
||||||
{"without ext", []Option{WithoutExtension()}, "abc"},
|
{"without ext", []Option{WithoutExtension()}, "abc"},
|
||||||
{"with ext", []Option{WithExtension("xml")}, "abc.xml"},
|
{"with ext", []Option{WithExtension("xml")}, "abc.xml"},
|
||||||
{"with sep", []Option{WithOriginal("file"), WithSeparator("---")}, "abc---file.txt"},
|
|
||||||
{
|
{
|
||||||
"with all",
|
"with all",
|
||||||
[]Option{
|
[]Option{
|
||||||
|
|
@ -28,9 +27,8 @@ func TestMake(t *testing.T) {
|
||||||
WithOriginal("file"),
|
WithOriginal("file"),
|
||||||
WithSuffix("suff"),
|
WithSuffix("suff"),
|
||||||
WithExtension("svg"),
|
WithExtension("svg"),
|
||||||
WithSeparator("+"),
|
|
||||||
},
|
},
|
||||||
"pre+abc+file+suff.svg",
|
"pre_abc_file_suff.svg",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
13
options.go
13
options.go
|
|
@ -9,21 +9,21 @@ type Option func(c *Config)
|
||||||
// This will be included in the generated name after the generated string and before the suffix.
|
// This will be included in the generated name after the generated string and before the suffix.
|
||||||
func WithOriginal(o string) Option {
|
func WithOriginal(o string) Option {
|
||||||
return func(c *Config) {
|
return func(c *Config) {
|
||||||
c.original = o
|
c.original = "_" + o
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithPrefix sets a prefix for the generated name.
|
// WithPrefix sets a prefix for the generated name.
|
||||||
func WithPrefix(p string) Option {
|
func WithPrefix(p string) Option {
|
||||||
return func(c *Config) {
|
return func(c *Config) {
|
||||||
c.prefix = p
|
c.prefix = p + "_"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithSuffix sets a suffix for the generated name. It will be included in the base name before the suffix.
|
// WithSuffix sets a suffix for the generated name. It will be included in the base name before the suffix.
|
||||||
func WithSuffix(s string) Option {
|
func WithSuffix(s string) Option {
|
||||||
return func(c *Config) {
|
return func(c *Config) {
|
||||||
c.suffix = s
|
c.suffix = "_" + s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -40,10 +40,3 @@ func WithExtension(ext string) Option {
|
||||||
c.extension = "." + strings.TrimPrefix(ext, ".")
|
c.extension = "." + strings.TrimPrefix(ext, ".")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithSeparator sets the separator for the generated filename.
|
|
||||||
func WithSeparator(sep string) Option {
|
|
||||||
return func(c *Config) {
|
|
||||||
c.separator = sep
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -10,21 +10,21 @@ func TestWithOriginal(t *testing.T) {
|
||||||
var c Config
|
var c Config
|
||||||
name := "foobar"
|
name := "foobar"
|
||||||
WithOriginal(name)(&c)
|
WithOriginal(name)(&c)
|
||||||
assert.Equal(t, name, c.original)
|
assert.Equal(t, "_"+name, c.original)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWithPrefix(t *testing.T) {
|
func TestWithPrefix(t *testing.T) {
|
||||||
var c Config
|
var c Config
|
||||||
pref := "draft"
|
pref := "draft"
|
||||||
WithPrefix(pref)(&c)
|
WithPrefix(pref)(&c)
|
||||||
assert.Equal(t, pref, c.prefix)
|
assert.Equal(t, pref+"_", c.prefix)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWithSuffix(t *testing.T) {
|
func TestWithSuffix(t *testing.T) {
|
||||||
var c Config
|
var c Config
|
||||||
suff := "out"
|
suff := "out"
|
||||||
WithSuffix(suff)(&c)
|
WithSuffix(suff)(&c)
|
||||||
assert.Equal(t, suff, c.suffix)
|
assert.Equal(t, "_"+suff, c.suffix)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWithoutExtension(t *testing.T) {
|
func TestWithoutExtension(t *testing.T) {
|
||||||
|
|
@ -39,10 +39,3 @@ func TestWithExtension(t *testing.T) {
|
||||||
WithExtension(ext)(&c)
|
WithExtension(ext)(&c)
|
||||||
assert.Equal(t, "."+ext, c.extension)
|
assert.Equal(t, "."+ext, c.extension)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWithSeparator(t *testing.T) {
|
|
||||||
var c Config
|
|
||||||
sep := "---"
|
|
||||||
WithSeparator(sep)(&c)
|
|
||||||
assert.Equal(t, sep, c.separator)
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue