-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into isAlpha-refactor
- Loading branch information
Showing
7 changed files
with
78 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,21 @@ | ||
import assertString from './util/assertString'; | ||
import merge from './util/merge'; | ||
|
||
export default function isJSON(str) { | ||
const default_json_options = { | ||
allow_primitives: false, | ||
}; | ||
|
||
export default function isJSON(str, options) { | ||
assertString(str); | ||
try { | ||
options = merge(options, default_json_options); | ||
let primitives = []; | ||
if (options.allow_primitives) { | ||
primitives = [null, false, true]; | ||
} | ||
|
||
const obj = JSON.parse(str); | ||
return !!obj && typeof obj === 'object'; | ||
return primitives.includes(obj) || (!!obj && typeof obj === 'object'); | ||
} catch (e) { /* ignore */ } | ||
return false; | ||
} |
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
Large diffs are not rendered by default.
Oops, something went wrong.