Skip to content

Commit 7de4257

Browse files
authored
fix: number locale support (#472)
Add support for Number.toLocaleString() which was previously just using toString. Default locale is: en-US Fixes #285
1 parent 93fb47c commit 7de4257

7 files changed

+37
-6
lines changed

array_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func TestArray_toLocaleString(t *testing.T) {
107107

108108
test(`
109109
[ 3.14159, "abc", undefined, new Date(0) ].toLocaleString();
110-
`, "3.14159,abc,,1970-01-01 00:00:00")
110+
`, "3.142,abc,,1970-01-01 00:00:00")
111111

112112
test(`raise:
113113
[ { toLocaleString: undefined } ].toLocaleString();

builtin_number.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ package otto
33
import (
44
"math"
55
"strconv"
6+
7+
"golang.org/x/text/language"
8+
"golang.org/x/text/message"
9+
"golang.org/x/text/number"
610
)
711

812
// Number
@@ -96,5 +100,13 @@ func builtinNumber_isNaN(call FunctionCall) Value {
96100
}
97101

98102
func builtinNumber_toLocaleString(call FunctionCall) Value {
99-
return builtinNumber_toString(call)
103+
value := call.thisClassObject(classNumber).primitiveValue()
104+
locale := call.Argument(0)
105+
lang := defaultLanguage
106+
if locale.IsDefined() {
107+
lang = language.MustParse(locale.string())
108+
}
109+
110+
p := message.NewPrinter(lang)
111+
return toValue_string(p.Sprintf("%v", number.Decimal(value.value)))
100112
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.18
44

55
require (
66
github.com/stretchr/testify v1.8.1
7+
golang.org/x/text v0.4.0
78
gopkg.in/readline.v1 v1.0.0-20160726135117-62c6fe619375
89
gopkg.in/sourcemap.v1 v1.0.5
910
)

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
1414
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
1515
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
1616
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
17+
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
18+
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
1719
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1820
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1921
gopkg.in/readline.v1 v1.0.0-20160726135117-62c6fe619375 h1:hPki/oSSWOLiI9Gc9jyIoj33O3j29fUc9PlLha2yDj0=

issue_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -1122,3 +1122,12 @@ func Test_issue177(t *testing.T) {
11221122
require.NoError(t, err)
11231123
require.Equal(t, float64(9), exp)
11241124
}
1125+
1126+
func Test_issue285(t *testing.T) {
1127+
vm := New()
1128+
val, err := vm.Run(`(1451).toLocaleString('en-US')`)
1129+
require.NoError(t, err)
1130+
exp, err := val.Export()
1131+
require.NoError(t, err)
1132+
require.Equal(t, "1,451", exp)
1133+
}

locale.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package otto
2+
3+
import "golang.org/x/text/language"
4+
5+
var (
6+
defaultLanguage = language.MustParse("en-US")
7+
)

number_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ func TestNumber_toLocaleString(t *testing.T) {
136136

137137
test(`
138138
[
139-
new Number(451).toLocaleString(),
140-
new Number(451).toLocaleString(10),
141-
new Number(451).toLocaleString(8)
139+
new Number(4510).toLocaleString(),
140+
new Number(4510).toLocaleString('en-US'),
141+
new Number(4510).toLocaleString('nl-NL')
142142
];
143-
`, "451,451,703")
143+
`, "4,510,4,510,4.510")
144144
})
145145
}
146146

0 commit comments

Comments
 (0)