-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fec981a
commit 445d747
Showing
7 changed files
with
161 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { JsonLike } from "./JsonLike"; | ||
|
||
describe('parse', () => { | ||
it('should parse regular JSON', () => { | ||
const json = '{"name": "John", "age": 30}'; | ||
const parsed = JsonLike.parse(json); | ||
expect(parsed).toEqual({ name: 'John', age: 30 }); | ||
}); | ||
|
||
it('should parse indented JSON-like string with unquoted keys', () => { | ||
const json = `{ | ||
name: "John", | ||
age: 30, | ||
address: { | ||
street: "123 Main St", | ||
city: "Anytown", | ||
state: "CA" | ||
} | ||
}`; | ||
const parsed = JsonLike.parse(json); | ||
expect(parsed).toEqual({ | ||
name: 'John', | ||
age: 30, | ||
address: { | ||
street: '123 Main St', | ||
city: 'Anytown', | ||
state: 'CA' | ||
} | ||
}); | ||
}); | ||
|
||
it('cant parse unquoted keys without indentation', () => { | ||
const json = `{name: "John", age: 30}` | ||
|
||
expect(() => JsonLike.parse(json)).toThrow() | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export const JsonLike = { | ||
parse(json: string) { | ||
// Check if the string is already valid JSON | ||
try { | ||
return JSON.parse(json); | ||
} catch (e) {} | ||
|
||
// Handle JSON-like string where keys may not have quotes | ||
const matchKey = /\s+(\w+)\s*:/g; | ||
const fixed = json.replace(matchKey, '"$1":'); | ||
|
||
try { | ||
return JSON.parse(fixed); | ||
} catch (e) { | ||
throw new Error('Could not parse JSON-like string.'); | ||
} | ||
}, | ||
}; |