mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.1 KiB
51 lines
1.1 KiB
'use strict';
|
|
|
|
/**
|
|
* Get the count of the longest repeating streak of
|
|
* `character` in `value`.
|
|
*
|
|
* @example
|
|
* longestStreak('` foo `` bar `', '`') // 2
|
|
*
|
|
* @param {string} value - Content, coerced to string.
|
|
* @param {string} character - Single character to look
|
|
* for.
|
|
* @return {number} - Number of characters at the place
|
|
* where `character` occurs in its longest streak in
|
|
* `value`.
|
|
* @throws {Error} - when `character` is not a single
|
|
* character.
|
|
*/
|
|
function longestStreak(value, character) {
|
|
var count = 0;
|
|
var maximum = 0;
|
|
var index = -1;
|
|
var length;
|
|
|
|
value = String(value);
|
|
length = value.length;
|
|
|
|
if (typeof character !== 'string' || character.length !== 1) {
|
|
throw new Error('Expected character');
|
|
}
|
|
|
|
while (++index < length) {
|
|
if (value.charAt(index) === character) {
|
|
count++;
|
|
|
|
if (count > maximum) {
|
|
maximum = count;
|
|
}
|
|
} else {
|
|
count = 0;
|
|
}
|
|
}
|
|
|
|
return maximum;
|
|
}
|
|
|
|
/*
|
|
* Expose.
|
|
*/
|
|
|
|
module.exports = longestStreak;
|
|
|