sift
Advanced tools
Comparing version 11.0.7 to 11.0.8
@@ -162,2 +162,3 @@ var __extends = (this && this.__extends) || (function () { | ||
export { EqualsOperation }; | ||
export var createEqualsOperation = function (params, owneryQuery, options) { return new EqualsOperation(params, owneryQuery, options); }; | ||
var NopeOperation = /** @class */ (function (_super) { | ||
@@ -164,0 +165,0 @@ __extends(NopeOperation, _super); |
import * as defaultOperations from "./operations"; | ||
import { createQueryTester, EqualsOperation } from "./core"; | ||
import { createQueryTester, EqualsOperation, createEqualsOperation } from "./core"; | ||
var createDefaultQueryTester = function (query, _a) { | ||
@@ -10,3 +10,3 @@ var _b = _a === void 0 ? {} : _a, compare = _b.compare, operations = _b.operations; | ||
}; | ||
export { EqualsOperation, createQueryTester }; | ||
export { EqualsOperation, createQueryTester, createEqualsOperation }; | ||
export default createDefaultQueryTester; |
132
index.d.ts
@@ -1,78 +0,80 @@ | ||
export type SupportedType = | ||
| string | ||
| { [index: string]: RegExp | any } | ||
| number | ||
| null | ||
| any; | ||
export type KeyOrValue<T extends SupportedType> = T; | ||
// export type SupportedType = | ||
// | string | ||
// | { [index: string]: RegExp | any } | ||
// | number | ||
// | null | ||
// | any; | ||
// export type KeyOrValue<T extends SupportedType> = T; | ||
export type ElemMatch<T> = { [P in keyof T]?: SiftQuery<T[P]> }; | ||
// export type ElemMatch<T> = { [P in keyof T]?: SiftQuery<T[P]> }; | ||
export type Query<T extends SupportedType> = { | ||
$eq?: T; | ||
$ne?: T; | ||
$or?: T[]; | ||
$gt?: T; | ||
$gte?: T; | ||
$lt?: T; | ||
$lte?: T; | ||
$mod?: number[]; | ||
$in?: T[]; | ||
$nin?: T[]; | ||
$not?: SiftQuery<T>; | ||
$type?: any; | ||
$all?: T[]; | ||
$size?: number; | ||
$nor?: T[]; | ||
$and?: T[]; | ||
$regex?: RegExp | string; | ||
$elemMatch?: ExternalQuery<T>; | ||
$exists?: boolean; | ||
$where?: string | WhereFn<T>; | ||
}; | ||
// export type Query<T extends SupportedType> = { | ||
// $eq?: T; | ||
// $ne?: T; | ||
// $or?: T[]; | ||
// $gt?: T; | ||
// $gte?: T; | ||
// $lt?: T; | ||
// $lte?: T; | ||
// $mod?: number[]; | ||
// $in?: T[]; | ||
// $nin?: T[]; | ||
// $not?: SiftQuery<T>; | ||
// $type?: any; | ||
// $all?: T[]; | ||
// $size?: number; | ||
// $nor?: T[]; | ||
// $and?: T[]; | ||
// $regex?: RegExp | string; | ||
// $elemMatch?: ExternalQuery<T>; | ||
// $exists?: boolean; | ||
// $where?: string | WhereFn<T>; | ||
// }; | ||
export interface InternalQuery<T extends SupportedType> extends Query<T> {} | ||
// export interface InternalQuery<T extends SupportedType> extends Query<T> {} | ||
export type ExternalQuery<T extends SupportedType> = ElemMatch<T>; | ||
// export type ExternalQuery<T extends SupportedType> = ElemMatch<T>; | ||
export type WhereFn<T extends SupportedType> = ( | ||
this: T, | ||
value: T, | ||
index: number, | ||
array: T | ||
) => boolean; | ||
// export type WhereFn<T extends SupportedType> = ( | ||
// this: T, | ||
// value: T, | ||
// index: number, | ||
// array: T | ||
// ) => boolean; | ||
export type FilterFn = <T>(value: T, index?: number, array?: T[]) => boolean; | ||
// export type FilterFn = <T>(value: T, index?: number, array?: T[]) => boolean; | ||
export type SiftQuery<T extends SupportedType> = | ||
| ExternalQuery<T> | ||
| InternalQuery<T>; | ||
// export type SiftQuery<T extends SupportedType> = | ||
// | ExternalQuery<T> | ||
// | InternalQuery<T>; | ||
export type PluginDefinition<T> = { | ||
[index: string]: (a: T, b: T) => boolean | number; | ||
}; | ||
// export type PluginDefinition<T> = { | ||
// [index: string]: (a: T, b: T) => boolean | number; | ||
// }; | ||
export type PluginFunction<T> = (sift: Sift) => PluginDefinition<T>; | ||
// export type PluginFunction<T> = (sift: Sift) => PluginDefinition<T>; | ||
export type Exec = <T extends SupportedType>(array: T) => T; | ||
// export type Exec = <T extends SupportedType>(array: T) => T; | ||
type Options<T> = { | ||
expressions?: { | ||
[identifier: string]: ( | ||
item: T, | ||
query: SiftQuery<T>, | ||
options: Options<T> | ||
) => boolean; | ||
}; | ||
}; | ||
// type Options<T> = { | ||
// expressions?: { | ||
// [identifier: string]: ( | ||
// item: T, | ||
// query: SiftQuery<T>, | ||
// options: Options<T> | ||
// ) => boolean; | ||
// }; | ||
// }; | ||
export interface Sift { | ||
<T extends SupportedType>( | ||
query: SiftQuery<T>, | ||
options?: Options<T> | ||
): FilterFn; | ||
compare<T, K>(a: T, b: K): 0 | -1 | 1; | ||
} | ||
// export interface Sift { | ||
// <T extends SupportedType>( | ||
// query: SiftQuery<T>, | ||
// options?: Options<T> | ||
// ): FilterFn; | ||
// compare<T, K>(a: T, b: K): 0 | -1 | 1; | ||
// } | ||
declare const Sift: Sift; | ||
export default Sift; | ||
// declare const Sift: Sift; | ||
// export default Sift; | ||
export * from "./core"; |
@@ -1,4 +0,4 @@ | ||
const lib = require('./lib'); | ||
const lib = require("./lib"); | ||
module.exports = lib.default; | ||
Object.assign(module.exports, lib); | ||
Object.assign(module.exports, lib); |
@@ -164,2 +164,3 @@ "use strict"; | ||
exports.EqualsOperation = EqualsOperation; | ||
exports.createEqualsOperation = function (params, owneryQuery, options) { return new EqualsOperation(params, owneryQuery, options); }; | ||
var NopeOperation = /** @class */ (function (_super) { | ||
@@ -166,0 +167,0 @@ __extends(NopeOperation, _super); |
@@ -7,2 +7,3 @@ "use strict"; | ||
exports.EqualsOperation = core_1.EqualsOperation; | ||
exports.createEqualsOperation = core_1.createEqualsOperation; | ||
var createDefaultQueryTester = function (query, _a) { | ||
@@ -9,0 +10,0 @@ var _b = _a === void 0 ? {} : _a, compare = _b.compare, operations = _b.operations; |
@@ -1,1 +0,1 @@ | ||
export * from "./es5m/operations"; | ||
export * from "./es5m/operations"; |
{ | ||
"name": "sift", | ||
"description": "mongodb query style array filtering", | ||
"version": "11.0.7", | ||
"version": "11.0.8", | ||
"repository": "crcn/sift.js", | ||
@@ -6,0 +6,0 @@ "sideEffects": false, |
@@ -395,2 +395,18 @@ **Installation**: `npm install sift`, or `yarn add sift` | ||
### Date comparison | ||
Mongodb allows you to do date comparisons like so: | ||
```javascript | ||
db.collection.find({ createdAt: { $gte: "2018-03-22T06:00:00Z" } }); | ||
``` | ||
In Sift, you'll need to specify a Date object: | ||
```javascript | ||
collection.find( | ||
sift({ createdAt: { $gte: new Date("2018-03-22T06:00:00Z") } }) | ||
); | ||
``` | ||
## Custom behavior | ||
@@ -427,9 +443,9 @@ | ||
#### Tree-shaking | ||
#### Omitting built-in operations | ||
If you're looking to create a filter _without_ the standard operations, you can also do that: | ||
You can create a filter function that omits the built-in operations like so: | ||
```javascript | ||
import { createQueryTester } from "sift/lib/core"; | ||
import * as operations from "sift/lib/operations"; | ||
import { createQueryTester } from "sift"; | ||
import { $in, $all, $nin, $lt } from "sift/operations"; | ||
const test = createQueryTester( | ||
@@ -439,3 +455,3 @@ { | ||
}, | ||
{ operations } | ||
{ $in, $all, $nin, $lt } | ||
); | ||
@@ -446,16 +462,2 @@ | ||
#### Date comparison | ||
Mongodb allows you to do date comparisons like so: | ||
```javascript | ||
db.collection.find({ createdAt: { $gte: "2018-03-22T06:00:00Z" } }); | ||
``` | ||
In Sift, you'll need to specify a Date object: | ||
```javascript | ||
collection.find( | ||
sift({ createdAt: { $gte: new Date("2018-03-22T06:00:00Z") } }) | ||
); | ||
``` | ||
For bundlers like `Webpack` and `Rollup`, operations that aren't used are omitted from application bundles via tree-shaking. |
@@ -219,2 +219,8 @@ import { | ||
export const createEqualsOperation = ( | ||
params: any, | ||
owneryQuery: any, | ||
options: Options | ||
) => new EqualsOperation(params, owneryQuery, options); | ||
export class NopeOperation<TParam> extends BaseOperation<TParam> { | ||
@@ -221,0 +227,0 @@ next() { |
import * as defaultOperations from "./operations"; | ||
import { Query, Options, createQueryTester, EqualsOperation } from "./core"; | ||
import { | ||
Query, | ||
Options, | ||
createQueryTester, | ||
EqualsOperation, | ||
createEqualsOperation | ||
} from "./core"; | ||
@@ -14,4 +20,4 @@ const createDefaultQueryTester = ( | ||
export { Query, EqualsOperation, createQueryTester }; | ||
export { Query, EqualsOperation, createQueryTester, createEqualsOperation }; | ||
export default createDefaultQueryTester; |
18
test.js
@@ -1,12 +0,14 @@ | ||
import {createQueryTester, EqualsOperation} from "./es5m"; | ||
import { createQueryTester, EqualsOperation } from "./es5m"; | ||
import * as operations from "./operations"; | ||
const tester = createQueryTester({ $test: 1 }, { | ||
operations: { | ||
$where: defaultOperations.$where, | ||
$test: (params, ownertQuery) => { | ||
return new EqualsOperation(params, ownertQuery); | ||
const tester = createQueryTester( | ||
{ $test: 1 }, | ||
{ | ||
operations: { | ||
$where: defaultOperations.$where, | ||
$test: (params, ownertQuery) => { | ||
return new EqualsOperation(params, ownertQuery); | ||
} | ||
} | ||
} | ||
}); | ||
); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
327830
3140
460