Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 362 - sortNumeric #392

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 15 additions & 15 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -320,6 +319,7 @@ var genericMap = map[string]interface{}{
"mustLast": mustLast,
"initial": initial,
"mustInitial": mustInitial,
"sortNumeric": sortNumeric,
"reverse": reverse,
"mustReverse": mustReverse,
"uniq": uniq,
Expand All @@ -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,
Expand Down
22 changes: 22 additions & 0 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down