rule-judgment
TypeScript icon, indicating that this package has built-in type declarations

1.1.5 • Public • Published

rule-judgment

A query statement similar to mongodb, judge and retrieve data.

NPM Version NPM Downloads Build Status Gratipay

Installation

$ npm install rule-judgment
#
$ yarn add rule-judgment

Features

Usages

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 } ]

API

ruleJudgment (query: MongoQuery, options?: Options): (data: any) => boolean

Creates a filter with all of the built-in MongoDB query operations.

  • query - the filter to use against the target data
  • options - context hash
  • data - 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]

Supported Operators

$lt

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]

$lte

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]

$gt

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]

$gte

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]

$eq

Matches values that are equal to a specified value.

// types: any

['admin', 'thondery', 'test'].filter( ruleJudgment({ $eq: 'thondery' }) )
// ['thondery']

$ne

Matches all values that are not equal to a specified value.

// types: any

['admin', 'thondery', 'test'].filter( ruleJudgment({ $ne: 'thondery' }) )
// ['admin', 'test']

$regex

Selects documents where values match a specified regular expression.

// types: string

['admin', 'thondery', 'test'].filter( ruleJudgment({ $regex: /thondery/i }) )
// ['thondery']

$mod

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]

$in

Matches any of the values specified in an array.

// types: any

['admin', 'thondery', 'test'].filter( ruleJudgment({ $in: ['thondery', 'admin'] }) )
// ['admin', 'thondery']

$nin

Matches none of the values specified in an array.

// types: any

['admin', 'thondery', 'test'].filter( ruleJudgment({ $nin: ['thondery', 'admin'] }) )
// ['test']

$_in

Match any value in the array.

// 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.

// 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.

// 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'] ]

$exists

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]

$type

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]

$where

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']

$and

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']

$or

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]

$not

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]

$nor

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]

License

this repo is released under the MIT License.

Readme

Keywords

Package Sidebar

Install

npm i rule-judgment

Weekly Downloads

9

Version

1.1.5

License

MIT

Unpacked Size

34.3 kB

Total Files

12

Last publish

Collaborators

  • thondery