Skip to content

Commit

Permalink
tpl: updates cast.ToTruth to conform to hreflect.IsTruthful
Browse files Browse the repository at this point in the history
Concession from review, where cast.ToTruth should behave exactly like hreflect.IsTruthful.
Additionally, cast.ToBool always returns a bool with nil error.
  • Loading branch information
khayyamsaleem committed Oct 20, 2023
1 parent 5a72de2 commit a93a70e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
10 changes: 5 additions & 5 deletions docs/content/en/functions/truth.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ It follows the same rules as [`bool`](/functions/bool), but with increased flexi

```
{{ truth "true" }} → true
{{ truth "false" }} → false
{{ truth "false" }} → true
{{ truth "TRUE" }} → true
{{ truth "FALSE" }} → false
{{ truth "FALSE" }} → true
{{ truth "t" }} → true
{{ truth "f" }} → false
{{ truth "f" }} → true
{{ truth "T" }} → true
{{ truth "F" }} → false
{{ truth "F" }} → true
{{ truth "1" }} → true
{{ truth "0" }} → false
{{ truth "0" }} → true
{{ truth 1 }} → true
{{ truth 0 }} → false
Expand Down
18 changes: 7 additions & 11 deletions tpl/cast/cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cast
import (
"html/template"

"github.com/gohugoio/hugo/common/hreflect"
_cast "github.com/spf13/cast"
)

Expand Down Expand Up @@ -49,23 +50,18 @@ func (ns *Namespace) ToFloat(v any) (float64, error) {
// ToBool converts v to a boolean.
func (ns *Namespace) ToBool(v any) (bool, error) {
v = convertTemplateToString(v)
return _cast.ToBoolE(v)
result, err := _cast.ToBoolE(v)
if err != nil {
return false, nil
}
return result, nil
}

// ToTruth yields the same behavior as ToBool when possible.
// If the cast is unsuccessful, ToTruth converts v to a boolean using the JavaScript [definition of truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy).
// Accordingly, it never yields an error, but maintains the signature of other cast methods for consistency.
func (ns *Namespace) ToTruth(v any) (bool, error) {
result, err := ns.ToBool(v)
if err != nil {
switch v {
case "", "nil", "null", "undefined", "NaN":
return false, nil
default:
return true, nil
}
}
return result, nil
return hreflect.IsTruthful(v), nil
}

func convertTemplateToString(v any) any {
Expand Down
17 changes: 8 additions & 9 deletions tpl/cast/cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ func TestToBool(t *testing.T) {
// error cases
{"cheese", nil, false},
{"", nil, false},
{1.67, nil, false},
} {
errMsg := qt.Commentf("[%d] %v", i, test.v)

Expand All @@ -173,15 +172,15 @@ func TestToTruth(t *testing.T) {
expect any
}{
{"true", true},
{"false", false},
{"false", true},
{"TRUE", true},
{"FALSE", false},
{"FALSE", true},
{"t", true},
{"f", false},
{"f", true},
{"T", true},
{"F", false},
{"F", true},
{"1", true},
{"0", false},
{"0", true},
{1, true},
{0, false},
{"cheese", true},
Expand All @@ -194,9 +193,9 @@ func TestToTruth(t *testing.T) {
{template.JSStr("6"), true},
{t, true},
{nil, false},
{"null", false},
{"undefined", false},
{"NaN", false},
{"null", true},
{"undefined", true},
{"NaN", true},
} {
errMsg := qt.Commentf("[%d] %v", i, test.v)

Expand Down

0 comments on commit a93a70e

Please sign in to comment.