@technobuddha/library
Version:
A large library of useful functions
20 lines (19 loc) • 549 B
JavaScript
/**
* Compute the number of times a substring occors within a string
*
* @param input The string
* @param supstring The substring to look for
* @param __namedParameters see {@link Options}
* @return number of times *substring* occurs within *input*
*/
export function count(input, substring, { overlap = false } = {}) {
const step = overlap ? 1 : substring.length;
let cnt = 0;
let pos = 0;
while ((pos = input.indexOf(substring, pos)) >= 0) {
cnt++;
pos += step;
}
return cnt;
}
export default count;