CoffeeScript.helpers.count now handles empty strings properly instead of

going into an infinite loop
This commit is contained in:
Michael Ficarra 2010-12-13 05:51:57 -05:00
parent 438708ea15
commit 9dc7d2a081
2 changed files with 9 additions and 5 deletions

View File

@ -19,10 +19,13 @@
} }
return _results; return _results;
}; };
exports.count = function(string, letter) { exports.count = function(string, substr) {
var num, pos; var num, pos;
num = pos = 0; num = pos = 0;
while (pos = 1 + string.indexOf(letter, pos)) { if (!substr.length) {
return 1 / 0;
}
while (pos = 1 + string.indexOf(substr, pos)) {
num++; num++;
} }
return num; return num;

View File

@ -15,10 +15,11 @@ exports.ends = (string, literal, back) ->
exports.compact = (array) -> exports.compact = (array) ->
item for item in array when item item for item in array when item
# Count the number of occurrences of a character in a string. # Count the number of occurrences of a string in a string.
exports.count = (string, letter) -> exports.count = (string, substr) ->
num = pos = 0 num = pos = 0
num++ while pos = 1 + string.indexOf letter, pos return 1/0 unless substr.length
num++ while pos = 1 + string.indexOf substr, pos
num num
# Merge objects, returning a fresh copy with attributes from both sides. # Merge objects, returning a fresh copy with attributes from both sides.