Skip to content

Commit

Permalink
add except method (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
natefinch authored Oct 23, 2017
1 parent 973f5d9 commit 5b20ff0
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 4 deletions.
4 changes: 3 additions & 1 deletion cli/gnorm.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# ConnStr is the connection string for the database.
# ConnStr is the connection string for the database. Any environment variables
# in this string will be expanded, so for example dbname=$MY_DDB will do the
# right thing.
# MySQL example:
# ConnStr = "root:admin@tcp/"
# Postgres example:
Expand Down
4 changes: 3 additions & 1 deletion cli/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ package cli
// fmt.Println("`")
// }
// gocog]]]
const sample = `# ConnStr is the connection string for the database.
const sample = `# ConnStr is the connection string for the database. Any environment variables
# in this string will be expanded, so for example dbname=$MY_DDB will do the
# right thing.
# MySQL example:
# ConnStr = "root:admin@tcp/"
# Postgres example:
Expand Down
20 changes: 20 additions & 0 deletions run/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,26 @@ func (s Strings) Sprintf(format string) Strings {
return ret
}

// Except returns a copy of the Strings with the given values removed.
func (s Strings) Except(excludes []string) Strings {
ret := make(Strings, 0, len(s))
for x := range s {
if !contains(excludes, s[x]) {
ret = append(ret, s[x])
}
}
return ret
}

func contains(list []string, s string) bool {
for x := range list {
if s == list[x] {
return true
}
}
return false
}

// Columns represents the ordered list of columns in a table.
type Columns []*Column

Expand Down
13 changes: 12 additions & 1 deletion run/data/data_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package data

import "testing"
import (
"reflect"
"testing"
)

func TestStringsSprintf(t *testing.T) {
s := Strings{"one", "two", "three"}
Expand All @@ -14,3 +17,11 @@ func TestStringsSprintf(t *testing.T) {
}
}
}

func TestStringsExcept(t *testing.T) {
vals := Strings{"one", "two", "three", "four"}.Except([]string{"ONE", "two", "three"})
expected := Strings{"one", "four"}
if !reflect.DeepEqual(vals, expected) {
t.Fatalf("expected %q, but got %q", expected, vals)
}
}
4 changes: 3 additions & 1 deletion site/content/cli/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ func main() {
}
gocog}}} -->
```
# ConnStr is the connection string for the database.
# ConnStr is the connection string for the database. Any environment variables
# in this string will be expanded, so for example dbname=$MY_DDB will do the
# right thing.
# MySQL example:
# ConnStr = "root:admin@tcp/"
# Postgres example:
Expand Down
1 change: 1 addition & 0 deletions site/content/templates/data.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ Strings is a list of string values with the following methods
| Method | Arguments | Description |
| --- | ---- | --- |
| Sprintf | format (string) | Sprintf calls [fmt.Sprintf](https://golang.org/pkg/fmt/#Sprintf)(format, str) for every string in this value and returns the results as a new Strings value.
| Except | vals ([]string) | Except returns a Strings value with the given values removed from the list (if they existed). The check is case sensitive.

### Table

Expand Down

0 comments on commit 5b20ff0

Please sign in to comment.