Skip to content

Commit

Permalink
allow custom ops
Browse files Browse the repository at this point in the history
  • Loading branch information
crcn committed Jul 28, 2021
1 parent 206604d commit 6926a90
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
10 changes: 5 additions & 5 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,9 @@ const createNamedOperation = (
return operationCreator(params, parentQuery, options, name);
};

export const containsOperation = (query: any) => {
export const containsOperation = (query: any, options: Options) => {
for (const key in query) {
if (key.charAt(0) === "$") return true;
if (options.operations[key]) return true;
}
return false;
};
Expand All @@ -358,7 +358,7 @@ const createNestedOperation = (
owneryQuery: any,
options: Options
) => {
if (containsOperation(nestedQuery)) {
if (containsOperation(nestedQuery, options)) {
const [selfOperations, nestedOperations] = createQueryOperations(
nestedQuery,
parentKey,
Expand Down Expand Up @@ -426,11 +426,11 @@ const createQueryOperations = (
return [selfOperations, nestedOperations];
}
for (const key in query) {
if (key.charAt(0) === "$") {
if (options.operations[key]) {
const op = createNamedOperation(key, query[key], query, options);

if (op) {
if (!op.propop && parentKey && parentKey.charAt(0) !== "$") {
if (!op.propop && parentKey && !options.operations[parentKey]) {
throw new Error(
`Malformed query. ${key} cannot be matched against property.`
);
Expand Down
2 changes: 1 addition & 1 deletion src/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class $In extends NamedBaseOperation<any> {
private _testers: Tester[];
init() {
this._testers = this.params.map(value => {
if (containsOperation(value)) {
if (containsOperation(value, this.options)) {
throw new Error(
`cannot nest $ under ${this.constructor.name.toLowerCase()}`
);
Expand Down
19 changes: 7 additions & 12 deletions 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 } = require("../src");
const { default: sift, createQueryTester, $mod, $eq } = require("../src");
const { ObjectID } = require("bson");

describe(__filename + "#", function() {
Expand Down Expand Up @@ -34,17 +34,6 @@ describe(__filename + "#", function() {
assert.equal(filtered[0], people[0]);
});

it("throws an error if the operation is invalid", function() {
var err;
try {
sift({ $aaa: 1 })("b");
} catch (e) {
err = e;
}

assert.equal(err.message, "Unsupported operation: $aaa");
});

it("can match empty arrays", function() {
var statusQuery = {
$or: [
Expand Down Expand Up @@ -530,4 +519,10 @@ describe(__filename + "#", function() {
})({ responsible: { name: "Poyan" } });
}, new Error("Malformed query. $or cannot be matched against property."));
});

it("can register an operation without $", () => {
const filter = createQueryTester({ eq: 2 }, { operations: { eq: $eq } });

assert.equal(filter(2), true);
});
});

0 comments on commit 6926a90

Please sign in to comment.