reviving an old es-disscuss topic.
the problem RegExp.p.count()
solves deal primarily with converting absolute-offsets to line-numbers:
- Performantly get line-numbers directly from raw v8-coverage-files:
startLine = /\n/.count(
source,
0,
v8CoverageObj.startOffset
)
endLine = startLine + /\n/.count(
source,
v8CoverageObj.startOffset,
v8CoverageObj.endOffset
)
- preserve line-numbering when linting embedded css/js in .html files:
Array.from(htmlSource.matchAll(/<script>([\S\s])<\/script>/)).forEach(function (matchObj) {
let lineOffset = /n/.count(htmlSource, 0, matchObj.index);
let warnings = lint(matchObj[1]);
warnings.forEach(function ({
line,
...
}) {
console.error(`warning at line ${line + lineOffset} ...`);
});
});
- counting number of subdirectories in pathname
count = /\//.count(urlParsed.pathname); // "/aa/bb/cc.html"
html += `href="${"../".repeat(count - 1)}index.html"`; // "../../index.html"