From d4b14afa894698eb3a50a1e3c158dd4bf889fd9a Mon Sep 17 00:00:00 2001 From: Fabian Meyer <3982806+meyfa@users.noreply.github.com> Date: Sun, 17 Sep 2023 10:43:48 +0200 Subject: [PATCH 1/2] fix: Use `String.prototype.slice()` instead of deprecated `.substr()` --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 7d7e41c..ad0bd13 100644 --- a/src/index.ts +++ b/src/index.ts @@ -66,7 +66,7 @@ function stringifyString (string: string): string { // eslint-disable-next-line no-control-regex return '"' + string.replace(/[\\"\u0000-\u001F\u2028\u2029]/g, (m) => { if (escMap[m] !== undefined) return escMap[m] - return '\\u' + (m.charCodeAt(0) + 0x10000).toString(16).substr(1) + return '\\u' + (m.charCodeAt(0) + 0x10000).toString(16).slice(1) }) + '"' } From d0251fb2b25ca29e16eaaab7fe37197dacd514da Mon Sep 17 00:00:00 2001 From: Fabian Meyer <3982806+meyfa@users.noreply.github.com> Date: Sun, 17 Sep 2023 10:55:06 +0200 Subject: [PATCH 2/2] test: Improve string escape test for control characters --- test/index.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/index.test.ts b/test/index.test.ts index f72e6bc..851f807 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -119,8 +119,8 @@ describe('index.js', function () { it('should escape serialized strings', function () { const result: string = index(function (a: string, b: string) { return a + ' ' + b - }, 'foo"bar\\"\n', 'baz') - if (!result.includes(')("foo\\"bar\\\\\\"\\n", "baz")')) { + }, 'foo"bar\\"\n-\u0000\u2028\u2029\u0007', 'baz') + if (!result.includes(')("foo\\"bar\\\\\\"\\n-\\u0000\\u2028\\u2029\\u0007", "baz")')) { throw new Error('incorrect serialization: ' + result) } })