Skip to content

Commit 78891ff

Browse files
committed
fix: make sure environments without negative lookbehind work
This changes the regular expression that detects characters that require escaping not to use a negative lookbehind. Fixes: #26
1 parent 6b11e3a commit 78891ff

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## v2.3.1
4+
5+
- Fix `invalid regexp group` error in browsers or environments that do not support the negative lookbehind regular expression assertion.
6+
37
## v2.3.0
48

59
- Accept the `Error` constructor as `circularValue` option to throw on circular references as the regular JSON.stringify would:

index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ exports.configure = configure
1818
module.exports = stringify
1919

2020
// eslint-disable-next-line
21-
const strEscapeSequencesRegExp = /[\u0000-\u001f\u0022\u005c\ud800-\udfff]|[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/
21+
const strEscapeSequencesRegExp = /[\u0000-\u001f\u0022\u005c\ud800-\udfff]|[\ud800-\udbff](?![\udc00-\udfff])|(?:[^\ud800-\udbff]|^)[\udc00-\udfff]/
2222
// eslint-disable-next-line
23-
const strEscapeSequencesReplacer = /[\u0000-\u001f\u0022\u005c\ud800-\udfff]|[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/g
23+
const strEscapeSequencesReplacer = /[\u0000-\u001f\u0022\u005c\ud800-\udfff]|[\ud800-\udbff](?![\udc00-\udfff])|(?:[^\ud800-\udbff]|^)[\udc00-\udfff]/g
2424

2525
// Escaped special characters. Use empty strings to fill up unused entries.
2626
const meta = [
@@ -40,6 +40,10 @@ const meta = [
4040
]
4141

4242
function escapeFn (str) {
43+
if (str.length === 2) {
44+
const charCode = str.charCodeAt(1)
45+
return `${str[0]}\\u${charCode.toString(16)}`
46+
}
4347
const charCode = str.charCodeAt(0)
4448
return meta.length > charCode
4549
? meta[charCode]

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "safe-stable-stringify",
3-
"version": "2.3.0",
3+
"version": "2.3.1",
44
"description": "Deterministic and safely JSON.stringify to quickly serialize JavaScript objects",
55
"exports": {
66
"require": "./index.js",

0 commit comments

Comments
 (0)