• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

albe / node-event-storage / 26529902254

27 May 2026 06:14PM UTC coverage: 98.42% (-0.04%) from 98.459%
26529902254

push

github

web-flow
Merge pull request #318 from albe/copilot/refactor-codebase-maintainability

Reduce cyclomatic complexity in raw-stream hotspots and consolidate utilities under `src/utils`

1200 of 1246 branches covered (96.31%)

Branch coverage included in aggregate %.

283 of 287 new or added lines in 14 files covered. (98.61%)

5901 of 5969 relevant lines covered (98.86%)

882.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.31
/src/utils/jsonUtil.js
1
const BYTE_QUOTE = 0x22;
4✔
2
const BYTE_ESCAPE = 0x5c;
4✔
3
const BYTE_OPEN_OBJECT = 0x7b;
4✔
4
const BYTE_CLOSE_OBJECT = 0x7d;
4✔
5
const BYTE_OPEN_ARRAY = 0x5b;
4✔
6
const BYTE_CLOSE_ARRAY = 0x5d;
4✔
7
const BYTE_COMMA = 0x2c;
4✔
8

4✔
9
/**
4✔
10
 * Find the position of `pattern` within `buffer` at depth 0 (the top-level object), starting
4✔
11
 * from `startOffset`.  It scans character-by-character tracking JSON nesting depth and string
4✔
12
 * quoting.  If `matchPosition` arrives at depth > 0 it means the pattern is inside a nested
4✔
13
 * object/array, so the scan continues searching for the next candidate at depth 0.  Returns -1
4✔
14
 * when no such position exists before the end of the buffer or when a closing brace reduces depth
4✔
15
 * below zero (the top-level object has ended).
4✔
16
 */
4✔
17
function indexOfSameLevel(buffer, pattern, startOffset = 0, matchPosition) {
136✔
18
    /* c8 ignore start */
4✔
19
    // Defensive fallback: public call path precomputes an initial candidate in preCheck.
4✔
20
    if (matchPosition === undefined) {
4✔
21
        matchPosition = buffer.indexOf(pattern, startOffset);
4✔
22
    }
4✔
23
    if (matchPosition === -1) {
4✔
24
        return -1;
4✔
25
    }
4✔
26
    /* c8 ignore stop */
4✔
27

132✔
28
    let depth = 0;
132✔
29
    let inString = false;
132✔
30
    let i = startOffset;
132✔
31

132✔
32
    while (i < buffer.length) {
136✔
33
        if (inString) {
984✔
34
            if (buffer[i] === BYTE_ESCAPE) { // '\\'
640✔
35
                i += 2;
12✔
36
                continue;
12✔
37
            }
12✔
38
            if (buffer[i] === BYTE_QUOTE) { // '"'
640✔
39
                inString = false;
96✔
40
            }
96✔
41
            i++;
628✔
42
            continue;
628✔
43
        }
628✔
44

344✔
45
        const ch = buffer[i];
344✔
46
        if (ch === BYTE_OPEN_OBJECT || ch === BYTE_OPEN_ARRAY) { // '{' or '['
984✔
47
            depth++;
12✔
48
            i++;
12✔
49
            continue;
12✔
50
        } else if (ch === BYTE_CLOSE_OBJECT || ch === BYTE_CLOSE_ARRAY) { // '}' or ']'
984✔
51
            depth--;
8✔
52

8✔
53
            if (depth < 0) {
8✔
54
                return -1;
4✔
55
            }
4✔
56

4✔
57
            i++;
4✔
58
            continue;
4✔
59
        } else if (ch === BYTE_QUOTE) { // '"'
332✔
60
            inString = true;
224✔
61
        }
224✔
62

324✔
63
        if (i >= matchPosition) {
984✔
64
            if (i === matchPosition && ch === BYTE_QUOTE && depth === 0) { // '"'
128✔
65
                const end = i + pattern.length;
120✔
66
                if (pattern[pattern.length - 1] === BYTE_OPEN_OBJECT) { // '{'
120✔
67
                    return i;
52✔
68
                }
52✔
69
                if (buffer[end] === BYTE_COMMA || buffer[end] === BYTE_CLOSE_OBJECT || buffer[end] === BYTE_CLOSE_ARRAY) { // ',' or '}' or ']'
120✔
70
                    return i;
64✔
71
                }
64✔
72
            }
120✔
73

12✔
74
            matchPosition = buffer.indexOf(pattern, matchPosition + 1);
12✔
75
            if (matchPosition < 0) {
12✔
76
                return -1;
12✔
77
            }
12✔
78
        }
128✔
79

196✔
80
        i++;
196✔
81
    }
196✔
NEW
82

×
83
    /* c8 ignore next */
4✔
84
    return -1;
4✔
85
}
136✔
86

4✔
87
export { BYTE_OPEN_OBJECT, indexOfSameLevel };
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc