Skip to content

Commit

Permalink
Support custom operator in addCondition method (#226)
Browse files Browse the repository at this point in the history
* feat: support custom operator in addCondition method

* build: bump to version 2.0.2
  • Loading branch information
alirezanet authored Sep 30, 2024
1 parent a3b2f21 commit 0e8645c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gridify-client",
"version": "2.0.1",
"version": "2.0.2",
"description": "Client JS library for the dotnet Gridify project",
"main": "dist/index.cjs.js",
"module": "dist/index.esm.js",
Expand Down
8 changes: 7 additions & 1 deletion client/src/GridifyQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class GridifyQueryBuilder {

addCondition(
field: string,
operator: ConditionalOperator,
operator: ConditionalOperator | string,
value: string | number | boolean,
caseSensitive: boolean = true,
escapeValue: boolean = true
Expand All @@ -45,6 +45,12 @@ export class GridifyQueryBuilder {
filterValue = `${filterValue.toString()}/i`;
}

if (typeof operator === "string" && !Object.values(ConditionalOperator).includes(operator as ConditionalOperator)) {
if (!operator.startsWith('#')) {
throw new Error(`Custom operators must start with the '#' character. Received: ${operator}`);
}
}

var filterExpression = `${field.trim()}${operator}${filterValue}`;
this.filteringExpressions.push({
value: filterExpression,
Expand Down
19 changes: 19 additions & 0 deletions client/test/GridifyQueryBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ describe("GridifyQueryBuilder", () => {
});
});

describe("GridifyQueryBuilder Custom Operator", () => {
it("should allow custom operator that starts with #", () => {
const query = new GridifyQueryBuilder()
.addCondition("age", "#customOp", 30)
.build();

expect(query.filter).toEqual("age#customOp30");
});

it("should throw an error for custom operator without # prefix", () => {
expect(() => {
new GridifyQueryBuilder()
.addCondition("age", "customOp", 30)
.build();
}).toThrow("Custom operators must start with the '#' character. Received: customOp");
});
});

describe("GridifyQueryBuilder Validation", () => {
it("should allow balanced parentheses", () => {
const query = new GridifyQueryBuilder()
Expand Down Expand Up @@ -161,3 +179,4 @@ describe("GridifyQueryBuilder Validation", () => {
});

});

0 comments on commit 0e8645c

Please sign in to comment.