Skip to content

Commit 7ed8f82

Browse files
committed
add not ends with operator
1 parent 72a9761 commit 7ed8f82

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package operator
2+
3+
import (
4+
"reflect"
5+
"strings"
6+
)
7+
8+
func init() {
9+
AddOperator(NotEndsWith)
10+
}
11+
12+
var NotEndsWith = &Operator{
13+
Name: "ends_with",
14+
Evaluate: func(input, value interface{}) bool {
15+
rv := reflect.ValueOf(value)
16+
if rv.Kind() != reflect.String {
17+
return false
18+
}
19+
20+
return !strings.HasSuffix(input.(string), value.(string))
21+
},
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package operator
2+
3+
import "testing"
4+
5+
func TestNotEndsWith(t *testing.T) {
6+
var inputs = []struct {
7+
title string
8+
value interface{}
9+
input interface{}
10+
want bool
11+
}{
12+
{title: "with prefix", value: "test", input: "string to the suffix test", want: false},
13+
{title: "without prefix", value: "test", input: "string to the suffix", want: true},
14+
}
15+
16+
for _, input := range inputs {
17+
t.Run(input.title, func(t *testing.T) {
18+
got := NotEndsWith.Evaluate(input.input, input.value)
19+
if got != input.want {
20+
t.Errorf("%v ends with %v got: %t, want: %t", input.input, input.value, got, input.want)
21+
}
22+
})
23+
}
24+
}

0 commit comments

Comments
 (0)