String-related helpers return undefined for empty strings
See original GitHub issueA number of string-related helpers return undefined
when passed empty strings:
capitalize
capitalizeAll
center
ellipsis
hyphenate
isString
lowercase
pathcase
plusify
replace
reverse
sentence
split
titleize
truncate
uppercase
The library uses this expression to validate string arguments:
utils.isString = function(val) {
return val && typeof val === 'string';
};
This returns false
for empty strings even though they’re valid strings. Usually, this is not an issue in a Handlebars template as undefined
renders empty but, might be a problem if one is calling any of these helpers from other helpers.
I do understand that this might be intentional to skip string manipulation for empty strings and, that existing code could rely on these behavior but, interestingly, rectifying the above expression does not break any of the 419 tests.
I would propose changing the code or, docs and tests to make this explicit.
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (8 by maintainers)
Top Results From Across the Web
using .map in array of string but return undefined for empty string
try this. let l=[1, 2, 3, '', '', '', ''].filter(function (n) { if (n != '') { return n; } }); console.log(l);. Run...
Read more >Checking for Empty or Blank Strings in Java - Baeldung
Check out some simple ways in Java to test if a string is blank or empty.
Read more >PHP 8.0: New str_starts_with and str_ends_with functions
PHP 8's new behavior that it considers there is an empty string at every position of a string can be tricky. Note that...
Read more >Chap 4. Built-in Functions · GitBook
An error occurs when a string is passed, not a numeric representation, and values other than the defined value are returned undefined.
Read more >A quick way to check for null | undefined | empty string
Since day one of using retool it bugged me that I had to manually make sure some component or query variable is not...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yes, that looks good to me, thanks.
agreed, I think we should go with the first option as well. thanks
I mentioned that this would break code in the linked pr. Just because tests aren’t breaking doesn’t mean that downstream implementations won’t break.
Any subexpressions using these helpers that check for either a string or
undefined
will break.edit: try the following:
When the value returned by a helper is
undefined
, handlebars renders an empty string. Are you using the helpers as subexpressions, and checking for an empty string? If so, that’s the problem. I believe these helpers are returning the correct value.Also, since you pointed out a few helpers that aren’t using
isString
, we should probably update those to useisString
as well.