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

Empty values (null, undefined) in 'contains' method #18

Open
CoonJS opened this issue Jul 31, 2018 · 3 comments
Open

Empty values (null, undefined) in 'contains' method #18

CoonJS opened this issue Jul 31, 2018 · 3 comments

Comments

@CoonJS
Copy link

CoonJS commented Jul 31, 2018

I use odata-builder for building large filter from table columns.
Each value can be empty string or undefined value and i want to skip 'contains' operator if value === undefined.

For example:

const filter = {
  name: 'MyName',
  age: undefined  // init value
};

f.build('and').contains('name', filter.name).contains('age', filter.age).toString(); 
// (contains(name, 'MyName')) and (contains(age));

I need to skip contains(age) operator because age === undefined, how to solve this problem?

@bodia-uz
Copy link
Owner

bodia-uz commented Jul 31, 2018

Hi.

Example 1:
https://jsbin.com/zudajiw/edit?html,js,console

var filter = f();

var filterProps = {
  name: 'MyName',
  age: undefined  // init value
}

Object
  .keys(filterProps)
  .forEach((key) => {
  if (typeof filterProps[key] !== 'undefined') {
    filter.contains(key, filterProps[key]);
  }
});

console.log('$filter=', filter.toString());

Example 2:
https://jsbin.com/jemases/edit?html,js,console

var filter = f();

var filterProps = {
  name: 'MyName',
  age: undefined  // init value
}

function safeContains(filterBuilder, key, value) {
  if (typeof value !== 'undefined') {
    filterBuilder.contains(key, value);
  }
}

safeContains(filter, 'name', filterProps.name);
safeContains(filter, 'age', filterProps.age);

console.log('$filter=', filter.toString());

@CoonJS
Copy link
Author

CoonJS commented Jul 31, 2018

This solution solve my problem, thanks! But you didn't think about encapsulation of this solution in your lib?

@bodia-uz
Copy link
Owner

Not sure if the silent skip is better. Need to think what to do with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants