All files / util strCnt.ts

100% Statements 12/12
100% Branches 3/3
100% Functions 1/1
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 121x 25x 25x 25x 23x 49x 49x 26x 26x      
export const strCnt = (needle: string, haystack: string, offset: number = 0): number => {
  let cnt = 0;
  const needleLen = needle.length;
  if (needleLen === 0) return 0;
  while (true) {
    const index = haystack.indexOf(needle, offset);
    if (index < 0) return cnt;
    cnt++;
    offset = index + needleLen;
  }
};