Skip to content

Commit 90111c1

Browse files
committed
Modified len to be encoding aware
1 parent c5ca679 commit 90111c1

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

jsone.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111
"time"
1212
"unicode"
13+
"unicode/utf8"
1314

1415
i "github.com/taskcluster/json-e/interpreter"
1516
)
@@ -200,7 +201,7 @@ var builtin = map[string]interface{}{
200201
"len": i.WrapFunction(func(v interface{}) (float64, error) {
201202
switch val := v.(type) {
202203
case string:
203-
return float64(len(val)), nil
204+
return float64(utf8.RuneCountInString(val)), nil
204205
case []interface{}:
205206
return float64(len(val)), nil
206207
}

specification.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,6 +1530,11 @@ context: {key: 'abc'}
15301530
template: {$eval: 'len(key)'}
15311531
result: 3
15321532
---
1533+
title: len of grinning face unicode character
1534+
context: {key: "\U0001F601"}
1535+
template: {$eval: 'len(key)'}
1536+
result: 1
1537+
---
15331538
title: len (4)
15341539
context: {key: [1,2,3]}
15351540
template: {$eval: 'len("abc")'}

src/builtins.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ module.exports = (context) => {
9494

9595
define('len', builtins, {
9696
argumentTests: ['string|array'],
97-
invoke: obj => obj.length,
97+
invoke: obj => Array.from(obj).length,
9898
});
9999

100100
define('strip', builtins, {

0 commit comments

Comments
 (0)