npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rule-judgment

v1.1.5

Published

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

Downloads

33

Readme

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.