UNPKG

@thisisagile/easy

Version:

Straightforward library for building domain-driven microservice architectures

122 lines (120 loc) 3.91 kB
import { isPageList, toPageList } from "./chunk-GYJHZJKL.mjs"; import { asJson, json } from "./chunk-A7KUHZ3A.mjs"; import { choose, ifDefined, isEnumConstructor, isEqual, meta } from "./chunk-AVHYDITZ.mjs"; import { traverse } from "./chunk-SSROJBD5.mjs"; import { ofGet } from "./chunk-SJGQU3OG.mjs"; import { isConstructor, use } from "./chunk-PF7HDF6B.mjs"; import { isArray, isBoolean, isFunction, isString } from "./chunk-AAND4MKF.mjs"; // src/utils/View.ts var ignore = Symbol("view.ignore"); var keep = Symbol("view.keep"); var json2 = Symbol("view.json"); var spread = "view.spread"; var toViewer = (key, value) => choose(value).is.not.defined((v) => v, { key, f: () => void 0 }).type(isBoolean, (b) => ({ key, f: () => b })).equals(ignore, { key, f: () => void 0 }).equals(keep, { key, f: (a) => traverse(a, key) }).equals(json2, { key, f: (a) => asJson(traverse(a, key)) }).type(isString, (s) => ({ key, f: (a) => traverse(a, s) })).type(isEnumConstructor, (c) => ({ key, f: (a, key2) => use(traverse(a, key2), (v) => isArray(v) ? c.byIds(v) : c.byId(v)) })).type(isConstructor, (c) => ({ key, f: (a, key2) => use(traverse(a, key2), (v) => isArray(v) ? v.map((i) => optional(c, i)) : optional(c, v)) })).type(isSimpleView, (f) => ({ key, f: (a) => ifDefined(traverse(a, key), (v) => f.from(v)) })).type(isFunction, (f) => ({ key, f })).else((v) => ({ key, f: () => v })); var optional = (c, v) => ifDefined( v, (i) => new c(i), () => v ); var toViewers = (views2) => meta(views2).entries().map(([k, v]) => toViewer(k, v)); var View = class _View { constructor(views2 = {}, startsFrom = "scratch", viewers = toViewers(views2), spreads = []) { this.views = views2; this.startsFrom = startsFrom; this.viewers = viewers; this.spreads = spreads; } get fromSource() { return new _View(this.views, "source", this.viewers, this.spreads); } spread(f) { return new _View(this.views, this.startsFrom, this.viewers, [...this.spreads, f]); } from(source) { if (isPageList(source)) return toPageList( source.map((s) => this.reduce(s)), source.options ); if (isArray(source)) return source.map((s) => this.reduce(s)); return this.reduce(source); } same = (one, another) => isEqual(this.from(one), this.from(another)); reduce = (source) => use(asJson(source), (src) => { const mapped = this.viewers.reduce( (acc, v) => v.key === spread ? { ...acc, ...asJson(v.f(src, v.key)) } : json.set(acc, v.key, v.f(src, v.key)), this.startsFrom === "scratch" ? {} : src ); return this.spreads.reduce((acc, f) => ({ ...acc, ...asJson(ofGet(f, src)) }), mapped); }); }; var isSimpleView = (a) => a instanceof View; var view = (views2) => new View(views2); var views = { ignore, keep, json: json2, spread, skip: ignore, equals: (key, value) => (a) => isEqual(traverse(a, key), value), value: (value) => () => value, coalesce: (...keys) => (a) => keys.reduce((result, k) => result ?? traverse(a, k), void 0), or: { key: (altKey) => (a, key) => traverse(a, key) ?? traverse(a, altKey), value: (altValue) => (a, key) => traverse(a, key) ?? altValue, func: (altFunc) => (a, key) => traverse(a, key) ?? altFunc(a, key) }, to: (ctorOrFunc) => { const apply = (v) => isConstructor(ctorOrFunc) ? new ctorOrFunc(v) : ctorOrFunc(v); const base = (a, key) => { const v = traverse(a, key); return v != null ? apply(v) : void 0; }; return Object.assign(base, { or: { key: (altKey) => (a, key) => base(a, key) ?? traverse(a, altKey), value: (altValue) => (a, key) => base(a, key) ?? altValue, func: (altFunc) => (a, key) => base(a, key) ?? altFunc(a, key) } }); } }; export { toViewer, View, isSimpleView, view, views }; //# sourceMappingURL=chunk-PDPNMABX.mjs.map