UNPKG

@parischap/pretty-print

Version:
118 lines (116 loc) 4.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.utilInspectLikeMaker = exports.moduleTag = exports.make = exports.id = exports.has = exports.equivalence = void 0; var _effectLib = /*#__PURE__*/require("@parischap/effect-lib"); var _effect = /*#__PURE__*/require("effect"); 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 takes care of the formatting of primitive values, e.g. * surround strings in quotes, add 'n' at the end of a bigint, display numbers with a thousand * separator,... * * 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/PrimitiveFormatter/'; 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'); /** * PropertyFormatter contructor that builds an instance that works like util.inspect * * @category Constructors */ const utilInspectLikeMaker = ({ id, maxStringLength, numberFormatter } = { id: 'UtilInspectLike', maxStringLength: 10000, numberFormatter: new Intl.NumberFormat() }) => make({ id, action: (0, _effect.flow)(PPValue.content, _effectLib.MMatch.make, _effectLib.MMatch.when(_effectLib.MTypes.isString, (0, _effect.flow)(_effect.Either.liftPredicate((0, _effect.flow)(_effect.String.length, _effect.Number.greaterThan(maxStringLength)), _effect.Function.identity), _effect.Either.map((0, _effect.flow)(_effect.String.takeLeft(maxStringLength), _effectLib.MString.append('...'))), _effect.Either.merge, _effectLib.MString.append("'"), _effectLib.MString.prepend("'"))), _effectLib.MMatch.when(_effectLib.MTypes.isNumber, (0, _effect.flow)(n => numberFormatter.format(n))), _effectLib.MMatch.when(_effectLib.MTypes.isBigInt, (0, _effect.flow)(n => numberFormatter.format(n), _effectLib.MString.append('n'))), _effectLib.MMatch.orElse(_effectLib.MString.fromPrimitive)) }); exports.utilInspectLikeMaker = utilInspectLikeMaker; //# sourceMappingURL=PrimitiveFormatter.js.map