UNPKG

@parischap/pretty-print

Version:
138 lines 3.71 kB
/** * This module implements a type that takes care of filtering properties when printing non-primitive * values. * * With the make function, you can define your own instances if the provided ones don't suit your * needs. */ import { MFunction, MInspectable, MPipeable, MPredicate } from '@parischap/effect-lib'; import { Array, Boolean, Equal, Hash, pipe, Predicate, Struct } from 'effect'; import * as PPValue from './Value.js'; /** * Module tag * * @category Models */ export const moduleTag = '@parischap/pretty-print/PropertyFilter/'; 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; /** Base */ const _TypeIdHash = /*#__PURE__*/Hash.hash(_TypeId); const base = { [_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 = ({ id, action }) => Object.assign(MFunction.clone(action), { id, ...base }); /** * Returns the `id` property of `self` * * @category Destructors */ export const id = /*#__PURE__*/Struct.get('id'); /** * PropertyFilter instance that removes properties of non-primitive values whose value is not a * function * * @category Instances */ export const removeNonFunctions = /*#__PURE__*/make({ id: 'RemoveNonFunctions', action: /*#__PURE__*/Array.filter(PPValue.isFunction) }); /** * PropertyFilter instance that removes properties of non-primitive values whose value is a function * * @category Instances */ export const removeFunctions = /*#__PURE__*/make({ id: 'RemoveFunctions', action: /*#__PURE__*/Array.filter(/*#__PURE__*/Predicate.not(PPValue.isFunction)) }); /** * PropertyFilter instance that removes non-enumerable properties of non-primitive values * * @category Instances */ export const removeNonEnumerables = /*#__PURE__*/make({ id: 'RemoveNonEnumerables', action: /*#__PURE__*/Array.filter(PPValue.isEnumerable) }); /** * PropertyFilter instance that removes enumerable properties of non-primitive values * * @category Instances */ export const removeEnumerables = /*#__PURE__*/make({ id: 'RemoveEnumerables', action: /*#__PURE__*/Array.filter(/*#__PURE__*/Predicate.not(PPValue.isEnumerable)) }); /** * PropertyFilter instance that removes properties of non-primitive values with a string key * * @category Instances */ export const removeStringKeys = /*#__PURE__*/make({ id: 'RemoveStringKeys', action: /*#__PURE__*/Array.filter(PPValue.hasSymbolicKey) }); /** * PropertyFilter instance that removes properties of non-primitive values with a symbolic key * * @category Instances */ export const removeSymbolicKeys = /*#__PURE__*/make({ id: 'RemoveSymbolicKeys', action: /*#__PURE__*/Array.filter(/*#__PURE__*/Predicate.not(PPValue.hasSymbolicKey)) }); /** * Constructor of a propertyFilter instance that removes properties of non-primitive values whose * key is: * * - A string that does not fulfill `predicate` * - A symbol * * @category Constructors */ export const removeNotFulfillingKeyPredicateMaker = ({ id, predicate }) => make({ id, action: Array.filter(MPredicate.struct({ oneLineStringKey: predicate, hasSymbolicKey: Boolean.not })) }); //# sourceMappingURL=PropertyFilter.js.map