Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 8 additions & 2 deletions src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export class Visitor{
this.Visit(node.value.left, context);
this.Visit(node.value.right, context);

if (context.identifier) context.query[context.identifier] = context.literal;
if (context.identifier) context.query[context.identifier] = { $eq: context.literal };
delete context.identifier;
delete context.literal;
}
Expand Down Expand Up @@ -281,17 +281,23 @@ export class Visitor{
switch (method) {
case "contains":
context.query[context.identifier] = new RegExp(context.literal, "gi");
delete context.identifier;
break;
case "endswith":
context.query[context.identifier] = new RegExp(context.literal + "$", "gi");
break;
case "startswith":
context.query[context.identifier] = new RegExp("^" + context.literal, "gi");
break;
case "tolower":
context.query[context.identifier] = new RegExp(context.literal, "gi");
break;
case "toupper":
context.query[context.identifier] = new RegExp(context.literal, "gi");
break;
default:
throw new Error("Method call not implemented.")
}
delete context.identifier;
}
}

Expand Down
102 changes: 71 additions & 31 deletions test/visitor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe("mongodb visitor", () => {
});

//all numbers are referencing this:
//http://docs.oasis-open.org/odata/odata/v4.0/errata02/os/complete/part2-url-conventions/odata-v4.0-errata02-os-part2-url-conventions-complete.html#_Toc406398116
//https://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part2-url-conventions.html#_Toc31361028

