mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-15 02:02:27 +08:00
feat: add TemplateReplace
This commit is contained in:
@@ -1658,4 +1658,38 @@ func main() {
|
|||||||
// oHell
|
// oHell
|
||||||
// loHel
|
// loHel
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="TemplateReplace">TemplateReplace</span>
|
||||||
|
|
||||||
|
<p>将模板字符串中的占位符替换为数据映射中的相应值。占位符括在花括号中,例如 {key}。例如,模板字符串为“Hello, {name}!”,数据映射为{"name": "world"},结果将为“Hello, world!”。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func TemplateReplace(template string, data map[string]string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:<span style="float:right;display:inline-block;">[运行]()</span></b>
|
||||||
|
|
||||||
|
```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.
|
||||||
|
}
|
||||||
```
|
```
|
||||||
@@ -1659,4 +1659,38 @@ func main() {
|
|||||||
// Hello
|
// Hello
|
||||||
// oHell
|
// oHell
|
||||||
// loHel
|
// loHel
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
### <span id="TemplateReplace">TemplateReplace</span>
|
||||||
|
|
||||||
|
<p>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!".</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func TemplateReplace(template string, data map[string]string string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>example:<span style="float:right;display:inline-block;">[运行]()</span></b>
|
||||||
|
|
||||||
|
```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.
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -704,3 +704,26 @@ func Rotate(str string, shift int) string {
|
|||||||
|
|
||||||
return sb.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
|
||||||
|
}
|
||||||
|
|||||||
@@ -724,3 +724,18 @@ func ExampleRotate() {
|
|||||||
// oHell
|
// oHell
|
||||||
// loHel
|
// 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.
|
||||||
|
}
|
||||||
|
|||||||
@@ -766,3 +766,47 @@ func TestRotate(t *testing.T) {
|
|||||||
assert.Equal(tt.expected, Rotate(tt.input, tt.shift))
|
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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user