Pop-up dictionary browser extension for language learning. Successor to Yomichan. (PERSONAL FORK)
at lambda-fork/main 81 lines 3.2 kB view raw
1/* 2 * Copyright (C) 2023-2025 Yomitan Authors 3 * Copyright (C) 2022 Yomichan Authors 4 * 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <https://www.gnu.org/licenses/>. 17 */ 18 19/** 20 * Reads code points from a string in the forward direction. 21 * @param {string} text The text to read the code points from. 22 * @param {number} position The index of the first character to read. 23 * @param {number} count The number of code points to read. 24 * @returns {string} The code points from the string. 25 */ 26export function readCodePointsForward(text, position, count) { 27 const textLength = text.length; 28 let result = ''; 29 for (; count > 0; --count) { 30 const char = text[position]; 31 result += char; 32 if (++position >= textLength) { break; } 33 const charCode = char.charCodeAt(0); 34 if (charCode >= 0xd800 && charCode < 0xdc00) { // charCode is a high surrogate code 35 const char2 = text[position]; 36 const charCode2 = char2.charCodeAt(0); 37 if (charCode2 >= 0xdc00 && charCode2 < 0xe000) { // charCode2 is a low surrogate code 38 result += char2; 39 if (++position >= textLength) { break; } 40 } 41 } 42 } 43 return result; 44} 45 46/** 47 * Reads code points from a string in the backward direction. 48 * @param {string} text The text to read the code points from. 49 * @param {number} position The index of the first character to read. 50 * @param {number} count The number of code points to read. 51 * @returns {string} The code points from the string. 52 */ 53export function readCodePointsBackward(text, position, count) { 54 let result = ''; 55 for (; count > 0; --count) { 56 const char = text[position]; 57 result = char + result; 58 if (--position < 0) { break; } 59 const charCode = char.charCodeAt(0); 60 if (charCode >= 0xdc00 && charCode < 0xe000) { // charCode is a low surrogate code 61 const char2 = text[position]; 62 const charCode2 = char2.charCodeAt(0); 63 if (charCode2 >= 0xd800 && charCode2 < 0xdc00) { // charCode2 is a high surrogate code 64 result = char2 + result; 65 if (--position < 0) { break; } 66 } 67 } 68 } 69 return result; 70} 71 72/** 73 * Trims and condenses trailing whitespace and adds a space on the end if it needed trimming. 74 * @param {string} text 75 * @returns {string} 76 */ 77export function trimTrailingWhitespacePlusSpace(text) { 78 // Consense multiple leading and trailing newlines into one newline 79 // Trim trailing whitespace excluding newlines 80 return text.replaceAll(/(\n+$|^\n+)/g, '\n').replaceAll(/[^\S\n]+$/g, ' '); 81}