UNPKG

@onesy/utils

Version:
38 lines (37 loc) 1.52 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const getEnvironment_1 = __importDefault(require("./getEnvironment")); const setObjectValue_1 = __importDefault(require("./setObjectValue")); const optionsDefault = { returnSame: true }; const parse = (value, type = 'JSON', options_ = {}) => { var _a, _b, _c, _d; const options = Object.assign(Object.assign({}, optionsDefault), options_); switch (type) { case 'JSON': try { return JSON.parse(value); } catch (error) { if (options.log) { console.error('Parse JSON: ', error); const env = (0, getEnvironment_1.default)(); if (((_a = env.ONESY) === null || _a === void 0 ? void 0 : _a.env) === 'test') { if (!((_d = (_c = (_b = env.ONESY) === null || _b === void 0 ? void 0 : _b.test) === null || _c === void 0 ? void 0 : _c.parse) === null || _d === void 0 ? void 0 : _d.logs)) (0, setObjectValue_1.default)(env, 'ONESY.test.parse.logs', []); env.ONESY.test.parse.logs.push(error); } } } break; default: break; } if (options.returnSame) return value; }; exports.default = parse;