UNPKG

@parischap/pretty-print

Version:
164 lines (162 loc) 6.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.objectToString = exports.moduleTag = exports.make = exports.id = exports.has = exports.functionToName = exports.equivalence = exports.empty = void 0; var _effectLib = /*#__PURE__*/require("@parischap/effect-lib"); var _ansiStyles = /*#__PURE__*/require("@parischap/ansi-styles"); 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; } /** * This module implements a type that defines a specific stringification process for certain values * (the normal stringification process is by-passed, hence its name). For instance, you may prefer * printing a Date as a string rather than as an object with all its technical properties. * * With the make function, you can define your own instances if the provided ones don't suit your * needs. */ /** * Module tag * * @category Models */ const moduleTag = exports.moduleTag = '@parischap/pretty-print/ByPasser/'; const _TypeId = /*#__PURE__*/Symbol.for(moduleTag); /** * Type guard * * @category Guards */ const has = u => _effect.Predicate.hasProperty(u, _TypeId); /** * Equivalence * * @category Equivalences */ exports.has = has; const equivalence = (self, that) => that.id === self.id; /** Base */ exports.equivalence = equivalence; const _TypeIdHash = /*#__PURE__*/_effect.Hash.hash(_TypeId); const base = { [_TypeId]: _TypeId, [_effect.Equal.symbol](that) { return has(that) && equivalence(this, that); }, [_effect.Hash.symbol]() { return (0, _effect.pipe)(this.id, _effect.Hash.hash, _effect.Hash.combine(_TypeIdHash), _effect.Hash.cached(this)); }, [_effectLib.MInspectable.IdSymbol]() { return this.id; }, ... /*#__PURE__*/_effectLib.MInspectable.BaseProto(moduleTag), ..._effectLib.MPipeable.BaseProto }; /** * Constructor * * @category Constructors */ const make = ({ id, action }) => Object.assign(_effectLib.MFunction.clone(action), { id, ...base }); /** * Returns the `id` property of `self` * * @category Destructors */ exports.make = make; const id = exports.id = /*#__PURE__*/_effect.Struct.get('id'); /** * ByPasser instance that does not bypass any value * * @category Instances */ const empty = exports.empty = /*#__PURE__*/make({ id: 'Empty', action: () => () => _effect.Option.none() }); /** * ByPasser instance that has the following behavior: * * - For any function: a some of the function name surrounded by the function delimiters and the * message delimiters. If the function name is an empty string, `anonymous` is used instead. * - For any other value: returns a `none` * * @category Instances */ const functionToName = exports.functionToName = /*#__PURE__*/make({ id: 'FunctionToName', action: ({ valueBasedStylerConstructor, markShowerConstructor }) => { const messageTextFormatter = valueBasedStylerConstructor('Message'); const functionNameStartDelimiterMarkShower = markShowerConstructor('FunctionNameStartDelimiter'); const functionNameEndDelimiterMarkShower = markShowerConstructor('FunctionNameEndDelimiter'); const messageStartDelimiterMarkShower = markShowerConstructor('MessageStartDelimiter'); const messageEndDelimiterMarkShower = markShowerConstructor('MessageEndDelimiter'); return value => (0, _effect.pipe)(value, _effect.Option.liftPredicate(PPValue.isFunction), _effect.Option.map((0, _effect.flow)(PPValue.content, _effectLib.MFunction.name, _effect.Option.liftPredicate(_effect.String.isNonEmpty), _effect.Option.getOrElse(_effect.Function.constant('anonymous')), messageTextFormatter(value), _ansiStyles.ASText.surround(functionNameStartDelimiterMarkShower(value), functionNameEndDelimiterMarkShower(value)), _ansiStyles.ASText.surround(messageStartDelimiterMarkShower(value), messageEndDelimiterMarkShower(value)), PPStringifiedValue.fromText))); } }); /** * ByPasser instance that has the following behavior: * * - For any non-primitive value which is not an iterable or a function : tries to call the toString * method (only if it is different from Object.prototype.toString). Returns a `some` of the result * if successful. Returns a `none` otherwise. Calling the .toString method on an Iterable will not * be as efficient as using the `FromValueIterable` or `FromKeyValueIterable` property sources. * Calling the .toString method on a function will not work properly. * - For any other value: returns a `none` * * @category Instances */ const objectToString = exports.objectToString = /*#__PURE__*/make({ id: 'ObjectToString', action: ({ valueBasedStylerConstructor }) => { const toStringedObjectTextFormatter = valueBasedStylerConstructor('ToStringedObject'); return value => { const inContextToStringedObjectTextFormatter = toStringedObjectTextFormatter(value); return (0, _effect.pipe)(value.content, _effect.Option.liftPredicate(_effectLib.MTypes.isNonPrimitive), _effect.Option.filter(_effect.Predicate.not(_effect.Predicate.or(_effectLib.MTypes.isIterable, _effectLib.MTypes.isFunction))), _effect.Option.flatMap((0, _effect.flow)(_effectLib.MRecord.tryZeroParamStringFunction({ functionName: 'toString', /* eslint-disable-next-line @typescript-eslint/unbound-method */ exception: Object.prototype.toString }))), _effect.Option.map((0, _effect.flow)( // split resets RegExp.prototype.lastIndex after executing _effect.String.split(_effectLib.MRegExp.globalLineBreak), _effect.Array.map((s, _i) => inContextToStringedObjectTextFormatter(s))))); }; } }); //# sourceMappingURL=ByPasser.js.map