Skip to content

Commit

Permalink
fix: #89 wrongly parsing strings that contain a double quote left or …
Browse files Browse the repository at this point in the history
…right
  • Loading branch information
josdejong committed Apr 17, 2023
1 parent 5903851 commit 4023ece
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/jsonrepair.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ describe('jsonRepair', () => {
strictEqual(jsonrepair('{`a´:`b´}'), '{"a":"b"}')
})

it('should not replace special quotes inside a normal string', () => {
strictEqual(jsonrepair('"Rounded “ quote"'), '"Rounded “ quote"')
})

it('should leave string content untouched', () => {
strictEqual(jsonrepair('"{a:b}"'), '"{a:b}"')
})
Expand Down
12 changes: 7 additions & 5 deletions src/jsonrepair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ import {
isDelimiter,
isDigit,
isDoubleQuote,
isDoubleQuoteLike,
isHex,
isNonZeroDigit,
isQuote,
isSingleQuote,
isSingleQuoteLike,
isSpecialWhitespace,
isStartOfValue,
isValidStringCharacter,
Expand Down Expand Up @@ -372,11 +373,12 @@ export function jsonrepair(text: string): string {
}

if (isQuote(text.charCodeAt(i))) {
const isEndQuote = isSingleQuote(text.charCodeAt(i)) ? isSingleQuote : isDoubleQuote
const isEndQuote = isSingleQuoteLike(text.charCodeAt(i))
? isSingleQuoteLike
: isDoubleQuote(text.charCodeAt(i))
? isDoubleQuote // eslint-disable-line indent
: isDoubleQuoteLike // eslint-disable-line indent

if (text.charCodeAt(i) !== codeDoubleQuote) {
// repair non-normalized quote
}
output += '"'
i++

Expand Down
14 changes: 11 additions & 3 deletions src/stringUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,31 @@ export function isSpecialWhitespace(code: number): boolean {
*/
export function isQuote(code: number): boolean {
// the first check double quotes, since that occurs most often
return isDoubleQuote(code) || isSingleQuote(code)
return isDoubleQuoteLike(code) || isSingleQuoteLike(code)
}

/**
* Test whether the given character is a double quote character.
* Also tests for special variants of double quotes.
*/
export function isDoubleQuote(code: number): boolean {
export function isDoubleQuoteLike(code: number): boolean {
// the first check double quotes, since that occurs most often
return code === codeDoubleQuote || code === codeDoubleQuoteLeft || code === codeDoubleQuoteRight
}

/**
* Test whether the given character is a double quote character.
* Does NOT test for special variants of double quotes.
*/
export function isDoubleQuote(code: number): boolean {
return code === codeDoubleQuote
}

/**
* Test whether the given character is a single quote character.
* Also tests for special variants of single quotes.
*/
export function isSingleQuote(code: number): boolean {
export function isSingleQuoteLike(code: number): boolean {
return (
code === codeQuote ||
code === codeQuoteLeft ||
Expand Down

0 comments on commit 4023ece

Please sign in to comment.