Skip to content

fix(index): return undefined on error instead of null #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function safeParse (text, reviver) {
try {
return _parse(text, reviver, { safe: true })
} catch (_e) {
return null
return undefined
} finally {
Error.stackTraceLimit = stackTraceLimit
}
Expand Down
4 changes: 2 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,10 @@ test('safeParse', t => {
t.end()
})

t.test('returns null on invalid object string', t => {
t.test('returns undefined on invalid object string', t => {
t.strictEqual(
j.safeParse('{"a": 5, "b": 6'),
null
undefined
)
t.end()
})
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ declare namespace parse {
*
* @param text The JSON text string.
* @param reviver The `JSON.parse()` optional `reviver` argument.
* @returns The parsed object, or `null` if there was an error or if the JSON contained possibly insecure properties.
* @returns The parsed object, or `undefined` if there was an error or if the JSON contained possibly insecure properties.
*/
export function safeParse (text: string | Buffer, reviver?: Reviver | null): any

Expand Down