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,7 @@
"use strict";
const shared = require("./shared");
const { addErrorContext, forEachHeading } = shared;
module.exports = {
"names": [ "MD024", "no-duplicate-heading", "no-duplicate-header" ],
@ -14,7 +15,7 @@ module.exports = {
const knownContents = [ null, [] ];
let lastLevel = 1;
let knownContent = knownContents[lastLevel];
shared.forEachHeading(params, function forHeading(heading, content) {
forEachHeading(params, (heading, content) => {
if (siblingsOnly) {
const newLevel = heading.tag.slice(1);
while (lastLevel < newLevel) {
@ -27,11 +28,11 @@ module.exports = {
}
knownContent = knownContents[newLevel];
}
if (knownContent.indexOf(content) === -1) {
knownContent.push(content);
} else {
shared.addErrorContext(onError, heading.lineNumber,
if (knownContent.includes(content)) {
addErrorContext(onError, heading.lineNumber,
heading.line.trim());
} else {
knownContent.push(content);
}
});
}