Skip to content

Commit

Permalink
Merge pull request #268 from Monsterblader/bugfix-occurrences
Browse files Browse the repository at this point in the history
Bugfix occurrences
  • Loading branch information
jonschlinkert committed May 15, 2017
2 parents a3683ba + cda78ff commit 0b8390a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
16 changes: 6 additions & 10 deletions lib/string.js
Expand Up @@ -205,16 +205,16 @@ helpers.lowercase = function(str) {
};

/**
* Return the number of occurrances of `substring` within the
* Return the number of occurrences of `substring` within the
* given `string`.
*
* ```handlebars
* {{occurrances "foo bar foo bar baz" "foo"}}
* {{occurrences "foo bar foo bar baz" "foo"}}
* //=> 2
* ```
* @param {String} `str`
* @param {String} `substring`
* @return {Number} Number of occurrances
* @return {Number} Number of occurrences
* @api public
*/

Expand All @@ -224,13 +224,9 @@ helpers.occurrences = function(str, substring) {
var pos = 0;
var n = 0;

while ((pos = str.indexOf(substring, pos))) {
if (pos > -1) {
n++;
pos += len;
} else {
break;
}
while ((pos = str.indexOf(substring, pos)) > -1) {
n++;
pos += len;
}
return n;
}
Expand Down
2 changes: 1 addition & 1 deletion test/string.js
Expand Up @@ -127,7 +127,7 @@ describe('string', function() {
assert.equal(fn(), '');
});
it('should return the number of occurrences of a string, within a string.', function() {
var fn = hbs.compile('{{occurrences "Death by Snu-Snu" "Snu"}}');
var fn = hbs.compile('{{occurrences "Jar-Jar Binks" "Jar"}}');
assert.equal(fn(), '2');
});
});
Expand Down

0 comments on commit 0b8390a

Please sign in to comment.