@jbrowse/jexl 3.2.0 → 3.3.0
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/Jexl.d.ts +6 -6
- package/dist/Jexl.js +7 -1
- package/dist/Jexl.js.map +1 -1
- package/dist/grammar.d.ts +19 -2
- package/dist/grammar.js.map +1 -1
- package/esm/Jexl.d.ts +6 -6
- package/esm/Jexl.js +7 -1
- package/esm/Jexl.js.map +1 -1
- package/esm/grammar.d.ts +19 -2
- package/esm/grammar.js.map +1 -1
- package/package.json +1 -1
- package/src/Jexl.ts +18 -9
- package/src/grammar.ts +22 -2
package/dist/Jexl.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import Expression from './Expression.ts';
|
|
2
2
|
import Lexer from './Lexer.ts';
|
|
3
|
-
import type {
|
|
3
|
+
import type { BinaryOpEvalOnDemand, Grammar, GrammarElement, GrammarFn, UncheckedFn } from './grammar.ts';
|
|
4
4
|
import type { JexlValue } from './types.ts';
|
|
5
5
|
/**
|
|
6
6
|
* Jexl is the Javascript Expression Language, capable of parsing and
|
|
@@ -31,7 +31,7 @@ declare class Jexl {
|
|
|
31
31
|
* left.eval() or right.eval() will return that operand's actual value.
|
|
32
32
|
* This is useful to conditionally evaluate operands.
|
|
33
33
|
*/
|
|
34
|
-
addBinaryOp(operator: string, precedence: number, fn:
|
|
34
|
+
addBinaryOp(operator: string, precedence: number, fn: UncheckedFn, manualEval?: false): void;
|
|
35
35
|
addBinaryOp(operator: string, precedence: number, fn: BinaryOpEvalOnDemand, manualEval: true): void;
|
|
36
36
|
/**
|
|
37
37
|
* Adds or replaces an expression function in this Jexl instance.
|
|
@@ -41,14 +41,14 @@ declare class Jexl {
|
|
|
41
41
|
* expression function is invoked. It will be provided with each argument
|
|
42
42
|
* supplied in the expression, in the same order.
|
|
43
43
|
*/
|
|
44
|
-
addFunction(name: string, fn:
|
|
44
|
+
addFunction(name: string, fn: UncheckedFn): void;
|
|
45
45
|
/**
|
|
46
46
|
* Syntactic sugar for calling {@link #addFunction} repeatedly. This function
|
|
47
47
|
* accepts a map of one or more expression function names to their javascript
|
|
48
48
|
* function counterpart.
|
|
49
49
|
* @param {{}} map A map of expression function names to javascript functions
|
|
50
50
|
*/
|
|
51
|
-
addFunctions(map: Record<string,
|
|
51
|
+
addFunctions(map: Record<string, UncheckedFn>): void;
|
|
52
52
|
/**
|
|
53
53
|
* Adds a unary operator to Jexl. Unary operators are currently only supported
|
|
54
54
|
* on the left side of the value on which it will operate.
|
|
@@ -57,7 +57,7 @@ declare class Jexl {
|
|
|
57
57
|
* will be called with one argument: the literal value to the right of the
|
|
58
58
|
* operator. It should return the resulting value.
|
|
59
59
|
*/
|
|
60
|
-
addUnaryOp(operator: string, fn:
|
|
60
|
+
addUnaryOp(operator: string, fn: UncheckedFn): void;
|
|
61
61
|
/**
|
|
62
62
|
* Creates an Expression object from the given Jexl expression string, and
|
|
63
63
|
* immediately compiles it. The returned Expression object can then be
|
|
@@ -79,7 +79,7 @@ declare class Jexl {
|
|
|
79
79
|
* @param {string} name The name of the expression function
|
|
80
80
|
* @returns {function} The expression function
|
|
81
81
|
*/
|
|
82
|
-
getFunction(name: string):
|
|
82
|
+
getFunction(name: string): GrammarFn | undefined;
|
|
83
83
|
/**
|
|
84
84
|
* Evaluates a Jexl string within an optional context.
|
|
85
85
|
* @param {string} expression The Jexl expression to be evaluated
|
package/dist/Jexl.js
CHANGED
|
@@ -36,7 +36,11 @@ class Jexl {
|
|
|
36
36
|
precedence,
|
|
37
37
|
evalOnDemand: fn
|
|
38
38
|
}
|
|
39
|
-
: {
|
|
39
|
+
: {
|
|
40
|
+
type: 'binaryOp',
|
|
41
|
+
precedence,
|
|
42
|
+
eval: fn
|
|
43
|
+
});
|
|
40
44
|
}
|
|
41
45
|
/**
|
|
42
46
|
* Adds or replaces an expression function in this Jexl instance.
|
|
@@ -47,6 +51,8 @@ class Jexl {
|
|
|
47
51
|
* supplied in the expression, in the same order.
|
|
48
52
|
*/
|
|
49
53
|
addFunction(name, fn) {
|
|
54
|
+
// jexl hands the function whatever the operands evaluated to, which it
|
|
55
|
+
// cannot reconcile with the caller's parameter types — see UncheckedFn
|
|
50
56
|
this._grammar.functions[name] = fn;
|
|
51
57
|
}
|
|
52
58
|
/**
|
package/dist/Jexl.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Jexl.js","sourceRoot":"","sources":["../src/Jexl.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;AAEH,oEAAwC;AACxC,0DAA8B;AAC9B,6CAAyC;
|
|
1
|
+
{"version":3,"file":"Jexl.js","sourceRoot":"","sources":["../src/Jexl.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;AAEH,oEAAwC;AACxC,0DAA8B;AAC9B,6CAAyC;AAazC;;;;;GAKG;AACH,MAAM,IAAI;IACR,QAAQ,CAAS;IACjB,0EAA0E;IAC1E,gEAAgE;IAChE,MAAM,CAAO;IAEb;QACE,IAAI,CAAC,QAAQ,GAAG,IAAA,uBAAU,GAAE,CAAA;QAC5B,IAAI,CAAC,MAAM,GAAG,IAAI,kBAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;QACtC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAClC,CAAC;IAiCD,WAAW,CACT,QAAgB,EAChB,UAAkB,EAClB,EAAsC,EACtC,UAAoB;QAEpB,sEAAsE;QACtE,iEAAiE;QACjE,IAAI,CAAC,kBAAkB,CACrB,QAAQ,EACR,UAAU;YACR,CAAC,CAAC;gBACE,IAAI,EAAE,UAAU;gBAChB,UAAU;gBACV,YAAY,EAAE,EAA0B;aACzC;YACH,CAAC,CAAC;gBACE,IAAI,EAAE,UAAU;gBAChB,UAAU;gBACV,IAAI,EAAE,EAA6B;aACpC,CACN,CAAA;IACH,CAAC;IAED;;;;;;;OAOG;IACH,WAAW,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/B,kBAAe,YAAY,CAAA"}
|
package/dist/grammar.d.ts
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
import type { JexlValue } from './types.ts';
|
|
2
2
|
export type BinaryOpEval = (left: JexlValue, right: JexlValue) => JexlValue;
|
|
3
|
+
/**
|
|
4
|
+
* A function or operator implementation as its author writes it, for the
|
|
5
|
+
* registration methods on {@link Jexl} to accept.
|
|
6
|
+
*
|
|
7
|
+
* Jexl cannot check what a registered callback will be handed. The arguments
|
|
8
|
+
* are whatever the expression evaluated to, drawn from a context supplied at
|
|
9
|
+
* evaluation time, and that context routinely holds values jexl has no literal
|
|
10
|
+
* for — a host object with methods, a callback, a class instance. Requiring
|
|
11
|
+
* {@link JexlValue} parameters therefore rejected every callback actually
|
|
12
|
+
* written against such a value, since parameters are contravariant. The
|
|
13
|
+
* argument types are the caller's business; jexl only promises to pass along
|
|
14
|
+
* whatever the operands evaluated to.
|
|
15
|
+
*/
|
|
16
|
+
export type UncheckedFn = (...args: never[]) => unknown;
|
|
3
17
|
export type BinaryOpEvalOnDemand = (left: {
|
|
4
18
|
eval: () => JexlValue;
|
|
5
19
|
}, right: {
|
|
@@ -17,10 +31,11 @@ export interface BinaryOp {
|
|
|
17
31
|
*/
|
|
18
32
|
unaryEval?: (right: JexlValue) => JexlValue;
|
|
19
33
|
}
|
|
34
|
+
export type UnaryOpEval = (right: JexlValue) => JexlValue;
|
|
20
35
|
export interface UnaryOp {
|
|
21
36
|
type: 'unaryOp';
|
|
22
37
|
precedence: number;
|
|
23
|
-
eval:
|
|
38
|
+
eval: UnaryOpEval;
|
|
24
39
|
}
|
|
25
40
|
/**
|
|
26
41
|
* The punctuation elements of the grammar. Unlike operators these carry no
|
|
@@ -32,9 +47,11 @@ export interface SimpleElement {
|
|
|
32
47
|
type: 'dot' | 'openBracket' | 'closeBracket' | 'openCurl' | 'closeCurl' | 'colon' | 'comma' | 'openParen' | 'closeParen' | 'question' | 'semicolon';
|
|
33
48
|
}
|
|
34
49
|
export type GrammarElement = BinaryOp | UnaryOp | SimpleElement;
|
|
50
|
+
/** A registered function, as jexl calls it once the operands are evaluated. */
|
|
51
|
+
export type GrammarFn = (...args: JexlValue[]) => JexlValue;
|
|
35
52
|
export interface Grammar {
|
|
36
53
|
elements: Record<string, GrammarElement>;
|
|
37
|
-
functions: Record<string,
|
|
54
|
+
functions: Record<string, GrammarFn>;
|
|
38
55
|
}
|
|
39
56
|
/**
|
|
40
57
|
* Returns the binding power of a grammar element, or 0 for elements that
|
package/dist/grammar.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"grammar.js","sourceRoot":"","sources":["../src/grammar.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;
|
|
1
|
+
{"version":3,"file":"grammar.js","sourceRoot":"","sources":["../src/grammar.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAoFH,oCAIC;AARD;;;GAGG;AACH,SAAgB,YAAY,CAAC,IAAgC;IAC3D,OAAO,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,KAAK,UAAU,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS,CAAC;QAClE,CAAC,CAAC,IAAI,CAAC,UAAU;QACjB,CAAC,CAAC,CAAC,CAAA;AACP,CAAC;AAEM,MAAM,UAAU,GAAG,GAAY,EAAE,CAAC,CAAC;IACxC;;;;OAIG;IACH,QAAQ,EAAE;QACR,GAAG,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE;QACpB,GAAG,EAAE,EAAE,IAAI,EAAE,aAAa,EAAE;QAC5B,GAAG,EAAE,EAAE,IAAI,EAAE,cAAc,EAAE;QAC7B,GAAG,EAAE,EAAE,IAAI,EAAE,UAAU,EAAE;QACzB,GAAG,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE;QAC1B,GAAG,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE;QACtB,GAAG,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE;QACtB,GAAG,EAAE,EAAE,IAAI,EA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A8B,CAAC,CAAA;YACjD,CAAC;SACF;KACF;IAED;;;;;;;;;;;;;OAaG;IACH,SAAS,EAAE,EAAE;CACd,CAAC,CAAA;AAzJW,QAAA,UAAU,cAyJrB"}
|
package/esm/Jexl.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import Expression from './Expression.ts';
|
|
2
2
|
import Lexer from './Lexer.ts';
|
|
3
|
-
import type {
|
|
3
|
+
import type { BinaryOpEvalOnDemand, Grammar, GrammarElement, GrammarFn, UncheckedFn } from './grammar.ts';
|
|
4
4
|
import type { JexlValue } from './types.ts';
|
|
5
5
|
/**
|
|
6
6
|
* Jexl is the Javascript Expression Language, capable of parsing and
|
|
@@ -31,7 +31,7 @@ declare class Jexl {
|
|
|
31
31
|
* left.eval() or right.eval() will return that operand's actual value.
|
|
32
32
|
* This is useful to conditionally evaluate operands.
|
|
33
33
|
*/
|
|
34
|
-
addBinaryOp(operator: string, precedence: number, fn:
|
|
34
|
+
addBinaryOp(operator: string, precedence: number, fn: UncheckedFn, manualEval?: false): void;
|
|
35
35
|
addBinaryOp(operator: string, precedence: number, fn: BinaryOpEvalOnDemand, manualEval: true): void;
|
|
36
36
|
/**
|
|
37
37
|
* Adds or replaces an expression function in this Jexl instance.
|
|
@@ -41,14 +41,14 @@ declare class Jexl {
|
|
|
41
41
|
* expression function is invoked. It will be provided with each argument
|
|
42
42
|
* supplied in the expression, in the same order.
|
|
43
43
|
*/
|
|
44
|
-
addFunction(name: string, fn:
|
|
44
|
+
addFunction(name: string, fn: UncheckedFn): void;
|
|
45
45
|
/**
|
|
46
46
|
* Syntactic sugar for calling {@link #addFunction} repeatedly. This function
|
|
47
47
|
* accepts a map of one or more expression function names to their javascript
|
|
48
48
|
* function counterpart.
|
|
49
49
|
* @param {{}} map A map of expression function names to javascript functions
|
|
50
50
|
*/
|
|
51
|
-
addFunctions(map: Record<string,
|
|
51
|
+
addFunctions(map: Record<string, UncheckedFn>): void;
|
|
52
52
|
/**
|
|
53
53
|
* Adds a unary operator to Jexl. Unary operators are currently only supported
|
|
54
54
|
* on the left side of the value on which it will operate.
|
|
@@ -57,7 +57,7 @@ declare class Jexl {
|
|
|
57
57
|
* will be called with one argument: the literal value to the right of the
|
|
58
58
|
* operator. It should return the resulting value.
|
|
59
59
|
*/
|
|
60
|
-
addUnaryOp(operator: string, fn:
|
|
60
|
+
addUnaryOp(operator: string, fn: UncheckedFn): void;
|
|
61
61
|
/**
|
|
62
62
|
* Creates an Expression object from the given Jexl expression string, and
|
|
63
63
|
* immediately compiles it. The returned Expression object can then be
|
|
@@ -79,7 +79,7 @@ declare class Jexl {
|
|
|
79
79
|
* @param {string} name The name of the expression function
|
|
80
80
|
* @returns {function} The expression function
|
|
81
81
|
*/
|
|
82
|
-
getFunction(name: string):
|
|
82
|
+
getFunction(name: string): GrammarFn | undefined;
|
|
83
83
|
/**
|
|
84
84
|
* Evaluates a Jexl string within an optional context.
|
|
85
85
|
* @param {string} expression The Jexl expression to be evaluated
|
package/esm/Jexl.js
CHANGED
|
@@ -30,7 +30,11 @@ class Jexl {
|
|
|
30
30
|
precedence,
|
|
31
31
|
evalOnDemand: fn
|
|
32
32
|
}
|
|
33
|
-
: {
|
|
33
|
+
: {
|
|
34
|
+
type: 'binaryOp',
|
|
35
|
+
precedence,
|
|
36
|
+
eval: fn
|
|
37
|
+
});
|
|
34
38
|
}
|
|
35
39
|
/**
|
|
36
40
|
* Adds or replaces an expression function in this Jexl instance.
|
|
@@ -41,6 +45,8 @@ class Jexl {
|
|
|
41
45
|
* supplied in the expression, in the same order.
|
|
42
46
|
*/
|
|
43
47
|
addFunction(name, fn) {
|
|
48
|
+
// jexl hands the function whatever the operands evaluated to, which it
|
|
49
|
+
// cannot reconcile with the caller's parameter types — see UncheckedFn
|
|
44
50
|
this._grammar.functions[name] = fn;
|
|
45
51
|
}
|
|
46
52
|
/**
|
package/esm/Jexl.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Jexl.js","sourceRoot":"","sources":["../src/Jexl.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,UAAU,MAAM,iBAAiB,CAAA;AACxC,OAAO,KAAK,MAAM,YAAY,CAAA;AAC9B,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;
|
|
1
|
+
{"version":3,"file":"Jexl.js","sourceRoot":"","sources":["../src/Jexl.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,UAAU,MAAM,iBAAiB,CAAA;AACxC,OAAO,KAAK,MAAM,YAAY,CAAA;AAC9B,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AAazC;;;;;GAKG;AACH,MAAM,IAAI;IACR,QAAQ,CAAS;IACjB,0EAA0E;IAC1E,gEAAgE;IAChE,MAAM,CAAO;IAEb;QACE,IAAI,CAAC,QAAQ,GAAG,UAAU,EAAE,CAAA;QAC5B,IAAI,CAAC,MAAM,GAAG,IAAI,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;QACtC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAClC,CAAC;IAiCD,WAAW,CACT,QAAgB,EAChB,UAAkB,EAClB,EAAsC,EACtC,UAAoB;QAEpB,sEAAsE;QACtE,iEAAiE;QACjE,IAAI,CAAC,kBAAkB,CACrB,QAAQ,EACR,UAAU;YACR,CAAC,CAAC;gBACE,IAAI,EAAE,UAAU;gBAChB,UAAU;gBACV,YAAY,EAAE,EAA0B;aACzC;YACH,CAAC,CAAC;gBACE,IAAI,EAAE,UAAU;gBAChB,UAAU;gBACV,IAAI,EAAE,EAA6B;aACpC,CACN,CAAA;IACH,CAAC;IAED;;;;;;;OAOG;IACH,WAAW,CAAC,IAAY,EAAE,EAAe;QACvC,uEAAuE;QACvE,uEAAuE;QACvE,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,EAA0B,CAAA;IAC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
2
2
|
export type BinaryOpEval = (left: JexlValue, right: JexlValue) => JexlValue;
|
|
3
|
+
/**
|
|
4
|
+
* A function or operator implementation as its author writes it, for the
|
|
5
|
+
* registration methods on {@link Jexl} to accept.
|
|
6
|
+
*
|
|
7
|
+
* Jexl cannot check what a registered callback will be handed. The arguments
|
|
8
|
+
* are whatever the expression evaluated to, drawn from a context supplied at
|
|
9
|
+
* evaluation time, and that context routinely holds values jexl has no literal
|
|
10
|
+
* for — a host object with methods, a callback, a class instance. Requiring
|
|
11
|
+
* {@link JexlValue} parameters therefore rejected every callback actually
|
|
12
|
+
* written against such a value, since parameters are contravariant. The
|
|
13
|
+
* argument types are the caller's business; jexl only promises to pass along
|
|
14
|
+
* whatever the operands evaluated to.
|
|
15
|
+
*/
|
|
16
|
+
export type UncheckedFn = (...args: never[]) => unknown;
|
|
3
17
|
export type BinaryOpEvalOnDemand = (left: {
|
|
4
18
|
eval: () => JexlValue;
|
|
5
19
|
}, right: {
|
|
@@ -17,10 +31,11 @@ export interface BinaryOp {
|
|
|
17
31
|
*/
|
|
18
32
|
unaryEval?: (right: JexlValue) => JexlValue;
|
|
19
33
|
}
|
|
34
|
+
export type UnaryOpEval = (right: JexlValue) => JexlValue;
|
|
20
35
|
export interface UnaryOp {
|
|
21
36
|
type: 'unaryOp';
|
|
22
37
|
precedence: number;
|
|
23
|
-
eval:
|
|
38
|
+
eval: UnaryOpEval;
|
|
24
39
|
}
|
|
25
40
|
/**
|
|
26
41
|
* The punctuation elements of the grammar. Unlike operators these carry no
|
|
@@ -32,9 +47,11 @@ export interface SimpleElement {
|
|
|
32
47
|
type: 'dot' | 'openBracket' | 'closeBracket' | 'openCurl' | 'closeCurl' | 'colon' | 'comma' | 'openParen' | 'closeParen' | 'question' | 'semicolon';
|
|
33
48
|
}
|
|
34
49
|
export type GrammarElement = BinaryOp | UnaryOp | SimpleElement;
|
|
50
|
+
/** A registered function, as jexl calls it once the operands are evaluated. */
|
|
51
|
+
export type GrammarFn = (...args: JexlValue[]) => JexlValue;
|
|
35
52
|
export interface Grammar {
|
|
36
53
|
elements: Record<string, GrammarElement>;
|
|
37
|
-
functions: Record<string,
|
|
54
|
+
functions: Record<string, GrammarFn>;
|
|
38
55
|
}
|
|
39
56
|
/**
|
|
40
57
|
* Returns the binding power of a grammar element, or 0 for elements that
|
package/esm/grammar.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"grammar.js","sourceRoot":"","sources":["../src/grammar.ts"],"names":[],"mappings":"AAAA;;;GAGG;
|
|
1
|
+
{"version":3,"file":"grammar.js","sourceRoot":"","sources":["../src/grammar.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAgFH;;;GAGG;AACH,MAAM,UAAU,YAAY,CAAC,IAAgC;IAC3D,OAAO,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,KAAK,UAAU,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS,CAAC;QAClE,CAAC,CAAC,IAAI,CAAC,UAAU;QACjB,CAAC,CAAC,CAAC,CAAA;AACP,CAAC;AAED,MAAM,CAAC,MAAM,UAAU,GAAG,GAAY,EAAE,CAAC,CAAC;IACxC;;;;OAIG;IACH,QAAQ,EAAE;QACR,GAAG,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE;QACpB,GAAG,EAAE,EAAE,IAAI,EAAE,aAAa,EAAE;QAC5B,GAAG,EAAE,EAAE,IAAI,EAAE,cAAc,EAAE;QAC7B,GAAG,EAAE,EAAE,IAAI,EAAE,UAAU,EAAE;QACzB,GAAG,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE;QAC1B,GAAG,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE;QACtB,GAAG,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE;QACtB,GAAG,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE;QAC1B,GAAG,EAAE,EAAE,IAAI,EAAE,YAAY,EAAE;QAC3B,GAAG,EAAE,EAAE,IAAI,EAAE,UAAU,EAAE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json
CHANGED
package/src/Jexl.ts
CHANGED
|
@@ -11,7 +11,10 @@ import type {
|
|
|
11
11
|
BinaryOpEval,
|
|
12
12
|
BinaryOpEvalOnDemand,
|
|
13
13
|
Grammar,
|
|
14
|
-
GrammarElement
|
|
14
|
+
GrammarElement,
|
|
15
|
+
GrammarFn,
|
|
16
|
+
UnaryOpEval,
|
|
17
|
+
UncheckedFn
|
|
15
18
|
} from './grammar.ts'
|
|
16
19
|
import type { JexlValue } from './types.ts'
|
|
17
20
|
|
|
@@ -55,7 +58,7 @@ class Jexl {
|
|
|
55
58
|
addBinaryOp(
|
|
56
59
|
operator: string,
|
|
57
60
|
precedence: number,
|
|
58
|
-
fn:
|
|
61
|
+
fn: UncheckedFn,
|
|
59
62
|
manualEval?: false
|
|
60
63
|
): void
|
|
61
64
|
addBinaryOp(
|
|
@@ -67,7 +70,7 @@ class Jexl {
|
|
|
67
70
|
addBinaryOp(
|
|
68
71
|
operator: string,
|
|
69
72
|
precedence: number,
|
|
70
|
-
fn:
|
|
73
|
+
fn: UncheckedFn | BinaryOpEvalOnDemand,
|
|
71
74
|
manualEval?: boolean
|
|
72
75
|
) {
|
|
73
76
|
// the overloads above pair `fn` with `manualEval`; the implementation
|
|
@@ -80,7 +83,11 @@ class Jexl {
|
|
|
80
83
|
precedence,
|
|
81
84
|
evalOnDemand: fn as BinaryOpEvalOnDemand
|
|
82
85
|
}
|
|
83
|
-
: {
|
|
86
|
+
: {
|
|
87
|
+
type: 'binaryOp',
|
|
88
|
+
precedence,
|
|
89
|
+
eval: fn as unknown as BinaryOpEval
|
|
90
|
+
}
|
|
84
91
|
)
|
|
85
92
|
}
|
|
86
93
|
|
|
@@ -92,8 +99,10 @@ class Jexl {
|
|
|
92
99
|
* expression function is invoked. It will be provided with each argument
|
|
93
100
|
* supplied in the expression, in the same order.
|
|
94
101
|
*/
|
|
95
|
-
addFunction(name: string, fn:
|
|
96
|
-
|
|
102
|
+
addFunction(name: string, fn: UncheckedFn) {
|
|
103
|
+
// jexl hands the function whatever the operands evaluated to, which it
|
|
104
|
+
// cannot reconcile with the caller's parameter types — see UncheckedFn
|
|
105
|
+
this._grammar.functions[name] = fn as unknown as GrammarFn
|
|
97
106
|
}
|
|
98
107
|
|
|
99
108
|
/**
|
|
@@ -102,7 +111,7 @@ class Jexl {
|
|
|
102
111
|
* function counterpart.
|
|
103
112
|
* @param {{}} map A map of expression function names to javascript functions
|
|
104
113
|
*/
|
|
105
|
-
addFunctions(map: Record<string,
|
|
114
|
+
addFunctions(map: Record<string, UncheckedFn>) {
|
|
106
115
|
Object.assign(this._grammar.functions, map)
|
|
107
116
|
}
|
|
108
117
|
|
|
@@ -114,11 +123,11 @@ class Jexl {
|
|
|
114
123
|
* will be called with one argument: the literal value to the right of the
|
|
115
124
|
* operator. It should return the resulting value.
|
|
116
125
|
*/
|
|
117
|
-
addUnaryOp(operator: string, fn:
|
|
126
|
+
addUnaryOp(operator: string, fn: UncheckedFn) {
|
|
118
127
|
this._addGrammarElement(operator, {
|
|
119
128
|
type: 'unaryOp',
|
|
120
129
|
precedence: Infinity,
|
|
121
|
-
eval: fn
|
|
130
|
+
eval: fn as unknown as UnaryOpEval
|
|
122
131
|
})
|
|
123
132
|
}
|
|
124
133
|
|
package/src/grammar.ts
CHANGED
|
@@ -9,6 +9,21 @@ import type { JexlValue } from './types.ts'
|
|
|
9
9
|
|
|
10
10
|
export type BinaryOpEval = (left: JexlValue, right: JexlValue) => JexlValue
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* A function or operator implementation as its author writes it, for the
|
|
14
|
+
* registration methods on {@link Jexl} to accept.
|
|
15
|
+
*
|
|
16
|
+
* Jexl cannot check what a registered callback will be handed. The arguments
|
|
17
|
+
* are whatever the expression evaluated to, drawn from a context supplied at
|
|
18
|
+
* evaluation time, and that context routinely holds values jexl has no literal
|
|
19
|
+
* for — a host object with methods, a callback, a class instance. Requiring
|
|
20
|
+
* {@link JexlValue} parameters therefore rejected every callback actually
|
|
21
|
+
* written against such a value, since parameters are contravariant. The
|
|
22
|
+
* argument types are the caller's business; jexl only promises to pass along
|
|
23
|
+
* whatever the operands evaluated to.
|
|
24
|
+
*/
|
|
25
|
+
export type UncheckedFn = (...args: never[]) => unknown
|
|
26
|
+
|
|
12
27
|
export type BinaryOpEvalOnDemand = (
|
|
13
28
|
left: { eval: () => JexlValue },
|
|
14
29
|
right: { eval: () => JexlValue }
|
|
@@ -27,10 +42,12 @@ export interface BinaryOp {
|
|
|
27
42
|
unaryEval?: (right: JexlValue) => JexlValue
|
|
28
43
|
}
|
|
29
44
|
|
|
45
|
+
export type UnaryOpEval = (right: JexlValue) => JexlValue
|
|
46
|
+
|
|
30
47
|
export interface UnaryOp {
|
|
31
48
|
type: 'unaryOp'
|
|
32
49
|
precedence: number
|
|
33
|
-
eval:
|
|
50
|
+
eval: UnaryOpEval
|
|
34
51
|
}
|
|
35
52
|
|
|
36
53
|
/**
|
|
@@ -56,9 +73,12 @@ export interface SimpleElement {
|
|
|
56
73
|
|
|
57
74
|
export type GrammarElement = BinaryOp | UnaryOp | SimpleElement
|
|
58
75
|
|
|
76
|
+
/** A registered function, as jexl calls it once the operands are evaluated. */
|
|
77
|
+
export type GrammarFn = (...args: JexlValue[]) => JexlValue
|
|
78
|
+
|
|
59
79
|
export interface Grammar {
|
|
60
80
|
elements: Record<string, GrammarElement>
|
|
61
|
-
functions: Record<string,
|
|
81
|
+
functions: Record<string, GrammarFn>
|
|
62
82
|
}
|
|
63
83
|
|
|
64
84
|
/**
|