Add includesSorted function, use for faster searches of sorted arrays.

This commit is contained in:
David Anson 2019-03-28 22:06:42 -07:00
parent d7c0d195d7
commit 9b9532e163
8 changed files with 86 additions and 50 deletions

View file

@ -3,6 +3,8 @@
"use strict";
const shared = require("./shared");
const { addErrorDetailIf, bareUrlRe, escapeForRegExp, filterTokens,
forEachInlineChild, newLineRe } = shared;
module.exports = {
"names": [ "MD044", "proper-names" ],
@ -12,35 +14,35 @@ module.exports = {
const names = params.config.names || [];
const codeBlocks = params.config.code_blocks;
const includeCodeBlocks = (codeBlocks === undefined) ? true : !!codeBlocks;
names.forEach(function forName(name) {
const escapedName = shared.escapeForRegExp(name);
names.forEach((name) => {
const escapedName = escapeForRegExp(name);
const namePattern = "\\S*\\b(" + escapedName + ")\\b\\S*";
const anyNameRe = new RegExp(namePattern, "gi");
function forToken(token) {
const fenceOffset = (token.type === "fence") ? 1 : 0;
token.content.split(shared.newLineRe)
.forEach(function forLine(line, index) {
token.content.split(newLineRe)
.forEach((line, index) => {
let match = null;
while ((match = anyNameRe.exec(line)) !== null) {
const fullMatch = match[0];
if (!shared.bareUrlRe.test(fullMatch)) {
if (!bareUrlRe.test(fullMatch)) {
const wordMatch = fullMatch
.replace(/^\W*/, "").replace(/\W*$/, "");
if (names.indexOf(wordMatch) === -1) {
if (!names.includes(wordMatch)) {
const lineNumber = token.lineNumber + index + fenceOffset;
const range = [ match.index + 1, wordMatch.length ];
shared.addErrorDetailIf(onError, lineNumber,
addErrorDetailIf(onError, lineNumber,
name, match[1], null, null, range);
}
}
}
});
}
shared.forEachInlineChild(params, "text", forToken);
forEachInlineChild(params, "text", forToken);
if (includeCodeBlocks) {
shared.forEachInlineChild(params, "code_inline", forToken);
shared.filterTokens(params, "code_block", forToken);
shared.filterTokens(params, "fence", forToken);
forEachInlineChild(params, "code_inline", forToken);
filterTokens(params, "code_block", forToken);
filterTokens(params, "fence", forToken);
}
});
}