From 1008a064d017aa259db31db1112af7b0c2e7f5d2 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Sat, 15 Mar 2025 16:24:37 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20Make=20method=20to=20Generato?= =?UTF-8?q?r?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generators.go | 6 ++++++ generators_examples_test.go | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/generators.go b/generators.go index 94326dc..fabf650 100644 --- a/generators.go +++ b/generators.go @@ -7,6 +7,12 @@ import "errors" // for example. type Generator func(conf *Config) (string, error) +// 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...)) +} + // WithGenerator sets the specified generator func WithGenerator(g Generator) Option { return func(c *Config) { diff --git a/generators_examples_test.go b/generators_examples_test.go index a7f7a1c..31951f5 100644 --- a/generators_examples_test.go +++ b/generators_examples_test.go @@ -26,6 +26,19 @@ func ExampleWithGenerator_customGenerator() { // hello } +func ExampleGenerator_Make() { + g := nomino.Incremental() + st, _ := g.Make() + fmt.Println(st) + + st, _ = g.Make(nomino.WithPrefix("foo")) + fmt.Println(st) + + // Output: + // 0.txt + // foo_1.txt +} + func ExampleMultiGeneratorInOrder() { gen1 := func(*nomino.Config) (string, error) { return "hello", nil