UNPKG

veffect

Version:

powerful TypeScript validation library built on the robust foundation of Effect combining exceptional type safety, high performance, and developer experience. Taking inspiration from Effect's functional principles, VEffect delivers a balanced approach tha

88 lines (86 loc) 2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toStringUnknown = exports.toJSON = exports.stringifyCircular = exports.format = exports.NodeInspectSymbol = exports.Class = exports.BaseProto = void 0; var _Predicate = /*#__PURE__*/require("./Predicate.js"); /** * @since 2.0.0 */ /** * @since 2.0.0 * @category symbols */ const NodeInspectSymbol = exports.NodeInspectSymbol = /*#__PURE__*/Symbol.for("nodejs.util.inspect.custom"); /** * @since 2.0.0 */ const toJSON = x => { if ((0, _Predicate.hasProperty)(x, "toJSON") && (0, _Predicate.isFunction)(x["toJSON"]) && x["toJSON"].length === 0) { return x.toJSON(); } else if (Array.isArray(x)) { return x.map(toJSON); } return x; }; /** * @since 2.0.0 */ exports.toJSON = toJSON; const format = x => JSON.stringify(x, null, 2); /** * @since 2.0.0 */ exports.format = format; const BaseProto = exports.BaseProto = { toJSON() { return toJSON(this); }, [NodeInspectSymbol]() { return this.toJSON(); }, toString() { return format(this.toJSON()); } }; /** * @since 2.0.0 */ class Class { /** * @since 2.0.0 */ [NodeInspectSymbol]() { return this.toJSON(); } /** * @since 2.0.0 */ toString() { return format(this.toJSON()); } } /** * @since 2.0.0 */ exports.Class = Class; const toStringUnknown = (u, whitespace = 2) => { try { return typeof u === "object" ? stringifyCircular(u, whitespace) : String(u); } catch (_) { return String(u); } }; /** * @since 2.0.0 */ exports.toStringUnknown = toStringUnknown; const stringifyCircular = (obj, whitespace) => { let cache = []; const retVal = JSON.stringify(obj, (_key, value) => typeof value === "object" && value !== null ? cache.includes(value) ? undefined // circular reference : cache.push(value) && value : value, whitespace); cache = undefined; return retVal; }; exports.stringifyCircular = stringifyCircular; //# sourceMappingURL=Inspectable.js.map