mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 22:40:13 +01:00
Refactor micromark helper getTokenTextByType to be more efficient, remove tokenIfType helper for being redundant.
This commit is contained in:
parent
b749d9fbb4
commit
2ea3f95fd1
6 changed files with 82 additions and 120 deletions
|
|
@ -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
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue