Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add several features and tests #5

Merged
merged 25 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5542648
refactor(parser): create lexer to hold lexing logic
koladilip Nov 9, 2022
9c1e29f
Merge branch 'main' into refactor.parser
koladilip Nov 9, 2022
5e605c9
refactor: add support for more function features
koladilip Nov 9, 2022
73f1350
refactor: Fix filters and add basic support for context vars
koladilip Nov 11, 2022
f048b79
Add support for complex assignment path
koladilip Nov 12, 2022
20cc9de
Add support for object vars
koladilip Nov 12, 2022
fb9582d
Add support for lambda functions
koladilip Nov 13, 2022
09ad239
Add support for to array
koladilip Nov 14, 2022
1c4d10d
Fix issues with function calls
koladilip Nov 14, 2022
bbab29d
Fix function call expr
koladilip Nov 14, 2022
e317bd8
feat: add support for conditional expressions
saikumarrs Nov 14, 2022
581cc50
Add support for context variables
koladilip Nov 15, 2022
70fa9fc
Fix formating issues
koladilip Nov 15, 2022
7d435f7
Add test framework
koladilip Nov 15, 2022
5c566ef
Add test scenarios
koladilip Nov 15, 2022
97a135d
add tests for all use cases
koladilip Nov 16, 2022
ffb1254
update .gitinore to ignore manual test file
koladilip Nov 16, 2022
0ffd6cf
Update manual test files
koladilip Nov 17, 2022
0ecd224
Merge other features
koladilip Nov 17, 2022
8485435
rerefactor binary operators
koladilip Nov 17, 2022
eefab56
Refactor object filters
koladilip Nov 17, 2022
7f47077
Improve the performance of subpaths
koladilip Nov 17, 2022
d4ae606
Avoid using hasOwnProperty as it is effecting performance
koladilip Nov 17, 2022
4df9768
Add example of using function in object filter
koladilip Nov 18, 2022
6a38375
Use of hasOwnProperty is required
koladilip Nov 18, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,30 @@
"type": "node"
},
{
"name": "Test Extractor",
"program": "${workspaceFolder}/test/test_extractor.ts",
"name": "Test Scenario",
"program": "${workspaceFolder}/test/test_scenario.ts",
"request": "launch",
"runtimeArgs": ["--nolazy", "-r", "ts-node/register/transpile-only"],
"skipFiles": ["<node_internals>/**"],
"args": ["--scenario=${input:scenario}", "--index=${input:index}"],
"type": "node"
},
{
"runtimeExecutable": "/usr/local/bin/node",
"type": "node",
"request": "launch",
"name": "Jest Scenarios",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"args": ["test/e2e.test.ts", "--config", "jest.config.ts", "--scenarios=${input:scenarios}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"windows": {
"program": "${workspaceFolder}/node_modules/jest/bin/jest"
}
},
{
"name": "Test Engine",
"program": "${workspaceFolder}/test/test_engine.ts",
"request": "launch",
"runtimeArgs": ["--nolazy", "-r", "ts-node/register/transpile-only"],
"skipFiles": ["<node_internals>/**"],
Expand All @@ -33,5 +55,25 @@
"program": "${workspaceFolder}/node_modules/jest/bin/jest"
}
}
],
"inputs": [
{
"id": "scenarios",
"type": "promptString",
"description": "Enter Scenarios",
"default": "all"
},
{
"id": "scenario",
"type": "promptString",
"description": "Enter Scenario",
"default": "assignments"
},
{
"id": "index",
"type": "promptString",
"description": "Enter test index",
"default": "0"
}
]
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
"lint:fix": "eslint . --fix",
"lint:check": "eslint . || exit 1",
"format": "prettier --write '**/*.ts' '**/*.js' '**/*.json'",
"prepare": "husky install"
"prepare": "husky install",
"jest:scenarios": "jest e2e.test.ts --verbose",
"test:scenario": "ts-node test/test_scenario.ts"
},
"engines": {
"node": ">=14.15.0 <15",
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const VARS_PREFIX = '___';
export const DATA_PARAM_KEY = '___d';
export const BINDINGS_PARAM_KEY = '___b';
export const RESULT_KEY = '___r';
export const FUNCTION_RESULT_KEY = '___f';
11 changes: 7 additions & 4 deletions src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@ import { BINDINGS_PARAM_KEY, DATA_PARAM_KEY } from './constants';
import { JsonTemplateLexer } from './lexer';
import { JsonTemplateParser } from './parser';
import { JsonTemplateTranslator } from './translator';
import { CommonUtils } from './utils';

export class JsonTemplateEngine {
private readonly fn: Function;
constructor(template: string) {
this.fn = JsonTemplateEngine.compile(template);
}
private static compile(template: string) {

private static compile(template: string): Function {
const lexer = new JsonTemplateLexer(template);
const parser = new JsonTemplateParser(lexer);
const translator = new JsonTemplateTranslator(parser.parse());
return new Function(DATA_PARAM_KEY, BINDINGS_PARAM_KEY, translator.translate());
const code = translator.translate();
return CommonUtils.CreateAsyncFunction(DATA_PARAM_KEY, BINDINGS_PARAM_KEY, code);
}

evaluate(data: any, bindings: any = {}) {
return this.fn(data, bindings);
evaluate(data: any, bindings: any = {}): Promise<any> {
return this.fn(data || {}, bindings);
}
}
10 changes: 7 additions & 3 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
export class JsonTemplateLexerError extends Error {
readonly column: number;
constructor(message: string, column: number) {
constructor(message: string) {
super(message);
this.column = column;
}
}

Expand All @@ -11,3 +9,9 @@ export class JsosTemplateParserError extends Error {
super(message);
}
}

export class JsosTemplateTranslatorError extends Error {
constructor(message: string) {
super(message);
}
}
Loading