✨ Config.AddOptions and Generator.MakeWithConfig
This commit is contained in:
parent
d7b14f804c
commit
1abfaa44d1
3 changed files with 25 additions and 3 deletions
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue