UNPKG

@future-widget-lab/safe-ops

Version:

A set of helper functions for performing operations safely, preventing runtime errors from disrupting your application.

12 lines (11 loc) 282 B
/** * @description * Use this helper to safely parse a possibly faulty JSON object. */ export const safeJsonParse = <TData>(input: string | null | undefined, fallback: TData): TData => { try { return !!input ? JSON.parse(input) : fallback; } catch { return fallback; } };