@parischap/pretty-print
Version:
A functional library to pretty-print and treeify objects
170 lines • 4.25 kB
JavaScript
/**
* Type that represents a value in its stringification context.
*
* This module provides several Order instances to sort Value's according to your needs
*/
import { MInspectable, MPipeable, MString, MTypes } from '@parischap/effect-lib';
import { Array, Equal, Hash, pipe, Predicate, Struct } from 'effect';
/**
* Module tag
*
* @category Models
*/
export const moduleTag = '@parischap/pretty-print/Value/';
const _TypeId = /*#__PURE__*/Symbol.for(moduleTag);
/**
* Type guard
*
* @category Guards
*/
export const has = u => Predicate.hasProperty(u, _TypeId);
/**
* Value equivalence based on strict equality of the content properties. Used for cycle detection
*
* @category Equivalences
*/
export const equivalence = (self, that) => that.content === self.content;
/** Prototype */
const _TypeIdHash = /*#__PURE__*/Hash.hash(_TypeId);
const proto = {
[_TypeId]: {
_V: MTypes.covariantValue
},
[Equal.symbol](that) {
return has(that) && equivalence(this, that);
},
[Hash.symbol]() {
return pipe(this.content, Hash.hash, Hash.combine(_TypeIdHash), Hash.cached(this));
},
... /*#__PURE__*/MInspectable.BaseProto(moduleTag),
...MPipeable.BaseProto
};
/** Constructor */
const _make = params => MTypes.objectFromDataAndProto(proto, params);
/**
* Constructor from the top value to stringify
*
* @category Constructors
*/
export const fromTopValue = content => _make({
content,
contentType: MTypes.Category.fromValue(content),
depth: 0,
protoDepth: 0,
stringKey: Array.of(''),
oneLineStringKey: '',
hasSymbolicKey: false,
isEnumerable: false
});
/**
* Constructor from the property of a non-primitive value
*
* @category Constructors
*/
export const fromNonPrimitiveValueAndKey = ({
nonPrimitiveContent,
key,
depth,
protoDepth
}) => {
const oneLineStringKey = MString.fromNonNullablePrimitive(key);
const content = nonPrimitiveContent[key];
return _make({
content,
contentType: MTypes.Category.fromValue(content),
depth,
protoDepth,
stringKey: Array.of(oneLineStringKey),
oneLineStringKey,
hasSymbolicKey: MTypes.isSymbol(key),
isEnumerable: Object.prototype.propertyIsEnumerable.call(nonPrimitiveContent, key)
});
};
/**
* Constructor from a value extracted from an iterable non-primitive value
*
* @category Constructors
*/
export const fromIterable = ({
content,
stringKey,
depth
}) => {
return _make({
content,
contentType: MTypes.Category.fromValue(content),
depth,
protoDepth: 0,
stringKey,
oneLineStringKey: Array.join(stringKey, ''),
hasSymbolicKey: false,
isEnumerable: true
});
};
/**
* Type guard
*
* @category Guards
*/
export const isPrimitive = u => MTypes.Category.isPrimitive(u.contentType);
/**
* Type guard
*
* @category Guards
*/
export const isNonPrimitive = u => MTypes.Category.isNonPrimitive(u.contentType);
/**
* Type guard
*
* @category Guards
*/
export const isFunction = u => MTypes.Category.isFunction(u.contentType);
/**
* Returns the `content` property of `self`
*
* @category Destructors
*/
export const content = /*#__PURE__*/Struct.get('content');
/**
* Returns the `contentType` property of `self`
*
* @category Destructors
*/
export const contentType = /*#__PURE__*/Struct.get('contentType');
/**
* Returns the `depth` property of `self`
*
* @category Destructors
*/
export const depth = /*#__PURE__*/Struct.get('depth');
/**
* Returns the `protoDepth` property of `self`
*
* @category Destructors
*/
export const protoDepth = /*#__PURE__*/Struct.get('protoDepth');
/**
* Returns the `stringKey` property of `self`
*
* @category Destructors
*/
export const stringKey = /*#__PURE__*/Struct.get('stringKey');
/**
* Returns the `oneLineStringKey` property of `self`
*
* @category Destructors
*/
export const oneLineStringKey = /*#__PURE__*/Struct.get('oneLineStringKey');
/**
* Returns the `hasSymbolicKey` property of `self`
*
* @category Destructors
*/
export const hasSymbolicKey = /*#__PURE__*/Struct.get('hasSymbolicKey');
/**
* Returns the `isEnumerable` property of `self`
*
* @category Destructors
*/
export const isEnumerable = /*#__PURE__*/Struct.get('isEnumerable');
//# sourceMappingURL=Value.js.map