From 4b509d31bb9c300595f53da55c18e5de37bebd1f Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Tue, 11 Mar 2025 16:42:39 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20separator=20option?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.go | 2 ++ config_test.go | 2 +- make.go | 11 +++++++++++ make_test.go | 4 +++- options.go | 13 ++++++++++--- options_test.go | 13 ++++++++++--- 6 files changed, 37 insertions(+), 8 deletions(-) diff --git a/config.go b/config.go index dc6af3d..5c87729 100644 --- a/config.go +++ b/config.go @@ -5,12 +5,14 @@ type Config struct { prefix string suffix string extension string + separator string generator Generator } func NewConfig(options ...Option) Config { conf := Config{ extension: ".txt", + separator: "_", generator: uuidGen, } for _, opt := range options { diff --git a/config_test.go b/config_test.go index bb88b53..aeee9b8 100644 --- a/config_test.go +++ b/config_test.go @@ -18,5 +18,5 @@ func TestNewConf(t *testing.T) { func TestNewConfWithOpts(t *testing.T) { c := NewConfig(WithoutExtension(), WithPrefix("foobar")) assert.Equal(t, "", c.extension) - assert.Equal(t, "foobar_", c.prefix) + assert.Equal(t, "foobar", c.prefix) } diff --git a/make.go b/make.go index 3de34a9..3d15b4a 100644 --- a/make.go +++ b/make.go @@ -11,5 +11,16 @@ func Make(conf Config) (string, error) { 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 } diff --git a/make_test.go b/make_test.go index 1196967..dffc981 100644 --- a/make_test.go +++ b/make_test.go @@ -20,6 +20,7 @@ func TestMake(t *testing.T) { {"with original", []Option{WithOriginal("file")}, "abc_file.txt"}, {"without ext", []Option{WithoutExtension()}, "abc"}, {"with ext", []Option{WithExtension("xml")}, "abc.xml"}, + {"with sep", []Option{WithOriginal("file"), WithSeparator("---")}, "abc---file.txt"}, { "with all", []Option{ @@ -27,8 +28,9 @@ func TestMake(t *testing.T) { WithOriginal("file"), WithSuffix("suff"), WithExtension("svg"), + WithSeparator("+"), }, - "pre_abc_file_suff.svg", + "pre+abc+file+suff.svg", }, } diff --git a/options.go b/options.go index fa6c81c..b51b756 100644 --- a/options.go +++ b/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. func WithOriginal(o string) Option { return func(c *Config) { - c.original = "_" + o + c.original = o } } // WithPrefix sets a prefix for the generated name. func WithPrefix(p string) Option { 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. func WithSuffix(s string) Option { return func(c *Config) { - c.suffix = "_" + s + c.suffix = s } } @@ -40,3 +40,10 @@ func WithExtension(ext string) Option { 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 + } +} diff --git a/options_test.go b/options_test.go index 12a2bb5..6aded3a 100644 --- a/options_test.go +++ b/options_test.go @@ -10,21 +10,21 @@ func TestWithOriginal(t *testing.T) { var c Config name := "foobar" WithOriginal(name)(&c) - assert.Equal(t, "_"+name, c.original) + assert.Equal(t, name, c.original) } func TestWithPrefix(t *testing.T) { var c Config pref := "draft" WithPrefix(pref)(&c) - assert.Equal(t, pref+"_", c.prefix) + assert.Equal(t, pref, c.prefix) } func TestWithSuffix(t *testing.T) { var c Config suff := "out" WithSuffix(suff)(&c) - assert.Equal(t, "_"+suff, c.suffix) + assert.Equal(t, suff, c.suffix) } func TestWithoutExtension(t *testing.T) { @@ -39,3 +39,10 @@ func TestWithExtension(t *testing.T) { WithExtension(ext)(&c) assert.Equal(t, "."+ext, c.extension) } + +func TestWithSeparator(t *testing.T) { + var c Config + sep := "---" + WithSeparator(sep)(&c) + assert.Equal(t, sep, c.separator) +}