Refactor to remove micromark helper matchAndGetTokensByType.

This commit is contained in:
David Anson 2024-09-18 21:02:59 -07:00
parent 2ea3f95fd1
commit 87daa5b06c
4 changed files with 66 additions and 184 deletions

View file

@ -475,31 +475,6 @@ function getTokenTextByType(tokens, type) {
return null;
}
/**
* Determines a list of Micromark tokens matches and returns a subset.
*
* @param {Token[]} tokens Micromark tokens.
* @param {TokenType[]} matchTypes Types to match.
* @param {TokenType[]} [resultTypes] Types to return.
* @returns {Token[] | null} Matching tokens.
*/
function matchAndGetTokensByType(tokens, matchTypes, resultTypes) {
if (tokens.length !== matchTypes.length) {
return null;
}
resultTypes = resultTypes || matchTypes;
const result = [];
// eslint-disable-next-line unicorn/no-for-loop
for (let i = 0; i < matchTypes.length; i++) {
if (tokens[i].type !== matchTypes[i]) {
return null;
} else if (resultTypes.includes(matchTypes[i])) {
result.push(tokens[i]);
}
}
return result;
}
/**
* Set containing token types that do not contain content.
*
@ -531,6 +506,5 @@ module.exports = {
getTokenTextByType,
inHtmlFlow,
isHtmlFlowComment,
matchAndGetTokensByType,
nonContentTokens
};