@aeriajs/common 0.0.104 → 0.0.106
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.
- package/dist/evaluateCondition.js +32 -19
- package/dist/evaluateCondition.mjs +29 -19
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/index.mjs +0 -1
- package/package.json +3 -3
- package/dist/isObjectId.d.ts +0 -2
- package/dist/isObjectId.js +0 -14
- package/dist/isObjectId.mjs +0 -4
|
@@ -2,6 +2,15 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.evaluateCondition = void 0;
|
|
4
4
|
const arraysIntersect_js_1 = require("./arraysIntersect.js");
|
|
5
|
+
const isCondition = (subject) => {
|
|
6
|
+
if (!subject || typeof subject !== 'object') {
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
return ('and' in subject
|
|
10
|
+
|| 'or' in subject
|
|
11
|
+
|| 'not' in subject
|
|
12
|
+
|| ('operator' in subject && 'term1' in subject));
|
|
13
|
+
};
|
|
5
14
|
const equalOrContains = (term1, term2) => {
|
|
6
15
|
if (Array.isArray(term1) && Array.isArray(term2)) {
|
|
7
16
|
return (0, arraysIntersect_js_1.arraysIntersect)(term1, term2);
|
|
@@ -13,34 +22,38 @@ const equalOrContains = (term1, term2) => {
|
|
|
13
22
|
return term2.includes(term1);
|
|
14
23
|
}
|
|
15
24
|
};
|
|
16
|
-
const
|
|
17
|
-
if (
|
|
25
|
+
const evaluateExpression = (subject, expression) => {
|
|
26
|
+
if (!isCondition(expression)) {
|
|
27
|
+
return expression;
|
|
28
|
+
}
|
|
29
|
+
if ('term1' in expression) {
|
|
18
30
|
if (!subject || typeof subject !== 'object') {
|
|
19
31
|
return false;
|
|
20
32
|
}
|
|
21
|
-
const term1 = subject[
|
|
22
|
-
if (
|
|
33
|
+
const term1 = subject[expression.term1];
|
|
34
|
+
if (expression.operator === 'truthy') {
|
|
23
35
|
return !!term1;
|
|
24
36
|
}
|
|
25
|
-
const { operator, term2 } =
|
|
37
|
+
const { operator, term2 } = expression;
|
|
38
|
+
const right = evaluateExpression(subject, term2);
|
|
26
39
|
switch (operator) {
|
|
27
|
-
case 'equal': return term1 ===
|
|
28
|
-
case 'in': return !!equalOrContains(term1,
|
|
29
|
-
case 'gt': return term1 > Number(
|
|
30
|
-
case 'lt': return term1 < Number(
|
|
31
|
-
case 'gte': return term1 >= Number(
|
|
32
|
-
case 'lte': return term1 <= Number(
|
|
33
|
-
case 'regex': return new RegExp(
|
|
40
|
+
case 'equal': return term1 === right;
|
|
41
|
+
case 'in': return !!equalOrContains(term1, right);
|
|
42
|
+
case 'gt': return term1 > Number(right);
|
|
43
|
+
case 'lt': return term1 < Number(right);
|
|
44
|
+
case 'gte': return term1 >= Number(right);
|
|
45
|
+
case 'lte': return term1 <= Number(right);
|
|
46
|
+
case 'regex': return new RegExp(right).test(term1);
|
|
34
47
|
}
|
|
35
48
|
}
|
|
36
|
-
if ('and' in
|
|
37
|
-
return
|
|
49
|
+
if ('and' in expression) {
|
|
50
|
+
return expression.and.every((expression) => evaluateExpression(subject, expression));
|
|
38
51
|
}
|
|
39
|
-
if ('or' in
|
|
40
|
-
return
|
|
52
|
+
if ('or' in expression) {
|
|
53
|
+
return expression.or.some((expression) => evaluateExpression(subject, expression));
|
|
41
54
|
}
|
|
42
|
-
if ('not' in
|
|
43
|
-
return !
|
|
55
|
+
if ('not' in expression) {
|
|
56
|
+
return !evaluateExpression(subject, expression.not);
|
|
44
57
|
}
|
|
45
58
|
return false;
|
|
46
59
|
};
|
|
@@ -49,7 +62,7 @@ const evaluateCondition = (subject, condition) => {
|
|
|
49
62
|
satisfied: false,
|
|
50
63
|
else: null,
|
|
51
64
|
};
|
|
52
|
-
const satisfied = result.satisfied =
|
|
65
|
+
const satisfied = result.satisfied = !!evaluateExpression(subject, condition);
|
|
53
66
|
if (!satisfied && 'else' in condition) {
|
|
54
67
|
result.else = condition.else;
|
|
55
68
|
}
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
import { arraysIntersect } from "./arraysIntersect.mjs";
|
|
3
|
+
const isCondition = (subject) => {
|
|
4
|
+
if (!subject || typeof subject !== "object") {
|
|
5
|
+
return false;
|
|
6
|
+
}
|
|
7
|
+
return "and" in subject || "or" in subject || "not" in subject || "operator" in subject && "term1" in subject;
|
|
8
|
+
};
|
|
3
9
|
const equalOrContains = (term1, term2) => {
|
|
4
10
|
if (Array.isArray(term1) && Array.isArray(term2)) {
|
|
5
11
|
return arraysIntersect(term1, term2);
|
|
@@ -11,41 +17,45 @@ const equalOrContains = (term1, term2) => {
|
|
|
11
17
|
return term2.includes(term1);
|
|
12
18
|
}
|
|
13
19
|
};
|
|
14
|
-
const
|
|
15
|
-
if (
|
|
20
|
+
const evaluateExpression = (subject, expression) => {
|
|
21
|
+
if (!isCondition(expression)) {
|
|
22
|
+
return expression;
|
|
23
|
+
}
|
|
24
|
+
if ("term1" in expression) {
|
|
16
25
|
if (!subject || typeof subject !== "object") {
|
|
17
26
|
return false;
|
|
18
27
|
}
|
|
19
|
-
const term1 = subject[
|
|
20
|
-
if (
|
|
28
|
+
const term1 = subject[expression.term1];
|
|
29
|
+
if (expression.operator === "truthy") {
|
|
21
30
|
return !!term1;
|
|
22
31
|
}
|
|
23
|
-
const { operator, term2 } =
|
|
32
|
+
const { operator, term2 } = expression;
|
|
33
|
+
const right = evaluateExpression(subject, term2);
|
|
24
34
|
switch (operator) {
|
|
25
35
|
case "equal":
|
|
26
|
-
return term1 ===
|
|
36
|
+
return term1 === right;
|
|
27
37
|
case "in":
|
|
28
|
-
return !!equalOrContains(term1,
|
|
38
|
+
return !!equalOrContains(term1, right);
|
|
29
39
|
case "gt":
|
|
30
|
-
return term1 > Number(
|
|
40
|
+
return term1 > Number(right);
|
|
31
41
|
case "lt":
|
|
32
|
-
return term1 < Number(
|
|
42
|
+
return term1 < Number(right);
|
|
33
43
|
case "gte":
|
|
34
|
-
return term1 >= Number(
|
|
44
|
+
return term1 >= Number(right);
|
|
35
45
|
case "lte":
|
|
36
|
-
return term1 <= Number(
|
|
46
|
+
return term1 <= Number(right);
|
|
37
47
|
case "regex":
|
|
38
|
-
return new RegExp(
|
|
48
|
+
return new RegExp(right).test(term1);
|
|
39
49
|
}
|
|
40
50
|
}
|
|
41
|
-
if ("and" in
|
|
42
|
-
return
|
|
51
|
+
if ("and" in expression) {
|
|
52
|
+
return expression.and.every((expression2) => evaluateExpression(subject, expression2));
|
|
43
53
|
}
|
|
44
|
-
if ("or" in
|
|
45
|
-
return
|
|
54
|
+
if ("or" in expression) {
|
|
55
|
+
return expression.or.some((expression2) => evaluateExpression(subject, expression2));
|
|
46
56
|
}
|
|
47
|
-
if ("not" in
|
|
48
|
-
return !
|
|
57
|
+
if ("not" in expression) {
|
|
58
|
+
return !evaluateExpression(subject, expression.not);
|
|
49
59
|
}
|
|
50
60
|
return false;
|
|
51
61
|
};
|
|
@@ -54,7 +64,7 @@ export const evaluateCondition = (subject, condition) => {
|
|
|
54
64
|
satisfied: false,
|
|
55
65
|
else: null
|
|
56
66
|
};
|
|
57
|
-
const satisfied = result.satisfied =
|
|
67
|
+
const satisfied = result.satisfied = !!evaluateExpression(subject, condition);
|
|
58
68
|
if (!satisfied && "else" in condition) {
|
|
59
69
|
result.else = condition.else;
|
|
60
70
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -12,7 +12,6 @@ export * from './getReferenceProperty.js';
|
|
|
12
12
|
export * from './getValueFromPath.js';
|
|
13
13
|
export * from './http.js';
|
|
14
14
|
export * from './isGranted.js';
|
|
15
|
-
export * from './isObjectId.js';
|
|
16
15
|
export * from './isReference.js';
|
|
17
16
|
export * from './isRequired.js';
|
|
18
17
|
export * from './pipe.js';
|
package/dist/index.js
CHANGED
|
@@ -28,7 +28,6 @@ __exportStar(require("./getReferenceProperty.js"), exports);
|
|
|
28
28
|
__exportStar(require("./getValueFromPath.js"), exports);
|
|
29
29
|
__exportStar(require("./http.js"), exports);
|
|
30
30
|
__exportStar(require("./isGranted.js"), exports);
|
|
31
|
-
__exportStar(require("./isObjectId.js"), exports);
|
|
32
31
|
__exportStar(require("./isReference.js"), exports);
|
|
33
32
|
__exportStar(require("./isRequired.js"), exports);
|
|
34
33
|
__exportStar(require("./pipe.js"), exports);
|
package/dist/index.mjs
CHANGED
|
@@ -13,7 +13,6 @@ export * from "./getReferenceProperty.mjs";
|
|
|
13
13
|
export * from "./getValueFromPath.mjs";
|
|
14
14
|
export * from "./http.mjs";
|
|
15
15
|
export * from "./isGranted.mjs";
|
|
16
|
-
export * from "./isObjectId.mjs";
|
|
17
16
|
export * from "./isReference.mjs";
|
|
18
17
|
export * from "./isRequired.mjs";
|
|
19
18
|
export * from "./pipe.mjs";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aeriajs/common",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.106",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -31,11 +31,11 @@
|
|
|
31
31
|
"bson": "^6.5.0"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"@aeriajs/types": "^0.0.
|
|
34
|
+
"@aeriajs/types": "^0.0.89",
|
|
35
35
|
"bson": "^6.5.0"
|
|
36
36
|
},
|
|
37
37
|
"scripts": {
|
|
38
|
-
"test": "
|
|
38
|
+
"test": "vitest run",
|
|
39
39
|
"lint": "eslint src",
|
|
40
40
|
"lint:fix": "eslint src --fix",
|
|
41
41
|
"build": "pnpm build:cjs && pnpm build:esm",
|
package/dist/isObjectId.d.ts
DELETED
package/dist/isObjectId.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isObjectId = void 0;
|
|
4
|
-
const isObjectId = (value) => {
|
|
5
|
-
// we use this comparation instead of `value instanceof ObjectId` because
|
|
6
|
-
// the latter is prone to errors when `mongodb` dependency is duplicated
|
|
7
|
-
// -- when ./node_modules/mongodb and ../node_modules/mongodb are both
|
|
8
|
-
// present and the bundler doesn't handle this correctly
|
|
9
|
-
return !!(value
|
|
10
|
-
&& typeof value === 'object'
|
|
11
|
-
&& '_bsontype' in value
|
|
12
|
-
&& value._bsontype === 'ObjectId');
|
|
13
|
-
};
|
|
14
|
-
exports.isObjectId = isObjectId;
|
package/dist/isObjectId.mjs
DELETED