Skip to content

Commit

Permalink
Add without and has functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
technosophos committed Mar 14, 2017
1 parent a1c06b6 commit 1f3c301
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ These are used to manipulate lists: `{{ list 1 2 3 | reverse | first }}`
- initial: Get all but the last item in a list: 'list 1 2 3 | initial' returns '[1 2]'
- append: Add an item to the end of a list: 'append $list 4' adds '4' to the end of '$list'
- prepend: Add an item to the beginning of a list: 'prepend $list 4' puts 4 at the beginning of the list.
- reverse: Reverse the items in a list.
- uniq: Remove duplicates from a list.
- without: Return a list with the given values removed: 'without (list 1 2 3) 1' would return '[2 3]'
- has: Return 'tru' if the item is found in the list: 'has "foo" $list' will return 'true' if the list contains "foo"

### Dict Functions:

Expand Down
2 changes: 2 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ These are used to manipulate lists: '{{ list 1 2 3 | reverse | first }}'
- prepend: Add an item to the beginning of a list: 'prepend $list 4' puts 4 at the beginning of the list.
- reverse: Reverse the items in a list.
- uniq: Remove duplicates from a list.
- without: Return a list with the given values removed: 'without (list 1 2 3) 1' would return '[2 3]'
- has: Return 'true' if the item is found in the list: 'has "foo" $list' will return 'true' if the list contains "foo"
Dict Functions:
Expand Down
2 changes: 2 additions & 0 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ var genericMap = map[string]interface{}{
"initial": initial,
"reverse": reverse,
"uniq": uniq,
"without": without,
"has": func(needle interface{}, haystack []interface{}) bool { return inList(haystack, needle) },

// Crypto:
"genPrivateKey": generatePrivateKey,
Expand Down
29 changes: 19 additions & 10 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,29 @@ func compact(list []interface{}) []interface{} {

func uniq(list []interface{}) []interface{} {
dest := []interface{}{}
for _, item := range list {
if !inList(dest, item) {
dest = append(dest, item)
}
}
return dest
}

skip := func(haystack []interface{}, needle interface{}) bool {
for _, h := range haystack {
if reflect.DeepEqual(needle, h) {
return true
}
func inList(haystack []interface{}, needle interface{}) bool {
for _, h := range haystack {
if reflect.DeepEqual(needle, h) {
return true
}
return false
}
return false
}

for _, item := range list {
if !skip(dest, item) {
dest = append(dest, item)
func without(list []interface{}, omit ...interface{}) []interface{} {
res := []interface{}{}
for _, i := range list {
if !inList(omit, i) {
res = append(res, i)
}
}
return dest
return res
}
24 changes: 24 additions & 0 deletions list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,27 @@ func TestUniq(t *testing.T) {
assert.NoError(t, runt(tpl, expect))
}
}

func TestWithout(t *testing.T) {
tests := map[string]string{
`{{ without (list 1 2 3 4) 1 }}`: `[2 3 4]`,
`{{ without (list "a" "b" "c" "d") "a" }}`: `[b c d]`,
`{{ without (list 1 1 1 1 2) 1 }}`: `[2]`,
`{{ without (list) 1 }}`: `[]`,
`{{ without (list 1 2 3) }}`: `[1 2 3]`,
`{{ without list }}`: `[]`,
}
for tpl, expect := range tests {
assert.NoError(t, runt(tpl, expect))
}
}

func TestHas(t *testing.T) {
tests := map[string]string{
`{{ list 1 2 3 | has 1 }}`: `true`,
`{{ list 1 2 3 | has 4 }}`: `false`,
}
for tpl, expect := range tests {
assert.NoError(t, runt(tpl, expect))
}
}

0 comments on commit 1f3c301

Please sign in to comment.