Config.AddOptions and Generator.MakeWithConfig

This commit is contained in:
Dan Jones 2025-03-18 09:22:30 -05:00
commit 1abfaa44d1
3 changed files with 25 additions and 3 deletions

View file

@ -21,3 +21,11 @@ func NewConfig(options ...Option) Config {
} }
return conf 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
}

View file

@ -20,3 +20,12 @@ func TestNewConfWithOpts(t *testing.T) {
assert.Equal(t, "", c.extension) assert.Equal(t, "", c.extension)
assert.Equal(t, "foobar", c.prefix) 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)
}

View file

@ -10,10 +10,15 @@ import (
// for example. // for example.
type Generator func(conf *Config) (string, error) 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) { func (g Generator) Make(opts ...Option) (string, error) {
opts = append(opts, WithGenerator(g)) return g.MakeWithConfig(NewConfig(opts...))
return Make(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 // WithGenerator sets the specified generator