From fc128db599a086d5787bec7183f734f25c03f51c Mon Sep 17 00:00:00 2001 From: Fabian Meyer <3982806+meyfa@users.noreply.github.com> Date: Sun, 17 Sep 2023 10:56:07 +0200 Subject: [PATCH] fix: Use `String.prototype.slice()` instead of deprecated `.substr()` (#90) * fix: Use `String.prototype.slice()` instead of deprecated `.substr()` * test: Improve string escape test for control characters --- src/index.ts | 2 +- test/index.test.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) 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) }) + '"' } 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) } })