UNPKG

@parischap/pretty-print

Version:
103 lines (101 loc) 4.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fromValueIterable = exports.fromProperties = exports.fromKeyValueIterable = void 0; var _ansiStyles = /*#__PURE__*/require("@parischap/ansi-styles"); var _effectLib = /*#__PURE__*/require("@parischap/effect-lib"); var _effect = /*#__PURE__*/require("effect"); var PPStringifiedValue = /*#__PURE__*/_interopRequireWildcard(/*#__PURE__*/require("./StringifiedValue.js")); var PPValue = /*#__PURE__*/_interopRequireWildcard(/*#__PURE__*/require("./Value.js")); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } /** Type that is an alias for an array of Value's (see Value.ts). */ /** * Builds a Values from the keys of a non-primitive value and its prototypes * * @category Constructors */ const fromProperties = maxPrototypeDepth => nonPrimitive => { const depth = nonPrimitive.depth + 1; const nonPrimitiveContent = nonPrimitive.content; return (0, _effect.pipe)({ protoDepth: 0, nonPrimitiveContent }, _effectLib.MArray.unfoldNonEmpty(_effectLib.MTuple.makeBothBy({ toFirst: ({ protoDepth, nonPrimitiveContent }) => { // Record.map will not return all keys const ownKeys = Reflect.ownKeys(nonPrimitiveContent); const isFunctionProto = nonPrimitiveContent === _effectLib.MFunction.proto; return _effect.Array.filterMap(ownKeys, key => // The arguments and caller properties of the function prototype are deprecated, reading them causes an error isFunctionProto && (key === 'arguments' || key === 'caller') ? _effect.Option.none() : _effect.Option.some(PPValue.fromNonPrimitiveValueAndKey({ nonPrimitiveContent, key, depth, protoDepth }))); }, toSecond: (0, _effect.flow)(_effectLib.MStruct.evolve({ protoDepth: _effect.Number.increment, nonPrimitiveContent: Reflect.getPrototypeOf }), _effect.Option.liftPredicate(_effectLib.MPredicate.struct({ protoDepth: _effect.Number.lessThanOrEqualTo(maxPrototypeDepth), nonPrimitiveContent: _effectLib.MTypes.isNonPrimitive }))) })), _effect.Array.flatten, // Removes __proto__ properties if there are some because we have already read that property with getPrototypeOf _effect.Array.filter(_effectLib.MPredicate.struct({ oneLineStringKey: _effect.Predicate.not(_effectLib.MFunction.strictEquals('__proto__')) }))); }; /** * Builds a Values from an iterable non-primitive value returning a value iterator * * @category Constructors */ exports.fromProperties = fromProperties; const fromValueIterable = nonPrimitive => (0, _effect.pipe)(nonPrimitive.content, _effect.Option.liftPredicate(_effectLib.MTypes.isIterable), _effect.Option.map((0, _effect.flow)(_effect.Array.fromIterable, _effect.Array.map((content, i) => PPValue.fromIterable({ content, stringKey: (0, _effect.pipe)(i, _effectLib.MString.fromNonNullablePrimitive, _effect.Array.of), depth: nonPrimitive.depth + 1 })))), _effect.Option.getOrElse(_effect.Function.constant(_effect.Array.empty()))); /** * Builds a Values from an iterable non-primitive value returning a key/value iterator * * @category Constructors */ exports.fromValueIterable = fromValueIterable; const fromKeyValueIterable = stringifier => nonPrimitive => (0, _effect.pipe)(nonPrimitive.content, _effect.Option.liftPredicate(_effectLib.MTypes.isIterable), _effect.Option.map((0, _effect.flow)(_effect.Array.fromIterable, _effect.Array.filterMap(nextProp => (0, _effect.pipe)(nextProp, _effect.Option.liftPredicate(_effectLib.MTypes.isArray), _effect.Option.filter(_effectLib.MTypes.isPair), _effect.Option.map(([key, content]) => PPValue.fromIterable({ content, stringKey: (0, _effect.pipe)(key, _effect.Either.liftPredicate(_effectLib.MTypes.isString, stringifier), _effect.Either.map((0, _effect.flow)(_ansiStyles.ASText.fromString, PPStringifiedValue.fromText)), _effect.Either.merge, PPStringifiedValue.toUnstyledStrings), depth: nonPrimitive.depth + 1 })))))), _effect.Option.getOrElse(_effect.Function.constant(_effect.Array.empty()))); exports.fromKeyValueIterable = fromKeyValueIterable; //# sourceMappingURL=Values.js.map