it("expression: 1 eq 1", () => {
expect(f).to.deep.eql({})
Expand All @@ -21,56 +21,56 @@ describe("mongodb visitor", () => {
expect(f).to.deep.eql({})
})

it("expression 5.1.1.6.1: NullValue eq null", () => {
expect(f).to.deep.eql({ NullValue: null })
it("expression 5.1.1.14.1: NullValue eq null", () => {
expect(f).to.deep.eql({ NullValue: {$eq: null} })
})

it("expression 5.1.1.6.1: TrueValue eq true", () => {
expect(f).to.deep.eql({ TrueValue: true })
it("expression 5.1.1.14.1: TrueValue eq true", () => {
expect(f).to.deep.eql({ TrueValue: {$eq: true} })
})

it("expression 5.1.1.6.1: FalseValue eq false", () => {
expect(f).to.deep.eql({ FalseValue: false })
it("expression 5.1.1.14.1: FalseValue eq false", () => {
expect(f).to.deep.eql({ FalseValue: {$eq: false} })
})

it("expression 5.1.1.6.1: IntegerValue lt -128", () => {
it("expression 5.1.1.14.1: IntegerValue lt -128", () => {
expect(f).to.deep.eql({ IntegerValue: { $lt: -128 } })
})

it("expression 5.1.1.6.1: DecimalValue eq 34.95", () => {
expect(f).to.deep.eql({ DecimalValue: 34.95 })
it("expression 5.1.1.14.1: DecimalValue eq 34.95", () => {
expect(f).to.deep.eql({ DecimalValue: {$eq: 34.95} })
})

it("expression 5.1.1.6.1: StringValue eq 'Say Hello,then go'", () => {
expect(f).to.deep.eql({ StringValue: 'Say Hello,then go' })
it("expression 5.1.1.14.1: StringValue eq 'Say Hello,then go'", () => {
expect(f).to.deep.eql({ StringValue: {$eq: 'Say Hello,then go'} })
})

xit("expression 5.1.1.6.1: DurationValue eq duration'P12DT23H59M59.999999999999S'", () => {
expect(f).to.deep.eql({ DurationValue: 1033199000 })
xit("expression 5.1.1.14.1: DurationValue eq duration'P12DT23H59M59.999999999999S'", () => {
expect(f).to.deep.eql({ DurationValue: {$eq: 1033199000} })
})

it("expression 5.1.1.6.1: DateValue eq 2012-12-03", () => {
expect(f).to.deep.eql({ DateValue: '2012-12-03' })
it("expression 5.1.1.14.1: DateValue eq 2012-12-03", () => {
expect(f).to.deep.eql({ DateValue: {$eq: '2012-12-03'} })
})

it("expression 5.1.1.6.1: DateTimeOffsetValue eq 2012-12-03T07:16:23Z", () => {
expect(f).to.deep.eql({ DateTimeOffsetValue: new Date('2012-12-03T07:16:23Z') })
it("expression 5.1.1.14.1: DateTimeOffsetValue eq 2012-12-03T07:16:23Z", () => {
expect(f).to.deep.eql({ DateTimeOffsetValue: {$eq: new Date('2012-12-03T07:16:23Z')} })
})

it("expression 5.1.1.6.1: GuidValue eq 01234567-89ab-cdef-0123-456789abcdef", () => {
expect(f).to.deep.eql({ GuidValue: '01234567-89ab-cdef-0123-456789abcdef' })
it("expression 5.1.1.14.1: GuidValue eq 01234567-89ab-cdef-0123-456789abcdef", () => {
expect(f).to.deep.eql({ GuidValue: {$eq: '01234567-89ab-cdef-0123-456789abcdef'} })
})

it("expression 5.1.1.6.1: Int64Value eq 0", () => {
expect(f).to.deep.eql({ Int64Value: 0 })
it("expression 5.1.1.14.1: Int64Value eq 0", () => {
expect(f).to.deep.eql({ Int64Value: {$eq: 0 } })
})

it("expression 5.1.1.6.1: A eq INF", () => {
expect(f).to.deep.eql({ A: Infinity })
it("expression 5.1.1.14.1: A eq INF", () => {
expect(f).to.deep.eql({ A: {$eq: Infinity } })
})

it("expression 5.1.1.6.1: A eq 0.31415926535897931e1", () => {
expect(f).to.deep.eql({ A: 0.31415926535897931e1 })
it("expression 5.1.1.14.1: A eq 0.31415926535897931e1", () => {
expect(f).to.deep.eql({ A: {$eq: 0.31415926535897931e1 } })
})

it("expression 5.1.1.1.2: A ne 1", () => {
Expand All @@ -94,11 +94,11 @@ describe("mongodb visitor", () => {
})

it("expression: A/b eq 1", () => {
expect(f).to.deep.eql({ 'A.b': 1 })
expect(f).to.deep.eql({ 'A.b': {$eq: 1 } })
})

it("expression 5.1.1.3: (A/b eq 2) or (B/c lt 4) and ((E gt 5) or (E lt -1))", () => {
expect(f).to.deep.eql({ $or: [{ 'A.b': 2 }, { $and: [{ 'B.c': { $lt: 4 } }, { $or: [{ E: { $gt: 5 } }, { E: { $lt: -1 } }] }] }] })
expect(f).to.deep.eql({ $or: [{ 'A.b': { $eq: 2 } }, { $and: [{ 'B.c': { $lt: 4 } }, { $or: [{ E: { $gt: 5 } }, { E: { $lt: -1 } }] }] }] })
})

it("expression 5.1.1.4.1: contains(A, 'BC')", () => {
Expand All @@ -109,15 +109,55 @@ describe("mongodb visitor", () => {
expect(f).to.deep.eql({ $or: [{ A: /BC/gi }, { D: /EF/gi }]});
})

it("expression 5.1.1.4.2: endswith(A, 'CD')", () => {
it("expression 5.1.1.5.3: endswith(A, 'CD')", () => {
expect(f).to.deep.eql({ A: /CD$/gi });
})

it("expression 5.1.1.4.3: startswith(A, 'CD')", () => {
it("expression 5.1.1.5.6: startswith(A, 'CD')", () => {
expect(f).to.deep.eql({ A: /^CD/gi });
})

it("expression 5.1.1.1.11: not endswith(Name,'ilk')", () => {
it("expression 5.1.1.1.12: not endswith(Name,'ilk')", () => {
expect(f).to.deep.eql({ Name: { $not: /ilk$/gi } });
})

it("expression 5.1.1.7.2: tolower(displayName) eq 'device1'", () => {
expect(f).to.deep.eql({ displayName: { $eq: 'device1'} });
})

it("expression 5.1.1.7.3: toupper(displayName) eq 'device1'", () => {
expect(f).to.deep.eql({ displayName: { $eq: 'device1'} });
})

it("expression 5.1.1.1.9: not (displayName eq 'device1')", () => {
expect(f).to.deep.eql({ displayName: { $not: { $eq: 'device1'} } });
})

it("expression 5.1.1.1.9: not (tolower(displayName) eq 'device1')", () => {
expect(f).to.deep.eql({ displayName: { $not: { $eq: 'device1'} } });
})

it("expression 5.1.1.1.9: not (toupper(displayName) eq 'device1')", () => {
expect(f).to.deep.eql({ displayName: { $not: { $eq: 'device1'} } });
})

it("expression 5.1.1.7.2: tolower(displayName) ne 'device1'", () => {
expect(f).to.deep.eql({displayName: {$ne: 'device1'}});
})

it("expression 5.1.1.7.3: toupper(displayName) ne 'device1'", () => {
expect(f).to.deep.eql({ displayName: { $ne: 'device1'} });
})

it("expression 5.1.1.1.9: not (displayName ne 'device1')", () => {
expect(f).to.deep.eql({ displayName: { $not: { $ne: 'device1'} } });
})

it("expression 5.1.1.1.9: not (tolower(displayName) ne 'device1')", () => {
expect(f).to.deep.eql({ displayName: { $not: { $ne: 'device1'} } });
})

it("expression 5.1.1.1.9: not (toupper(displayName) ne 'device1')", () => {
expect(f).to.deep.eql({ displayName: { $not: { $ne: 'device1'} } });
})
})