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

Fix bugs #244

Merged
merged 6 commits into from
Feb 15, 2022
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ src/*.js
lib/
es5m/
es/
*.tgz
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 16.0.0

- Fix https://github.com/crcn/sift.js/issues/243
- Fix https://github.com/crcn/sift.js/issues/242

## 13.1.0

- Added stronger types for queries: https://github.com/crcn/sift.js/issues/197
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "sift",
"description": "MongoDB query filtering in JavaScript",
"version": "15.1.3",
"version": "16.0.0",
"repository": "crcn/sift.js",
"sideEffects": false,
"author": {
Expand Down Expand Up @@ -51,6 +51,7 @@
"es",
"es5m",
"lib",
"src",
"*.d.ts",
"*.js.map",
"index.js",
Expand Down
61 changes: 31 additions & 30 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ export interface Operation<TItem> {
readonly done: boolean;
propop: boolean;
reset();
next(item: TItem, key?: Key, owner?: any);
next(item: TItem, key?: Key, owner?: any, root?: boolean);
}

export type Tester = (item: any, key?: Key, owner?: any) => boolean;
export type Tester = (
item: any,
key?: Key,
owner?: any,
root?: boolean
) => boolean;

export interface NamedOperation {
name: string;
Expand Down Expand Up @@ -100,7 +105,7 @@ const walkKeyPathValues = (
}

if (depth === keyPath.length || item == null) {
return next(item, key, owner);
return next(item, key, owner, depth === 0);
}

return walkKeyPathValues(
Expand All @@ -113,14 +118,16 @@ const walkKeyPathValues = (
);
};

abstract class BaseOperation<TParams, TItem = any> implements Operation<TItem> {
export abstract class BaseOperation<TParams, TItem = any>
implements Operation<TItem> {
keep: boolean;
done: boolean;
abstract propop: boolean;
constructor(
readonly params: TParams,
readonly owneryQuery: any,
readonly options: Options
readonly options: Options,
readonly name?: string
) {
this.init();
}
Expand All @@ -129,21 +136,7 @@ abstract class BaseOperation<TParams, TItem = any> implements Operation<TItem> {
this.done = false;
this.keep = false;
}
abstract next(item: any, key: Key, parent: any);
}

export abstract class NamedBaseOperation<TParams, TItem = any>
extends BaseOperation<TParams, TItem>
implements NamedOperation {
abstract propop: boolean;
constructor(
params: TParams,
owneryQuery: any,
options: Options,
readonly name: string
) {
super(params, owneryQuery, options);
}
abstract next(item: any, key: Key, parent: any, root: boolean);
}

abstract class GroupOperation extends BaseOperation<any> {
Expand All @@ -170,17 +163,19 @@ abstract class GroupOperation extends BaseOperation<any> {
}
}

abstract next(item: any, key: Key, owner: any);
abstract next(item: any, key: Key, owner: any, root: boolean);

/**
*/

