diff --git a/README.md b/README.md index df9fc34..474c3cc 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ $ yarn add rule-judgment ## Features -- Supported operators: [\$lt](#lt), [\$lte](#lte), [\$gt](#gt), [\$gte](#gte), [\$eq](#eq), [\$ne](#ne), [\$regex](#regex), [\$mod](#mod), [\$in](#in), [\$nin](#nin), [\$size](#size), [\$exists](#exists), [\$type](#type), [\$where](#where), [\$and](#and), [\$or](#or), [\$not](#not), [\$nor](#nor) +- Supported operators: [\$lt](#lt), [\$lte](#lte), [\$gt](#gt), [\$gte](#gte), [\$eq](#eq), [\$ne](#ne), [\$regex](#regex), [\$mod](#mod), [\$in](#in), [\$nin](#nin), [\$_in](#-in), [\$_nin](#-nin), [\$size](#size), [\$exists](#exists), [\$type](#type), [\$where](#where), [\$and](#and), [\$or](#or), [\$not](#not), [\$nor](#nor) - Regexp searches - Supports node.js, and web @@ -188,6 +188,32 @@ Matches none of the values specified in an array. // ['test'] ``` +### \$_in + +Match any value in the array. + +```js +// types: any[] + +ruleJudgment({ $_in: 'thondery' })(['admin', 'thondery', 'test']) +// true +ruleJudgment({ $_in: ['admin', 'thondery'] })(['admin', 'thondery', 'test']) +// true +``` + +### \$_nin + +Match any value not in the array. + +```js +// types: any[] + +ruleJudgment({ $_nin: 'thondery' })(['admin', 'thondery', 'test']) +// false +ruleJudgment({ $_nin: ['admin', 'thondery'] })(['admin', 'thondery', 'test']) +// false +``` + ### \$size Selects documents if the array field is a specified size. diff --git a/package.json b/package.json index 6902350..3b25653 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rule-judgment", - "version": "1.1.3", + "version": "1.1.4", "description": "A query statement similar to mongodb, judge and retrieve data.", "main": "dist/index.js", "typings": "types/index.d.ts", diff --git a/src/index.ts b/src/index.ts index fd41fd2..8eb1d55 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ import { isEqual, isString, isDate, isArray, intersection, toPairs, isPlainObject, isEmpty, isUndefined, isNumber, isNull, result } from 'lodash' +import { FilterQuery, QuerySelector } from '../types' let __BigInt try { @@ -250,7 +251,7 @@ function assign (key: string, value: any): [ RegExp, any ] { return [ search, value ] } -function ruleJudgment (query: Partial, options?: Partial): (data: any) => boolean { +function ruleJudgment (query: T extends object ? FilterQuery : QuerySelector, options?: Partial): (data: any) => boolean { return function (data: any): boolean { return judgment(data, query, options) } diff --git a/tests/index.test.ts b/tests/index.test.ts index 1e30269..c65435f 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -64,6 +64,22 @@ describe('\nTests', () => { let result = ruleJudgment({ $nin: ['thondery', 'admin'] })('test') expect(result).toBe(true) }) + test('Operator $_in: [\'thondery\', \'admin\'] of \'thondery\'', () => { + let result = ruleJudgment({ $_in: 'thondery' })(['thondery', 'admin']) + expect(result).toBe(true) + }) + test('Operator $_in: [\'thondery\', \'admin\'] of [\'thondery\']', () => { + let result = ruleJudgment({ $_in: ['thondery'] })(['thondery', 'admin']) + expect(result).toBe(true) + }) + test('Operator $_nin: [\'thondery\', \'admin\'] not of \'test\'', () => { + let result = ruleJudgment({ $_nin: 'test' })(['thondery', 'admin']) + expect(result).toBe(true) + }) + test('Operator $_nin: [\'thondery\', \'admin\'] not of [\'test\']', () => { + let result = ruleJudgment({ $_nin: ['test'] })(['thondery', 'admin']) + expect(result).toBe(true) + }) test('Operator $size: [\'thondery\', \'admin\'].length === 2', () => { let result = ruleJudgment({ $size: 2 })(['thondery', 'admin']) expect(result).toBe(true) diff --git a/types/index.d.ts b/types/index.d.ts index e991578..cbf9089 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -3,4 +3,61 @@ export function parseAssign (data: any, options?: Partial): any export function isDateString (value: string): boolean export function emit (value: string): any -export default function (query: Partial, options?: Partial): (data: any) => boolean \ No newline at end of file +export declare type FilterQuery = { + [P in keyof T]?: T[P] | QuerySelector +} & { + $where ?: (item: T) => boolean + $and ?: Array> + $or ?: Array> + $nor ?: Array> +} + +export type QuerySelector = { + // + $lt ?: T extends (number | bigint | Date) ? number | bigint | Date : never + $lte ?: T extends (number | bigint | Date) ? number | bigint | Date : never + $gt ?: T extends (number | bigint | Date) ? number | bigint | Date : never + $gte ?: T extends (number | bigint | Date) ? number | bigint | Date : never + $eq ?: T + $ne ?: T + $regex ?: T extends string ? RegExp | string : never + $mod ?: T extends (number | bigint) ? T[] : never + $in ?: T[] + $nin ?: T[] + $_in ?: T | T[] + $_nin ?: T | T[] + $size ?: T extends ReadonlyArray ? number | QuerySelector : never + $exists ?: boolean + $type ?: BSONTypeAlias + $not ?: T extends string ? QuerySelector | RegExp | string : QuerySelector + $where ?: (item: T) => boolean + $and ?: Array> + $or ?: Array> + $nor ?: Array> +} + +export type BSONTypeAlias = + | 'number' + | 'double' + | 'string' + | 'object' + | 'array' + | 'binData' + | 'undefined' + | 'objectId' + | 'bool' + | 'date' + | 'null' + | 'regex' + | 'dbPointer' + | 'javascript' + | 'symbol' + | 'javascriptWithScope' + | 'int' + | 'timestamp' + | 'long' + | 'decimal' + | 'minKey' + | 'maxKey' + +export default function (query: T extends object ? FilterQuery : QuerySelector, options?: Partial): (data: any) => boolean \ No newline at end of file