Skip to content

Commit 3b29e5e

Browse files
committed
configuring jest testcases
1 parent b3572ab commit 3b29e5e

14 files changed

+5246
-794
lines changed

jest.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/** @type {import('ts-jest').JestConfigWithTsJest} */
2+
module.exports = {
3+
preset: 'ts-jest',
4+
testEnvironment: 'node',
5+
testPathIgnorePatterns: ['/node_modules/', '/dist/'],
6+
};

package-lock.json

Lines changed: 4085 additions & 637 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,43 @@
11
{
2-
"devDependencies": {
3-
"eslint": "^9.2.0",
4-
"eslint-config-prettier": "^9.1.0",
5-
"eslint-plugin-prettier": "^5.1.3",
6-
"prettier": "^3.2.5",
7-
"rimraf": "^5.0.7",
8-
"ts-node": "^10.9.2",
9-
"typescript": "^5.4.5"
10-
},
11-
"name": "odata-query-parser",
12-
"description": "Odata query params parser.",
13-
"version": "1.0.0",
14-
"main": "dist/src/index.js",
15-
"scripts": {
16-
"test": "jest",
17-
"test:report": "jest --coverage",
18-
"lint": "eslint --ext .ts,.js .",
19-
"lint:fix": "eslint --ext .ts,.js . --fix",
20-
"clean": "rimraf ./dist ",
21-
"build": "tsc -p tsconfig.build.json",
22-
"start": "npm run clean && npm run build && node ./dist/index.js"
23-
},
24-
"repository": {
25-
"type": "git",
26-
"url": "git+https://github.com/udaysingh236/odata-query-parser.git"
27-
},
28-
"keywords": [
29-
"odata",
30-
"pasrser",
31-
"filter",
32-
"v4"
33-
],
34-
"author": "Uday Singh",
35-
"license": "MIT",
36-
"bugs": {
37-
"url": "https://github.com/udaysingh236/odata-query-parser/issues"
38-
},
39-
"homepage": "https://github.com/udaysingh236/odata-query-parser#readme"
2+
"devDependencies": {
3+
"@types/jest": "^29.5.12",
4+
"eslint": "^9.2.0",
5+
"eslint-config-prettier": "^9.1.0",
6+
"eslint-plugin-prettier": "^5.1.3",
7+
"jest": "^29.7.0",
8+
"prettier": "^3.2.5",
9+
"rimraf": "^5.0.7",
10+
"ts-jest": "^29.1.3",
11+
"ts-node": "^10.9.2",
12+
"typescript": "^5.4.5"
13+
},
14+
"name": "odata-query-parser",
15+
"description": "Odata query params parser.",
16+
"version": "1.0.0",
17+
"main": "dist/src/index.js",
18+
"scripts": {
19+
"test": "jest",
20+
"test:report": "jest --coverage",
21+
"lint": "eslint --ext .ts,.js .",
22+
"lint:fix": "eslint --ext .ts,.js . --fix",
23+
"clean": "rimraf ./dist ",
24+
"build": "tsc -p tsconfig.build.json",
25+
"parse": "npm run clean && npm run build && node ./dist/index.js"
26+
},
27+
"repository": {
28+
"type": "git",
29+
"url": "git+https://github.com/udaysingh236/odata-query-parser.git"
30+
},
31+
"keywords": [
32+
"odata",
33+
"pasrser",
34+
"filter",
35+
"v4"
36+
],
37+
"author": "Uday Singh",
38+
"license": "MIT",
39+
"bugs": {
40+
"url": "https://github.com/udaysingh236/odata-query-parser/issues"
41+
},
42+
"homepage": "https://github.com/udaysingh236/odata-query-parser#readme"
4043
}

src/index.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
import { OdataQueryParser } from './parser';
1+
import { IOdataFilterToken, IParsedFilterRes, IParsedOrderByRes, IParsedSkipRes, IParsedTopRes, odataQueryParser } from './lib/parser';
22

3-
export const odataQueryParser = (): OdataQueryParser => {
4-
const odataQueryDb = new OdataQueryParser();
5-
return odataQueryDb;
6-
};
3+
export const parseFilter = (source: string): IParsedFilterRes => odataQueryParser.getParsedFilter(source);
4+
export const parseOrderby = (source: string): IParsedOrderByRes => odataQueryParser.getParsedOrderBy(source);
5+
export const parseSkip = (source: string): IParsedSkipRes => odataQueryParser.getParsedSkipToken(source);
6+
export const parseTop = (source: string): IParsedTopRes => odataQueryParser.getParsedTopToken(source);
77

