Urara-Blog/node_modules/.pnpm-store/v3/files/07/7a0a4f94c3ae2fc8696b1fce9b881e05acb3ed73b09337d6c1ce744204fece98811b2bb0aa958148b4dec2f57785f962eaaf73ad6c4e02d7eae5f25a188cdd
2022-08-14 01:14:53 +08:00

36 lines
812 B
Text

/**
* Get the count of the longest repeating streak of `character` in `value`.
*
* @param {string} value
* Content to search in.
* @param {string} character
* Single character to look for.
* @returns {number}
* Count of most frequent adjacent `character`s in `value`.
*/
export function longestStreak(value, character) {
const source = String(value)
let index = source.indexOf(character)
let expected = index
let count = 0
let max = 0
if (typeof character !== 'string' || character.length !== 1) {
throw new Error('Expected character')
}
while (index !== -1) {
if (index === expected) {
if (++count > max) {
max = count
}
} else {
count = 1
}
expected = index + 1
index = source.indexOf(character, expected)
}
return max
}