mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
CoffeeScript.helpers.count now handles empty strings properly instead of
going into an infinite loop
This commit is contained in:
parent
438708ea15
commit
9dc7d2a081
2 changed files with 9 additions and 5 deletions
|
@ -19,10 +19,13 @@
|
|||
}
|
||||
return _results;
|
||||
};
|
||||
exports.count = function(string, letter) {
|
||||
exports.count = function(string, substr) {
|
||||
var num, pos;
|
||||
num = pos = 0;
|
||||
while (pos = 1 + string.indexOf(letter, pos)) {
|
||||
if (!substr.length) {
|
||||
return 1 / 0;
|
||||
}
|
||||
while (pos = 1 + string.indexOf(substr, pos)) {
|
||||
num++;
|
||||
}
|
||||
return num;
|
||||
|
|
|
@ -15,10 +15,11 @@ exports.ends = (string, literal, back) ->
|
|||
exports.compact = (array) ->
|
||||
item for item in array when item
|
||||
|
||||
# Count the number of occurrences of a character in a string.
|
||||
exports.count = (string, letter) ->
|
||||
# Count the number of occurrences of a string in a string.
|
||||
exports.count = (string, substr) ->
|
||||
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
|
||||
|
||||
# Merge objects, returning a fresh copy with attributes from both sides.
|
||||
|
|
Loading…
Add table
Reference in a new issue