Skip to content

Commit

Permalink
#234 Added new func to std_seq.go.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericzhang6222 committed May 11, 2020
1 parent 2f40b95 commit 61b1845
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
36 changes: 36 additions & 0 deletions syntax/std_seq.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,41 @@ func stdSeq() rel.Attr {
return rel.NewTupleAttr("seq",
rel.NewNativeFunctionAttr("concat", stdSeqConcat),
rel.NewNativeFunctionAttr("repeat", stdSeqRepeat),
createNestedFuncAttr("contains", 2, func(args ...rel.Value) rel.Value {
return rel.NewBool(strings.Contains(mustAsString(args[0]), mustAsString(args[1])))
}),
createNestedFuncAttr("split", 2, func(args ...rel.Value) rel.Value {
splitted := strings.Split(mustAsString(args[0]), mustAsString(args[1]))
vals := make([]rel.Value, 0, len(splitted))
for _, s := range splitted {
vals = append(vals, rel.NewString([]rune(s)))
}
return rel.NewArray(vals...)
}),
createNestedFuncAttr("sub", 3, func(args ...rel.Value) rel.Value {
return rel.NewString(
[]rune(
strings.ReplaceAll(
mustAsString(args[0]),
mustAsString(args[1]),
mustAsString(args[2]),
),
),
)
}),
createNestedFuncAttr("has_prefix", 2, func(args ...rel.Value) rel.Value {
return rel.NewBool(strings.HasPrefix(mustAsString(args[0]), mustAsString(args[1])))
}),
createNestedFuncAttr("has_suffix", 2, func(args ...rel.Value) rel.Value {
return rel.NewBool(strings.HasPrefix(mustAsString(args[0]), mustAsString(args[1])))
}),
createNestedFuncAttr("join", 2, func(args ...rel.Value) rel.Value {
strs := args[0].(rel.Set)
toJoin := make([]string, 0, strs.Count())
for i, ok := strs.(rel.Set).ArrayEnumerator(); ok && i.MoveNext(); {
toJoin = append(toJoin, mustAsString(i.Current()))
}
return rel.NewString([]rune(strings.Join(toJoin, mustAsString(args[1]))))
}),
)
}
5 changes: 5 additions & 0 deletions syntax/std_seq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ func TestSeqRepeat(t *testing.T) {

AssertCodePanics(t, `//seq.repeat(2, 3.4)`)
}

func TestSeqContains(t *testing.T) {
t.Parallel()

}

0 comments on commit 61b1845

Please sign in to comment.