@parischap/pretty-print
Version:
A functional library to pretty-print and treeify objects
114 lines • 3.43 kB
JavaScript
/**
* Type that represents the output of the stringification process of a value. It is in fact an alias
* for an array of ASText's (see Text.ts in @parischap/ansi-styles). Each elament of the array
* represents a line of the stringified value. There must always be at least one line. But that line
* may contain en empty text.
*/
import { ASText } from '@parischap/ansi-styles';
import { MArray } from '@parischap/effect-lib';
import { Array, flow, Function, Number, pipe, Predicate } from 'effect';
/**
* Equivalence for StringifiedValue's. To be removed when Equal.equals will handle Arrays properly
* (from Effect 4.0 onwards)
*
* @category Equivalences
*/
export const equivalence = /*#__PURE__*/Array.getEquivalence(ASText.equivalence);
/**
* Builds a StringifiedValue from a Text
*
* @category Constructors
*/
export const fromText = Array.of;
/**
* Empty StringifiedValue instance
*
* @category Instances
*/
export const empty = /*#__PURE__*/pipe(ASText.empty, fromText);
/**
* Builds a StringifiedValue from a StringifiedProperties
*
* @category Constructors
*/
export const fromStringifiedProperties = /*#__PURE__*/Array.match({
onEmpty: /*#__PURE__*/Function.constant(empty),
onNonEmpty: Array.flatten
});
/**
* Returns a single-line version of `self`
*
* @category Utils
*/
export const toSingleLine = /*#__PURE__*/flow(/*#__PURE__*/ASText.join(ASText.empty), fromText);
/**
* Returns `true` if `self` is empty.
*
* @category Predicates
*/
export const isEmpty = /*#__PURE__*/MArray.match012({
onEmpty: Function.constTrue,
onSingleton: ASText.isEmpty,
onOverTwo: Function.constFalse
});
/**
* Returns `true` if `self` is not empty.
*
* @category Predicates
*/
export const isNotEmpty = /*#__PURE__*/Predicate.not(isEmpty);
/**
* Returns a copy of `self` with a new line at the end
*
* @category Utils
*/
export const addLineAfter = line => Array.append(line);
/**
* Returns a copy of `self` with a new line at the start
*
* @category Utils
*/
export const addLineBefore = line => Array.prepend(line);
/**
* Returns a copy of `self` in which `text` has been prepended to each line
*
* @category Utils
*/
export const prependToAllLines = text => Array.map(ASText.prepend(text));
/**
* Returns a copy of `self` in which `text` has been appended to the last line
*
* @category Utils
*/
export const appendToLastLine = text => Array.modifyNonEmptyLast(ASText.append(text));
/**
* Returns a copy of `self` in which `text` has been prepended to the first line
*
* @category Utils
*/
export const prependToFirstLine = text => Array.modifyNonEmptyHead(ASText.prepend(text));
/**
* Returns a copy of `self` in which `text` has been prepended to all lines but the first
*
* @category Utils
*/
export const prependToTailLines = text => MArray.modifyTail(ASText.prepend(text));
/**
* Returns the length of `self`
*
* @category Destructors
*/
export const toLength = /*#__PURE__*/flow(/*#__PURE__*/Array.map(ASText.toLength), Number.sumAll);
/**
* Returns the ANSI string corresponding to `self`
*
* @category Destructors
*/
export const toAnsiString = (sep = ASText.lineBreak) => flow(ASText.join(sep), ASText.toAnsiString);
/**
* Returns the stringq corresponding to `self` without any styling
*
* @category Destructors
*/
export const toUnstyledStrings = /*#__PURE__*/Array.map(ASText.toUnstyledString);
//# sourceMappingURL=StringifiedValue.js.map