diff --git a/functions.go b/functions.go index 57fcec1d..44f1f2d0 100644 --- a/functions.go +++ b/functions.go @@ -22,8 +22,7 @@ import ( // // Use this to pass the functions into the template engine: // -// tpl := template.New("foo").Funcs(sprig.FuncMap())) -// +// tpl := template.New("foo").Funcs(sprig.FuncMap())) func FuncMap() template.FuncMap { return HtmlFuncMap() } @@ -320,6 +319,7 @@ var genericMap = map[string]interface{}{ "mustLast": mustLast, "initial": initial, "mustInitial": mustInitial, + "sortNumeric": sortNumeric, "reverse": reverse, "mustReverse": mustReverse, "uniq": uniq, @@ -336,20 +336,20 @@ var genericMap = map[string]interface{}{ "mustChunk": mustChunk, // Crypto: - "bcrypt": bcrypt, - "htpasswd": htpasswd, - "genPrivateKey": generatePrivateKey, - "derivePassword": derivePassword, - "buildCustomCert": buildCustomCertificate, - "genCA": generateCertificateAuthority, - "genCAWithKey": generateCertificateAuthorityWithPEMKey, - "genSelfSignedCert": generateSelfSignedCertificate, + "bcrypt": bcrypt, + "htpasswd": htpasswd, + "genPrivateKey": generatePrivateKey, + "derivePassword": derivePassword, + "buildCustomCert": buildCustomCertificate, + "genCA": generateCertificateAuthority, + "genCAWithKey": generateCertificateAuthorityWithPEMKey, + "genSelfSignedCert": generateSelfSignedCertificate, "genSelfSignedCertWithKey": generateSelfSignedCertificateWithPEMKey, - "genSignedCert": generateSignedCertificate, - "genSignedCertWithKey": generateSignedCertificateWithPEMKey, - "encryptAES": encryptAES, - "decryptAES": decryptAES, - "randBytes": randBytes, + "genSignedCert": generateSignedCertificate, + "genSignedCertWithKey": generateSignedCertificateWithPEMKey, + "encryptAES": encryptAES, + "decryptAES": decryptAES, + "randBytes": randBytes, // UUIDs: "uuidv4": uuidv4, diff --git a/list.go b/list.go index ca0fbb78..791a62d5 100644 --- a/list.go +++ b/list.go @@ -243,6 +243,28 @@ func sortAlpha(list interface{}) []string { return []string{strval(list)} } +func sortNumeric(list interface{}) []string { + k := reflect.Indirect(reflect.ValueOf(list)).Kind() + switch k { + case reflect.Slice, reflect.Array: + switch v := list.(type) { + case []interface{}: + b := make([]int, 0, len(v)) + for _, s := range v { + if s != nil { + b = append(b, toInt(s)) + } + } + sort.IntSlice(b).Sort() + return strslice(b) + default: + return strslice(list) + } + + } + return strslice(list) +} + func reverse(v interface{}) []interface{} { l, err := mustReverse(v) if err != nil { diff --git a/list_test.go b/list_test.go index ec4c4c14..7a64dcab 100644 --- a/list_test.go +++ b/list_test.go @@ -188,6 +188,17 @@ func TestMustRest(t *testing.T) { } } +func TestSortNumeric(t *testing.T) { + // Named `append` in the function map + tests := map[string]string{ + `{{ list 2 1 4 3 | sortNumeric }}`: "[1 2 3 4]", + `{{ list 0 10 2 4 1 11 | sortNumeric }}`: "[0 1 2 4 10 11]", + } + for tpl, expect := range tests { + assert.NoError(t, runt(tpl, expect)) + } +} + func TestReverse(t *testing.T) { tests := map[string]string{ `{{ list 1 2 3 | reverse | first }}`: "3",