-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (32 loc) · 823 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* Get the count of the longest repeating streak of `substring` in `value`.
*
* @param {string} value
* Content to search in.
* @param {string} substring
* Substring to look for, typically one character.
* @returns {number}
* Count of most frequent adjacent `substring`s in `value`.
*/
export function longestStreak(value, substring) {
const source = String(value)
let index = source.indexOf(substring)
let expected = index
let count = 0
let max = 0
if (typeof substring !== 'string') {
throw new TypeError('Expected substring')
}
while (index !== -1) {
if (index === expected) {
if (++count > max) {
max = count
}
} else {
count = 1
}
expected = index + substring.length
index = source.indexOf(substring, expected)
}
return max
}