feat(tally): wire regex counting path in main with invalid-regex exit-2
Step 2 of plan.md (Spor B B3 pipeline run). Wires the --regex/-r flag into main(): when set, compileRegex(pattern) is used and the count is text.match(re).length. Invalid regex exits 2 via the existing fail() helper. JSON output now includes flags.regex so consumers can tell the mode apart. Baseline tests remain green; -i/--ignore-case has no effect when --regex is set (out of brief scope). Verify covered: SC #1 (any position), SC #2 (-r short form), SC #3 (regex semantics differ), SC #4 (invalid exits 2), SC #5 (JSON regex), SC #6 (byte-identical baseline). [skip-docs]
This commit is contained in:
parent
c4cf49f1d2
commit
44d7f339f5
1 changed files with 10 additions and 4 deletions
|
|
@ -70,13 +70,19 @@ function main() {
|
||||||
process.stderr.write(`tally: ${what}: ${file}\n`);
|
process.stderr.write(`tally: ${what}: ${file}\n`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
const count = flags.lines
|
let count;
|
||||||
? countLines(text, pattern, flags.ignoreCase)
|
if (flags.regex) {
|
||||||
: countOccurrences(text, pattern, flags.ignoreCase);
|
const re = compileRegex(pattern);
|
||||||
|
count = (text.match(re) || []).length;
|
||||||
|
} else if (flags.lines) {
|
||||||
|
count = countLines(text, pattern, flags.ignoreCase);
|
||||||
|
} else {
|
||||||
|
count = countOccurrences(text, pattern, flags.ignoreCase);
|
||||||
|
}
|
||||||
if (flags.json) {
|
if (flags.json) {
|
||||||
process.stdout.write(JSON.stringify({
|
process.stdout.write(JSON.stringify({
|
||||||
pattern, file, count,
|
pattern, file, count,
|
||||||
flags: { json: flags.json, ignoreCase: flags.ignoreCase, lines: flags.lines },
|
flags: { json: flags.json, ignoreCase: flags.ignoreCase, lines: flags.lines, regex: flags.regex },
|
||||||
}) + '\n');
|
}) + '\n');
|
||||||
} else {
|
} else {
|
||||||
process.stdout.write(count + '\n');
|
process.stdout.write(count + '\n');
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue