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
This commit is contained in:
Cameron Moore
2018-10-02 09:41:48 -05:00
committed by Bjørn Erik Pedersen
parent 91f49c0700
commit ce264b936c
3 changed files with 38 additions and 14 deletions

View File

@@ -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: