UNPKG

domain-objects

Version:

A simple, convenient way to represent domain objects, leverage domain knowledge, and add runtime validation in your code base.

60 lines 3.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.omitMetadataValues = void 0; const error_fns_1 = require("@ehmpathy/error-fns"); const type_fns_1 = require("type-fns"); const assertDomainObjectIsSafeToManipulate_1 = require("../constraints/assertDomainObjectIsSafeToManipulate"); const DomainObject_1 = require("../instantiation/DomainObject"); const getMetadataKeys_1 = require("./getMetadataKeys"); /** * exposes a function which properly handles any value that can could have been defined for an object property * - if domain object, omits autogenerated values * - if array, recursively omits on each item in the array * - if neither of the above, then its the terminal condition - return it, its fully omitted */ const recursivelyOmitMetadataValuesFromObjectValue = (thisValue) => { // handle directly nested domain object if (thisValue instanceof DomainObject_1.DomainObject) return (0, exports.omitMetadataValues)(thisValue); // eslint-disable-line @typescript-eslint/no-use-before-define // handle an array of one level deep (doesn't handle Array of Array, for simplicity) if (Array.isArray(thisValue)) return thisValue.map(recursivelyOmitMetadataValuesFromObjectValue); // run self on each item in the array, (i.e., recursively) // handle any other value type return thisValue; }; /** * omits all metadata values on a domain object * * relevance: * - often in change detection, metadata values are not relevant, this provides an easy way to omit them * * features: * - utilizes the `.metadata` property of the domain object definition to identify metadata keys * - recall, the default is `id`, `uuid`, `createdAt`, `updatedAt`, `effectiveAt` * - recursive, applies omission deeply */ const omitMetadataValues = (obj) => { // handle arrays if (Array.isArray(obj)) return recursivelyOmitMetadataValuesFromObjectValue(obj); // make sure its an instance of DomainObject if (!(obj instanceof DomainObject_1.DomainObject)) throw new error_fns_1.UnexpectedCodePathError('omitMetadataValues called on object that is not an instance of a DomainObject. Are you sure you instantiated the object? (Related: see `DomainObject.nested`)', { obj }); // get the metadata keys const Constructor = obj.constructor; // https://stackoverflow.com/a/61444747/3068233 const metadataKeys = (0, getMetadataKeys_1.getMetadataKeys)(obj, { nameOfFunctionNeededFor: 'omitMetadataValues', }); // make sure that its safe to manipulate (0, assertDomainObjectIsSafeToManipulate_1.assertDomainObjectIsSafeToManipulate)(obj); // object with omit applied recursively on each property const objectWithEachDomainObjectKeyRecursivelyOmitted = Object.entries(obj).reduce((summary, [thisKey, thisValue]) => { return Object.assign(Object.assign({}, summary), { [thisKey]: recursivelyOmitMetadataValuesFromObjectValue(thisValue) }); }, {}); // omit all of the metadata keys const objWithoutBaseCaseAutogeneratedValues = (0, type_fns_1.omit)(objectWithEachDomainObjectKeyRecursivelyOmitted, metadataKeys); // return the instantiated object return new Constructor(objWithoutBaseCaseAutogeneratedValues); }; exports.omitMetadataValues = omitMetadataValues; //# sourceMappingURL=omitMetadataValues.js.map