Skip to content

Commit

Permalink
fix #239
Browse files Browse the repository at this point in the history
  • Loading branch information
crcn committed Dec 2, 2021
1 parent 7446bc1 commit c65e91c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
11 changes: 9 additions & 2 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,19 @@ const createNamedOperation = (
) => {
const operationCreator = options.operations[name];
if (!operationCreator) {
throw new Error(`Unsupported operation: ${name}`);
throwUnsupportedOperation(name);
}
return operationCreator(params, parentQuery, options, name);
};

const throwUnsupportedOperation = (name: string) => {
throw new Error(`Unsupported operation: ${name}`);
};

export const containsOperation = (query: any, options: Options) => {
for (const key in query) {
if (options.operations.hasOwnProperty(key)) return true;
if (options.operations.hasOwnProperty(key) || key.charAt(0) === "$")
return true;
}
return false;
};
Expand Down Expand Up @@ -441,6 +446,8 @@ const createQueryOperations = (
if (op != null) {
selfOperations.push(op);
}
} else if (key.charAt(0) === "$") {
throwUnsupportedOperation(key);
} else {
nestedOperations.push(
createNestedOperation(key.split("."), query[key], key, query, options)
Expand Down
2 changes: 1 addition & 1 deletion test/async-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const assert = require("assert");
const sift = require("../src");
const sift = require("../lib");

describe(__filename + "#", () => {
[
Expand Down
11 changes: 10 additions & 1 deletion test/basic-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const assert = require("assert");
const _eval = require("eval");
const { default: sift, createQueryTester, $mod, $eq } = require("../src");
const { default: sift, createQueryTester, $mod, $eq } = require("../lib");
const { ObjectID } = require("bson");

describe(__filename + "#", function() {
Expand Down Expand Up @@ -544,6 +544,15 @@ describe(__filename + "#", function() {
assert.equal(test({ _id: new ObjectID("610b6bc9e29dbd1bb5f045bf") }), true);
});

// fix https://github.com/crcn/sift.js/issues/239
it("Throws an error if an operation is not found", () => {
assert.throws(() => {
sift({
_id: { $notFound: "blah" }
});
}, new Error("Unsupported operation: $notFound"));
});

it("Empty $or/$and/$nor throws error if empty", () => {
assert.throws(() => {
sift({ $or: [] });
Expand Down
7 changes: 5 additions & 2 deletions test/operations-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ const assert = require("assert");
var ObjectID = require("bson").ObjectID;
const MongoClient = require("mongodb").MongoClient;
const { promisify } = require("util");
const { default: sift, createQueryTester } = require("../src");
const defaultOperations = require("../src/operations");
const {
default: sift,
createQueryTester,
...defaultOperations
} = require("../lib");

describe(__filename + "#", function() {
[
Expand Down

0 comments on commit c65e91c

Please sign in to comment.