@parischap/pretty-print
Version:
A functional library to pretty-print and treeify objects
195 lines (193 loc) • 7.31 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.valueOnly = exports.treeifyHideLeafValues = exports.treeify = exports.moduleTag = exports.make = exports.keyAndValue = exports.id = exports.has = exports.equivalence = 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"));
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 stringification of the properties of a
* non-primitive value. From the stringified representation of the value of a property which it
* receives, it must return the stringified representation of the whole property (key and value).
*
* 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/PropertyFormatter/';
const _TypeId = /*#__PURE__*/Symbol.for(moduleTag);
/**
* Type guard
*
* @category Guards
*/
const has = u => _effect.Predicate.hasProperty(u, _TypeId);
/**
* PropertyFormatter 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 instance that prints only the value of a property (similar to the usual way an
* array is printed).
*
* @category Instances
*/
const valueOnly = exports.valueOnly = /*#__PURE__*/make({
id: 'ValueOnly',
action: () => () => _effect.Function.identity
});
/* if onSameLine=false and isLeaf=false , the lines of the value are appended to the lines of the key and no keyValueSeparator is used. In all other cases, the last line of the key and the first line of the value are merged and separated by the keyValueSeparator. */
const _keyAndValueAction = ({
onSameLine,
dontShowLeafValue
}) => function ({
valueBasedStylerConstructor
}) {
const propertyKeyTextFormatter = valueBasedStylerConstructor('PropertyKey');
const prototypeDelimitersTextFormatter = valueBasedStylerConstructor('PrototypeDelimiters');
const KeyValueSeparatorTextFormatter = valueBasedStylerConstructor('KeyValueSeparator');
return ({
value,
isLeaf
}) => {
const stringKey = value.stringKey;
const protoDepth = value.protoDepth;
if (_effectLib.MTypes.isSingleton(stringKey) && _effect.String.isEmpty(stringKey[0])) return _effect.Function.identity;
const inContextPropertyKeyTextFormatter = propertyKeyTextFormatter(value);
const inContextPrototypeDelimitersTextFormatter = prototypeDelimitersTextFormatter(value);
const keyValueSeparator = (0, _effect.pipe)(this.keyValueSeparatorMark, KeyValueSeparatorTextFormatter(value));
const key = (0, _effect.pipe)(stringKey, _effect.Array.map((line, _i) => inContextPropertyKeyTextFormatter(line)), _effectLib.MFunction.fIfTrue({
condition: protoDepth > 0,
f: (0, _effect.flow)(PPStringifiedValue.prependToFirstLine((0, _effect.pipe)(this.prototypeStartDelimiterMark, inContextPrototypeDelimitersTextFormatter, _ansiStyles.ASText.repeat(protoDepth))), PPStringifiedValue.appendToLastLine((0, _effect.pipe)(this.prototypeEndDelimiterMark, inContextPrototypeDelimitersTextFormatter, _ansiStyles.ASText.repeat(protoDepth))))
}));
return stringifiedValue => {
if (!onSameLine && !isLeaf) return (0, _effect.pipe)(key, _effect.Array.appendAll(stringifiedValue));
const firstLine = _effect.Array.headNonEmpty(stringifiedValue);
const showValue = !isLeaf || !dontShowLeafValue;
return (0, _effect.pipe)(key, _effect.Array.initNonEmpty, _effect.Array.append((0, _effect.pipe)(key,
// cannot be an empty string
_effect.Array.lastNonEmpty, _effectLib.MFunction.fIfTrue({
condition: showValue && _ansiStyles.ASText.isNotEmpty(firstLine),
f: (0, _effect.flow)(_ansiStyles.ASText.append(keyValueSeparator), _ansiStyles.ASText.append(firstLine))
}))), _effectLib.MFunction.fIfTrue({
condition: showValue,
f: _effect.Array.appendAll(_effect.Array.tailNonEmpty(stringifiedValue))
}));
};
};
};
/**
* PropertyFormatter instance that prints the key and value of a property (similar to the usual way
* a record is printed). A mark can be prepended or appended to the key to show if the property
* comes from the object itself or from one of its prototypes.
*
* @category Instances
*/
const keyAndValue = exports.keyAndValue = /*#__PURE__*/make({
id: 'KeyAndValue',
action: /*#__PURE__*/_keyAndValueAction({
onSameLine: true,
dontShowLeafValue: false
})
});
/**
* PropertyFormatter instance that :
*
* - For a leaf: does the same as keyAndValue
* - For a non-leaf: prints the key and value on separate lines without any key/value separator
*
* @category Instances
*/
const treeify = exports.treeify = /*#__PURE__*/make({
id: 'Treeify',
action: /*#__PURE__*/_keyAndValueAction({
onSameLine: false,
dontShowLeafValue: false
})
});
/**
* PropertyFormatter instance that :
*
* - For a leaf: prints only the key
* - For a non-leaf: prints the key and value on separate lines without any key/value separator
*
* @category Instances
*/
const treeifyHideLeafValues = exports.treeifyHideLeafValues = /*#__PURE__*/make({
id: 'Treeify',
action: /*#__PURE__*/_keyAndValueAction({
onSameLine: false,
dontShowLeafValue: true
})
});
//# sourceMappingURL=PropertyFormatter.js.map