get-json-val
Version:
At the same time, it is a function to judge whether it is a JSON string and the result of parsing the JSON string.
22 lines (17 loc) • 363 B
text/typescript
const getJSONVal = (val: string): [Error | null, any] => {
if (!val || typeof val !== 'string') {
return [new Error('value must be a string'), null]
}
let result = null
let error = null
try {
result = JSON.parse(val)
} catch (err) {
error = err as Error
}
return [error, result]
}
export {
getJSONVal
}
export default getJSONVal