Skip to content

Commit

Permalink
Merge pull request #101 from jamslinger/fix-size
Browse files Browse the repository at this point in the history
Count number of characters in size filter instead of number of bytes
  • Loading branch information
danog authored Oct 19, 2024
2 parents 71b8fa2 + 449510e commit c1f5e56
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 2 additions & 0 deletions filters/standard_filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ var filterTests = []struct {
// sequence (array or string) filters
{`"Ground control to Major Tom." | size`, 28},
{`"apples, oranges, peaches, plums" | split: ", " | size`, 4},
// count chars, not bytes
{`"Straße" | size`, 6},

// string filters
{`"Take my protein pills and put my helmet on" | replace: "my", "your"`, "Take your protein pills and put your helmet on"},
Expand Down
5 changes: 4 additions & 1 deletion values/arrays.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package values

import (
"reflect"
"unicode/utf8"
)

// TODO Length is now only used by the "size" filter.
Expand All @@ -13,8 +14,10 @@ func Length(value any) int {
value = ToLiquid(value)
ref := reflect.ValueOf(value)
switch ref.Kind() {
case reflect.Array, reflect.Slice, reflect.String:
case reflect.Array, reflect.Slice:
return ref.Len()
case reflect.String:
return utf8.RuneCountInString(ref.String())
default:
return 0
}
Expand Down

0 comments on commit c1f5e56

Please sign in to comment.