From 93be25920f2d9e59d77b7ee310ccb07b5e0a920b Mon Sep 17 00:00:00 2001 From: dudaodong Date: Fri, 6 Sep 2024 11:04:54 +0800 Subject: [PATCH] feat: add TemplateReplace --- docs/api/packages/strutil.md | 34 +++++++++++++++++++++++++ docs/en/api/packages/strutil.md | 36 ++++++++++++++++++++++++++- strutil/string.go | 23 +++++++++++++++++ strutil/string_example_test.go | 15 +++++++++++ strutil/string_test.go | 44 +++++++++++++++++++++++++++++++++ 5 files changed, 151 insertions(+), 1 deletion(-) diff --git a/docs/api/packages/strutil.md b/docs/api/packages/strutil.md index 26745e8..94fe18e 100644 --- a/docs/api/packages/strutil.md +++ b/docs/api/packages/strutil.md @@ -1658,4 +1658,38 @@ func main() { // oHell // loHel } +``` + +### TemplateReplace + +

将模板字符串中的占位符替换为数据映射中的相应值。占位符括在花括号中,例如 {key}。例如,模板字符串为“Hello, {name}!”,数据映射为{"name": "world"},结果将为“Hello, world!”。

+ +函数签名: + +```go +func TemplateReplace(template string, data map[string]string) string +``` + +示例:[运行]() + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + template := `Hello, my name is {name}, I'm {age} years old.` + data := map[string]string{ + "name": "Bob", + "age": "20", + } + + result := strutil.TemplateReplace(template, data) + + fmt.Println(result) + + // Output: + // Hello, my name is Bob, I'm 20 years old. +} ``` \ No newline at end of file diff --git a/docs/en/api/packages/strutil.md b/docs/en/api/packages/strutil.md index de26035..1ed8551 100644 --- a/docs/en/api/packages/strutil.md +++ b/docs/en/api/packages/strutil.md @@ -1659,4 +1659,38 @@ func main() { // Hello // oHell // loHel -} \ No newline at end of file +} +``` +### TemplateReplace + +

Replaces the placeholders in the template string with the corresponding values in the data map.The placeholders are enclosed in curly braces, e.g. {key}. for example, the template string is "Hello, {name}!", and the data map is {"name": "world"}, the result will be "Hello, world!".

+ +函数签名: + +```go +func TemplateReplace(template string, data map[string]string string +``` + +example:[运行]() + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + template := `Hello, my name is {name}, I'm {age} years old.` + data := map[string]string{ + "name": "Bob", + "age": "20", + } + + result := strutil.TemplateReplace(template, data) + + fmt.Println(result) + + // Output: + // Hello, my name is Bob, I'm 20 years old. +} +``` \ No newline at end of file diff --git a/strutil/string.go b/strutil/string.go index 981cf54..daf2a43 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -704,3 +704,26 @@ func Rotate(str string, shift int) string { return sb.String() } + +// TemplateReplace replaces the placeholders in the template string with the corresponding values in the data map. +// The placeholders are enclosed in curly braces, e.g. {key}. +// for example, the template string is "Hello, {name}!", and the data map is {"name": "world"}, +// the result will be "Hello, world!". +// Play: todo +func TemplateReplace(template string, data map[string]string) string { + re := regexp.MustCompile(`\{(\w+)\}`) + + result := re.ReplaceAllStringFunc(template, func(s string) string { + key := strings.Trim(s, "{}") + if val, ok := data[key]; ok { + return val + } + + return s + }) + + result = strings.ReplaceAll(result, "{{", "{") + result = strings.ReplaceAll(result, "}}", "}") + + return result +} diff --git a/strutil/string_example_test.go b/strutil/string_example_test.go index fe81f5e..ac4d946 100644 --- a/strutil/string_example_test.go +++ b/strutil/string_example_test.go @@ -724,3 +724,18 @@ func ExampleRotate() { // oHell // loHel } + +func ExampleTemplateReplace() { + template := `Hello, my name is {name}, I'm {age} years old.` + data := map[string]string{ + "name": "Bob", + "age": "20", + } + + result := TemplateReplace(template, data) + + fmt.Println(result) + + // Output: + // Hello, my name is Bob, I'm 20 years old. +} diff --git a/strutil/string_test.go b/strutil/string_test.go index 3ad4bc7..dd83d7a 100644 --- a/strutil/string_test.go +++ b/strutil/string_test.go @@ -766,3 +766,47 @@ func TestRotate(t *testing.T) { assert.Equal(tt.expected, Rotate(tt.input, tt.shift)) } } + +func TestTemplateReplace(t *testing.T) { + t.Parallel() + + assert := internal.NewAssert(t, "TestTemplateReplace") + + t.Run("basic", func(t *testing.T) { + template := `Hello, my name is {name}, I'm {age} years old.` + data := map[string]string{ + "name": "Bob", + "age": "20", + } + + expected := `Hello, my name is Bob, I'm 20 years old.` + result := TemplateReplace(template, data) + + assert.Equal(expected, result) + }) + + t.Run("not found", func(t *testing.T) { + template := `Hello, my name is {name}, I'm {age} years old.` + data := map[string]string{ + "name": "Bob", + } + + expected := `Hello, my name is Bob, I'm {age} years old.` + result := TemplateReplace(template, data) + + assert.Equal(expected, result) + }) + + t.Run("brackets", func(t *testing.T) { + template := `Hello, my name is {name}, I'm {{age}} years old.` + data := map[string]string{ + "name": "Bob", + "age": "20", + } + + expected := `Hello, my name is Bob, I'm {20} years old.` + result := TemplateReplace(template, data) + + assert.Equal(expected, result) + }) +}