Skip to content

Commit a6de877

Browse files
committed
Add support for "surrogate pairs" in unmatched characters
1 parent 2b19219 commit a6de877

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

src/index.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ const getStringTruncatedWidth = ( input: string, truncationOptions: TruncationOp
4545
let truncationEnabled = false;
4646
let truncationIndex = length;
4747
let truncationLimit = Math.max ( 0, LIMIT - ELLIPSIS_WIDTH );
48-
let unmatchedStart = 0;
49-
let unmatchedEnd = 0;
5048
let width = 0;
5149
let widthExtra = 0;
5250

@@ -57,9 +55,9 @@ const getStringTruncatedWidth = ( input: string, truncationOptions: TruncationOp
5755

5856
/* UNMATCHED */
5957

60-
if ( ( unmatchedEnd > unmatchedStart ) || ( index >= length && index > indexPrev ) ) {
58+
if ( index > indexPrev ) {
6159

62-
const unmatched = input.slice ( unmatchedStart, unmatchedEnd ) || input.slice ( indexPrev, index );
60+
const unmatched = input.slice ( indexPrev, index );
6361

6462
lengthExtra = 0;
6563

@@ -78,7 +76,7 @@ const getStringTruncatedWidth = ( input: string, truncationOptions: TruncationOp
7876
}
7977

8078
if ( ( width + widthExtra ) > truncationLimit ) {
81-
truncationIndex = Math.min ( truncationIndex, Math.max ( unmatchedStart, indexPrev ) + lengthExtra );
79+
truncationIndex = Math.min ( truncationIndex, indexPrev + lengthExtra );
8280
}
8381

8482
if ( ( width + widthExtra ) > LIMIT ) {
@@ -91,8 +89,7 @@ const getStringTruncatedWidth = ( input: string, truncationOptions: TruncationOp
9189

9290
}
9391

94-
unmatchedStart = unmatchedEnd = 0;
95-
92+
indexPrev = index;
9693
}
9794

9895
/* EXITING */
@@ -118,8 +115,6 @@ const getStringTruncatedWidth = ( input: string, truncationOptions: TruncationOp
118115
}
119116

120117
width += widthExtra;
121-
unmatchedStart = indexPrev;
122-
unmatchedEnd = index;
123118
index = indexPrev = LATIN_RE.lastIndex;
124119

125120
continue;
@@ -142,8 +137,6 @@ const getStringTruncatedWidth = ( input: string, truncationOptions: TruncationOp
142137
}
143138

144139
width += ANSI_WIDTH;
145-
unmatchedStart = indexPrev;
146-
unmatchedEnd = index;
147140
index = indexPrev = ANSI_RE.lastIndex;
148141

149142
continue;
@@ -169,8 +162,6 @@ const getStringTruncatedWidth = ( input: string, truncationOptions: TruncationOp
169162
}
170163

171164
width += widthExtra;
172-
unmatchedStart = indexPrev;
173-
unmatchedEnd = index;
174165
index = indexPrev = CONTROL_RE.lastIndex;
175166

176167
continue;
@@ -196,8 +187,6 @@ const getStringTruncatedWidth = ( input: string, truncationOptions: TruncationOp
196187
}
197188

198189
width += widthExtra;
199-
unmatchedStart = indexPrev;
200-
unmatchedEnd = index;
201190
index = indexPrev = TAB_RE.lastIndex;
202191

203192
continue;
@@ -220,8 +209,6 @@ const getStringTruncatedWidth = ( input: string, truncationOptions: TruncationOp
220209
}
221210

222211
width += EMOJI_WIDTH;
223-
unmatchedStart = indexPrev;
224-
unmatchedEnd = index;
225212
index = indexPrev = EMOJI_RE.lastIndex;
226213

227214
continue;
@@ -230,8 +217,7 @@ const getStringTruncatedWidth = ( input: string, truncationOptions: TruncationOp
230217

231218
/* UNMATCHED INDEX */
232219

233-
index += 1;
234-
220+
index += ( input.codePointAt(index) || 0 ) > 0xffff ? 2 : 1;
235221
}
236222

237223
/* RETURN */

test/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,17 @@ describe ( 'Fast String Width', () => {
278278

279279
});
280280

281+
it( 'supports "surrogate pairs" in unmatched characters', (t) => {
282+
283+
t.is(getTruncated("██ █", { limit: 4 }), "██ █");
284+
t.is(getTruncated("██ █", { limit: 3 }), "██ ");
285+
t.is(getTruncated("██ █", { limit: 2 }), "██");
286+
287+
t.is(getTruncated("██ █", { limit: 4, ellipsis: "…" }), "██ █");
288+
t.is(getTruncated("██ █", { limit: 3, ellipsis: "…" }), "██…");
289+
t.is(getTruncated("██ █", { limit: 2, ellipsis: "…" }), "█…");
290+
291+
});
281292
});
282293

283294
});

0 commit comments

Comments
 (0)