Skip to content

Commit 6366ef0

Browse files
authored
feat: improve code quality (#40)
1 parent b2efd94 commit 6366ef0

6 files changed

Lines changed: 27 additions & 35 deletions

File tree

.github/workflows/ci.yml

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,20 @@ jobs:
2525
node-version: [ 18.x, 20.x, 22.x, 24.x ]
2626

2727
steps:
28-
29-
- name: Echo env variables
30-
run: |
31-
echo ref: ${{ github.event.client_payload.ref || github.ref }}
32-
echo sha: ${{ github.event.client_payload.sha || github.sha }}
33-
echo head ref: ${{ github.event.client_payload.head_ref || github.head_ref }}
34-
echo base ref: ${{ github.event.client_payload.base_ref || github.base_ref }}
35-
echo action: ${{ github.action }}
36-
echo event: ${{ github.event_name }}
37-
38-
- uses: actions/checkout@v2
28+
- uses: actions/checkout@v5
3929
name: Checkout
4030
with:
4131
ref: ${{ github.event.client_payload.ref || github.ref }}
4232

4333
- name: Use Node.js ${{ matrix.node-version }}
44-
uses: actions/setup-node@v2
34+
uses: actions/setup-node@v6
4535
with:
4636
node-version: ${{ matrix.node-version }}
4737

4838
- name: Get yarn cache directory
4939
id: yarn-cache-dir
5040
run: |
51-
echo "::set-output name=dir::$(yarn cache dir)"
41+
echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT
5242
5343
- uses: actions/cache@v4
5444
id: yarn-cache
@@ -101,22 +91,22 @@ jobs:
10191
steps:
10292

10393
- name: Checkout
104-
uses: actions/checkout@v2
94+
uses: actions/checkout@v5
10595

10696
- name: fetch
10797
run: |
10898
git fetch --prune --unshallow
10999
110100
- name: Use Node.js ${{ matrix.node-version }}
111-
uses: actions/setup-node@v2
101+
uses: actions/setup-node@v6
112102
with:
113103
node-version: ${{ matrix.node-version }}
114104
registry-url: ${{ env.REGISTRY }}
115105

116106
- name: Get yarn cache directory
117107
id: yarn-cache-dir
118108
run: |
119-
echo "::set-output name=dir::$(yarn cache dir)"
109+
echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT
120110
121111
- uses: actions/cache@v4
122112
id: yarn-cache

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-expression-eval",
3-
"version": "8.0.0",
3+
"version": "8.0.1",
44
"description": "json serializable rule engine / boolean expression evaluator",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/examples/evaluator/example.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import {ExpressionContext, ExpressionFunction, getEvaluator} from './lib/evaluator';
1+
import {ExpressionContext, ExpressionFunction, getEvaluator, CustomEvaluatorFuncRunOptions} from './lib/evaluator';
22
import {Expression, ExpressionParts} from '../..';
33
import {Moment} from 'moment';
44
import moment = require('moment');
5-
import {CustomEvaluatorFuncRunOptions} from './lib/evaluator';
65

76
const context: ExpressionContext = {
87
userId: 'a@b.com',

src/lib/evaluator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ async function evaluateCompareOp<C extends Context, Ignore>(expressionValue: Ext
102102
contextStringAssertion(expressionKey, contextValue);
103103
const regexpValue = computeValue(context, validation, expressionValue.regexp, expressionKey);
104104
expressionStringAssertion(expressionKey, regexpValue);
105-
return Boolean(contextValue.match(new RegExp(regexpValue)));
105+
return Boolean(new RegExp(regexpValue).exec(contextValue));
106106
} else if (isRegexiCompareOp(expressionValue)) {
107107
contextStringAssertion(expressionKey, contextValue);
108108
const regexpiValue = computeValue(context, validation, expressionValue.regexpi, expressionKey);
109109
expressionStringAssertion(expressionKey, regexpiValue);
110-
return Boolean(contextValue.match(new RegExp(regexpiValue, `i`)));
110+
return Boolean(new RegExp(regexpiValue, `i`).exec(contextValue));
111111
} else if (isGtCompareOp(expressionValue)) {
112112
contextNumberAssertion(expressionKey, contextValue);
113113
const gtValue = computeValue(context, validation, expressionValue.gt, expressionKey);

src/test/types/expressionParts.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ type Expected = {
9393
},
9494
};
9595

96-
declare var r: Result;
97-
declare var e: Expected;
96+
declare let r: Result;
97+
declare let e: Expected;
9898

9999
r = e;
100100
e = r;

src/types/evaluator.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,43 +32,46 @@ export interface MathOp<C extends Context, Ignore> {
3232
lhs: number | PropertyRef<C, Ignore, number>;
3333
}
3434

35+
export type CompareValue<C extends Context, Ignore, V extends Primitive> =
36+
V | PropertyRef<C, Ignore, V> | (V extends number ? MathOp<C, Ignore> : never);
37+
38+
export type NumberCompareValue<C extends Context, Ignore> =
39+
number | PropertyRef<C, Ignore, number> | MathOp<C, Ignore>;
40+
3541
export interface EqualCompareOp<C extends Context, Ignore, V extends Primitive> {
36-
eq: V | PropertyRef<C, Ignore, V> | (V extends number ? MathOp<C, Ignore> : never);
42+
eq: CompareValue<C, Ignore, V>;
3743
}
3844

3945
export interface NotEqualCompareOp<C extends Context, Ignore, V extends Primitive> {
40-
neq: V | PropertyRef<C, Ignore, V> | (V extends number ? MathOp<C, Ignore> : never);
46+
neq: CompareValue<C, Ignore, V>;
4147
}
4248

4349
export interface InqCompareOp<C extends Context, Ignore, V extends Primitive> {
44-
inq: (V | PropertyRef<C, Ignore, V> | (V extends number ? MathOp<C, Ignore> : never))[];
50+
inq: CompareValue<C, Ignore, V>[];
4551
}
4652

4753
export interface NinCompareOp<C extends Context, Ignore, V extends Primitive> {
48-
nin: (V | PropertyRef<C, Ignore, V> | (V extends number ? MathOp<C, Ignore> : never))[];
54+
nin: CompareValue<C, Ignore, V>[];
4955
}
5056

5157
export interface BetweenCompareOp<C extends Context, Ignore> {
52-
between: readonly [
53-
number | PropertyRef<C, Ignore, number> | MathOp<C, Ignore>,
54-
number | PropertyRef<C, Ignore, number> | MathOp<C, Ignore>
55-
];
58+
between: readonly [NumberCompareValue<C, Ignore>, NumberCompareValue<C, Ignore>];
5659
}
5760

5861
export interface GtCompareOp<C extends Context, Ignore> {
59-
gt: number | PropertyRef<C, Ignore, number> | MathOp<C, Ignore>;
62+
gt: NumberCompareValue<C, Ignore>;
6063
}
6164

6265
export interface GteCompareOp<C extends Context, Ignore> {
63-
gte: number | PropertyRef<C, Ignore, number> | MathOp<C, Ignore>;
66+
gte: NumberCompareValue<C, Ignore>;
6467
}
6568

6669
export interface LtCompareOp<C extends Context, Ignore> {
67-
lt: number | PropertyRef<C, Ignore, number> | MathOp<C, Ignore>;
70+
lt: NumberCompareValue<C, Ignore>;
6871
}
6972

7073
export interface LteCompareOp<C extends Context, Ignore> {
71-
lte: number | PropertyRef<C, Ignore, number> | MathOp<C, Ignore>;
74+
lte: NumberCompareValue<C, Ignore>;
7275
}
7376

7477
export interface RegexCompareOp<C extends Context, Ignore> {

0 commit comments

Comments
 (0)