diff options
author | Cameron Moore <moorereason@gmail.com> | 2018-10-02 17:41:48 +0300 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-10-02 17:41:48 +0300 |
commit | ce264b936ce9f589bd889e18762881cff8bc9cd0 (patch) | |
tree | 1a244eff8d90784f01ab0336e9fafcb8d0b018ce /tpl/lang | |
parent | 91f49c0700dde13e16f42c745584a0bef60c6fe2 (diff) |
tpl: Add a delimiter parameter to lang.NumFmt
The original implementation of NumFmt did not take into account that the
options delimiter (a space) could be a valid option. Adding a delim
parameter seems like the simplest, safest, and most flexible way to
solve this oversight in a backwards-compatible way.
Fixes #5260
Diffstat (limited to 'tpl/lang')
-rw-r--r-- | tpl/lang/lang.go | 16 | ||||
-rw-r--r-- | tpl/lang/lang_test.go | 29 |
2 files changed, 33 insertions, 12 deletions
diff --git a/tpl/lang/lang.go b/tpl/lang/lang.go index 430b5e442..4c011b1ce 100644 --- a/tpl/lang/lang.go +++ b/tpl/lang/lang.go @@ -67,15 +67,27 @@ func (ns *Namespace) NumFmt(precision, number interface{}, options ...interface{ var neg, dec, grp string if len(options) == 0 { - // TODO(moorereason): move to site config + // defaults neg, dec, grp = "-", ".", "," } else { + delim := " " + + if len(options) == 2 { + // custom delimiter + s, err := cast.ToStringE(options[1]) + if err != nil { + return "", nil + } + + delim = s + } + s, err := cast.ToStringE(options[0]) if err != nil { return "", nil } - rs := strings.Fields(s) + rs := strings.Split(s, delim) switch len(rs) { case 0: case 1: diff --git a/tpl/lang/lang_test.go b/tpl/lang/lang_test.go index c494fd03a..aee567502 100644 --- a/tpl/lang/lang_test.go +++ b/tpl/lang/lang_test.go @@ -18,22 +18,27 @@ func TestNumFormat(t *testing.T) { prec int n float64 runes string + delim string want string }{ - {2, -12345.6789, "", "-12,345.68"}, - {2, -12345.6789, "- . ,", "-12,345.68"}, - {2, -12345.1234, "- . ,", "-12,345.12"}, + {2, -12345.6789, "", "", "-12,345.68"}, + {2, -12345.6789, "- . ,", "", "-12,345.68"}, + {2, -12345.1234, "- . ,", "", "-12,345.12"}, - {2, 12345.6789, "- . ,", "12,345.68"}, - {0, 12345.6789, "- . ,", "12,346"}, - {11, -12345.6789, "- . ,", "-12,345.67890000000"}, + {2, 12345.6789, "- . ,", "", "12,345.68"}, + {0, 12345.6789, "- . ,", "", "12,346"}, + {11, -12345.6789, "- . ,", "", "-12,345.67890000000"}, - {3, -12345.6789, "- ,", "-12345,679"}, - {6, -12345.6789, "- , .", "-12.345,678900"}, + {3, -12345.6789, "- ,", "", "-12345,679"}, + {6, -12345.6789, "- , .", "", "-12.345,678900"}, + + {3, -12345.6789, "-|,| ", "|", "-12 345,679"}, + {6, -12345.6789, "-|,| ", "|", "-12 345,678900"}, // Arabic, ar_AE - {6, -12345.6789, "- ٫ ٬", "-12٬345٫678900"}, + {6, -12345.6789, "- ٫ ٬", "", "-12٬345٫678900"}, + {6, -12345.6789, "-|٫| ", "|", "-12 345٫678900"}, } for i, c := range cases { @@ -45,7 +50,11 @@ func TestNumFormat(t *testing.T) { if len(c.runes) == 0 { s, err = ns.NumFmt(c.prec, c.n) } else { - s, err = ns.NumFmt(c.prec, c.n, c.runes) + if c.delim == "" { + s, err = ns.NumFmt(c.prec, c.n, c.runes) + } else { + s, err = ns.NumFmt(c.prec, c.n, c.runes, c.delim) + } } require.NoError(t, err, errMsg) |