Skip to content

Commit

Permalink
Merge pull request crcn#259 from crcn/fix-255
Browse files Browse the repository at this point in the history
Fix 255
  • Loading branch information
crcn authored Mar 4, 2023
2 parents 51f22a5 + 0022d32 commit 1c613bf
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 23 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 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": "17.0.0",
"version": "17.0.1",
"repository": "crcn/sift.js",
"sideEffects": false,
"author": {
Expand Down
20 changes: 6 additions & 14 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
Comparator,
isVanillaObject,
comparable,
equals
equals,
coercePotentiallyNull
} from "./utils";

export interface Operation<TItem> {
Expand Down Expand Up @@ -294,21 +295,9 @@ export const createEqualsOperation = (
options: Options
) => new EqualsOperation(params, owneryQuery, options);

export class NopeOperation<TParam> extends BaseOperation<TParam> {
readonly propop = true;
next(item) {
this.done = true;
this.keep = item == null;
}
}

export const numericalOperationCreator = (
createNumericalOperation: OperationCreator<any>
) => (params: any, owneryQuery: any, options: Options, name: string) => {
if (params == null) {
return new NopeOperation(params, owneryQuery, options, name);
}

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

Expand All @@ -319,7 +308,10 @@ export const numericalOperation = (createTester: (any) => Tester) =>
const test = createTester(params);
return new EqualsOperation(
b => {
return typeof comparable(b) === typeofParams && test(b);
const actualValue = coercePotentiallyNull(b);
return (
typeof comparable(actualValue) === typeofParams && test(actualValue)
);
},
owneryQuery,
options,
Expand Down
19 changes: 13 additions & 6 deletions src/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import {
Query,
NamedGroupOperation,
numericalOperation,
containsOperation,
NamedOperation
containsOperation
} from "./core";
import { Key, comparable, isFunction, isArray } from "./utils";

Expand Down Expand Up @@ -305,10 +304,18 @@ export const $in = (
return new $In(params, owneryQuery, options, name);
};

export const $lt = numericalOperation(params => b => b < params);
export const $lte = numericalOperation(params => b => b <= params);
export const $gt = numericalOperation(params => b => b > params);
export const $gte = numericalOperation(params => b => b >= params);
export const $lt = numericalOperation(params => b => {
return b != null && b < params;
});
export const $lte = numericalOperation(params => b => {
return b === params || b <= params;
});
export const $gt = numericalOperation(params => b => {
return b != null && b > params;
});
export const $gte = numericalOperation(params => b => {
return b === params || b >= params;
});
export const $mod = (
[mod, equalsValue]: number[],
owneryQuery: Query<any>,
Expand Down
3 changes: 3 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export const comparable = (value: any) => {
return value;
};

export const coercePotentiallyNull = (value: any) =>
value == null ? null : value;

export const isArray = typeChecker<Array<any>>("Array");
export const isObject = typeChecker<Object>("Object");
export const isFunction = typeChecker<Function>("Function");
Expand Down
13 changes: 13 additions & 0 deletions test/operations-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ describe(__filename + "#", function() {
[{ $lte: "5" }, [4, 3, 2, 1], [], false],
[{ $lte: "5" }, ["4", "3", "2"], ["4", "3", "2"], false],
[{ ab: { $lte: null } }, [{ ab: 5 }, { cd: 5 }], [{ cd: 5 }], false],
[
{ ab: { $lte: "blue" } },
[{ ab: "blue" }, { cd: 5 }],
[{ ab: "blue" }],
false
],
[
{ groups: { $lt: 5 } },
[{ groups: [1, 2, 3, 4] }, { groups: [7, 8] }],
Expand All @@ -212,6 +218,13 @@ describe(__filename + "#", function() {
// $gte
[{ $gte: 5 }, [3, 4, 5, 6], [5, 6], false],
[{ $gte: "5" }, [4, 3, 2, 1], [], false],
[{ ab: { $gte: null } }, [{ ab: 5 }, { cd: 5 }], [{ cd: 5 }], false],
[
{ ab: { $gte: "blue" } },
[{ ab: "blue" }, { cd: 5 }],
[{ ab: "blue" }],
false
],
[
{ groups: { $gte: 5 } },
[{ groups: [1, 2, 3, 4] }, { groups: [7, 8] }],
Expand Down

0 comments on commit 1c613bf

Please sign in to comment.