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

v0.17.1

Published

Serializable object assertions.

Downloads

3,426

Readme

Module for testing objects. There's a variety of uses for this module, such as schema validation.

Motivation for this is that there isn't anything of expressive/flexible enough for my needs. The closest thing would have to be Joi, but for some reason there isn't much in terms of serialization solutions. I also don't like the API...

Performance should be pretty good, since the underlying data structure used to store the data is already json-convertable (just stringify it). The method chaining only provides a more fluid api.

const Rule = require('rule.js/core'
const test = Rule()
	.equal('foo.bar', 'hello world!')
	.test;

test({
	foo: {
		bar: 'hello world!'
	}
}); // =>  true

This module can be useful for creating serializable conditions based on a certain context. In other words, user defined logic.

const data = Rule()
	.null('created')
	.serialize(); // outputs an array of objects ready to be stored.

// ... imagine pulling this out of a db
const test = Rule(data).test;

if(test({ created: new Date() })) {
	sendEmail({...}); // etc...
}

Core Comparison Methods

equal(key, value)

Does a strict equality check against a property in the object.

const result = Rule().equal('foo', 1).test({ foo: 1 }) // => true

propsEqual(key1, key2)

Does a strict equality check on two properties in the object.

const result = Rule()
	.propsEqual('foo', 'bar')
	.test({ foo: 1, bar: 1 }) // => true

null(key)

Will be true if the key is equal to null (not strict).

const result = Rule().null('foo').test({ foo: null }) // => true

true(key)

Checks if the given property is equal to true.

const result = Rule().true('foo').test({ foo: false }) // => returns false

false(key)

Checks if the given property is equal to false.

const result = Rule().false('foo').test({ foo: false }) // => true

gt(key, value)

Checks if the given property is greater than the given value.

const result1 = Rule().gt('one', 1).test({ one: 1 }) // => false

const result2 = Rule().gt('two', 1).test({ two: 2 }) // => true

lt(key, value)

Checks if the given property is less than the given value

const result = Rule().lt('one', 2).test({ one: 1 }) // => true

any(key, values)

Checks if the given property(key) is equal to any of the values.

const result = Rule().any('letter', ['a', 'b']).test({ letter: 'a' }) // => true

Not (Logical Inversion)

When you call not, the next method chain's result will be inverted.

Rule()
	.not().equal('a', 1)
	.not().exist('c')
	.test({ a: 2 }); // => true

Binding to a specific key

Rule()
	.bind('fullName')
	.equal('firstName', 'Jonathan')
	.equal('lastName', 'Boudreau')
	.end()
	.test({
		fullName: {
			firstName: 'Jonathan',
			lastName: 'Boudreau'
		}
	}); // => true

Or Statement

Rule()
	.or()
		.equal('a', 1)
		.equal('a', 2)
	.end()
	.test({ a: 2 }); // => true

Rule()
	.or()
		.equal('a', 1)
		.and()
			.equal('a', 2)
			.equal('b', 3)
		.end()
	.end()
	.test({ a: 2, b: 3 }) // => true

Composition

Because the instances are immutable, you can use an existing instance to concatenate it with another.

// you can currently do this
const cond = Rule().equal('name', 'Jonathan');
// You get a new object which will test for both properties.
const merged = Rule().equal('fullName.lastName', 'Boudreau').concat(cond);

Extensions

This module is somewhat extendable. The first argument is the series of asserts runners, and the second argument is the set of methods to add to the builder.

const objectIs = Rule.extend({
	foo(context, args) {
		return Rule.get(context, args.key) === 'bar';
	}
}, {
	foo(key) {
		return this.concat({
			type: 'foo', // this specifies what assertion to call
			key
		});
	}
});

objectIs
	.foo('a')
	.exist('today') // previous methods remain, but they can be overriden.
	.test({ today: new Date(), a: 'bar' }); // returns true!

Or even...

const eql = require('deep-eql');
const objectIs = Rule.extend({
	eql(context, args) {
		return eql(args.compare);
	}
}, {
	eql(compare) {
		return this.concat({
			type: 'eql',
			compare
		});
	}
});

To keep this module lightweight I decided to not include a ton of depdencies. Instead the module aims to be extendable for your specific needs. Mainly, this module defines a "grammar", you can add whatever "nouns" you want.