Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Count number of characters in size filter instead of number of bytes #101

Merged
merged 1 commit into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading