Skip to content

Commit ec12b8c

Browse files
committed
Revise count
1 parent 79af0fc commit ec12b8c

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

packages/react-error-overlay/src/utils/unmapper.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,15 @@ import { getLinesAround } from './getLinesAround';
55
import path from 'path';
66

77
function count(search: string, string: string): number {
8-
let count = -1, index = 0;
9-
for (; index !== -1; count++, (index = string.indexOf(search, index + 1)));
8+
// Count starts at -1 becuse a do-while loop always runs at least once
9+
let count = -1, index = -1;
10+
do {
11+
// First call or the while case evaluated true, meaning we have to make
12+
// count 0 or we found a character
13+
++count;
14+
// Find the index of our search string, starting after the previous index
15+
index = string.indexOf(search, index + 1);
16+
} while (index !== -1);
1017
return count;
1118
}
1219

0 commit comments

Comments
 (0)