From 1c2ef1f0ddb4c849a1ad3a6e7698fd698a239f3e Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sat, 1 Nov 2025 13:02:08 +0800 Subject: [PATCH] Extract `isNewlineCharacter` to eliminate duplication (#18182) Co-authored-by: fisker <172584+fisker@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> --- src/utils/skip-newline.js | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/utils/skip-newline.js b/src/utils/skip-newline.js index 5d8e38f59..b6e7395f1 100644 --- a/src/utils/skip-newline.js +++ b/src/utils/skip-newline.js @@ -1,5 +1,16 @@ /** @import {SkipOptions} from "./skip.js" */ +/** + * Check if a character is a newline character + * @param {string} character + * @returns {boolean} + */ +const isNewlineCharacter = (character) => + character === "\n" || + character === "\r" || + character === "\u2028" || + character === "\u2029"; + // This one doesn't use the above helper function because it wants to // test \r\n in order and `skip` doesn't support ordering and we only // want to skip one newline. It's simple to implement. @@ -22,12 +33,7 @@ function skipNewline(text, startIndex, options) { if (text.charAt(startIndex - 1) === "\r" && character === "\n") { return startIndex - 2; } - if ( - character === "\n" || - character === "\r" || - character === "\u2028" || - character === "\u2029" - ) { + if (isNewlineCharacter(character)) { return startIndex - 1; } } else { @@ -36,12 +42,7 @@ function skipNewline(text, startIndex, options) { if (character === "\r" && text.charAt(startIndex + 1) === "\n") { return startIndex + 2; } - if ( - character === "\n" || - character === "\r" || - character === "\u2028" || - character === "\u2029" - ) { + if (isNewlineCharacter(character)) { return startIndex + 1; } }