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