Skip to content

Commit

Permalink
enhanced queryer type.
Browse files Browse the repository at this point in the history
  • Loading branch information
thondery committed Jun 29, 2021
1 parent 501dcdd commit 0805679
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 4 deletions.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -250,7 +251,7 @@ function assign (key: string, value: any): [ RegExp, any ] {
return [ search, value ]
}

function ruleJudgment (query: Partial<any>, options?: Partial<any>): (data: any) => boolean {
function ruleJudgment<T = any> (query: T extends object ? FilterQuery<T> : QuerySelector<T>, options?: Partial<any>): (data: any) => boolean {
return function (data: any): boolean {
return judgment(data, query, options)
}
Expand Down
16 changes: 16 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
59 changes: 58 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,61 @@ export function parseAssign (data: any, options?: Partial<any>): any
export function isDateString (value: string): boolean
export function emit (value: string): any

export default function (query: Partial<any>, options?: Partial<any>): (data: any) => boolean
export declare type FilterQuery<T> = {
[P in keyof T]?: T[P] | QuerySelector<T[P]>
} & {
$where ?: (item: T) => boolean
$and ?: Array<FilterQuery<T>>
$or ?: Array<FilterQuery<T>>
$nor ?: Array<FilterQuery<T>>
}

export type QuerySelector<T> = {
//
$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<infer U> ? number | QuerySelector<number> : never
$exists ?: boolean
$type ?: BSONTypeAlias
$not ?: T extends string ? QuerySelector<T> | RegExp | string : QuerySelector<T>
$where ?: (item: T) => boolean
$and ?: Array<QuerySelector<T>>
$or ?: Array<QuerySelector<T>>
$nor ?: Array<QuerySelector<T>>
}

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<T = any> (query: T extends object ? FilterQuery<T> : QuerySelector<T>, options?: Partial<any>): (data: any) => boolean

0 comments on commit 0805679

Please sign in to comment.