diff options
Diffstat (limited to 'tpl/compare/compare.go')
-rw-r--r-- | tpl/compare/compare.go | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/tpl/compare/compare.go b/tpl/compare/compare.go index 251b3d13b..ec228822c 100644 --- a/tpl/compare/compare.go +++ b/tpl/compare/compare.go @@ -20,18 +20,20 @@ import ( "strconv" "time" - "github.com/gohugoio/hugo/common/types" - "github.com/gohugoio/hugo/compare" + + "github.com/gohugoio/hugo/common/types" ) // New returns a new instance of the compare-namespaced template functions. -func New() *Namespace { - return &Namespace{} +func New(caseInsensitive bool) *Namespace { + return &Namespace{caseInsensitive: caseInsensitive} } // Namespace provides template functions for the "compare" namespace. type Namespace struct { + // Enable to do case insensitive string compares. + caseInsensitive bool } // Default checks whether a given value is set and returns a default value if it @@ -89,7 +91,10 @@ func (*Namespace) Default(dflt interface{}, given ...interface{}) (interface{}, } // Eq returns the boolean truth of arg1 == arg2. -func (*Namespace) Eq(x, y interface{}) bool { +func (ns *Namespace) Eq(x, y interface{}) bool { + if ns.caseInsensitive { + panic("caseInsensitive not implemented for Eq") + } if e, ok := x.(compare.Eqer); ok { return e.Eq(y) } @@ -157,7 +162,7 @@ func (n *Namespace) Conditional(condition bool, a, b interface{}) interface{} { return b } -func (*Namespace) compareGet(a interface{}, b interface{}) (float64, float64) { +func (ns *Namespace) compareGet(a interface{}, b interface{}) (float64, float64) { if ac, ok := a.(compare.Comparer); ok { c := ac.Compare(b) if c < 0 { @@ -228,6 +233,17 @@ func (*Namespace) compareGet(a interface{}, b interface{}) (float64, float64) { } } + if ns.caseInsensitive && leftStr != nil && rightStr != nil { + c := compare.Strings(*leftStr, *rightStr) + if c < 0 { + return 0, 1 + } else if c > 0 { + return 1, 0 + } else { + return 0, 0 + } + } + switch { case leftStr == nil || rightStr == nil: case *leftStr < *rightStr: |