From 1abfaa44d117533a5b46c1f31d0046cdd87e133a Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Tue, 18 Mar 2025 09:22:30 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Config.AddOptions=20and=20Generator?= =?UTF-8?q?.MakeWithConfig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.go | 8 ++++++++ config_test.go | 9 +++++++++ generators.go | 11 ++++++++--- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/config.go b/config.go index 945e588..c889a06 100644 --- a/config.go +++ b/config.go @@ -21,3 +21,11 @@ func NewConfig(options ...Option) Config { } 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 +} diff --git a/config_test.go b/config_test.go index adc35e6..f6ed21d 100644 --- a/config_test.go +++ b/config_test.go @@ -20,3 +20,12 @@ func TestNewConfWithOpts(t *testing.T) { assert.Equal(t, "", c.extension) 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) +} diff --git a/generators.go b/generators.go index c7ad4e8..e01ccc6 100644 --- a/generators.go +++ b/generators.go @@ -10,10 +10,15 @@ import ( // for example. 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) { - opts = append(opts, WithGenerator(g)) - return Make(NewConfig(opts...)) + return g.MakeWithConfig(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