UNPKG

@thisisagile/easy

Version:

Straightforward library for building domain-driven microservice architectures

132 lines (130 loc) 3.82 kB
import { State } from "./chunk-FOVQUHHL.mjs"; import { Property } from "./chunk-IMJHOYU3.mjs"; import { json } from "./chunk-A7KUHZ3A.mjs"; import { ifNotEmpty, meta, toList } from "./chunk-AVHYDITZ.mjs"; import { ofGet } from "./chunk-SJGQU3OG.mjs"; import { isA } from "./chunk-D5IYAIMK.mjs"; import { ofConstruct } from "./chunk-PF7HDF6B.mjs"; import { isEmpty } from "./chunk-AAND4MKF.mjs"; // src/utils/Mapper.ts var isMapping = (m) => isA(m, "in", "out"); var Mapper = class extends State { constructor(options = { startFrom: "scratch" }, property = "") { super(); this.options = options; this.property = property; } map = mappings; // All properties that are a mapping get properties() { return this.get( "props", () => meta(this).entries().filter(([, v]) => isMapping(v)) ); } // All names of properties (in target) that have a Mapping get keys() { return this.get("keys", () => this.properties.map(([k]) => k)); } // All names of properties (in source) that are named in a Mapping get columns() { return this.get("columns", () => this.properties.mapDefined(([, p]) => ifNotEmpty(p.property, p.property))).distinct(); } // All names of properties (in source) that are NOT properties in target get droppedIn() { return this.get("droppedIn", () => this.columns.filter((c) => !this.keys.some((k) => k === c))); } // All names op properties (in target) that are NOT properties in source get droppedOut() { return this.get("droppedOut", () => this.properties.filter(([, p]) => !this.keys.some((k) => k === p.property)).map(([k]) => k)); } in(from = {}) { return json.omit( this.properties.reduce((a, [k, p]) => json.merge(a, { [k]: p.in({ ...a, ...from }) }), this.options.startFrom === "source" ? from : {}), ...this.droppedIn ); } out(to = {}) { return json.omit( this.properties.reduce( (a, [k, p]) => json.merge(a, isEmpty(p.property) ? p.out(to, k) : { [p.property ?? ""]: p.out({ ...a, ...to }, k) }), this.options.startFrom === "source" ? to : {} ), ...this.droppedOut ); } toString() { return this.constructor.name; } }; var mappings = { item: (property, options) => new Property(property, options), ignore: (property = "") => ({ property, in: () => void 0, out: () => void 0 }), skipIn: (property) => ({ property, in: () => void 0, out: (source = {}) => source[property] }), skipOut: (property) => ({ property, in: (source = {}) => source[property], out: () => void 0 }), func: (property, funcIn, funcOut) => ({ property, in: (source = {}) => ofGet(funcIn, source), out: (source = {}) => ofGet(funcOut, source) }), add: (funcIn) => ({ property: "", in: (source = {}) => ofGet(funcIn, source), out: () => void 0 }), map: (mapper, property = "") => ({ property, in: (source = {}) => ofConstruct(mapper).in(isEmpty(property) ? source : source[property]), out: (source = {}, key = "") => ofConstruct(mapper).out(isEmpty(key) ? source : source[key]) }), propsToList: (...maps) => ({ property: "", in: (source = {}) => toList(maps.map((m) => ofConstruct(m).in(source))).toJSON(), out: (source = {}, key = "") => maps.reduce((a, m, i) => { const res = toList(source[key])[i]; const out = m.out(res); return { ...a, [m.property]: out ?? {} }; }, {}) }), list: (mapper, property) => ({ property, in: (source = {}) => toList(source[property]).map((v) => mapper.in(v)).toJSON(), out: (source = {}, key = "") => toList(isEmpty(key) ? source : source[key]).map((v) => mapper.out(v)).toJSON() }) }; export { isMapping, Mapper, mappings }; //# sourceMappingURL=chunk-44GORRHT.mjs.map