@parischap/pretty-print
Version:
A functional library to pretty-print and treeify objects
262 lines (260 loc) • 11.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.treeify = exports.tabify = exports.splitOnTotalLengthMaker = exports.splitOnLongestPropLengthMaker = exports.splitOnConstituentNumberMaker = exports.singleLine = exports.moduleTag = exports.make = 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 PPStringifiedProperties = /*#__PURE__*/_interopRequireWildcard(/*#__PURE__*/require("./StringifiedProperties.js"));
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 formatting of non-primitive values. From the
* stringified representation of the properties of a non-primitive value which it receives, it must
* return the stringified representation of the whole non-primitive value. It can take care of
* aspects like adding specific array/object marks, printing on a single or multiple lines,
* indentation when printing on multiple lines, ...
*
* 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/NonPrimitiveFormatter/';
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');
/**
* NonPrimitiveFormatter instance that will always print non-primitive values on a single line
*
* @category Instances
*/
const singleLine = exports.singleLine = /*#__PURE__*/make({
id: 'SingleLine',
action: function ({
valueBasedStylerConstructor
}) {
const inBetweenPropertySeparatorTextFormatter = valueBasedStylerConstructor('InBetweenPropertySeparator');
const nonPrimitiveValueDelimitersTextFormatter = valueBasedStylerConstructor('NonPrimitiveValueDelimiters');
return ({
value,
header
}) => {
const inBetweenPropertySeparator = (0, _effect.pipe)(this.singleLineInBetweenPropertySeparatorMark, inBetweenPropertySeparatorTextFormatter(value));
const inContextNonPrimitiveValueDelimitersTextFormatter = nonPrimitiveValueDelimitersTextFormatter(value);
return _effect.Array.match({
onEmpty: (0, _effect.pipe)(this.multiLineStartDelimiterMark + this.multiLineEndDelimiterMark, inContextNonPrimitiveValueDelimitersTextFormatter, _ansiStyles.ASText.prepend(header), PPStringifiedValue.fromText, _effect.Function.constant),
onNonEmpty: (0, _effect.flow)(PPStringifiedProperties.addMarkInBetween(inBetweenPropertySeparator), PPStringifiedProperties.prependProperty((0, _effect.pipe)(this.singleLineStartDelimiterMark, inContextNonPrimitiveValueDelimitersTextFormatter, _ansiStyles.ASText.prepend(header))), PPStringifiedProperties.appendProperty(inContextNonPrimitiveValueDelimitersTextFormatter(this.singleLineEndDelimiterMark)), PPStringifiedValue.fromStringifiedProperties, PPStringifiedValue.toSingleLine)
});
};
}
});
/**
* NonPrimitiveFormatter instance that will always print non-primitive values on multiple lines with
* a tab indentation
*
* @category Instances
*/
const tabify = exports.tabify = /*#__PURE__*/make({
id: 'Tabify',
action: function ({
valueBasedStylerConstructor,
markShowerConstructor
}) {
const inBetweenPropertySeparatorTextFormatter = valueBasedStylerConstructor('InBetweenPropertySeparator');
const nonPrimitiveValueDelimitersTextFormatter = valueBasedStylerConstructor('NonPrimitiveValueDelimiters');
const tabIndentMarkShower = markShowerConstructor('TabIndent');
return ({
value,
header
}) => {
const inBetweenPropertySeparator = (0, _effect.pipe)(this.multiLineInBetweenPropertySeparatorMark, inBetweenPropertySeparatorTextFormatter(value));
const inContextNonPrimitiveValueDelimitersTextFormatter = nonPrimitiveValueDelimitersTextFormatter(value);
const startDelimiterMarkAndHeader = (0, _effect.pipe)(this.multiLineStartDelimiterMark, inContextNonPrimitiveValueDelimitersTextFormatter, _ansiStyles.ASText.prepend(header));
const endDelimiterMark = inContextNonPrimitiveValueDelimitersTextFormatter(this.multiLineEndDelimiterMark);
const tab = tabIndentMarkShower(value);
return (0, _effect.flow)(PPStringifiedProperties.addMarkInBetween(inBetweenPropertySeparator), PPStringifiedProperties.tabify(tab), PPStringifiedProperties.prependProperty(startDelimiterMarkAndHeader), PPStringifiedProperties.appendProperty(endDelimiterMark), PPStringifiedValue.fromStringifiedProperties);
};
}
});
/**
* NonPrimitiveFormatter instance that will always print non-primitive values in a tree-like fashion
*
* @category Instances
*/
const treeify = exports.treeify = /*#__PURE__*/make({
id: 'Treeify',
action: ({
markShowerConstructor
}) => {
const treeIndentForFirstLineOfInitPropsMarkShower = markShowerConstructor('TreeIndentForFirstLineOfInitProps');
const treeIndentForTailLinesOfInitPropsMarkShower = markShowerConstructor('TreeIndentForTailLinesOfInitProps');
const treeIndentForFirstLineOfLastPropMarkShower = markShowerConstructor('TreeIndentForFirstLineOfLastProp');
const treeIndentForTailLinesOfLastPropMarkShower = markShowerConstructor('TreeIndentForTailLinesOfLastProp');
return ({
value
}) => (0, _effect.flow)(PPStringifiedProperties.treeify({
treeIndentForFirstLineOfInitProps: treeIndentForFirstLineOfInitPropsMarkShower(value),
treeIndentForTailLinesOfInitProps: treeIndentForTailLinesOfInitPropsMarkShower(value),
treeIndentForFirstLineOfLastProp: treeIndentForFirstLineOfLastPropMarkShower(value),
treeIndentForTailLinesOfLastProp: treeIndentForTailLinesOfLastPropMarkShower(value)
}), PPStringifiedValue.fromStringifiedProperties);
}
});
/**
* NonPrimitiveFormatter instance maker that will print non-primitive values on a single line if the
* actual number of their constituents (after filtering,...) is less than or equal to `limit`.
*
* @category Constructors
*/
const splitOnConstituentNumberMaker = limit => make({
id: 'SplitWhenConstituentNumberExceeds' + limit,
action: function (params) {
const initializedSingleLine = singleLine.call(this, params);
const initilizedTabify = tabify.call(this, params);
return ({
value,
header
}) => (0, _effect.flow)(_effectLib.MMatch.make, _effectLib.MMatch.when((0, _effect.flow)(_effect.Array.length, _effect.Number.lessThanOrEqualTo(limit)), initializedSingleLine({
value,
header
})), _effectLib.MMatch.orElse(initilizedTabify({
value,
header
})));
}
});
/**
* Calls `singleLine` if the total length of the properties to print (excluding formatting
* characters) is less than or equal to `limit`. Calls `tabify` otherwise
*
* @category Constructors
*/
exports.splitOnConstituentNumberMaker = splitOnConstituentNumberMaker;
const splitOnTotalLengthMaker = limit => make({
id: 'SplitWhenTotalLengthExceeds' + limit,
action: function (params) {
const initializedSingleLine = singleLine.call(this, params);
const initilizedTabify = tabify.call(this, params);
const inBetweenSepLength = this.singleLineInBetweenPropertySeparatorMark.length;
const delimitersLength = this.singleLineStartDelimiterMark.length + this.singleLineEndDelimiterMark.length;
const delimitersLengthWhenEmpty = this.multiLineStartDelimiterMark.length + this.multiLineEndDelimiterMark.length;
return ({
value,
header
}) => (0, _effect.flow)(_effectLib.MMatch.make, _effectLib.MMatch.when((0, _effect.flow)(_effectLib.MTuple.makeBothBy({
toFirst: PPStringifiedProperties.toLength,
toSecond: _effect.Array.match({
onEmpty: _effect.Function.constant(_ansiStyles.ASText.toLength(header) + delimitersLengthWhenEmpty),
onNonEmpty: (0, _effect.flow)(_effect.Array.length, _effect.Number.decrement, _effect.Number.multiply(inBetweenSepLength), _effect.Number.sum(delimitersLength), _effect.Number.sum(_ansiStyles.ASText.toLength(header)))
})
}), _effect.Number.sumAll, _effect.Number.lessThanOrEqualTo(limit)), initializedSingleLine({
value,
header
})), _effectLib.MMatch.orElse(initilizedTabify({
value,
header
})));
}
});
/**
* Calls `singleLine` if the length of the longest property to print (excluding formatting
* characters and object marks) is less than or equal to `limit`. Calls `tabify` otherwise
*
* @category Constructors
*/
exports.splitOnTotalLengthMaker = splitOnTotalLengthMaker;
const splitOnLongestPropLengthMaker = limit => make({
id: 'SplitWhenLongestPropLengthExceeds' + limit,
action: function (params) {
const initializedSingleLine = singleLine.call(this, params);
const initilizedTabify = tabify.call(this, params);
return ({
value,
header
}) => (0, _effect.flow)(_effectLib.MMatch.make, _effectLib.MMatch.when((0, _effect.flow)(PPStringifiedProperties.toLongestPropLength, _effect.Number.lessThanOrEqualTo(limit)), initializedSingleLine({
value,
header
})), _effectLib.MMatch.orElse(initilizedTabify({
value,
header
})));
}
});
exports.splitOnLongestPropLengthMaker = splitOnLongestPropLengthMaker;
//# sourceMappingURL=NonPrimitiveFormatter.js.map