@aeriajs/common 0.0.144 → 0.0.145

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ import type { Condition } from '@aeriajs/types';
2
+ export declare const convertConditionToQuery: (condition: Condition, subject?: Record<string, unknown>) => Record<string, unknown>;
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertConditionToQuery = void 0;
4
+ const getValueFromPath_js_1 = require("./getValueFromPath.js");
5
+ const convertExpression = (condition, subject) => {
6
+ let term2 = null;
7
+ if ('term2' in condition) {
8
+ if (condition.fromState) {
9
+ term2 = subject && typeof condition.term2 === 'string'
10
+ ? (0, getValueFromPath_js_1.getValueFromPath)(subject, condition.term2)
11
+ : null;
12
+ }
13
+ else {
14
+ term2 = condition.term2;
15
+ }
16
+ }
17
+ switch (condition.operator) {
18
+ case 'truthy': return {
19
+ $ne: [
20
+ null,
21
+ undefined,
22
+ ],
23
+ };
24
+ case 'equal': return term2;
25
+ case 'gt': return {
26
+ $gt: term2,
27
+ };
28
+ case 'lt': return {
29
+ $lt: term2,
30
+ };
31
+ case 'gte': return {
32
+ $gte: term2,
33
+ };
34
+ case 'lte': return {
35
+ $lte: term2,
36
+ };
37
+ case 'regex': return {
38
+ $regex: term2,
39
+ $options: condition.regexOptions,
40
+ };
41
+ case 'in': {
42
+ return Array.isArray(term2)
43
+ ? {
44
+ $in: term2,
45
+ }
46
+ : term2;
47
+ }
48
+ }
49
+ };
50
+ const convertConditionToQuery = (condition, subject) => {
51
+ if ('or' in condition) {
52
+ return {
53
+ $or: condition.or.map((sub) => (0, exports.convertConditionToQuery)(sub, subject)),
54
+ };
55
+ }
56
+ if ('and' in condition) {
57
+ return {
58
+ $and: condition.and.map((sub) => (0, exports.convertConditionToQuery)(sub, subject)),
59
+ };
60
+ }
61
+ if ('not' in condition) {
62
+ return {
63
+ $not: (0, exports.convertConditionToQuery)(condition.not, subject),
64
+ };
65
+ }
66
+ return {
67
+ [condition.term1]: convertExpression(condition, subject),
68
+ };
69
+ };
70
+ exports.convertConditionToQuery = convertConditionToQuery;
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ import { getValueFromPath } from "./getValueFromPath.mjs";
3
+ const convertExpression = (condition, subject) => {
4
+ let term2 = null;
5
+ if ("term2" in condition) {
6
+ if (condition.fromState) {
7
+ term2 = subject && typeof condition.term2 === "string" ? getValueFromPath(subject, condition.term2) : null;
8
+ } else {
9
+ term2 = condition.term2;
10
+ }
11
+ }
12
+ switch (condition.operator) {
13
+ case "truthy":
14
+ return {
15
+ $ne: [
16
+ null,
17
+ void 0
18
+ ]
19
+ };
20
+ case "equal":
21
+ return term2;
22
+ case "gt":
23
+ return {
24
+ $gt: term2
25
+ };
26
+ case "lt":
27
+ return {
28
+ $lt: term2
29
+ };
30
+ case "gte":
31
+ return {
32
+ $gte: term2
33
+ };
34
+ case "lte":
35
+ return {
36
+ $lte: term2
37
+ };
38
+ case "regex":
39
+ return {
40
+ $regex: term2,
41
+ $options: condition.regexOptions
42
+ };
43
+ case "in": {
44
+ return Array.isArray(term2) ? {
45
+ $in: term2
46
+ } : term2;
47
+ }
48
+ }
49
+ };
50
+ export const convertConditionToQuery = (condition, subject) => {
51
+ if ("or" in condition) {
52
+ return {
53
+ $or: condition.or.map((sub) => convertConditionToQuery(sub, subject))
54
+ };
55
+ }
56
+ if ("and" in condition) {
57
+ return {
58
+ $and: condition.and.map((sub) => convertConditionToQuery(sub, subject))
59
+ };
60
+ }
61
+ if ("not" in condition) {
62
+ return {
63
+ $not: convertConditionToQuery(condition.not, subject)
64
+ };
65
+ }
66
+ return {
67
+ [condition.term1]: convertExpression(condition, subject)
68
+ };
69
+ };
package/dist/index.d.ts CHANGED
@@ -16,5 +16,6 @@ export * from './isGranted.js';
16
16
  export * from './isReference.js';
17
17
  export * from './isRequired.js';
18
18
  export * from './isValidCollection.js';
19
+ export * from './convertConditionToQuery.js';
19
20
  export * from './pipe.js';
20
21
  export * from './serialize.js';
package/dist/index.js CHANGED
@@ -32,5 +32,6 @@ __exportStar(require("./isGranted.js"), exports);
32
32
  __exportStar(require("./isReference.js"), exports);
33
33
  __exportStar(require("./isRequired.js"), exports);
34
34
  __exportStar(require("./isValidCollection.js"), exports);
35
+ __exportStar(require("./convertConditionToQuery.js"), exports);
35
36
  __exportStar(require("./pipe.js"), exports);
36
37
  __exportStar(require("./serialize.js"), exports);
package/dist/index.mjs CHANGED
@@ -17,5 +17,6 @@ export * from "./isGranted.mjs";
17
17
  export * from "./isReference.mjs";
18
18
  export * from "./isRequired.mjs";
19
19
  export * from "./isValidCollection.mjs";
20
+ export * from "./convertConditionToQuery.mjs";
20
21
  export * from "./pipe.mjs";
21
22
  export * from "./serialize.mjs";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/common",
3
- "version": "0.0.144",
3
+ "version": "0.0.145",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -31,7 +31,7 @@
31
31
  "bson": "^6.5.0"
32
32
  },
33
33
  "peerDependencies": {
34
- "@aeriajs/types": "^0.0.126",
34
+ "@aeriajs/types": "^0.0.127",
35
35
  "bson": "^6.5.0"
36
36
  },
37
37
  "scripts": {