A query statement similar to mongodb, judge and retrieve data.
$ npm install rule-judgment
#
$ yarn add rule-judgment
- Supported operators: $lt, $lte, $gt, $gte, $eq, $ne, $regex, $mod, $in, $nin, $_in, $_nin, $size, $exists, $type, $where, $and, $or, $not, $nor
- Regexp searches
- Supports node.js, and web
import ruleJudgment from 'rule-judgment'
// target data is non-object data
const filter = ruleJudgment({ $lt: 5 })
// target data is object data
const filter = ruleJudgment({ level: { $lt: 5 } })
// use context
const context = {
$__username: 'thondery',
$__level: 5
}
const filter = ruleJudgment({
$or: [
{ username: '$__username' },
{ level: { $gte: '$__level' }}
]
}, context)
[
{ username: 'admin', level: 9 },
{ username: 'thondery', level: 5 },
{ username: 'test', level: 1 },
].filter( filter )
// [ { username: 'admin', level: 9 }, { username: 'thondery', level: 5 } ]
Creates a filter with all of the built-in MongoDB query operations.
query
- the filter to use against the target dataoptions
-context
hashdata
- target data
Example:
import ruleJudgment from 'rule-judgment'
const filter = ruleJudgment({ $lt: 5 })
filter(6) // false
filter(4) // true
[0, 1, 2, 3, 4, 5].filter( filter )
// [0, 1, 2, 3, 4]
Matches values that are less than a specified value.
// types: number | bigint | Date
[0, 1, 2, 3, 4, 5].filter( ruleJudgment({ $lt: 3 }) )
// [0, 1, 2]
Matches values that are less than or equal to a specified value.
// types: number | bigint | Date
[0, 1, 2, 3, 4, 5].filter( ruleJudgment({ $lte: 3 }) )
// [0, 1, 2, 3]
Matches values that are greater than a specified value.
// types: number | bigint | Date
[0, 1, 2, 3, 4, 5].filter( ruleJudgment({ $gt: 3 }) )
// [4, 5]
Matches values that are greater than or equal to a specified value.
// types: number | bigint | Date
[0, 1, 2, 3, 4, 5].filter( ruleJudgment({ $gte: 3 }) )
// [3, 4, 5]
Matches values that are equal to a specified value.
// types: any
['admin', 'thondery', 'test'].filter( ruleJudgment({ $eq: 'thondery' }) )
// ['thondery']
Matches all values that are not equal to a specified value.
// types: any
['admin', 'thondery', 'test'].filter( ruleJudgment({ $ne: 'thondery' }) )
// ['admin', 'test']
Selects documents where values match a specified regular expression.
// types: string
['admin', 'thondery', 'test'].filter( ruleJudgment({ $regex: /thondery/i }) )
// ['thondery']
Performs a modulo operation on the value of a field and selects documents with a specified result.
// types: number | bigint
[0, 1, 2, 3, 4, 5].filter( ruleJudgment({ $mod: [2, 0] }) )
// [0, 2, 4]
Matches any of the values specified in an array.
// types: any
['admin', 'thondery', 'test'].filter( ruleJudgment({ $in: ['thondery', 'admin'] }) )
// ['admin', 'thondery']
Matches none of the values specified in an array.
// types: any
['admin', 'thondery', 'test'].filter( ruleJudgment({ $nin: ['thondery', 'admin'] }) )
// ['test']
Match any value in the array.
// types: any[]
ruleJudgment({ $_in: 'thondery' })(['admin', 'thondery', 'test'])
// true
ruleJudgment({ $_in: ['admin', 'thondery'] })(['admin', 'thondery', 'test'])
// true
Match any value not in the array.
// types: any[]
ruleJudgment({ $_nin: 'thondery' })(['admin', 'thondery', 'test'])
// false
ruleJudgment({ $_nin: ['admin', 'thondery'] })(['admin', 'thondery', 'test'])
// false
Selects documents if the array field is a specified size.
// types: any[]
[['admin', 'thondery', 'test'], [0, 1, 2, 3, 4, 5]].filter( ruleJudgment({ $size: 6 }) )
// [ [0, 1, 2, 3, 4, 5] ]
[['admin', 'thondery', 'test'], [0, 1, 2, 3, 4, 5]].filter( ruleJudgment({ $size: { $lt: 5 } }) )
// [ ['admin', 'thondery', 'test'] ]
Matches documents that have the specified field.
// types: any
['test', 0, 1, 2, 3, null, undefined, true, false].filter( ruleJudgment({ $exists: true }) )
// ['test', 0, 1, 2, 3, true, false]
['test', 0, 1, 2, 3, null, undefined, true, false].filter( ruleJudgment({ $exists: false }) )
// [null, undefined]
Selects documents if a field is of the specified type.
// types: any
['test', 0, 1, 2, 3, null, undefined, true, false].filter( ruleJudgment({ $type: 'number' }) )
// [0, 1, 2, 3]
Matches documents that satisfy a JavaScript expression.
// types: any
['test', 0, 1, 2, 3, null, undefined, true, false].filter( ruleJudgment({ $where: item => item === 'test' }) )
// ['test']
Joins query clauses with a logical AND returns all documents that match the conditions of both clauses.
// types: any
['test', 0, 1, 2, 3, null, undefined, true, false].filter( ruleJudgment({ $and: [{ $eq: 'test' }, { $type: 'string' }] }) )
// ['test']
Joins query clauses with a logical OR returns all documents that match the conditions of either clause.
// types: any
['test', 0, 1, 2, 3, null, undefined, true, false].filter( ruleJudgment({ $or: [{ $eq: 'test' }, { $type: 'boolean' }] }) )
// ['test', true, false]
Inverts the effect of a query expression and returns documents that do not match the query expression.
// types: any
['test', 0, 1, 2, 3, null, undefined, true, false].filter( ruleJudgment({ $not: { $eq: 'test' } }) )
// [0, 1, 2, 3, null, undefined, true, false]
Joins query clauses with a logical NOR returns all documents that fail to match both clauses.
// types: any
['test', 0, 1, 2, 3, null, undefined, true, false].filter( ruleJudgment({ $nor: [{ $eq: 'test' }, { $type: 'boolean' }] }) )
// [0, 1, 2, 3, null, undefined]
this repo is released under the MIT License.