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

grule

v0.1.1

Published

A simple and powerful mechanism for validating rules in JSON.

Downloads

54

Readme

About

Grule is a minimal mechanism for testing conditions against values using a JSON scheme. Its main objective is to work as a Rete mechanism and to solve all operations in a performatic and simple way.

Installation

Using NPM

npm i grule

Using Yarn

yarn add grule

Use

To set up your rules scheme, just follow the steps below.

Create an instance

To create a test instance, you will need a load of facts to power the engine.

// Import Engine
import { Grule } from 'grule'

// Create an type
type IUser = {
  id: number
  name: string
}

// Create metadata
const metadata: IUser = {
  id: 3,
  name: 'test',
}

// Create instance
const grule = new Grule<IUser>(metadata)

Subscribe rules

After creating an instance of the engine, pre-loading with facts, the next step is to register the rules. To register the rules, you must import the IRules <T> interface and create a set of rules for the attributes declared in the facts.

The rules follow the structure of the data declared in the facts. For each attribute a rule will be executed.

The rule creation function offers 2 arguments, the first being the attributes and the second the events.

You can use a logical expression for each test and perform an action you want, or you can use the when and then event to test and perform an action based on the result. This is a good option for dynamic testing.

After creating an instance and registering the rules, you can simply execute the run method by passing the rules defined as a parameter. At the end of the tests, if there is no error that interrupts the flow of the tests, a Boolean value will be returned.

  • true all conditions have passed.
  • false one or all of the conditions failed.
// ... Previous code

// Create Rules
const rules: IRules<IUser> = ({ id, name }, { when }) => ({
  id: when(id.diff(1)).then(() => {
    throw new Error('User not allowed.')
  }),
  name: name.in(['foo', 'test']),
})

// Enroll rules
grule.run(rules)

You can also simplify the previous flow in a cleaner way. (Example of final code)

// Import Engine
import { Grule } from 'grule'

// Create an type
type IUser = {
  id: number
  name: string
}

// Create instance
new Grule<IUser>({
  id: 3,
  name: 'test',
}).run(({ id, name }, { when }) => ({
  id: when(id.diff(1)).then(() => {
    throw new Error('User not allowed.')
  }),
  name: name.in(['foo', 'test']),
}))

Available methods

For Attributes

Each attribute has 9 methods available for the tests that are.

  • less
// Type: less(value: ILess): boolean
// Acceptable: ['number', 'bigint']
  • lessOrEqual
// Type: lessOrEqual(value: ILess): boolean
// Acceptable: ['number', 'bigint']
  • greater
// Type: greater(value: IGreater): boolean
// Acceptable: ['number', 'bigint']
  • greaterOrEqual
// Type: greaterOrEqual(value: IGreater): boolean
// Acceptable: ['number', 'bigint']
  • equal
// Type: equal(value: IEqual): boolean
// Acceptable: ['bigint', 'boolean', 'number', 'string', 'date']
  • diff
// Type: diff(value: IEqual): boolean
// Acceptable: ['bigint', 'boolean', 'number', 'string', 'date']
  • in
// Type: in(value: In): boolean
// Acceptable: ['bigint', 'boolean', 'number', 'string', 'array']
  • notIn
// Type: notIn(value: In): boolean
// Acceptable: ['bigint', 'boolean', 'number', 'string', 'array']
  • eval
// Type: eval(operator: IOperatorsList, arg1: A): boolean
// Acceptable: [Idle]

For Events

Grule currently has only one event than when. It returns a promise with a boolean result from the test performed.

  • when
// Type: when(test: boolean): Promise<boolean>
// Acceptable: ['boolean']

Contributing

Grule is in an initial version without many features yet, but you can feel free to send your suggestion or open a PR.