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.

String-related helpers return undefined for empty strings

See original GitHub issue

A 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:closed
  • Created 6 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
jonschlinkertcommented, May 28, 2017

Are we okay with documenting this?

Yes, that looks good to me, thanks.

they should all be consistent

agreed, I think we should go with the first option as well. thanks

1reaction
jonschlinkertcommented, May 28, 2017

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:

var handlebars = require('handlebars');

handlebars.registerHelper('foo', function() {
});

var fn = handlebars.compile('{{foo}}')
console.log(fn());

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 use isString as well.

Read more comments on GitHub >

github_iconTop 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 >

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