88
function filterParser() {
99
try {
10-
const odataQueryDb = new OdataQueryParser();
11-
// const filter = "endswith(CompanyName,'Futterkiste') and (Title le 'Article1' and Title le 30 and concat('Uday', 'singh') eq 'Uday Singh') or contains(CompanyName,'Alfreds') or contains(CompanyName,3";
10+
const filter = "Address/City eq 'Redmond' and Address/City ne 'London' or Price gt 20 or Price ge 10 not Price lt 20 and Price le 100 or Address/City in ('Redmond', 'London') not Price add 5 gt 10 or Price sub 5 gt 10 or Price mul 2 gt 2000 not Price div 2 gt 4 or Price mod 2 eq 0 and (Price sub 5) gt 10 and concat(concat(City,', '), Country) eq 'Berlin, Germany' and contains(CompanyName,'freds') and endswith(CompanyName,'Futterkiste') or indexof(CompanyName,'lfreds') eq 1 and length(CompanyName) eq 19 or startswith(CompanyName,’Alfr’) and substring(CompanyName,1) eq 'lfreds Futterkiste' or hassubset([4,1,3],[3,1]) and hassubsequence([4,1,3,1],[1,1]) or matchesPattern(CompanyName,'%5EA.*e$') and tolower(CompanyName) eq 'alfreds futterkiste' or toupper(CompanyName) eq 'ALFREDS FUTTERKISTE' and trim(CompanyName) eq 'Alfreds Futterkiste' or day(StartTime) eq 8 and date(StartTime) ne date(EndTime) and second(StartTime) eq 0 or hour(StartTime) eq 1 and EndTime eq maxdatetime() and StartTime eq mindatetime() and minute(StartTime) eq 0 and month(BirthDate) eq 12 or StartTime ge now() and second(StartTime) eq 0 or time(StartTime) le StartOfDay and totaloffsetminutes(StartTime) eq 60 or totalseconds(duration'PT1M') eq 60 and year(BirthDate) eq 0 and ceiling(Freight) eq 33 or floor(Freight) eq 32 and round(Freight) eq 32 or cast(ShipCountry,Edm.String) not isof(NorthwindModel.Order) and case(X gt 0:1,X lt 0:-1,true:0)";
1211
// const filter = "Title le 'Article1' and (Title le 'Article1' and Price in (10 , 20, 'Uday') and Title le 30) and concat(concat(City,', '), Country) eq 'Berlin, Germany'";
13-
// const orderBy = 'ReleaseDate asc, Rating desc'
14-
const top = '10'
15-
const skip = '1000'
12+
const orderBy = 'ReleaseDate asc, Rating desc'
13+
const top = '10';
14+
const skip = '1000';
1615
// console.log(odataQueryDb.getParsedFilter(filter).error?.message);
1716
// console.log(odataQueryDb.getParsedFilter(filter));
18-
console.log(odataQueryDb.getParsedSkipToken(skip));
19-
console.log(odataQueryDb.getParsedTopToken(top));
17+
// console.log(parseSkip(skip));
18+
// console.log(parseTop(top));
19+
console.log(JSON.stringify(parseFilter(filter)));
2020
} catch (error) {
2121
console.log(`Error: ${error}`);
2222
}
2323
}
2424

25-
filterParser();
25+
// filterParser();

src/lexer.ts renamed to src/lib/lexer.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,4 +406,3 @@ export const queryFuncLexer: Record<string, (funcArgs: string) => IOdataFilterTo
406406
};
407407
},
408408
};
409-

src/parser.ts renamed to src/lib/parser.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export interface IParsedTopRes {
4545
token: IOdataTopToken;
4646
}
4747

48-
export class OdataQueryParser {
48+
class OdataQueryParser {
4949
private filterTokens: IOdataFilterToken[];
5050
private filterToken: IOdataFilterToken;
5151
private orderByToken: IOdataOrderByToken;
@@ -79,6 +79,7 @@ export class OdataQueryParser {
7979
}
8080

8181
private parseFilter(sourceStr: string) {
82+
sourceStr = sourceStr.trim();
8283
// Needed for operators
8384
let numOfWhiteSpace = constants.INITIALIZE_NUM_ZERO;
8485
let operator = constants.INITIALIZE_STR;
@@ -332,3 +333,5 @@ export class OdataQueryParser {
332333
}
333334
}
334335
}
336+
337+
export const odataQueryParser = new OdataQueryParser();
File renamed without changes.
File renamed without changes.

src/tests/allQueries.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const allQueries =
2+
"Address/City eq 'Redmond' and Address/City ne 'London' or Price gt 20 or Price ge 10 not Price lt 20 and Price le 100 or Address/City in ('Redmond', 'London') not Price add 5 gt 10 or Price sub 5 gt 10 or Price mul 2 gt 2000 not Price div 2 gt 4 or Price mod 2 eq 0 and (Price sub 5) gt 10 and concat(concat(City,', '), Country) eq 'Berlin, Germany' and contains(CompanyName,'freds') and endswith(CompanyName,'Futterkiste') or indexof(CompanyName,'lfreds') eq 1 and length(CompanyName) eq 19 or startswith(CompanyName,'Alfr') and substring(CompanyName,1) eq 'lfreds Futterkiste' or hassubset([4,1,3],[3,1]) and hassubsequence([4,1,3,1],[1,1]) or matchesPattern(CompanyName,'%5EA.*e$') and tolower(CompanyName) eq 'alfreds futterkiste' or toupper(CompanyName) eq 'ALFREDS FUTTERKISTE' and trim(CompanyName) eq 'Alfreds Futterkiste' or day(StartTime) eq 8 and date(StartTime) ne date(EndTime) and second(StartTime) eq 0 or hour(StartTime) eq 1 and EndTime eq maxdatetime() and StartTime eq mindatetime() and minute(StartTime) eq 0 and month(BirthDate) eq 12 or StartTime ge now() and second(StartTime) eq 0 or time(StartTime) le StartOfDay and totaloffsetminutes(StartTime) eq 60 or totalseconds(duration'PT1M') eq 60 and year(BirthDate) eq 0 and ceiling(Freight) eq 33 or floor(Freight) eq 32 and round(Freight) eq 32 or cast(ShipCountry,Edm.String) not isof(NorthwindModel.Order) and case(X gt 0:1,X lt 0:-1,true:0)";

0 commit comments

Comments
 (0)