Skip to content

Commit d20dd64

Browse files
committed
Add support for "surrogate pairs" in unmatched characters
# Conflicts: # src/index.ts # test/index.js
1 parent ac8a806 commit d20dd64

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/index.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ const getStringTruncatedWidth = ( input: string, truncationOptions: TruncationOp
5252
let truncationEnabled = false;
5353
let truncationIndex = length;
5454
let truncationLimit = Math.max ( 0, LIMIT - ELLIPSIS_WIDTH );
55-
let unmatchedStart = 0;
56-
let unmatchedEnd = 0;
5755
let width = 0;
5856
let widthExtra = 0;
5957

@@ -64,9 +62,9 @@ const getStringTruncatedWidth = ( input: string, truncationOptions: TruncationOp
6462

6563
/* UNMATCHED */
6664

67-
if ( ( unmatchedEnd > unmatchedStart ) || ( index >= length && index > indexPrev ) ) {
65+
if ( index > indexPrev ) {
6866

69-
const unmatched = input.slice ( unmatchedStart, unmatchedEnd ) || input.slice ( indexPrev, index );
67+
const unmatched = input.slice ( indexPrev, index );
7068

7169
lengthExtra = 0;
7270

@@ -83,7 +81,7 @@ const getStringTruncatedWidth = ( input: string, truncationOptions: TruncationOp
8381
}
8482

8583
if ( ( width + widthExtra ) > truncationLimit ) {
86-
truncationIndex = Math.min ( truncationIndex, Math.max ( unmatchedStart, indexPrev ) + lengthExtra );
84+
truncationIndex = Math.min ( truncationIndex, indexPrev + lengthExtra );
8785
}
8886

8987
if ( ( width + widthExtra ) > LIMIT ) {
@@ -96,7 +94,7 @@ const getStringTruncatedWidth = ( input: string, truncationOptions: TruncationOp
9694

9795
}
9896

99-
unmatchedStart = unmatchedEnd = 0;
97+
indexPrev = index;
10098

10199
}
102100

@@ -129,8 +127,6 @@ const getStringTruncatedWidth = ( input: string, truncationOptions: TruncationOp
129127
}
130128

131129
width += widthExtra;
132-
unmatchedStart = indexPrev;
133-
unmatchedEnd = index;
134130
index = indexPrev = BLOCK_RE.lastIndex;
135131

136132
continue outer;
@@ -141,7 +137,7 @@ const getStringTruncatedWidth = ( input: string, truncationOptions: TruncationOp
141137

142138
/* UNMATCHED INDEX */
143139

144-
index += 1;
140+
index += ( input.codePointAt(index) || 0 ) > 0xffff ? 2 : 1;
145141

146142
}
147143

test/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,18 @@ describe ( 'Fast String Width', () => {
296296

297297
});
298298

299+
it( 'supports "surrogate pairs" in unmatched characters', (t) => {
300+
301+
t.is(getTruncated("██ █", { limit: 4 }), "██ █");
302+
t.is(getTruncated("██ █", { limit: 3 }), "██ ");
303+
t.is(getTruncated("██ █", { limit: 2 }), "██");
304+
305+
t.is(getTruncated("██ █", { limit: 4, ellipsis: "…" }), "██ █");
306+
t.is(getTruncated("██ █", { limit: 3, ellipsis: "…" }), "██…");
307+
t.is(getTruncated("██ █", { limit: 2, ellipsis: "…" }), "█…");
308+
309+
});
310+
299311
});
300312

301313
});

0 commit comments

Comments
 (0)