crcn / sift.js

Use Mongodb queries in JavaScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

{$size: 0} no longer works

ricky26 opened this issue · comments

Sift version: v13.4.0

Testing against {$size: 0} now always returns false:

Welcome to Node.js v14.12.0.
Type ".help" for more information.
> const sift = require('sift').default;
undefined
> let f = sift({ $size: 0 });
undefined
> f([])
false // <-- This should return true!
> f([1])
false
> f([2])
false
> f([1, 2, 2])
false
> f = sift({ $size: 1 });
[Function (anonymous)]
> f([])
false
> f([1])
true
> f([1, 2])
false

It looks like this is due to the recent change in how $size works:

export class $Size extends NamedBaseOperation<any> {
init() {}
next(item, key: Key, parent: any) {
if (parent && parent.length === this.params) {
this.done = true;
this.success = true;
}
}
}

In the case of {$size: 0} against [], parent will not be a truthy value and it is the only value that can satisfy the size check, resulting in this expression always returning false.

Perhaps just replacing parent with Array.isArray(parent) would be enough?

After a little investigating, it seems I was mistaken at first.

It looks like $Size only operates on the parent container. With an array with one or more elements, this works, since all of the child elements will have the same parent with the right size. However, when the array contains no elements, the $Size operator is executed on the array itself. In the case of just {$size: 0}, the $Size test operates on undefined.

Hmm, I'll need to shift some stuff around - sorry for not getting back to this sooner. Will have some time next week to fix this.

Fixed & published as 13.4.0! Let me know if you have any more issues.