Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify replace with/by empty string #820

Merged
merged 1 commit into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion ext/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ const (
// 'hello hello'.replace('he', 'we', -1) // returns 'wello wello'
// 'hello hello'.replace('he', 'we', 1) // returns 'wello hello'
// 'hello hello'.replace('he', 'we', 0) // returns 'hello hello'
// 'hello hello'.replace('', '_') // returns '_h_e_l_l_o_ _h_e_l_l_o_'
// 'hello hello'.replace('h', '') // returns 'ello ello'
//
// # Split
//
Expand Down Expand Up @@ -514,7 +516,7 @@ func (lib *stringLib) CompileOptions() []cel.EnvOption {
)
}
if lib.version >= 3 {
opts = append( opts,
opts = append(opts,
cel.Function("reverse",
cel.MemberOverload("reverse", []*cel.Type{cel.StringType}, cel.StringType,
cel.UnaryBinding(func(str ref.Val) ref.Val {
Expand Down
2 changes: 2 additions & 0 deletions ext/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ var stringTests = []struct {
{expr: `"{0} days {0} hours".replace("{0}", "2") == "2 days 2 hours"`},
{expr: `"{0} days {0} hours".replace("{0}", "2", 1).replace("{0}", "23") == "2 days 23 hours"`},
{expr: `"1 ©αT taco".replace("αT", "o©α") == "1 ©o©α taco"`},
{expr: `"hello hello".replace("", "_") == "_h_e_l_l_o_ _h_e_l_l_o_"`},
{expr: `"hello hello".replace("h", "") == "ello ello"`},
// Split tests.
{expr: `"hello world".split(" ") == ["hello", "world"]`},
{expr: `"hello world events!".split(" ", 0) == []`},
Expand Down