UNPKG

@ace-util/core

Version:
79 lines 2.65 kB
"use strict"; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.jsonDeserializeReviver = exports.jsonSerializeReviver = exports.jsonSafeParse = void 0; var tools_1 = require("./tools"); /** * Safe json parse */ function jsonSafeParse() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var value; var isValid = false; try { value = JSON.parse.apply(JSON, __spreadArray([], __read(args), false)); isValid = true; } catch (err) { (0, tools_1.warn)(process.env.NODE_ENV === 'production', "JSON safe parse error: ".concat(err)); } return isValid ? value : undefined; } exports.jsonSafeParse = jsonSafeParse; /** * Serialize to string json option */ function jsonSerializeReviver(key, value) { if (typeof value === 'function') { return value + ''; } return value; } exports.jsonSerializeReviver = jsonSerializeReviver; // const UCT_DATE_REG = /^\d{4}-[0-1]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-6]\d(.\d{3})?((\+|\-)\d\d(:)?\d\d|Z)$/; /** * Deserialize from string option */ function jsonDeserializeReviver(scope) { if (scope === void 0) { scope = {}; } return function (key, value) { if ((value === null || value === void 0 ? void 0 : value.indexOf) && value.indexOf('function') > -1) { // Function return new Function('$root', "with($root){ return (".concat(value, "); }"))(scope); } // else if (include.includes('DATE') && !exclude.includes('DATE') && UCT_DATE_REG.test(value)) { // // UCT Date // return new Date(value); // } return value; }; } exports.jsonDeserializeReviver = jsonDeserializeReviver; //# sourceMappingURL=json.js.map