Skip to content

Commit 0f2501c

Browse files
committed
make suggested changes
1 parent 207040a commit 0f2501c

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const escaped: Record<string, string> = {
1111
'\n': '\\n',
1212
'\r': '\\r',
1313
'\t': '\\t',
14-
'\0': '\\u0000',
1514
'\u2028': '\\u2028',
1615
'\u2029': '\\u2029'
1716
};
@@ -246,10 +245,10 @@ function stringifyString(str: string) {
246245
result += '\\"';
247246
} else if (char in escaped) {
248247
result += escaped[char];
249-
} else if ((code >= 0xD800 && code <= 0xDBFF)) {
248+
} else if (code >= 0xD800 && code <= 0xDBFF) {
250249
const next = str.charCodeAt(i + 1);
251250
if (next >= 0xDC00 && next <= 0xDFFF) {
252-
result += char;
251+
result += char + str[++i];
253252
} else {
254253
// lone surrogates
255254
result += `\\u${code.toString(16).toUpperCase()}`;

test/test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@ describe('devalue', () => {
3838
describe('strings', () => {
3939
test('newline', 'a\nb', JSON.stringify('a\nb'));
4040
test('double quotes', '"yar"', JSON.stringify('"yar"'));
41-
test('lone surrogate', "\uD800", '"\\uD800"');
41+
test('lone low surrogate', "a\uDC00b", '"a\\uDC00b"');
42+
test('lone high surrogate', "a\uD800b", '"a\\uD800b"');
43+
test('two low surrogates', "a\uDC00\uDC00b", '"a\\uDC00\\uDC00b"');
44+
test('two high surrogates', "a\uD800\uD800b", '"a\\uD800\\uD800b"');
4245
test('surrogate pair', '𝌆', JSON.stringify('𝌆'));
43-
test('nul', '\0', JSON.stringify('\0'));
46+
test('surrogate pair in wrong order', 'a\uDC00\uD800b', '"a\uDC00\uD800b"');
47+
test('nul', '\0', '"\0"');
4448
test('backslash', '\\', JSON.stringify('\\'));
4549
});
4650

0 commit comments

Comments
 (0)