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