Skip to content

Commit

Permalink
fix(conditions): moves logic related to compare complex types to @uca…
Browse files Browse the repository at this point in the history
…st/mongo2js
  • Loading branch information
stalniy committed Aug 11, 2020
1 parent 177966d commit 9bd6a1b
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/casl-ability/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@
"extra"
],
"dependencies": {
"@ucast/mongo2js": "^1.0.0"
"@ucast/mongo2js": "^1.1.1"
}
}
23 changes: 1 addition & 22 deletions packages/casl-ability/src/matchers/conditions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import {
$exists,
exists,
createFactory,
equal,
createGetter
} from '@ucast/mongo2js';
import type { MongoQuery } from '@ucast/mongo2js';
import { ConditionsMatcher } from '../types';
Expand Down Expand Up @@ -63,32 +61,13 @@ const defaultInterpreters = {
exists,
};

interface HasToJSON {
toJSON(): unknown
}

function toPrimitive(value: unknown) {
if (value instanceof Date) {
return value.getTime();
}

if (value && typeof (value as HasToJSON).toJSON === 'function') {
return (value as HasToJSON).toJSON();
}

return value;
}

const isEqual: typeof equal = (a, b) => equal(toPrimitive(a), toPrimitive(b));
const getField = createGetter((object, field) => toPrimitive(object[field]));

type MongoQueryMatcher =
(...args: Partial<Parameters<typeof createFactory>>) => ConditionsMatcher<MongoQuery>;
export const buildMongoQueryMatcher: MongoQueryMatcher = (instructions, interpreters, options) => {
return createFactory(
{ ...defaultInstructions, ...instructions },
{ ...defaultInterpreters, ...interpreters },
{ equal: isEqual, get: getField, ...options }
options
);
};

Expand Down

0 comments on commit 9bd6a1b

Please sign in to comment.