protected childrenNext(item: any, key: Key, owner: any) {
protected childrenNext(item: any, key: Key, owner: any, root: boolean) {
let done = true;
let keep = true;
for (let i = 0, { length } = this.children; i < length; i++) {
const childOperation = this.children[i];
childOperation.next(item, key, owner);
if (!childOperation.done) {
childOperation.next(item, key, owner, root);
}
if (!childOperation.keep) {
keep = false;
}
Expand Down Expand Up @@ -216,8 +211,8 @@ export class QueryOperation<TItem> extends GroupOperation {
/**
*/

next(item: TItem, key: Key, parent: any) {
this.childrenNext(item, key, parent);
next(item: TItem, key: Key, parent: any, root: boolean) {
this.childrenNext(item, key, parent, root);
}
}

Expand Down Expand Up @@ -249,8 +244,13 @@ export class NestedOperation extends GroupOperation {
/**
*/

private _nextNestedValue = (value: any, key: Key, owner: any) => {
this.childrenNext(value, key, owner);
private _nextNestedValue = (
value: any,
key: Key,
owner: any,
root: boolean
) => {
this.childrenNext(value, key, owner, root);
return !this.done;
};
}
Expand Down Expand Up @@ -304,23 +304,24 @@ export const numericalOperationCreator = (
createNumericalOperation: OperationCreator<any>
) => (params: any, owneryQuery: any, options: Options, name: string) => {
if (params == null) {
return new NopeOperation(params, owneryQuery, options);
return new NopeOperation(params, owneryQuery, options, name);
}

return createNumericalOperation(params, owneryQuery, options, name);
};

export const numericalOperation = (createTester: (any) => Tester) =>
numericalOperationCreator(
(params: any, owneryQuery: Query<any>, options: Options) => {
(params: any, owneryQuery: Query<any>, options: Options, name: string) => {
const typeofParams = typeof comparable(params);
const test = createTester(params);
return new EqualsOperation(
b => {
return typeof comparable(b) === typeofParams && test(b);
},
owneryQuery,
options
options,
name
);
}
);
Expand Down
68 changes: 45 additions & 23 deletions src/operations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
NamedBaseOperation,
BaseOperation,
EqualsOperation,
Options,
createTester,
Expand All @@ -15,7 +15,7 @@ import {
} from "./core";
import { Key, comparable, isFunction, isArray } from "./utils";

class $Ne extends NamedBaseOperation<any> {
class $Ne extends BaseOperation<any> {
readonly propop = true;
private _test: Tester;
init() {
Expand All @@ -33,7 +33,7 @@ class $Ne extends NamedBaseOperation<any> {
}
}
// https://docs.mongodb.com/manual/reference/operator/query/elemMatch/
class $ElemMatch extends NamedBaseOperation<Query<any>> {
class $ElemMatch extends BaseOperation<Query<any>> {
readonly propop = true;
private _queryOperation: QueryOperation<any>;
init() {
Expand All @@ -58,7 +58,7 @@ class $ElemMatch extends NamedBaseOperation<Query<any>> {
this._queryOperation.reset();

const child = item[i];
this._queryOperation.next(child, i, item);
this._queryOperation.next(child, i, item, false);
this.keep = this.keep || this._queryOperation.keep;
}
this.done = true;
Expand All @@ -69,7 +69,7 @@ class $ElemMatch extends NamedBaseOperation<Query<any>> {
}
}

class $Not extends NamedBaseOperation<Query<any>> {
class $Not extends BaseOperation<Query<any>> {
readonly propop = true;
private _queryOperation: QueryOperation<any>;
init() {
Expand All @@ -80,16 +80,17 @@ class $Not extends NamedBaseOperation<Query<any>> {
);
}
reset() {
super.reset();
this._queryOperation.reset();
}
next(item: any, key: Key, owner: any) {
this._queryOperation.next(item, key, owner);
next(item: any, key: Key, owner: any, root: boolean) {
this._queryOperation.next(item, key, owner, root);
this.done = this._queryOperation.done;
this.keep = !this._queryOperation.keep;
}
}

export class $Size extends NamedBaseOperation<any> {
export class $Size extends BaseOperation<any> {
readonly propop = true;
init() {}
next(item) {
Expand All @@ -110,7 +111,7 @@ const assertGroupNotEmpty = (values: any[]) => {
}
};

class $Or extends NamedBaseOperation<any> {
class $Or extends BaseOperation<any> {
readonly propop = false;
private _ops: Operation<any>[];
init() {
Expand Down Expand Up @@ -152,15 +153,13 @@ class $Nor extends $Or {
}
}

class $In extends NamedBaseOperation<any> {
class $In extends BaseOperation<any> {
readonly propop = true;
private _testers: Tester[];
init() {
this._testers = this.params.map(value => {
if (containsOperation(value, this.options)) {
throw new Error(
`cannot nest $ under ${this.constructor.name.toLowerCase()}`
);
throw new Error(`cannot nest $ under ${this.name.toLowerCase()}`);
}
return createTester(value, this.options.compare);
});
Expand All @@ -182,15 +181,36 @@ class $In extends NamedBaseOperation<any> {
}
}

class $Nin extends $In {
class $Nin extends BaseOperation<any> {
readonly propop = true;
next(item: any, key: Key, owner: any) {
super.next(item, key, owner);
this.keep = !this.keep;
private _in: $In;
constructor(params: any, ownerQuery: any, options: Options, name: string) {
super(params, ownerQuery, options, name);
this._in = new $In(params, ownerQuery, options, name);
}
next(item: any, key: Key, owner: any, root: boolean) {
this._in.next(item, key, owner);

if (isArray(owner) && !root) {
if (this._in.keep) {
this.keep = false;
this.done = true;
} else if (key == owner.length - 1) {
this.keep = true;
this.done = true;
}
} else {
this.keep = !this._in.keep;
this.done = true;
}
}
reset() {
super.reset();
this._in.reset();
}
}

class $Exists extends NamedBaseOperation<boolean> {
class $Exists extends BaseOperation<boolean> {
readonly propop = true;
next(item: any, key: Key, owner: any) {
if (owner.hasOwnProperty(key) === this.params) {
Expand Down Expand Up @@ -218,8 +238,8 @@ class $And extends NamedGroupOperation {

assertGroupNotEmpty(params);
}
next(item: any, key: Key, owner: any) {
this.childrenNext(item, key, owner);
next(item: any, key: Key, owner: any, root: boolean) {
this.childrenNext(item, key, owner, root);
}
}

Expand All @@ -239,8 +259,8 @@ class $All extends NamedGroupOperation {
name
);
}
next(item: any, key: Key, owner: any) {
this.childrenNext(item, key, owner);
next(item: any, key: Key, owner: any, root: boolean) {
this.childrenNext(item, key, owner, root);
}
}

Expand Down Expand Up @@ -281,7 +301,9 @@ export const $in = (
owneryQuery: Query<any>,
options: Options,
name: string
) => new $In(params, owneryQuery, options, name);
) => {
return new $In(params, owneryQuery, options, name);
};

export const $lt = numericalOperation(params => b => b < params);
export const $lte = numericalOperation(params => b => b <= params);
Expand Down
30 changes: 30 additions & 0 deletions test/basic-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,4 +564,34 @@ describe(__filename + "#", function() {
sift({ $nor: [] });
}, new Error("$and/$or/$nor must be a nonempty array"));
});

it(`supports implicit $and`, () => {
const result = [
{
tags: ["animal", "dog"]
},
{
tags: ["animal", "cat"]
},
{
tags: ["animal", "mouse"]
}
].filter(
sift({
tags: {
$in: ["animal"],
$nin: ["mouse"]
}
})
);

assert.deepEqual(result, [
{
tags: ["animal", "dog"]
},
{
tags: ["animal", "cat"]
}
]);
});
});