@axi-engine/expressions 0.2.1 → 0.2.3
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/index.d.mts +24 -3
- package/dist/index.d.ts +24 -3
- package/dist/index.js +43 -40
- package/dist/index.mjs +34 -33
- package/package.json +4 -4
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ScalarType, PathType, DataSource } from '@axi-engine/utils';
|
|
1
|
+
import { ScalarType, PathType, DataSource, Registry } from '@axi-engine/utils';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Represents an operand that is a direct, static value.
|
|
@@ -227,6 +227,27 @@ type Expression = ExpressionDefinitions[keyof ExpressionDefinitions];
|
|
|
227
227
|
/** The union of all possible expression names (keys). uses in ExpressionHandler */
|
|
228
228
|
type ExpressionName = keyof ExpressionDefinitions;
|
|
229
229
|
|
|
230
|
+
/**
|
|
231
|
+
* A safe utility function that performs a basic mathematical operation on two operands.
|
|
232
|
+
*
|
|
233
|
+
* This function includes built-in type checking. It first ensures
|
|
234
|
+
* that both `left` and `right` operands are numbers before performing the calculation.
|
|
235
|
+
* If the type check fails or if an unsupported operator is provided, it will throw
|
|
236
|
+
* a descriptive error.
|
|
237
|
+
*
|
|
238
|
+
* @param op The mathematical operator to apply ('+', '-', '*', '/').
|
|
239
|
+
* @param left The left-hand operand. It is validated to be a number.
|
|
240
|
+
* @param right The right-hand operand. It is validated to be a number.
|
|
241
|
+
* @returns {number} The numerical result of the calculation.
|
|
242
|
+
* @throws {Error} If either `left` or `right` is not a number.
|
|
243
|
+
* @throws {Error} If the `op` is not a recognized `MathOperationType`.
|
|
244
|
+
*
|
|
245
|
+
* @example
|
|
246
|
+
* const result = resolveMath('+', 10, 5); // returns 15
|
|
247
|
+
* const product = resolveMath('*', 2, 3); // returns 6
|
|
248
|
+
*/
|
|
249
|
+
declare function resolveMath(op: MathOperationType, left: unknown, right: unknown): number;
|
|
250
|
+
|
|
230
251
|
/**
|
|
231
252
|
* Recursively resolves an Operand into its final scalar value.
|
|
232
253
|
*
|
|
@@ -296,7 +317,7 @@ interface ExpressionEvaluatorContext {
|
|
|
296
317
|
*/
|
|
297
318
|
declare class ExpressionEvaluator {
|
|
298
319
|
/** @internal A map of registered expression handlers. */
|
|
299
|
-
handlers:
|
|
320
|
+
handlers: Registry<keyof ExpressionDefinitions, ExpressionHandler<Expression>>;
|
|
300
321
|
/**
|
|
301
322
|
* Registers a new `ExpressionHandler` with the evaluator.
|
|
302
323
|
* This is the primary mechanism for extending the expression language with
|
|
@@ -466,4 +487,4 @@ declare class OrExpressionHandler implements ExpressionHandler<OrExpression> {
|
|
|
466
487
|
*/
|
|
467
488
|
declare function createExpressionEvaluator(additionalHandlers?: ExpressionHandler[]): ExpressionEvaluator;
|
|
468
489
|
|
|
469
|
-
export { type AndExpression, AndExpressionHandler, type ArithmeticOperand, type ChanceExpression, ChanceExpressionHandler, type ComparisonExpression, ComparisonExpressionHandler, type ComparisonOperationType, type ExistsExpression, ExistsExpressionHandler, type Expression, type ExpressionDefinitions, ExpressionEvaluator, type ExpressionEvaluatorContext, type ExpressionHandler, type ExpressionName, type InExpression, InExpressionHandler, type LiteralExpression, LiteralExpressionHandler, type MathOperationType, type NotExpression, NotExpressionHandler, type Operand, type OrExpression, OrExpressionHandler, type ReferenceOperand, type ValueOperand, createExpressionEvaluator, isArithmeticOperand, isOperand, isReferenceOperand, isValueOperand, resolveOperand, resolveOperandAsScalar };
|
|
490
|
+
export { type AndExpression, AndExpressionHandler, type ArithmeticOperand, type ChanceExpression, ChanceExpressionHandler, type ComparisonExpression, ComparisonExpressionHandler, type ComparisonOperationType, type ExistsExpression, ExistsExpressionHandler, type Expression, type ExpressionDefinitions, ExpressionEvaluator, type ExpressionEvaluatorContext, type ExpressionHandler, type ExpressionName, type InExpression, InExpressionHandler, type LiteralExpression, LiteralExpressionHandler, type MathOperationType, type NotExpression, NotExpressionHandler, type Operand, type OrExpression, OrExpressionHandler, type ReferenceOperand, type ValueOperand, createExpressionEvaluator, isArithmeticOperand, isOperand, isReferenceOperand, isValueOperand, resolveMath, resolveOperand, resolveOperandAsScalar };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ScalarType, PathType, DataSource } from '@axi-engine/utils';
|
|
1
|
+
import { ScalarType, PathType, DataSource, Registry } from '@axi-engine/utils';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Represents an operand that is a direct, static value.
|
|
@@ -227,6 +227,27 @@ type Expression = ExpressionDefinitions[keyof ExpressionDefinitions];
|
|
|
227
227
|
/** The union of all possible expression names (keys). uses in ExpressionHandler */
|
|
228
228
|
type ExpressionName = keyof ExpressionDefinitions;
|
|
229
229
|
|
|
230
|
+
/**
|
|
231
|
+
* A safe utility function that performs a basic mathematical operation on two operands.
|
|
232
|
+
*
|
|
233
|
+
* This function includes built-in type checking. It first ensures
|
|
234
|
+
* that both `left` and `right` operands are numbers before performing the calculation.
|
|
235
|
+
* If the type check fails or if an unsupported operator is provided, it will throw
|
|
236
|
+
* a descriptive error.
|
|
237
|
+
*
|
|
238
|
+
* @param op The mathematical operator to apply ('+', '-', '*', '/').
|
|
239
|
+
* @param left The left-hand operand. It is validated to be a number.
|
|
240
|
+
* @param right The right-hand operand. It is validated to be a number.
|
|
241
|
+
* @returns {number} The numerical result of the calculation.
|
|
242
|
+
* @throws {Error} If either `left` or `right` is not a number.
|
|
243
|
+
* @throws {Error} If the `op` is not a recognized `MathOperationType`.
|
|
244
|
+
*
|
|
245
|
+
* @example
|
|
246
|
+
* const result = resolveMath('+', 10, 5); // returns 15
|
|
247
|
+
* const product = resolveMath('*', 2, 3); // returns 6
|
|
248
|
+
*/
|
|
249
|
+
declare function resolveMath(op: MathOperationType, left: unknown, right: unknown): number;
|
|
250
|
+
|
|
230
251
|
/**
|
|
231
252
|
* Recursively resolves an Operand into its final scalar value.
|
|
232
253
|
*
|
|
@@ -296,7 +317,7 @@ interface ExpressionEvaluatorContext {
|
|
|
296
317
|
*/
|
|
297
318
|
declare class ExpressionEvaluator {
|
|
298
319
|
/** @internal A map of registered expression handlers. */
|
|
299
|
-
handlers:
|
|
320
|
+
handlers: Registry<keyof ExpressionDefinitions, ExpressionHandler<Expression>>;
|
|
300
321
|
/**
|
|
301
322
|
* Registers a new `ExpressionHandler` with the evaluator.
|
|
302
323
|
* This is the primary mechanism for extending the expression language with
|
|
@@ -466,4 +487,4 @@ declare class OrExpressionHandler implements ExpressionHandler<OrExpression> {
|
|
|
466
487
|
*/
|
|
467
488
|
declare function createExpressionEvaluator(additionalHandlers?: ExpressionHandler[]): ExpressionEvaluator;
|
|
468
489
|
|
|
469
|
-
export { type AndExpression, AndExpressionHandler, type ArithmeticOperand, type ChanceExpression, ChanceExpressionHandler, type ComparisonExpression, ComparisonExpressionHandler, type ComparisonOperationType, type ExistsExpression, ExistsExpressionHandler, type Expression, type ExpressionDefinitions, ExpressionEvaluator, type ExpressionEvaluatorContext, type ExpressionHandler, type ExpressionName, type InExpression, InExpressionHandler, type LiteralExpression, LiteralExpressionHandler, type MathOperationType, type NotExpression, NotExpressionHandler, type Operand, type OrExpression, OrExpressionHandler, type ReferenceOperand, type ValueOperand, createExpressionEvaluator, isArithmeticOperand, isOperand, isReferenceOperand, isValueOperand, resolveOperand, resolveOperandAsScalar };
|
|
490
|
+
export { type AndExpression, AndExpressionHandler, type ArithmeticOperand, type ChanceExpression, ChanceExpressionHandler, type ComparisonExpression, ComparisonExpressionHandler, type ComparisonOperationType, type ExistsExpression, ExistsExpressionHandler, type Expression, type ExpressionDefinitions, ExpressionEvaluator, type ExpressionEvaluatorContext, type ExpressionHandler, type ExpressionName, type InExpression, InExpressionHandler, type LiteralExpression, LiteralExpressionHandler, type MathOperationType, type NotExpression, NotExpressionHandler, type Operand, type OrExpression, OrExpressionHandler, type ReferenceOperand, type ValueOperand, createExpressionEvaluator, isArithmeticOperand, isOperand, isReferenceOperand, isValueOperand, resolveMath, resolveOperand, resolveOperandAsScalar };
|
package/dist/index.js
CHANGED
|
@@ -34,6 +34,7 @@ __export(index_exports, {
|
|
|
34
34
|
isOperand: () => isOperand,
|
|
35
35
|
isReferenceOperand: () => isReferenceOperand,
|
|
36
36
|
isValueOperand: () => isValueOperand,
|
|
37
|
+
resolveMath: () => resolveMath,
|
|
37
38
|
resolveOperand: () => resolveOperand,
|
|
38
39
|
resolveOperandAsScalar: () => resolveOperandAsScalar
|
|
39
40
|
});
|
|
@@ -54,8 +55,29 @@ function isOperand(val) {
|
|
|
54
55
|
return isValueOperand(val) || isReferenceOperand(val) || isArithmeticOperand(val);
|
|
55
56
|
}
|
|
56
57
|
|
|
57
|
-
// src/resolve-
|
|
58
|
+
// src/resolve-math.ts
|
|
58
59
|
var import_utils2 = require("@axi-engine/utils");
|
|
60
|
+
function resolveMath(op, left, right) {
|
|
61
|
+
(0, import_utils2.throwIf)(
|
|
62
|
+
!(0, import_utils2.isNumber)(left) || !(0, import_utils2.isNumber)(right),
|
|
63
|
+
`Require number operands, but got ${typeof left} and ${typeof right}.`
|
|
64
|
+
);
|
|
65
|
+
switch (op) {
|
|
66
|
+
case "+":
|
|
67
|
+
return Number(left) + Number(right);
|
|
68
|
+
case "-":
|
|
69
|
+
return Number(left) - Number(right);
|
|
70
|
+
case "*":
|
|
71
|
+
return Number(left) * Number(right);
|
|
72
|
+
case "/":
|
|
73
|
+
return Number(left) / Number(right);
|
|
74
|
+
default:
|
|
75
|
+
return (0, import_utils2.throwError)(`Unknown arithmetic operator: ${op}`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// src/resolve-operand.ts
|
|
80
|
+
var import_utils3 = require("@axi-engine/utils");
|
|
59
81
|
function resolveOperand(op, source) {
|
|
60
82
|
if (isValueOperand(op)) {
|
|
61
83
|
return op.value;
|
|
@@ -66,40 +88,24 @@ function resolveOperand(op, source) {
|
|
|
66
88
|
if (isArithmeticOperand(op)) {
|
|
67
89
|
const leftVal = resolveOperand(op.arithmetic.left, source);
|
|
68
90
|
const rightVal = resolveOperand(op.arithmetic.right, source);
|
|
69
|
-
(
|
|
70
|
-
!(0, import_utils2.isNumber)(leftVal) || !(0, import_utils2.isNumber)(rightVal),
|
|
71
|
-
`Arithmetic operations require number operands, but got ${typeof leftVal} and ${typeof rightVal}.`
|
|
72
|
-
);
|
|
73
|
-
switch (op.arithmetic.op) {
|
|
74
|
-
case "+":
|
|
75
|
-
return Number(leftVal) + Number(rightVal);
|
|
76
|
-
case "-":
|
|
77
|
-
return Number(leftVal) - Number(rightVal);
|
|
78
|
-
case "*":
|
|
79
|
-
return Number(leftVal) * Number(rightVal);
|
|
80
|
-
case "/":
|
|
81
|
-
return Number(leftVal) / Number(rightVal);
|
|
82
|
-
default:
|
|
83
|
-
(0, import_utils2.throwIf)(true, `Unknown arithmetic operator: ${op.arithmetic.op}`);
|
|
84
|
-
}
|
|
91
|
+
return resolveMath(op.arithmetic.op, leftVal, rightVal);
|
|
85
92
|
}
|
|
86
|
-
(0,
|
|
87
|
-
return void 0;
|
|
93
|
+
return (0, import_utils3.throwError)(`Unknown operand type: ${JSON.stringify(op)}`);
|
|
88
94
|
}
|
|
89
95
|
function resolveOperandAsScalar(op, source) {
|
|
90
96
|
const value = resolveOperand(op, source);
|
|
91
|
-
(0,
|
|
92
|
-
!(0,
|
|
97
|
+
(0, import_utils3.throwIf)(
|
|
98
|
+
!(0, import_utils3.isScalar)(value),
|
|
93
99
|
`Expected a scalar value (string, number, boolean), but got ${typeof value}.`
|
|
94
100
|
);
|
|
95
101
|
return value;
|
|
96
102
|
}
|
|
97
103
|
|
|
98
104
|
// src/expression-evaluator.ts
|
|
99
|
-
var
|
|
105
|
+
var import_utils4 = require("@axi-engine/utils");
|
|
100
106
|
var ExpressionEvaluator = class {
|
|
101
107
|
/** @internal A map of registered expression handlers. */
|
|
102
|
-
handlers =
|
|
108
|
+
handlers = new import_utils4.Registry();
|
|
103
109
|
/**
|
|
104
110
|
* Registers a new `ExpressionHandler` with the evaluator.
|
|
105
111
|
* This is the primary mechanism for extending the expression language with
|
|
@@ -110,8 +116,7 @@ var ExpressionEvaluator = class {
|
|
|
110
116
|
* is already registered.
|
|
111
117
|
*/
|
|
112
118
|
register(handler) {
|
|
113
|
-
|
|
114
|
-
this.handlers.set(handler.type, handler);
|
|
119
|
+
this.handlers.register(handler.type, handler);
|
|
115
120
|
}
|
|
116
121
|
/**
|
|
117
122
|
* Resolves a given expression against a data source.
|
|
@@ -127,9 +132,8 @@ var ExpressionEvaluator = class {
|
|
|
127
132
|
* evaluation result.
|
|
128
133
|
*/
|
|
129
134
|
async resolve(expression, data) {
|
|
130
|
-
const key = (0,
|
|
131
|
-
const handler = this.handlers.
|
|
132
|
-
(0, import_utils3.throwIfEmpty)(handler, `Can't find expression handler for: '${key}' expression`);
|
|
135
|
+
const key = (0, import_utils4.firstKeyOf)(expression);
|
|
136
|
+
const handler = this.handlers.getOrThrow(key);
|
|
133
137
|
const context = {
|
|
134
138
|
resolve: (expression2) => this.resolve(expression2, data),
|
|
135
139
|
source: () => data
|
|
@@ -151,7 +155,7 @@ var AndExpressionHandler = class {
|
|
|
151
155
|
};
|
|
152
156
|
|
|
153
157
|
// src/handlers/chance-expression-handler.ts
|
|
154
|
-
var
|
|
158
|
+
var import_utils5 = require("@axi-engine/utils");
|
|
155
159
|
var ChanceExpressionHandler = class {
|
|
156
160
|
type = "chance";
|
|
157
161
|
/**
|
|
@@ -172,16 +176,16 @@ var ChanceExpressionHandler = class {
|
|
|
172
176
|
async resolve(exp, context) {
|
|
173
177
|
const resolvedValue = resolveOperandAsScalar(exp.chance, context.source());
|
|
174
178
|
let numericValue;
|
|
175
|
-
if ((0,
|
|
179
|
+
if ((0, import_utils5.isNumber)(resolvedValue)) {
|
|
176
180
|
numericValue = resolvedValue;
|
|
177
|
-
} else if ((0,
|
|
181
|
+
} else if ((0, import_utils5.isString)(resolvedValue)) {
|
|
178
182
|
const parsed = parseFloat(resolvedValue.replace("%", "").trim());
|
|
179
|
-
(0,
|
|
183
|
+
(0, import_utils5.throwIf)(isNaN(parsed), `Chance value as a string must be a valid number, but got '${resolvedValue}'.`);
|
|
180
184
|
numericValue = parsed;
|
|
181
185
|
} else {
|
|
182
|
-
(0,
|
|
186
|
+
(0, import_utils5.throwError)(`Chance value must be a number or a string, but got a boolean.`);
|
|
183
187
|
}
|
|
184
|
-
const randomRoll = (0,
|
|
188
|
+
const randomRoll = (0, import_utils5.randInt)(0, 100);
|
|
185
189
|
return randomRoll < numericValue;
|
|
186
190
|
}
|
|
187
191
|
};
|
|
@@ -218,7 +222,7 @@ var ExistsExpressionHandler = class {
|
|
|
218
222
|
};
|
|
219
223
|
|
|
220
224
|
// src/handlers/in-expression-handler.ts
|
|
221
|
-
var
|
|
225
|
+
var import_utils6 = require("@axi-engine/utils");
|
|
222
226
|
var InExpressionHandler = class {
|
|
223
227
|
type = "in";
|
|
224
228
|
/**
|
|
@@ -242,13 +246,13 @@ var InExpressionHandler = class {
|
|
|
242
246
|
async resolve(exp, context) {
|
|
243
247
|
const value = resolveOperandAsScalar(exp.in.value, context.source());
|
|
244
248
|
const rawArray = Array.isArray(exp.in.array) ? exp.in.array : resolveOperand(exp.in.array, context.source());
|
|
245
|
-
(0,
|
|
249
|
+
(0, import_utils6.throwIf)(
|
|
246
250
|
!Array.isArray(rawArray),
|
|
247
251
|
`The 'in' expression requires an array, but the provided source resolved to ${typeof rawArray}.`
|
|
248
252
|
);
|
|
249
253
|
const typedArray = rawArray;
|
|
250
254
|
const resolvedArray = typedArray.map(
|
|
251
|
-
(item) => (0,
|
|
255
|
+
(item) => (0, import_utils6.isScalar)(item) ? item : resolveOperandAsScalar(item, context.source())
|
|
252
256
|
);
|
|
253
257
|
return resolvedArray.includes(value);
|
|
254
258
|
}
|
|
@@ -293,9 +297,7 @@ function createExpressionEvaluator(additionalHandlers) {
|
|
|
293
297
|
evaluator.register(new LiteralExpressionHandler());
|
|
294
298
|
evaluator.register(new NotExpressionHandler());
|
|
295
299
|
evaluator.register(new OrExpressionHandler());
|
|
296
|
-
|
|
297
|
-
additionalHandlers.forEach((handler) => evaluator.register(handler));
|
|
298
|
-
}
|
|
300
|
+
additionalHandlers?.forEach((handler) => evaluator.register(handler));
|
|
299
301
|
return evaluator;
|
|
300
302
|
}
|
|
301
303
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -314,6 +316,7 @@ function createExpressionEvaluator(additionalHandlers) {
|
|
|
314
316
|
isOperand,
|
|
315
317
|
isReferenceOperand,
|
|
316
318
|
isValueOperand,
|
|
319
|
+
resolveMath,
|
|
317
320
|
resolveOperand,
|
|
318
321
|
resolveOperandAsScalar
|
|
319
322
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -13,8 +13,29 @@ function isOperand(val) {
|
|
|
13
13
|
return isValueOperand(val) || isReferenceOperand(val) || isArithmeticOperand(val);
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
// src/resolve-math.ts
|
|
17
|
+
import { isNumber, throwError, throwIf } from "@axi-engine/utils";
|
|
18
|
+
function resolveMath(op, left, right) {
|
|
19
|
+
throwIf(
|
|
20
|
+
!isNumber(left) || !isNumber(right),
|
|
21
|
+
`Require number operands, but got ${typeof left} and ${typeof right}.`
|
|
22
|
+
);
|
|
23
|
+
switch (op) {
|
|
24
|
+
case "+":
|
|
25
|
+
return Number(left) + Number(right);
|
|
26
|
+
case "-":
|
|
27
|
+
return Number(left) - Number(right);
|
|
28
|
+
case "*":
|
|
29
|
+
return Number(left) * Number(right);
|
|
30
|
+
case "/":
|
|
31
|
+
return Number(left) / Number(right);
|
|
32
|
+
default:
|
|
33
|
+
return throwError(`Unknown arithmetic operator: ${op}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
16
37
|
// src/resolve-operand.ts
|
|
17
|
-
import {
|
|
38
|
+
import { isScalar, throwError as throwError2, throwIf as throwIf2 } from "@axi-engine/utils";
|
|
18
39
|
function resolveOperand(op, source) {
|
|
19
40
|
if (isValueOperand(op)) {
|
|
20
41
|
return op.value;
|
|
@@ -25,29 +46,13 @@ function resolveOperand(op, source) {
|
|
|
25
46
|
if (isArithmeticOperand(op)) {
|
|
26
47
|
const leftVal = resolveOperand(op.arithmetic.left, source);
|
|
27
48
|
const rightVal = resolveOperand(op.arithmetic.right, source);
|
|
28
|
-
|
|
29
|
-
!isNumber(leftVal) || !isNumber(rightVal),
|
|
30
|
-
`Arithmetic operations require number operands, but got ${typeof leftVal} and ${typeof rightVal}.`
|
|
31
|
-
);
|
|
32
|
-
switch (op.arithmetic.op) {
|
|
33
|
-
case "+":
|
|
34
|
-
return Number(leftVal) + Number(rightVal);
|
|
35
|
-
case "-":
|
|
36
|
-
return Number(leftVal) - Number(rightVal);
|
|
37
|
-
case "*":
|
|
38
|
-
return Number(leftVal) * Number(rightVal);
|
|
39
|
-
case "/":
|
|
40
|
-
return Number(leftVal) / Number(rightVal);
|
|
41
|
-
default:
|
|
42
|
-
throwIf(true, `Unknown arithmetic operator: ${op.arithmetic.op}`);
|
|
43
|
-
}
|
|
49
|
+
return resolveMath(op.arithmetic.op, leftVal, rightVal);
|
|
44
50
|
}
|
|
45
|
-
|
|
46
|
-
return void 0;
|
|
51
|
+
return throwError2(`Unknown operand type: ${JSON.stringify(op)}`);
|
|
47
52
|
}
|
|
48
53
|
function resolveOperandAsScalar(op, source) {
|
|
49
54
|
const value = resolveOperand(op, source);
|
|
50
|
-
|
|
55
|
+
throwIf2(
|
|
51
56
|
!isScalar(value),
|
|
52
57
|
`Expected a scalar value (string, number, boolean), but got ${typeof value}.`
|
|
53
58
|
);
|
|
@@ -56,13 +61,12 @@ function resolveOperandAsScalar(op, source) {
|
|
|
56
61
|
|
|
57
62
|
// src/expression-evaluator.ts
|
|
58
63
|
import {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
firstKeyOf
|
|
64
|
+
firstKeyOf,
|
|
65
|
+
Registry
|
|
62
66
|
} from "@axi-engine/utils";
|
|
63
67
|
var ExpressionEvaluator = class {
|
|
64
68
|
/** @internal A map of registered expression handlers. */
|
|
65
|
-
handlers =
|
|
69
|
+
handlers = new Registry();
|
|
66
70
|
/**
|
|
67
71
|
* Registers a new `ExpressionHandler` with the evaluator.
|
|
68
72
|
* This is the primary mechanism for extending the expression language with
|
|
@@ -73,8 +77,7 @@ var ExpressionEvaluator = class {
|
|
|
73
77
|
* is already registered.
|
|
74
78
|
*/
|
|
75
79
|
register(handler) {
|
|
76
|
-
|
|
77
|
-
this.handlers.set(handler.type, handler);
|
|
80
|
+
this.handlers.register(handler.type, handler);
|
|
78
81
|
}
|
|
79
82
|
/**
|
|
80
83
|
* Resolves a given expression against a data source.
|
|
@@ -91,8 +94,7 @@ var ExpressionEvaluator = class {
|
|
|
91
94
|
*/
|
|
92
95
|
async resolve(expression, data) {
|
|
93
96
|
const key = firstKeyOf(expression);
|
|
94
|
-
const handler = this.handlers.
|
|
95
|
-
throwIfEmpty(handler, `Can't find expression handler for: '${key}' expression`);
|
|
97
|
+
const handler = this.handlers.getOrThrow(key);
|
|
96
98
|
const context = {
|
|
97
99
|
resolve: (expression2) => this.resolve(expression2, data),
|
|
98
100
|
source: () => data
|
|
@@ -114,7 +116,7 @@ var AndExpressionHandler = class {
|
|
|
114
116
|
};
|
|
115
117
|
|
|
116
118
|
// src/handlers/chance-expression-handler.ts
|
|
117
|
-
import { isNumber as isNumber2, isString, randInt, throwIf as throwIf3 } from "@axi-engine/utils";
|
|
119
|
+
import { isNumber as isNumber2, isString, randInt, throwError as throwError3, throwIf as throwIf3 } from "@axi-engine/utils";
|
|
118
120
|
var ChanceExpressionHandler = class {
|
|
119
121
|
type = "chance";
|
|
120
122
|
/**
|
|
@@ -142,7 +144,7 @@ var ChanceExpressionHandler = class {
|
|
|
142
144
|
throwIf3(isNaN(parsed), `Chance value as a string must be a valid number, but got '${resolvedValue}'.`);
|
|
143
145
|
numericValue = parsed;
|
|
144
146
|
} else {
|
|
145
|
-
|
|
147
|
+
throwError3(`Chance value must be a number or a string, but got a boolean.`);
|
|
146
148
|
}
|
|
147
149
|
const randomRoll = randInt(0, 100);
|
|
148
150
|
return randomRoll < numericValue;
|
|
@@ -256,9 +258,7 @@ function createExpressionEvaluator(additionalHandlers) {
|
|
|
256
258
|
evaluator.register(new LiteralExpressionHandler());
|
|
257
259
|
evaluator.register(new NotExpressionHandler());
|
|
258
260
|
evaluator.register(new OrExpressionHandler());
|
|
259
|
-
|
|
260
|
-
additionalHandlers.forEach((handler) => evaluator.register(handler));
|
|
261
|
-
}
|
|
261
|
+
additionalHandlers?.forEach((handler) => evaluator.register(handler));
|
|
262
262
|
return evaluator;
|
|
263
263
|
}
|
|
264
264
|
export {
|
|
@@ -276,6 +276,7 @@ export {
|
|
|
276
276
|
isOperand,
|
|
277
277
|
isReferenceOperand,
|
|
278
278
|
isValueOperand,
|
|
279
|
+
resolveMath,
|
|
279
280
|
resolveOperand,
|
|
280
281
|
resolveOperandAsScalar
|
|
281
282
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@axi-engine/expressions",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -24,18 +24,18 @@
|
|
|
24
24
|
}
|
|
25
25
|
},
|
|
26
26
|
"scripts": {
|
|
27
|
-
"test": "vitest run --root ../../ src/",
|
|
28
27
|
"build": "tsup",
|
|
29
28
|
"prebuild": "npm test",
|
|
29
|
+
"test": "vitest run --root ../../ src/",
|
|
30
30
|
"docs": "typedoc src/index.ts --out docs/api --options ../../typedoc.json"
|
|
31
31
|
},
|
|
32
32
|
"files": [
|
|
33
33
|
"dist"
|
|
34
34
|
],
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@axi-engine/utils": "^0.2.
|
|
36
|
+
"@axi-engine/utils": "^0.2.6"
|
|
37
37
|
},
|
|
38
38
|
"peerDependencies": {
|
|
39
|
-
"@axi-engine/utils": "^0.2.
|
|
39
|
+
"@axi-engine/utils": "^0.2.6"
|
|
40
40
|
}
|
|
41
41
|
}
|