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-kit/core

v0.2.1

Published

modern async rules engine

Downloads

5

Readme

@rule-kit/core

modern async rules engine.


Very experimental. A side project to explore the building blocks of composable software systems. The goal is to create something useful but who knows if this is the right path. Please check it out. Constructive thoughts are welcome.


When to use

  • Use were hand-coding javascript logic isn't practical or safe.
    • No code tools where users don't want to or can't be trusted to write javascript
    • Large and complex or dynamic forms where hand-coded validation is unreliable.
    • Workflows
    • Data processing

Features

  • No runtime dependencies in core engine, or transpiling (just needs async/await)
  • Small core, easy to read and if needed copypasta and hack up.
  • Specify rules in JSON which allows for
    • dynamic rules
    • fetch rules from server
    • run the same rules on client and server
  • compile a rule once at runtime and execute the rule multiple times
    • the compile function converts the rules to executeable code with out the use of eval or any other shady hacks.
  • Mix and match sync and async rules.
  • Rule Groups
    • and all rules must be true for group to be true (this is the default)
    • or if any rule returns true then the group is true
  • Groups can be nested.
  • The rule or rule group that causes the rule to be false is returned as errorRule
  • Add custom operators (these are just simple sync or async functions)
  • Operators can compare a field to a literal value or another field in the data object

Built-in Operators

  • is
  • is_empty
  • not_empty

See roadmap for more operators to come.

Installation

npm install @rule-kit/core

Basic Usage


import {compile, defaultOperators} from '@rule-kit/core';

const rules = [
    { field: 'first', operator: 'is', value: 'foo' },
    { field: 'last', not: true, operator: 'is', value: 'A' },
    { field: 'last', not: true, operator: 'is', value: 'C' },
];

const rule = compile({ rules: rules, operators:baseOperators })

let {result, errorRule} = await rule({
    first: 'foo',
    last: 'B'
});

console.log('Results (True)', result, errorRule);
// true, null

let {result, errorRule} = await rule({
    first: 'foo',
    last: 'A'
});

console.log('Results (False)', result, errorRule);
// false,  {  field: 'first', operator: 'is', value: 'foo' }

Nested Rules


const rule = { 
    operator: 'or', 
    rules:[
        { field: 'first', operator: 'is', value: 'foo' },
        {
            operator: 'or', 
            rules: [
                { field: 'last', operator: 'is', value: 'foo' },
                { field: 'last', operator: 'is', value: 'blort' }
            ]
        }
    ]
}

const rule = compile({ rules: rule })

let {result, errorRule} = await rule({
    first: 'foo',
    last: 'A'
});
// result === false (because last isn't foo or blort)

let {result, errorRule} = await rule({
    first: 'foo',
    last: 'foo'
});
// result === true (because first and last are foo )

In the above example the the field first must have a value or foo and the field last must have a value of either foo or blort

Compare one field to another


//value can be an object not just a literal. 
// in this case provide the field to compare
const rules = [{ field: 'password', operator: 'is', value: { field: 'confirmpass' } }];

const rule = compile({ rules });

let {result, errorRule} = await rule({
    password: 'foo',
    confirmpass: 'foo'
});
// result === true (because they match)


let {result, errorRule} = await rule({{
    password: 'foo',
    confirmpass: 'dddd'
});
// result === false (because they don't match)

Custom Operators

An operator is just a function it will receive to arguments

  • data the object that contains the data the function is to evaulate
  • config the configuration of the rule. this will contain at least the key field or the location of the data being evaulated by this rule.

const getValue = (data, config) => (config.value.field ? data[config.value.field] : config.value)

const sleep = ms => {
    return new Promise(resolve => setTimeout(resolve, ms))
}

function is_empty(data, config) {
    const test = data[config.field];
    return !test || (test.trim && test.trim().length === 0)
}

const defaultInputs = (config) => (config?.value?.field ? [config.field, config?.value?.field] : [config.field]);
const fieldOnlyInput = (config) => ([config.field]);

const defaultOperators = {
    is: {
        type: 'sync',
        inputs: defaultInputs,
        fn: (data, config) => (data[config.field] === getValue(data, config)),
    },
    async_is: {
        type: 'async',
        inputs: defaultInputs,
        fn: async (data, config) => {
            return sleep(config.time || 1000).then(() => data[config.field] === getValue(data, config))
        }
    },
    is_empty: {
        type: 'sync',
        inputs: fieldOnlyInput,
        fn: is_empty
    },
    not_empty: {
        type: 'sync',
        inputs: fieldOnlyInput,
        fn: (data, config) => !is_empty(data, config)
    }
}

Road Map

  • Real docs
  • More tests
  • Sync only comple (if you don't need async functions)
  • More Operators
    • Strings
    • Math/Numbers
    • Dates
    • Arrays
  • Extensions
    • react useRule hook
    • utlity to display user friendly localized error messages (eg form validation)
    • GUI builder