Skip to content
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

Add a way to dynamically add to an AND or OR expression #10

Open
prust opened this issue Jan 8, 2014 · 5 comments
Open

Add a way to dynamically add to an AND or OR expression #10

prust opened this issue Jan 8, 2014 · 5 comments

Comments

@prust
Copy link
Collaborator

prust commented Jan 8, 2014

Currently you have to do:

var criteria = and({'first_name': 'Fred'});
if (/* condition here */)
  criteria.expressions.push(eq('last_name', 'Flintstone'));

It would be better to define a push() method that accepts the same arguments as the and() constructor (either an expression or an object, where each key/value pair is converted into an eq() expression).

@sumitshinde-84
Copy link

i would like to work on this issue , can you please assign it to me?

@prust
Copy link
Collaborator Author

prust commented Apr 10, 2023

@sumitshinde-84: Assigned.

@sumitshinde-84
Copy link

Sir can you explain me what changes do you want exactly?

@sumitshinde-84
Copy link

Based on your suggestion I have implemented a push() method for dynamically adding conditions to an AND or OR expression. Here's the example implementation I came up with

function and(expression) {
return {
type: 'and',
expressions: expression || []
};
}

// Constructor for EQ expression
function eq(key, value) {
return {
type: 'eq',
key: key,
value: value
};
}

// Adding push() method to AND expression
and.prototype.push = function(expression) {
if (typeof expression === 'object') {
// If an object is provided, convert key-value pairs to EQ expressions
for (var key in expression) {
if (expression.hasOwnProperty(key)) {
this.expressions.push(eq(key, expression[key]));
}
}
} else {
// If an expression is provided, push it directly
this.expressions.push(expression);
}
};

Does this align with what you were expecting? I would appreciate your feedback.

@prust
Copy link
Collaborator Author

prust commented Apr 12, 2023

@sumitshinde-84: Yes, this looks like the right direction! It would just need to be written as a modification of the existing code: the and class is defined here (actually it's a Group class), and the eq class is defined here (actually it's a Binary class).

@sumitshinde-84 sumitshinde-84 removed their assignment May 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants