Skip to content

Commit c454907

Browse files
authored
fix: STRF-10180 Improve titleize and length helpers edge cases (#230)
1 parent 19b7768 commit c454907

4 files changed

Lines changed: 17 additions & 1 deletion

File tree

helpers/3p/collection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ helpers.iterate = function(context, options) {
8585
*/
8686

8787
helpers.length = function(value) {
88-
if (utils.isUndefined(value)) {return '';}
88+
if (!value || utils.isUndefined(value)) {return '';}
8989
if (typeof value === 'string' && /[[]/.test(value)) {
9090
value = utils.tryParse(value) || [];
9191
}

helpers/3p/string.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,9 @@ helpers.titleize = function(str) {
414414
if (str && typeof str === 'string') {
415415
var title = str.replace(/[ \-_]+/g, ' ');
416416
var words = title.match(/\w+/g);
417+
if (!words) {
418+
return str;
419+
}
417420
var len = words.length;
418421
var res = [];
419422
var i = 0;

spec/helpers/3p/collection.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ describe('collection', function() {
105105
done();
106106
});
107107

108+
it('should return empty strig when the value is null.', function(done) {
109+
var fn = hbs.compile('{{length foo}}');
110+
expect(fn({foo: null})).to.equal('');
111+
done();
112+
});
113+
108114
it('should return the length of a string.', function(done) {
109115
var fn = hbs.compile('{{length "foo"}}');
110116
expect(fn(context)).to.equal('3');

spec/helpers/3p/string.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,13 @@ describe('string', function() {
303303
expect(fn()).to.equal('');
304304
done();
305305
});
306+
307+
it('should return unchanged input string if string has only non-word characters', function(done) {
308+
var fn = hbs.compile('{{titleize ",!"}}');
309+
expect(fn()).to.equal(',!');
310+
done();
311+
});
312+
306313
it('should return the string in title case.', function(done) {
307314
var fn = hbs.compile('{{titleize "Bender-should-Not-be-allowed_on_Tv"}}');
308315
expect(fn()).to.equal('Bender Should Not Be Allowed On Tv');

0 commit comments

Comments
 (0)