fast-string-truncated-width
Advanced tools
+10
-10
| /* IMPORT */ | ||
| import { isFullWidth, isWideNotCJKTNotEmoji } from './utils.js'; | ||
| import { getCodePointsLength, isFullWidth, isWideNotCJKTNotEmoji } from './utils.js'; | ||
| /* HELPERS */ | ||
| const ANSI_RE = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]|\u001b\]8;[^;]*;.*?(?:\u0007|\u001b\u005c)/y; | ||
| const CONTROL_RE = /[\x00-\x08\x0A-\x1F\x7F-\x9F]{1,1000}/y; | ||
| const CJKT_WIDE_RE = /(?:(?![\uFF61-\uFF9F\uFF00-\uFFEF])[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\p{Script=Tangut}])/yu; | ||
| const CJKT_WIDE_RE = /(?:(?![\uFF61-\uFF9F\uFF00-\uFFEF])[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\p{Script=Tangut}]){1,1000}/yu; | ||
| const TAB_RE = /\t{1,1000}/y; | ||
@@ -26,8 +26,8 @@ const EMOJI_RE = /[\u{1F1E6}-\u{1F1FF}]{2}|\u{1F3F4}[\u{E0061}-\u{E007A}]{2}[\u{E0030}-\u{E0039}\u{E0061}-\u{E007A}]{1,3}\u{E007F}|(?:\p{Emoji}\uFE0F\u20E3?|\p{Emoji_Modifier_Base}\p{Emoji_Modifier}?|\p{Emoji_Presentation})(?:\u200D(?:\p{Emoji_Modifier_Base}\p{Emoji_Modifier}?|\p{Emoji_Presentation}|\p{Emoji}\uFE0F\u20E3?))*/yu; | ||
| const PARSE_BLOCKS = [ | ||
| [LATIN_RE, REGULAR_WIDTH, false], | ||
| [ANSI_RE, ANSI_WIDTH, false], | ||
| [CONTROL_RE, CONTROL_WIDTH, false], | ||
| [TAB_RE, TAB_WIDTH, false], | ||
| [EMOJI_RE, EMOJI_WIDTH, true], | ||
| [CJKT_WIDE_RE, WIDE_WIDTH, true] | ||
| [LATIN_RE, REGULAR_WIDTH], | ||
| [ANSI_RE, ANSI_WIDTH], | ||
| [CONTROL_RE, CONTROL_WIDTH], | ||
| [TAB_RE, TAB_WIDTH], | ||
| [EMOJI_RE, EMOJI_WIDTH], | ||
| [CJKT_WIDE_RE, WIDE_WIDTH], | ||
| ]; | ||
@@ -81,6 +81,6 @@ /* STATE */ | ||
| for (let i = 0, l = PARSE_BLOCKS.length; i < l; i++) { | ||
| const [BLOCK_RE, BLOCK_WIDTH, BLOCK_IS_SINGULAR] = PARSE_BLOCKS[i]; | ||
| const [BLOCK_RE, BLOCK_WIDTH] = PARSE_BLOCKS[i]; | ||
| BLOCK_RE.lastIndex = index; | ||
| if (BLOCK_RE.test(input)) { | ||
| lengthExtra = BLOCK_IS_SINGULAR ? 1 : BLOCK_RE.lastIndex - index; | ||
| lengthExtra = BLOCK_RE === CJKT_WIDE_RE ? getCodePointsLength(input.slice(index, BLOCK_RE.lastIndex)) : BLOCK_RE === EMOJI_RE ? 1 : BLOCK_RE.lastIndex - index; | ||
| widthExtra = lengthExtra * BLOCK_WIDTH; | ||
@@ -87,0 +87,0 @@ if ((width + widthExtra) > truncationLimit) { |
+2
-1
@@ -0,3 +1,4 @@ | ||
| declare const getCodePointsLength: (input: string) => number; | ||
| declare const isFullWidth: (x: number) => boolean; | ||
| declare const isWideNotCJKTNotEmoji: (x: number) => boolean; | ||
| export { isFullWidth, isWideNotCJKTNotEmoji }; | ||
| export { getCodePointsLength, isFullWidth, isWideNotCJKTNotEmoji }; |
+12
-1
| /* MAIN */ | ||
| const getCodePointsLength = (() => { | ||
| const SURROGATE_PAIR_RE = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g; | ||
| return (input) => { | ||
| let surrogatePairsNr = 0; | ||
| SURROGATE_PAIR_RE.lastIndex = 0; | ||
| while (SURROGATE_PAIR_RE.test(input)) { | ||
| surrogatePairsNr += 1; | ||
| } | ||
| return input.length - surrogatePairsNr; | ||
| }; | ||
| })(); | ||
| const isFullWidth = (x) => { | ||
@@ -9,2 +20,2 @@ return x === 0x3000 || x >= 0xFF01 && x <= 0xFF60 || x >= 0xFFE0 && x <= 0xFFE6; | ||
| /* EXPORT */ | ||
| export { isFullWidth, isWideNotCJKTNotEmoji }; | ||
| export { getCodePointsLength, isFullWidth, isWideNotCJKTNotEmoji }; |
+1
-1
@@ -6,3 +6,3 @@ { | ||
| "license": "MIT", | ||
| "version": "3.0.2", | ||
| "version": "3.0.3", | ||
| "type": "module", | ||
@@ -9,0 +9,0 @@ "main": "dist/index.js", |
12139
4.51%162
8%