Skip to content

Commit bb85148

Browse files
anush6898Eric Rubio
authored and
Eric Rubio
committed
Fix JSL parser returns entire data for non path attributes (#8)
1 parent 5cf2e47 commit bb85148

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-selector-lang",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "A tiny language for selecting JSON values",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/JSL.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class JSL {
1717
return parser.parseProgram();
1818
}
1919

20-
static evaluate(json: any, node: Program) {
20+
static evaluate(json: any, node: Program): any {
2121
const evaluator = new JSLEvaluator();
2222

2323
return evaluator.evaluate(json, node);

src/JSLEvaluator.ts

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class JSLEvaluator {
3131
// -- Program evaluation --
3232

3333
private evaluateProgram(program: Program, json: any): any {
34+
if (program?.statements?.length === 0) {
35+
return null;
36+
}
37+
3438
return program.statements.reduce((prev, statement) => this.evaluate(prev, statement), json);
3539
}
3640

src/__tests__/JSLEvaluator.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,19 @@ describe('JSLEvaluator', () => {
119119
expect(returnedError[idx]).toEqual(testCase);
120120
});
121121
});
122+
123+
it('tests with valid jsl input', () => {
124+
const input = '';
125+
const json = { data: [{}, {}, { width: 200 }] };
126+
try {
127+
const program = JSL.compile(input);
128+
const evaluator = new JSLEvaluator();
129+
const e = evaluator.evaluate(json, program);
130+
131+
expect(e).toBeNull();
132+
} catch (e: any) {
133+
console.log(e);
134+
fail();
135+
}
136+
});
122137
});

0 commit comments

Comments
 (0)