@parischap/pretty-print
Version:
A functional library to pretty-print and treeify objects
112 lines • 4.09 kB
JavaScript
/**
* This module implements a map of ContextStyler's (see
*
* @parischap/ansi-styles/ContextStyler.ts). These ContextStyler's are used to style the different parts of a
* stringified value.
*
* With the make function, you can define your own instances if the provided ones don't suit your
* needs.
*/
import { ASContextStyler, ASPalette, ASStyle } from '@parischap/ansi-styles';
import { MInspectable, MPipeable, MTypes } from '@parischap/effect-lib';
import { Equal, flow, Function, Hash, HashMap, Option, pipe, Predicate, Struct } from 'effect';
import * as PPValueBasedStyler from './ValueBasedStyler.js';
/**
* Module tag
*
* @category Models
*/
export const moduleTag = '@parischap/pretty-print/StyleMap/';
const _TypeId = /*#__PURE__*/Symbol.for(moduleTag);
/**
* Type guard
*
* @category Guards
*/
export const has = u => Predicate.hasProperty(u, _TypeId);
/**
* Equivalence
*
* @category Equivalences
*/
export const equivalence = (self, that) => that.id === self.id;
/** Prototype */
const _TypeIdHash = /*#__PURE__*/Hash.hash(_TypeId);
const proto = {
[_TypeId]: _TypeId,
[Equal.symbol](that) {
return has(that) && equivalence(this, that);
},
[Hash.symbol]() {
return pipe(this.id, Hash.hash, Hash.combine(_TypeIdHash), Hash.cached(this));
},
[MInspectable.IdSymbol]() {
return this.id;
},
... /*#__PURE__*/MInspectable.BaseProto(moduleTag),
...MPipeable.BaseProto
};
/**
* Constructor
*
* @category Constructors
*/
export const make = params => MTypes.objectFromDataAndProto(proto, params);
/**
* Returns the `id` property of `self`
*
* @category Destructors
*/
export const id = /*#__PURE__*/Struct.get('id');
/**
* Returns the `styles` property of `self`
*
* @category Destructors
*/
export const styles = /*#__PURE__*/Struct.get('styles');
/**
* Returns the ValueBasedStyler associated with `partName` which identifies a part of a stringified
* value. Returns `ASContextStyler.none` if `partName` is not present in `self`.
*
* @category Destructors
*/
export const get = partName => flow(styles, HashMap.get(partName), Option.getOrElse(Function.constant(ASContextStyler.none())));
/**
* StyleMap instance for ansi dark mode
*
* @category Instances
*/
export const darkMode = /*#__PURE__*/make({
id: 'DarkMode',
styles: /*#__PURE__*/HashMap.make(['Message', /*#__PURE__*/ASContextStyler.green()], ['ToStringedObject', /*#__PURE__*/ASContextStyler.yellow()], ['PrimitiveValue', /*#__PURE__*/PPValueBasedStyler.makeTypeIndexed(/*#__PURE__*/ASPalette.make(
// string
ASStyle.green,
// number
ASStyle.yellow,
// bigint
ASStyle.yellow,
// boolean
ASStyle.yellow,
// symbol
ASStyle.cyan,
/*#__PURE__*/
// null
pipe(ASStyle.green, /*#__PURE__*/ASStyle.mergeOver(ASStyle.bold)),
// undefined
ASStyle.green))], ['PropertyKey', /*#__PURE__*/PPValueBasedStyler.makeKeyTypeIndexed(/*#__PURE__*/ASPalette.make(
// string key
ASStyle.red,
// symbolic key
ASStyle.cyan))], ['PrototypeDelimiters', /*#__PURE__*/ASContextStyler.green()], ['KeyValueSeparator', /*#__PURE__*/ASContextStyler.white()], ['InBetweenPropertySeparator', /*#__PURE__*/ASContextStyler.white()], ['NonPrimitiveValueDelimiters', /*#__PURE__*/PPValueBasedStyler.makeDepthIndexed(/*#__PURE__*/ASPalette.make(ASStyle.red, ASStyle.green, ASStyle.yellow, ASStyle.blue, ASStyle.magenta, ASStyle.cyan, ASStyle.white))], ['Indentation', /*#__PURE__*/ASContextStyler.green()], ['NonPrimitiveValueId', /*#__PURE__*/ASContextStyler.green()], ['NonPrimitiveValueIdSeparator', /*#__PURE__*/ASContextStyler.green()], ['PropertyNumbers', /*#__PURE__*/ASContextStyler.green()], ['PropertyNumberSeparator', /*#__PURE__*/ASContextStyler.green()], ['PropertyNumberDelimiters', /*#__PURE__*/ASContextStyler.green()])
});
/**
* StyleMap instance that doesn't apply any formatting (uses the none ContextStyler of the
* ansi-styles library for all parts to be formatted)
*
* @category Instances
*/
export const none = /*#__PURE__*/make({
id: 'None',
styles: /*#__PURE__*/HashMap.empty()
});
//# sourceMappingURL=StyleMap.js.map