@axi-engine/expressions 0.2.2 → 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 +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +3 -5
- package/dist/index.mjs +9 -12
- package/package.json +3 -3
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.
|
|
@@ -317,7 +317,7 @@ interface ExpressionEvaluatorContext {
|
|
|
317
317
|
*/
|
|
318
318
|
declare class ExpressionEvaluator {
|
|
319
319
|
/** @internal A map of registered expression handlers. */
|
|
320
|
-
handlers:
|
|
320
|
+
handlers: Registry<keyof ExpressionDefinitions, ExpressionHandler<Expression>>;
|
|
321
321
|
/**
|
|
322
322
|
* Registers a new `ExpressionHandler` with the evaluator.
|
|
323
323
|
* This is the primary mechanism for extending the expression language with
|
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.
|
|
@@ -317,7 +317,7 @@ interface ExpressionEvaluatorContext {
|
|
|
317
317
|
*/
|
|
318
318
|
declare class ExpressionEvaluator {
|
|
319
319
|
/** @internal A map of registered expression handlers. */
|
|
320
|
-
handlers:
|
|
320
|
+
handlers: Registry<keyof ExpressionDefinitions, ExpressionHandler<Expression>>;
|
|
321
321
|
/**
|
|
322
322
|
* Registers a new `ExpressionHandler` with the evaluator.
|
|
323
323
|
* This is the primary mechanism for extending the expression language with
|
package/dist/index.js
CHANGED
|
@@ -105,7 +105,7 @@ function resolveOperandAsScalar(op, source) {
|
|
|
105
105
|
var import_utils4 = require("@axi-engine/utils");
|
|
106
106
|
var ExpressionEvaluator = class {
|
|
107
107
|
/** @internal A map of registered expression handlers. */
|
|
108
|
-
handlers =
|
|
108
|
+
handlers = new import_utils4.Registry();
|
|
109
109
|
/**
|
|
110
110
|
* Registers a new `ExpressionHandler` with the evaluator.
|
|
111
111
|
* This is the primary mechanism for extending the expression language with
|
|
@@ -116,8 +116,7 @@ var ExpressionEvaluator = class {
|
|
|
116
116
|
* is already registered.
|
|
117
117
|
*/
|
|
118
118
|
register(handler) {
|
|
119
|
-
|
|
120
|
-
this.handlers.set(handler.type, handler);
|
|
119
|
+
this.handlers.register(handler.type, handler);
|
|
121
120
|
}
|
|
122
121
|
/**
|
|
123
122
|
* Resolves a given expression against a data source.
|
|
@@ -134,8 +133,7 @@ var ExpressionEvaluator = class {
|
|
|
134
133
|
*/
|
|
135
134
|
async resolve(expression, data) {
|
|
136
135
|
const key = (0, import_utils4.firstKeyOf)(expression);
|
|
137
|
-
const handler = this.handlers.
|
|
138
|
-
(0, import_utils4.throwIfEmpty)(handler, `Can't find expression handler for: '${key}' expression`);
|
|
136
|
+
const handler = this.handlers.getOrThrow(key);
|
|
139
137
|
const context = {
|
|
140
138
|
resolve: (expression2) => this.resolve(expression2, data),
|
|
141
139
|
source: () => data
|
package/dist/index.mjs
CHANGED
|
@@ -61,13 +61,12 @@ function resolveOperandAsScalar(op, source) {
|
|
|
61
61
|
|
|
62
62
|
// src/expression-evaluator.ts
|
|
63
63
|
import {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
firstKeyOf
|
|
64
|
+
firstKeyOf,
|
|
65
|
+
Registry
|
|
67
66
|
} from "@axi-engine/utils";
|
|
68
67
|
var ExpressionEvaluator = class {
|
|
69
68
|
/** @internal A map of registered expression handlers. */
|
|
70
|
-
handlers =
|
|
69
|
+
handlers = new Registry();
|
|
71
70
|
/**
|
|
72
71
|
* Registers a new `ExpressionHandler` with the evaluator.
|
|
73
72
|
* This is the primary mechanism for extending the expression language with
|
|
@@ -78,8 +77,7 @@ var ExpressionEvaluator = class {
|
|
|
78
77
|
* is already registered.
|
|
79
78
|
*/
|
|
80
79
|
register(handler) {
|
|
81
|
-
|
|
82
|
-
this.handlers.set(handler.type, handler);
|
|
80
|
+
this.handlers.register(handler.type, handler);
|
|
83
81
|
}
|
|
84
82
|
/**
|
|
85
83
|
* Resolves a given expression against a data source.
|
|
@@ -96,8 +94,7 @@ var ExpressionEvaluator = class {
|
|
|
96
94
|
*/
|
|
97
95
|
async resolve(expression, data) {
|
|
98
96
|
const key = firstKeyOf(expression);
|
|
99
|
-
const handler = this.handlers.
|
|
100
|
-
throwIfEmpty(handler, `Can't find expression handler for: '${key}' expression`);
|
|
97
|
+
const handler = this.handlers.getOrThrow(key);
|
|
101
98
|
const context = {
|
|
102
99
|
resolve: (expression2) => this.resolve(expression2, data),
|
|
103
100
|
source: () => data
|
|
@@ -119,7 +116,7 @@ var AndExpressionHandler = class {
|
|
|
119
116
|
};
|
|
120
117
|
|
|
121
118
|
// src/handlers/chance-expression-handler.ts
|
|
122
|
-
import { isNumber as isNumber2, isString, randInt, throwError as throwError3, throwIf as
|
|
119
|
+
import { isNumber as isNumber2, isString, randInt, throwError as throwError3, throwIf as throwIf3 } from "@axi-engine/utils";
|
|
123
120
|
var ChanceExpressionHandler = class {
|
|
124
121
|
type = "chance";
|
|
125
122
|
/**
|
|
@@ -144,7 +141,7 @@ var ChanceExpressionHandler = class {
|
|
|
144
141
|
numericValue = resolvedValue;
|
|
145
142
|
} else if (isString(resolvedValue)) {
|
|
146
143
|
const parsed = parseFloat(resolvedValue.replace("%", "").trim());
|
|
147
|
-
|
|
144
|
+
throwIf3(isNaN(parsed), `Chance value as a string must be a valid number, but got '${resolvedValue}'.`);
|
|
148
145
|
numericValue = parsed;
|
|
149
146
|
} else {
|
|
150
147
|
throwError3(`Chance value must be a number or a string, but got a boolean.`);
|
|
@@ -186,7 +183,7 @@ var ExistsExpressionHandler = class {
|
|
|
186
183
|
};
|
|
187
184
|
|
|
188
185
|
// src/handlers/in-expression-handler.ts
|
|
189
|
-
import { isScalar as isScalar2, throwIf as
|
|
186
|
+
import { isScalar as isScalar2, throwIf as throwIf4 } from "@axi-engine/utils";
|
|
190
187
|
var InExpressionHandler = class {
|
|
191
188
|
type = "in";
|
|
192
189
|
/**
|
|
@@ -210,7 +207,7 @@ var InExpressionHandler = class {
|
|
|
210
207
|
async resolve(exp, context) {
|
|
211
208
|
const value = resolveOperandAsScalar(exp.in.value, context.source());
|
|
212
209
|
const rawArray = Array.isArray(exp.in.array) ? exp.in.array : resolveOperand(exp.in.array, context.source());
|
|
213
|
-
|
|
210
|
+
throwIf4(
|
|
214
211
|
!Array.isArray(rawArray),
|
|
215
212
|
`The 'in' expression requires an array, but the provided source resolved to ${typeof rawArray}.`
|
|
216
213
|
);
|
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": {
|
|
@@ -33,9 +33,9 @@
|
|
|
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
|
}
|