Skip to content

Commit

Permalink
Add len shorthand
Browse files Browse the repository at this point in the history
  • Loading branch information
eatonphil committed May 20, 2022
1 parent 9231a1b commit 1a2eb10
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func main() {
| reverse | | `reverse('abc') = 'cba'` |
| lpad | Omit the second argument to default to padding with spaces | `lpad('22', 3, '0') = '022'` |
| rpad | Omit the second argument to default to padding with spaces | `rpad('22', 3, '0') = '220'` |
| len | Shorthand for `length` | `len('my string') = '9'` |

## Aggregation

Expand Down
5 changes: 5 additions & 0 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ func rpad(_s any, length int, _padWith ...any) string {
return sb.String()
}

func length(s string) int64 {
return int64(len(s))
}

var stringFunctions = map[string]any{
"len": stringy1int64(length),
"repeat": repeat,
"replicate": repeat,
"strpos": stringy2int64(charindex),
Expand Down
4 changes: 4 additions & 0 deletions string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ func Test_string(t *testing.T) {
assert.Equal(t, "null", stringy(nil))
}

func Test_len(t *testing.T) {
assertQuery(t, "SELECT len('a')", "1")
}

func Test_repeat(t *testing.T) {
assertQuery(t, "SELECT repeat('a', 3)", "aaa")
assertQuery(t, "SELECT replicate('a', 3)", "aaa")
Expand Down

0 comments on commit 1a2eb10

Please sign in to comment.