question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A false result of a boolean helper is interpreted as a truthy "false" string

See original GitHub issue

A false result of a boolean helper is interpreted as a truthy "False" string.

Test case:

{{#if (Regex.IsMatch "a" "b")}}
 TRUE
{{else}}
 FALSE
{{/if}}

This prints TRUE; we should expect FALSE.

(Regex.IsMatch taken from https://github.com/StefH/Handlebars.Net.Helpers/wiki/Regex#ismatch .)

IsMatch correctly evaluates the regex match as (bool) False and prints it to the text writer. Then the sub-expression handler pulls it out as the "False" string and hands it over to the built-in “#if”. The “#if” expects a boolean value so it runs HandlebarsUtils.IsTruthyOrNonEmpty("False") which evaluates it as true.

I believe "False" should evaluate to false, especially if produced by a boolean expression. The user shouldn’t be affected by the fact that Handlebars.Net converts the value to string first (and pulls it out using CaptureTextWriterOutputFromHelper()).

For comparison, this works fine in JS:

handlebars.registerHelper('soBoolean', () => false);
const boolTemplate = handlebars.compile('{{#if (soBoolean)}}TRUE{{else}}FALSE{{/if}}');
console.log(boolTemplate()); // TRUE

The easy fix is to change IsFalsy() to perform Boolean.TryParse() for string values.

We might also want to consider my earlier suggestion that helpers should return value rather than print it to the text writer.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
StefHcommented, Sep 24, 2020

OK.

Once you are complete, create an issue in https://github.com/StefH/Handlebars.Net.Helpers project so I can take action to upgrade.

0reactions
sferencikcommented, Sep 24, 2020

Great news, @zjklee @rexm!

On Thu, Sep 24, 2020 at 6:04 PM Oleh Formaniuk notifications@github.com wrote:

@StefH https://github.com/StefH yes, I’m going to be one of the maintainers.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Handlebars-Net/Handlebars.Net/issues/361#issuecomment-698438695, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAGFM3KXQQH6FVPGUWTZ5ADSHNUYLANCNFSM4RQM6H7Q .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Truthy and Falsy Values: When All is Not Equal in JavaScript
Anything in JavaScript can be considered truthy or falsy. ... A more obvious false result occurs when comparing with === (strict equality) ...
Read more >
trouble with truthy/falsey - javascript
I have learned that: a falsey value is a value that is considered false when encountered in a boolean context ex: false, 0,...
Read more >
Truthy and Falsy Values in Python: A Detailed Introduction
Falsy values are values that evaluate to False in a boolean context. Falsy values include empty sequences (lists, tuples, strings, dictionaries, ...
Read more >
Falsy Values in JavaScript
A falsy value is something which evaluates to FALSE, for instance when checking a variable. There are only six falsey values in JavaScript: ......
Read more >
Truthy - MDN Web Docs Glossary: Definitions of Web-related ...
In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context. All values are truthy unless...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found