Skip to content

Commit

Permalink
feat: add new filters to go bucketing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
suthar26 committed Aug 12, 2024
1 parent 094ac8f commit d7e0944
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
4 changes: 4 additions & 0 deletions bucketing/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ const (
ComparatorNotExist = "!exist"
ComparatorContain = "contain"
ComparatorNotContain = "!contain"
ComparatorStartWith = "startWith"
ComparatorNotStartWith = "!startWith"
ComparatorEndWith = "endWith"
ComparatorNotEndWith = "!endWith"
)

const (
Expand Down
26 changes: 26 additions & 0 deletions bucketing/segmentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ func checkStringsFilter(str string, filter *UserFilter) bool {
return str != "" && stringArrayContains(values, str)
} else if operator == "!contain" {
return str == "" || !stringArrayContains(values, str)
} else if operator == "startWith" {
return str != "" && stringArrayStartsWith(values, str)
} else if operator == "!startWith" {
return str == "" || !stringArrayStartsWith(values, str)
} else if operator == "endWith" {
return str != "" && stringArrayEndsWith(values, str)
} else if operator == "!endWith" {
return str == "" || !stringArrayEndsWith(values, str)
} else {
return false
}
Expand All @@ -126,6 +134,24 @@ func stringArrayContains(substrings []string, search string) bool {
return false
}

func stringArrayStartsWith(prefixes []string, search string) bool {
for _, prefix := range prefixes {
if strings.HasPrefix(search, prefix) {
return true
}
}
return false
}

func stringArrayEndsWith(suffixes []string, search string) bool {
for _, suffix := range suffixes {
if strings.HasSuffix(search, suffix) {
return true
}
}
return false
}

func _checkBooleanFilter(b bool, filter *UserFilter) bool {
contains := func(arr []bool, search bool) bool {
for _, s := range arr {
Expand Down
24 changes: 24 additions & 0 deletions bucketing/segmentation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1592,6 +1592,30 @@ func TestDoesUserPassFilter_WithUserEmailFilter(t *testing.T) {
values: []interface{}{"@gmail.com", "@devcycle.com", "@hotmail.com"},
expected: true,
},
{
name: "User email starts with filter",
comparator: ComparatorStartWith,
values: []interface{}{"test"},
expected: true,
},
{
name: "User email emds with filter",
comparator: ComparatorEndWith,
values: []interface{}{"@devcycle.com"},
expected: true,
},
{
name: "User email does not start with filter",
comparator: ComparatorStartWith,
values: []interface{}{"user"},
expected: false,
},
{
name: "User email does not end with filter",
comparator: ComparatorEndWith,
values: []interface{}{"@devcycle.io"},
expected: false,
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit d7e0944

Please sign in to comment.