Skip to content

bugfix/Sets with outer and #29

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
56 changes: 41 additions & 15 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
"eslint-config-tfso": "npm:@tfso/eslint-config@^1.1.4",
"mocha": "^10.2.0",
"peggy": "^4.0.2",
"ts-node": "^10.9.1",
"typescript": "^4.9.5",
"ts-node": "^10.9.2",
"typescript": "^5.8.3",
"webpack": "^5.75.0",
"webpack-bundle-analyzer": "^4.7.0",
"webpack-cli": "^5.0.1"
Expand Down
64 changes: 51 additions & 13 deletions src/linq/peg/expression/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,51 @@ export abstract class Expression implements IExpression {
}

function * visit(expression: IExpression): Iterable<IterableIterator<IExpression>> {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { LogicalExpression } = require('./logicalexpression')

if(isLogicalExpression(expression)) {
if(expression.operator == LogicalOperatorType.Or) {
yield* visit(expression.left)
yield* visit(expression.right)
}
else {
yield visitLeaf(expression)
switch(expression.operator) {
case LogicalOperatorType.Or:
yield* visit(expression.left)
yield* visit(expression.right)
break

case LogicalOperatorType.And:
if(isLogicalExpression(expression.left) && expression.left.operator == LogicalOperatorType.Or) {
yield visitLeaf(new LogicalExpression(
LogicalOperatorType.And,
expression.right,
expression.left.left
))

yield visitLeaf(new LogicalExpression(
LogicalOperatorType.And,
expression.right,
expression.left.right
))
}
else if(isLogicalExpression(expression.right) && expression.right.operator == LogicalOperatorType.Or) {
yield visitLeaf(new LogicalExpression(
LogicalOperatorType.And,
expression.left,
expression.right.left
))

yield visitLeaf(new LogicalExpression(
LogicalOperatorType.And,
expression.left,
expression.right.right
))
}
else {
yield visitLeaf(expression)
}

break

default:
yield visitLeaf(expression)
}
}
else if(isLambdaExpression(expression)) {
Expand Down Expand Up @@ -184,31 +222,31 @@ function * visitLeaf(expression: IExpression): IterableIterator<IExpression> {
}

export function isLogicalExpression(expression?: IExpression): expression is ILogicalExpression {
return expression?.type == ExpressionType.Logical ?? false
return expression?.type == ExpressionType.Logical ? true : false
}

export function isLambdaExpression(expression?: IExpression): expression is ILambdaExpression {
return expression?.type == ExpressionType.Lambda ?? false
return expression?.type == ExpressionType.Lambda ? true : false
}

export function isBinaryExpression(expression?: IExpression): expression is IBinaryExpression {
return expression?.type == ExpressionType.Binary ?? false
return expression?.type == ExpressionType.Binary ? true : false
}

export function isMemberExpression(expression?: IExpression): expression is IMemberExpression {
return expression?.type == ExpressionType.Member ?? false
return expression?.type == ExpressionType.Member ? true : false
}

export function isIdentifierExpression(expression?: IExpression): expression is IIdentifierExpression {
return expression?.type == ExpressionType.Identifier ?? false
return expression?.type == ExpressionType.Identifier ? true : false
}

export function isLiteralExpression(expression?: IExpression): expression is ILiteralExpression {
return expression?.type == ExpressionType.Literal ?? false
return expression?.type == ExpressionType.Literal ? true : false
}

export function isArrayExpression(expression?: IExpression): expression is IArrayExpression {
return expression?.type == ExpressionType.Array ?? false
return expression?.type == ExpressionType.Array ? true : false
}

export { IExpression, ExpressionType }
2 changes: 1 addition & 1 deletion src/test/enumerable/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ describe('When using enumerable for record type', () => {
})

it('should handle remap of key', async () => {
let map = (name: string) => name == 'details/revisions' ? 'revisions' : 'yey' ?? name
let map = (name: string) => name == 'details/revisions' ? 'revisions' : 'yey'

let enumerable = new jsEnumerable.Enumerable(asyncIterator())
.where('details/revisions/any(e: e/year le 1970)')
Expand Down
25 changes: 16 additions & 9 deletions src/test/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,17 @@ describe('When using Expression', () => {
let count = 0
for(let part of expression.sets) {
switch(part.map(p => p.toString()).join(',')) {
case 'year == 2015': count++; break
case 'year == 2015,location == "DK"': count++; break
case 'year == 2015,location == "NO"': count++; break
case 'year == 2015': count++; break
case 'location == "SE",year == 2015': count++; break
}
}

chai.expect(count).to.equal(3)
chai.expect(count).to.equal(4)

chai.expect(Array.from(expression.sets).length).to.equal(3)
chai.expect(Array.from(expression.union).length).to.equal(4)
chai.expect(Array.from(expression.sets).length).to.equal(4)
chai.expect(Array.from(expression.union).length).to.equal(7)
chai.expect(Array.from(expression.intersection).length).to.equal(1)
})

Expand All @@ -88,27 +89,33 @@ describe('When using Expression', () => {

})

it('should not use sets with an outer and', () => {
it('should use sets with an outer and', () => {

let expression = reducer.parseOData(`((contains(id/value, '12')) or (contains(tolower(name), '12'))) and (id/type eq 1)`)

let count = 0
for(let part of expression.sets) {
count += part.length
switch(part.map(p => p.toString()).join(',')) {
case 'id.type == 1,contains(id.value, "12")': count++; break
case 'id.type == 1,contains(tolower(name), "12")': count++; break
}
}

chai.expect(count).to.equal(1)
chai.expect(count).to.equal(2)
})

it('should use sets with an inner and', () => {
let expression = reducer.parseOData(`(id/type eq 1 and contains(id/value, '12')) or (id/type eq 1 and contains(tolower(name), '12'))`)

let count = 0
for(let part of expression.sets) {
count += part.length
switch(part.map(p => p.toString()).join(',')) {
case 'id.type == 1,contains(id.value, "12")': count++; break
case 'id.type == 1,contains(tolower(name), "12")': count++; break
}
}

chai.expect(count).to.equal(4)
chai.expect(count).to.equal(2)
})

})
3 changes: 3 additions & 0 deletions src/test/expressionvisitor/logical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ describe('When using ExpressionVisitor', () => {

describe('Lambda expression', () => {
it('should return a logical operation', () => {
//@ts-expect-error simplified expression for test purposes
expr = visitor.parseLambda(() => 5 && 2)

if(expr.type == Expr.ExpressionType.Lambda)
Expand All @@ -235,6 +236,7 @@ describe('When using ExpressionVisitor', () => {
})

it('should handle logical operation for and', () => {
//@ts-expect-error simplified expression for test purposes
expr = visitor.parseLambda(() => 5 && 2)

if(expr.type == Expr.ExpressionType.Lambda)
Expand All @@ -245,6 +247,7 @@ describe('When using ExpressionVisitor', () => {
})

it('should handle logical operation for or', () => {
//@ts-expect-error simplified expression for test purposes
expr = visitor.parseLambda(() => 5 || 2)

if(expr.type == Expr.ExpressionType.Lambda)
Expand Down
Loading