@aeriajs/common 0.0.105 → 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.
@@ -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 evaluatesToTrue = (subject, condition) => {
17
- if ('term1' in condition) {
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[condition.term1];
22
- if (condition.operator === 'truthy') {
33
+ const term1 = subject[expression.term1];
34
+ if (expression.operator === 'truthy') {
23
35
  return !!term1;
24
36
  }
25
- const { operator, term2 } = condition;
37
+ const { operator, term2 } = expression;
38
+ const right = evaluateExpression(subject, term2);
26
39
  switch (operator) {
27
- case 'equal': return term1 === term2;
28
- case 'in': return !!equalOrContains(term1, term2);
29
- case 'gt': return term1 > Number(term2);
30
- case 'lt': return term1 < Number(term2);
31
- case 'gte': return term1 >= Number(term2);
32
- case 'lte': return term1 <= Number(term2);
33
- case 'regex': return new RegExp(term2).test(term1);
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 condition) {
37
- return condition.and.every((condition) => evaluatesToTrue(subject, condition));
49
+ if ('and' in expression) {
50
+ return expression.and.every((expression) => evaluateExpression(subject, expression));
38
51
  }
39
- if ('or' in condition) {
40
- return condition.or.some((condition) => evaluatesToTrue(subject, condition));
52
+ if ('or' in expression) {
53
+ return expression.or.some((expression) => evaluateExpression(subject, expression));
41
54
  }
42
- if ('not' in condition) {
43
- return !evaluatesToTrue(subject, condition.not);
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 = evaluatesToTrue(subject, condition);
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 evaluatesToTrue = (subject, condition) => {
15
- if ("term1" in condition) {
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[condition.term1];
20
- if (condition.operator === "truthy") {
28
+ const term1 = subject[expression.term1];
29
+ if (expression.operator === "truthy") {
21
30
  return !!term1;
22
31
  }
23
- const { operator, term2 } = condition;
32
+ const { operator, term2 } = expression;
33
+ const right = evaluateExpression(subject, term2);
24
34
  switch (operator) {
25
35
  case "equal":
26
- return term1 === term2;
36
+ return term1 === right;
27
37
  case "in":
28
- return !!equalOrContains(term1, term2);
38
+ return !!equalOrContains(term1, right);
29
39
  case "gt":
30
- return term1 > Number(term2);
40
+ return term1 > Number(right);
31
41
  case "lt":
32
- return term1 < Number(term2);
42
+ return term1 < Number(right);
33
43
  case "gte":
34
- return term1 >= Number(term2);
44
+ return term1 >= Number(right);
35
45
  case "lte":
36
- return term1 <= Number(term2);
46
+ return term1 <= Number(right);
37
47
  case "regex":
38
- return new RegExp(term2).test(term1);
48
+ return new RegExp(right).test(term1);
39
49
  }
40
50
  }
41
- if ("and" in condition) {
42
- return condition.and.every((condition2) => evaluatesToTrue(subject, condition2));
51
+ if ("and" in expression) {
52
+ return expression.and.every((expression2) => evaluateExpression(subject, expression2));
43
53
  }
44
- if ("or" in condition) {
45
- return condition.or.some((condition2) => evaluatesToTrue(subject, condition2));
54
+ if ("or" in expression) {
55
+ return expression.or.some((expression2) => evaluateExpression(subject, expression2));
46
56
  }
47
- if ("not" in condition) {
48
- return !evaluatesToTrue(subject, condition.not);
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 = evaluatesToTrue(subject, condition);
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.105",
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.88",
34
+ "@aeriajs/types": "^0.0.89",
35
35
  "bson": "^6.5.0"
36
36
  },
37
37
  "scripts": {
38
- "test": "echo skipping",
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",
@@ -1,2 +0,0 @@
1
- import type { ObjectId } from 'mongodb';
2
- export declare const isObjectId: (value: unknown) => value is ObjectId;
@@ -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;
@@ -1,4 +0,0 @@
1
- "use strict";
2
- export const isObjectId = (value) => {
3
- return !!(value && typeof value === "object" && "_bsontype" in value && value._bsontype === "ObjectId");
4
- };