Skip to content

Commit

Permalink
Better between operator
Browse files Browse the repository at this point in the history
  • Loading branch information
valkjsaaa committed Nov 27, 2023
1 parent f1335af commit a090660
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 35 deletions.
12 changes: 12 additions & 0 deletions lib/dsl/__test__/dsl-interpreter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ test("Array between", async () => {
});
});

test("Array between one side", async () => {
const interpreter = new DslInterpreter(allDescriptors);
const funcCallResult = await interpreter.interpret(
"Order.all().between(field: .dateTime, from: DateTime.today().addDateOffset(day: -7))[-1].restaurant.name"
);
expect(funcCallResult).toEqual({
objectType: "string",
type: "object",
value: "McDonald's"
});
});

test("Array equals", async () => {
const interpreter = new DslInterpreter(allDescriptors);
const funcCallResult = await interpreter.interpret(
Expand Down
64 changes: 34 additions & 30 deletions lib/dsl/dsl-interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ export class DslInterpreter {
"between",
[
new ParamDescriptor("field", "accessor"),
new ParamDescriptor("from", "object"),
new ParamDescriptor("to", "object"),
new ParamDescriptor("from", "object", false),
new ParamDescriptor("to", "object", false),
],
"object[]",
false
Expand Down Expand Up @@ -214,45 +214,49 @@ export class DslInterpreter {
array,
}: {
field: any;
from: any;
to: any;
from?: any;
to?: any;
array: any[];
}) => {
console.assert(field.type === "accessor");
const fieldName = field.field;
return array.filter((v) => {
let first: boolean;
let second: boolean;
let first: boolean = true;
let second: boolean = true;
let value = v[fieldName];
let fromIsPrimitive =
typeof from === "string" ||
typeof from === "number" ||
typeof from === "boolean";
let toIsPrimitive =
typeof to === "string" ||
typeof to === "number" ||
typeof to === "boolean";
let valueIsPrimitive =
typeof value === "string" ||
typeof value === "number" ||
typeof value === "boolean";
if (fromIsPrimitive && valueIsPrimitive) {
first = value >= from;
} else if (fromIsPrimitive && !valueIsPrimitive) {
first = value.constructor.compare(value, from) >= 0;
} else if (!fromIsPrimitive && valueIsPrimitive) {
first = from.constructor.compare(value, from) >= 0;
} else {
first = from.constructor.compare(value, from) >= 0;
if (from !== undefined) {
let fromIsPrimitive =
typeof from === "string" ||
typeof from === "number" ||
typeof from === "boolean";
if (fromIsPrimitive && valueIsPrimitive) {
first = value >= from;
} else if (fromIsPrimitive && !valueIsPrimitive) {
first = value.constructor.compare(value, from) >= 0;
} else if (!fromIsPrimitive && valueIsPrimitive) {
first = from.constructor.compare(value, from) >= 0;
} else {
first = from.constructor.compare(value, from) >= 0;
}
}
if (toIsPrimitive && valueIsPrimitive) {
second = value <= to;
} else if (toIsPrimitive && !valueIsPrimitive) {
second = value.constructor.compare(value, to) <= 0;
} else if (!toIsPrimitive && valueIsPrimitive) {
second = to.constructor.compare(value, to) <= 0;
} else {
second = to.constructor.compare(value, to) <= 0;
if (to !== undefined) {
let toIsPrimitive =
typeof to === "string" ||
typeof to === "number" ||
typeof to === "boolean";
if (toIsPrimitive && valueIsPrimitive) {
second = value <= to;
} else if (toIsPrimitive && !valueIsPrimitive) {
second = value.constructor.compare(value, to) <= 0;
} else if (!toIsPrimitive && valueIsPrimitive) {
second = to.constructor.compare(value, to) <= 0;
} else {
second = to.constructor.compare(value, to) <= 0;
}
}
return first && second;
});
Expand Down
4 changes: 2 additions & 2 deletions lib/nl/prompt-res.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export const array_definition =
" // Find items with exact fields in an array\n" +
" Array<Type> equals(field: Field, value: Type);\n" +
"\n" +
" // Find items with the field between two values in an array\n" +
" Array<Type> between(field: Field, from: Type, to: Type);\n" +
" // Find items with the field above from (if provided) and below to (if provided).\n" +
" Array<Type> between(field: Field, from?: Type, to?: Type);\n" +
"\n" +
" // Sort an array based on a specific field in ascending or descending order\n" +
" Array<Type> sort(field: Field, ascending: bool);\n" +
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reactgenie-dsl",
"version": "0.0.52",
"version": "0.0.53",
"description": "A natural language parser based on a large language model",
"scripts": {
"prepare": "peggy lib/dsl/parser.pegjs -o lib/dsl/parser.gen.js && tsc",
Expand Down

0 comments on commit a090660

Please sign in to comment.