Skip to content

Commit df1848a

Browse files
committed
escape surrogates except in [low, high] pair
1 parent 0d7c36a commit df1848a

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,14 @@ function stringifyString(str: string) {
245245
result += '\\"';
246246
} else if (char in escaped) {
247247
result += escaped[char];
248-
} else if (code >= 0xD800 && code <= 0xDBFF) {
248+
} else if (code >= 0xd800 && code <= 0xdfff) {
249249
const next = str.charCodeAt(i + 1);
250-
if (next >= 0xDC00 && next <= 0xDFFF) {
250+
251+
// If this is the beginning of a [low, high] surrogate pair,
252+
// add the next two characters, otherwise escape
253+
if (code <= 0xdbff && (next >= 0xdc00 && next <= 0xdfff)) {
251254
result += char + str[++i];
252255
} else {
253-
// lone surrogates
254256
result += `\\u${code.toString(16).toUpperCase()}`;
255257
}
256258
} else {

0 commit comments

Comments
 (0)