Skip to content

Commit

Permalink
fix: Use String.prototype.slice() instead of deprecated .substr() (
Browse files Browse the repository at this point in the history
…#90)

* fix: Use `String.prototype.slice()` instead of deprecated `.substr()`

* test: Improve string escape test for control characters
  • Loading branch information
meyfa committed Sep 17, 2023
1 parent 6a59c93 commit fc128db
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Expand Up @@ -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)
}) + '"'
}

Expand Down
4 changes: 2 additions & 2 deletions test/index.test.ts
Expand Up @@ -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)
}
})
Expand Down

0 comments on commit fc128db

Please sign in to comment.