Convert filterTokens from array to callback for ~7% time reduction.

This commit is contained in:
David Anson 2015-05-29 18:18:20 -07:00
parent 9bf0b1a554
commit 17763331a6

View file

@ -31,10 +31,12 @@ function unorderedListStyleFor(token) {
} }
} }
// Filters a list of tokens by type // Calls the provided function for each matching token
function filterTokens(tokens, typeA, typeB) { function filterTokens(tokens, type, callback) {
return tokens.filter(function filterToken(token) { tokens.forEach(function forToken(token) {
return ((token.type === typeA) || (token.type === typeB)); if (token.type === type) {
callback(token);
}
}); });
} }
@ -42,8 +44,7 @@ function filterTokens(tokens, typeA, typeB) {
function forEachLine(params, callback) { function forEachLine(params, callback) {
// Identify lines in code blocks // Identify lines in code blocks
var codeLines = []; var codeLines = [];
filterTokens(params.tokens, "code_block") filterTokens(params.tokens, "code_block", function forToken(token) {
.forEach(function forToken(token) {
for (var i = token.map[0]; i < token.map[1]; i++) { for (var i = token.map[0]; i < token.map[1]; i++) {
codeLines.push(i); codeLines.push(i);
} }
@ -62,10 +63,8 @@ function forEachLine(params, callback) {
// Calls the provided function for each specified inline child token // Calls the provided function for each specified inline child token
function forEachInlineChild(params, type, callback) { function forEachInlineChild(params, type, callback) {
filterTokens(params.tokens, "inline") filterTokens(params.tokens, "inline", function forToken(token) {
.forEach(function forToken(token) { filterTokens(token.children, type, function forChild(child) {
filterTokens(token.children, type)
.forEach(function forChild(child) {
callback(child, token); callback(child, token);
}); });
}); });
@ -131,8 +130,7 @@ module.exports = [
"tags": [ "headers" ], "tags": [ "headers" ],
"func": function MD001(params, errors) { "func": function MD001(params, errors) {
var prevLevel = 0; var prevLevel = 0;
filterTokens(params.tokens, "heading_open") filterTokens(params.tokens, "heading_open", function forToken(token) {
.forEach(function forToken(token) {
var level = parseInt(token.tag.slice(1), 10); var level = parseInt(token.tag.slice(1), 10);
if (prevLevel && (level > prevLevel + 1)) { if (prevLevel && (level > prevLevel + 1)) {
errors.push(token.lineNumber); errors.push(token.lineNumber);
@ -165,11 +163,10 @@ module.exports = [
"tags": [ "headers" ], "tags": [ "headers" ],
"func": function MD003(params, errors) { "func": function MD003(params, errors) {
var style = params.options.style || "consistent"; var style = params.options.style || "consistent";
var headings = filterTokens(params.tokens, "heading_open"); filterTokens(params.tokens, "heading_open", function forToken(token) {
if ((style === "consistent") && headings.length) { if (style === "consistent") {
style = headingStyleFor(headings[0]); style = headingStyleFor(token);
} }
headings.forEach(function forToken(token) {
if (headingStyleFor(token) !== style) { if (headingStyleFor(token) !== style) {
errors.push(token.lineNumber); errors.push(token.lineNumber);
} }
@ -321,8 +318,8 @@ module.exports = [
"desc": "Dollar signs used before commands without showing output", "desc": "Dollar signs used before commands without showing output",
"tags": [ "code" ], "tags": [ "code" ],
"func": function MD014(params, errors) { "func": function MD014(params, errors) {
filterTokens(params.tokens, "code_block", "fence") [ "code_block", "fence" ].forEach(function forType(type) {
.forEach(function forToken(token) { filterTokens(params.tokens, type, function forToken(token) {
if (token.content && token.content.split(shared.newLineRe) if (token.content && token.content.split(shared.newLineRe)
.filter(function filterLine(line) { .filter(function filterLine(line) {
return line; return line;
@ -332,6 +329,7 @@ module.exports = [
errors.push(token.lineNumber); errors.push(token.lineNumber);
} }
}); });
});
} }
}, },
@ -353,8 +351,7 @@ module.exports = [
"desc": "Multiple spaces after hash on atx style header", "desc": "Multiple spaces after hash on atx style header",
"tags": [ "headers", "atx", "spaces" ], "tags": [ "headers", "atx", "spaces" ],
"func": function MD019(params, errors) { "func": function MD019(params, errors) {
filterTokens(params.tokens, "heading_open") filterTokens(params.tokens, "heading_open", function forToken(token) {
.forEach(function forToken(token) {
if ((headingStyleFor(token) === "atx") && if ((headingStyleFor(token) === "atx") &&
/^#+\s\s/.test(token.line)) { /^#+\s\s/.test(token.line)) {
errors.push(token.lineNumber); errors.push(token.lineNumber);
@ -382,8 +379,7 @@ module.exports = [
"desc": "Multiple spaces inside hashes on closed atx style header", "desc": "Multiple spaces inside hashes on closed atx style header",
"tags": [ "headers", "atx_closed", "spaces" ], "tags": [ "headers", "atx_closed", "spaces" ],
"func": function MD021(params, errors) { "func": function MD021(params, errors) {
filterTokens(params.tokens, "heading_open") filterTokens(params.tokens, "heading_open", function forToken(token) {
.forEach(function forToken(token) {
if ((headingStyleFor(token) === "atx_closed") && if ((headingStyleFor(token) === "atx_closed") &&
(/^#+\s\s/.test(token.line) || /\s\s#+$/.test(token.line))) { (/^#+\s\s/.test(token.line) || /\s\s#+$/.test(token.line))) {
errors.push(token.lineNumber); errors.push(token.lineNumber);
@ -434,8 +430,7 @@ module.exports = [
"desc": "Headers must start at the beginning of the line", "desc": "Headers must start at the beginning of the line",
"tags": [ "headers", "spaces" ], "tags": [ "headers", "spaces" ],
"func": function MD023(params, errors) { "func": function MD023(params, errors) {
filterTokens(params.tokens, "heading_open") filterTokens(params.tokens, "heading_open", function forToken(token) {
.forEach(function forToken(token) {
if (/^\s/.test(token.line)) { if (/^\s/.test(token.line)) {
errors.push(token.lineNumber); errors.push(token.lineNumber);
} }
@ -465,8 +460,7 @@ module.exports = [
"tags": [ "headers" ], "tags": [ "headers" ],
"func": function MD025(params, errors) { "func": function MD025(params, errors) {
var hasTopLevelHeading = false; var hasTopLevelHeading = false;
filterTokens(params.tokens, "heading_open") filterTokens(params.tokens, "heading_open", function forToken(token) {
.forEach(function forToken(token) {
if (token.tag === "h1") { if (token.tag === "h1") {
if (hasTopLevelHeading) { if (hasTopLevelHeading) {
errors.push(token.lineNumber); errors.push(token.lineNumber);
@ -624,8 +618,7 @@ module.exports = [
"desc": "Inline HTML", "desc": "Inline HTML",
"tags": [ "html" ], "tags": [ "html" ],
"func": function MD033(params, errors) { "func": function MD033(params, errors) {
filterTokens(params.tokens, "html_block") filterTokens(params.tokens, "html_block", function forToken(token) {
.forEach(function forToken(token) {
errors.push(token.lineNumber); errors.push(token.lineNumber);
}); });
forEachInlineChild(params, "html_inline", function forToken(token) { forEachInlineChild(params, "html_inline", function forToken(token) {
@ -639,8 +632,7 @@ module.exports = [
"desc": "Bare URL used", "desc": "Bare URL used",
"tags": [ "links", "url" ], "tags": [ "links", "url" ],
"func": function MD034(params, errors) { "func": function MD034(params, errors) {
filterTokens(params.tokens, "inline") filterTokens(params.tokens, "inline", function forToken(token) {
.forEach(function forToken(token) {
var inLink = false; var inLink = false;
token.children.forEach(function forChild(child) { token.children.forEach(function forChild(child) {
if (child.type === "link_open") { if (child.type === "link_open") {
@ -663,11 +655,10 @@ module.exports = [
"tags": [ "hr" ], "tags": [ "hr" ],
"func": function MD035(params, errors) { "func": function MD035(params, errors) {
var style = params.options.style || "consistent"; var style = params.options.style || "consistent";
var horizontalRules = filterTokens(params.tokens, "hr"); filterTokens(params.tokens, "hr", function forToken(token) {
if ((style === "consistent") && horizontalRules.length) { if (style === "consistent") {
style = horizontalRules[0].line; style = token.line;
} }
horizontalRules.forEach(function forToken(token) {
if (token.line !== style) { if (token.line !== style) {
errors.push(token.lineNumber); errors.push(token.lineNumber);
} }
@ -739,8 +730,7 @@ module.exports = [
"desc": "Spaces inside link text", "desc": "Spaces inside link text",
"tags": [ "whitespace", "links" ], "tags": [ "whitespace", "links" ],
"func": function MD039(params, errors) { "func": function MD039(params, errors) {
filterTokens(params.tokens, "inline") filterTokens(params.tokens, "inline", function forToken(token) {
.forEach(function forToken(token) {
var inLink = false; var inLink = false;
var index = 0; var index = 0;
var lastChildRightSpaceLineNumber = 0; var lastChildRightSpaceLineNumber = 0;
@ -773,8 +763,7 @@ module.exports = [
"desc": "Fenced code blocks should have a language specified", "desc": "Fenced code blocks should have a language specified",
"tags": [ "code", "language" ], "tags": [ "code", "language" ],
"func": function MD040(params, errors) { "func": function MD040(params, errors) {
filterTokens(params.tokens, "fence") filterTokens(params.tokens, "fence", function forToken(token) {
.forEach(function forToken(token) {
if (!token.info.trim()) { if (!token.info.trim()) {
errors.push(token.lineNumber); errors.push(token.lineNumber);
} }