UNPKG

@thisisagile/easy

Version:

Straightforward library for building domain-driven microservice architectures

1 lines 2.4 kB
{"version":3,"sources":["../src/data/Condition.ts"],"sourcesContent":["import { json, Json, JsonValue } from '../types/Json';\nimport { Sort } from '../types/Sort';\nimport { convert, Convert } from '../utils/Convert';\nimport { isDefined } from '../types/Is';\n\nexport class Condition {\n constructor(\n readonly key: string,\n readonly operator: string,\n readonly value: unknown\n ) {}\n\n and = (...others: Condition[]): LogicalCondition => new LogicalCondition('and', [this, ...others]);\n or = (...others: Condition[]): LogicalCondition => new LogicalCondition('or', [this, ...others]);\n\n toJSON(): Json {\n return { [this.key]: { [`$${this.operator}`]: json.parse(this.value) } };\n }\n}\n\nexport class LogicalCondition {\n constructor(\n readonly operator: string,\n readonly conditions: Condition[]\n ) {}\n\n toJSON(): Json {\n return { [`$${this.operator}`]: this.conditions.map(c => c.toJSON()) };\n }\n}\n\nexport class SortCondition extends Condition implements Sort {\n constructor(\n readonly key: string,\n readonly value: -1 | 1\n ) {\n super(key, '', value);\n }\n\n toJSON(): Json {\n return { [this.key]: this.value };\n }\n}\n\nexport const toCondition = (field: string, operator: string, value: unknown, conv: Convert = convert.default): Condition =>\n new Condition(field, operator, conv.from(value) as JsonValue);\n\nexport const isSortCondition = (s?: unknown): s is SortCondition => isDefined(s) && s instanceof SortCondition;\n"],"mappings":";;;;;;;;;;;AAKO,IAAM,YAAN,MAAgB;AAAA,EACrB,YACW,KACA,UACA,OACT;AAHS;AACA;AACA;AAAA,EACR;AAAA,EAEH,MAAM,IAAI,WAA0C,IAAI,iBAAiB,OAAO,CAAC,MAAM,GAAG,MAAM,CAAC;AAAA,EACjG,KAAK,IAAI,WAA0C,IAAI,iBAAiB,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC;AAAA,EAE/F,SAAe;AACb,WAAO,EAAE,CAAC,KAAK,GAAG,GAAG,EAAE,CAAC,IAAI,KAAK,QAAQ,EAAE,GAAG,KAAK,MAAM,KAAK,KAAK,EAAE,EAAE;AAAA,EACzE;AACF;AAEO,IAAM,mBAAN,MAAuB;AAAA,EAC5B,YACW,UACA,YACT;AAFS;AACA;AAAA,EACR;AAAA,EAEH,SAAe;AACb,WAAO,EAAE,CAAC,IAAI,KAAK,QAAQ,EAAE,GAAG,KAAK,WAAW,IAAI,OAAK,EAAE,OAAO,CAAC,EAAE;AAAA,EACvE;AACF;AAEO,IAAM,gBAAN,cAA4B,UAA0B;AAAA,EAC3D,YACW,KACA,OACT;AACA,UAAM,KAAK,IAAI,KAAK;AAHX;AACA;AAAA,EAGX;AAAA,EAEA,SAAe;AACb,WAAO,EAAE,CAAC,KAAK,GAAG,GAAG,KAAK,MAAM;AAAA,EAClC;AACF;AAEO,IAAM,cAAc,CAAC,OAAe,UAAkB,OAAgB,OAAgB,QAAQ,YACnG,IAAI,UAAU,OAAO,UAAU,KAAK,KAAK,KAAK,CAAc;AAEvD,IAAM,kBAAkB,CAAC,MAAoC,UAAU,CAAC,KAAK,aAAa;","names":[]}