UNPKG

@web3r/flowerkit

Version:

Tree-shakable JavaScript and TypeScript utility library for frontend/browser apps: DOM, events, arrays, objects, strings, date, JSON, and network helpers (ESM/CJS, SSR-friendly).

20 lines (19 loc) 1.48 kB
Object.defineProperty(exports,"__esModule",{value:true}); /** * Safely parses a JSON string. * * @template T * @param {string} str Source string to parse * @param {(this: any, key: string, value: any) => any} [reviver] Optional reviver function * @param {(err: unknown) => void} [onError] Optional error callback * @returns {T | Record<string, never>} Parsed object or empty object on error * @throws {TypeError} getJSONFromStr: str must be a string * @throws {TypeError} getJSONFromStr: reviver must be a function if provided * @throws {TypeError} getJSONFromStr: onError must be a function if provided * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse * @example * // How to convert string to JSON? * const json = getJSONFromStr<{ hello: string }>('{"hello":"world"}'); * console.log(json.hello); // => "world" */const getJSONFromStr=(str,reviver,onError)=>{if(typeof str!=="string")throw new TypeError("getJSONFromStr: str must be a string");if(typeof reviver!=="undefined"&&typeof reviver!=="function")throw new TypeError("getJSONFromStr: reviver must be a function if provided");if(typeof onError!=="undefined"&&typeof onError!=="function")throw new TypeError("getJSONFromStr: onError must be a function if provided");let json={};try{json=JSON.parse(str,reviver)}catch(err){if(typeof onError==="function")onError(err)}return json};exports.getJSONFromStr=getJSONFromStr; //# sourceMappingURL=index.cjs.map