UNPKG

stable-ts-type

Version:

Obtain the most stable type code of 'typescript' through multiple network requests

38 lines (37 loc) 1.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.easyJSONParse = exports.easyJSONStringify = exports.isValidJSON = exports.simpleError = void 0; const simpleError = (error) => { const newError = new Error(); newError.stack = ''; if (error instanceof Error) { newError.name = error.name; newError.message = error.message; return newError; } else if (typeof error === 'string') { newError.name = 'Stable-ts-type Error'; newError.message = error; return newError; } if (error === null || error === void 0 ? void 0 : error.stack) { newError.stack = ''; } return error; }; exports.simpleError = simpleError; const isValidJSON = (json) => { try { JSON.stringify(json); return true; } catch (_a) { return false; } }; exports.isValidJSON = isValidJSON; /** 支持不严谨的json字符串(包含注释或key没有双引号) */ const easyJSONStringify = (jsCode) => eval(`JSON.stringify(${jsCode})`); exports.easyJSONStringify = easyJSONStringify; const easyJSONParse = (jsCode) => eval(`JSON.parse(JSON.stringify(${jsCode}))`); exports.easyJSONParse = easyJSONParse;