Refactor micromark helper getTokenTextByType to be more efficient, remove tokenIfType helper for being redundant.

This commit is contained in:
David Anson 2024-09-16 20:50:54 -07:00
parent b749d9fbb4
commit 2ea3f95fd1
6 changed files with 82 additions and 120 deletions

View file

@ -467,8 +467,12 @@ function getTokenParentOfType(token, types) {
* @returns {string | null} Text of token.
*/
function getTokenTextByType(tokens, type) {
const filtered = tokens.filter((token) => token.type === type);
return (filtered.length > 0) ? filtered[0].text : null;
for (const token of tokens) {
if (token.type === type) {
return token.text;
}
}
return null;
}
/**
@ -496,17 +500,6 @@ function matchAndGetTokensByType(tokens, matchTypes, resultTypes) {
return result;
}
/**
* Returns the specified token iff it is of the desired type.
*
* @param {Token} token Micromark token candidate.
* @param {TokenType} type Desired type.
* @returns {Token | null} Token instance.
*/
function tokenIfType(token, type) {
return (token && (token.type === type)) ? token : null;
}
/**
* Set containing token types that do not contain content.
*
@ -539,6 +532,5 @@ module.exports = {
inHtmlFlow,
isHtmlFlowComment,
matchAndGetTokensByType,
nonContentTokens,
tokenIfType
nonContentTokens
};