Source

utils/parseJSON.js

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var isStr_1 = __importDefault(require("../string/isStr"));
var isFunc_1 = __importDefault(require("../validators/isFunc"));
/**
 * Parse JSON string with error catched
 * @category utils
 * @module parseJSON
 */
function parseJSON(str, shouldNotFallbackAsObj, log) {
    var fallback = shouldNotFallbackAsObj ? str : {};
    try {
        return (0, isStr_1.default)(str) ? JSON.parse(str) : fallback;
    }
    catch (error) {
        if (error && (0, isFunc_1.default)(log))
            log(error);
        return fallback;
    }
}
exports.default = parseJSON;