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

feat: END-335 support for all and some #3

Merged
merged 6 commits into from
Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/node_modules/
coverage
110 changes: 109 additions & 1 deletion test/where-filter.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import whereFilter from '../where-filter';
import {expect, should} from 'chai';
import {expect, should, assert} from 'chai';
import {DateTime} from 'luxon';

should();
Expand Down Expand Up @@ -34,6 +34,114 @@ context('whereFilter', () => {
result.length.should.be.equal(1);
});

it('SOME query returns true when array property has at least one matching entry', () => {
const condition = {
lines: {
some: {
status: 'good',
},
},
};
const data = {
lines: [
{
status: 'good',
},
{
status: 'bad',
},
],
};

const result = whereFilter(condition)(data);

assert.isTrue(result);
});

it('SOME query returns false when array property is empty', () => {
const condition = {
lines: {
some: {
status: 'good',
},
},
};
const data = {
lines: [],
};

const result = whereFilter(condition)(data);

assert.isFalse(result);
});

it('ALL query returns true when array property has no non-matching entries', () => {
const condition = {
lines: {
all: {
status: 'good',
},
},
};
const data = {
lines: [
{
status: 'good',
},
{
status: 'good',
},
],
};

const result = whereFilter(condition)(data);

assert.isTrue(result);
});

it('ALL query returns true when array property is empty', () => {
const condition = {
lines: {
all: {
status: 'good',
},
},
};
const data = {
lines: [],
};

const result = whereFilter(condition)(data);

assert.isTrue(result);
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess a where filter with some for an empty array will also return false.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this is based on how Array.prototype.some and Array.prototype.every (we have chosen to alias as all) work.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool


it('still works with legacy implicit scalar some matching (matches)', function() {
const condition = {
lines: 'good',
};
const data = {
lines: ['bad', 'good'],
};

const result = whereFilter(condition)(data);

assert.isTrue(result);
});

it('still works with legacy implicit scalar some matching (no match)', function() {
const condition = {
lines: 'good',
};
const data = {
lines: ['bad', 'purple'],
};

const result = whereFilter(condition)(data);

assert.isFalse(result);
});

describe('date handling', () => {
const date = DateTime.local();
const dateFixtures = [
Expand Down
14 changes: 14 additions & 0 deletions where-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,20 @@ function whereFilter(where) {
if (Array.isArray(value)) {
const matcher = where[key];

if (matcher.some) {
return value.some(function(v) {
return whereFilter(matcher.some)(v);
});
}
if (matcher.all) {
return value.every(function(v) {
return whereFilter(matcher.all)(v);
});
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So these can nest, right?

I guess we can't ever have a property called some or all as part of job data now?

{
			lines: {
				some: {
					data: {
              all: {
                  value: 'good'
              },
          },
				},
			},
		};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If they can nest, which I think they can, it would be good to have a test for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can still use "some" and "all" as object keys. They only function in a "special" way when they are applied to an array property. Much like how use of "inq" and "eq" don't stop you from using those as property names. They only kick in as "special" when they are used on certain contexts where they only makes sense as special

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh.. that's good to know.


// below is code for doing `arrayOfScalars: singleContainedValue`
// or an `neq` variant of that

// The following condition is for the case where we are querying with
// a neq filter, and when the value is an empty array ([]).
if (matcher.neq !== undefined && value.length <= 0) {
Expand Down