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

ruler

v1.0.0

Published

composable assertions

Downloads

1,213

Readme

Build Status

ruler

composable object testing

npm:

npm install ruler

Features

  • simple implementation, < 100 lines of code.
  • built-in comparisons
    • eq
    • neq
    • contains
    • matches
    • gt
    • gte
    • lt
    • lte
  • extendable (see plugins)
  • composable (see composition)

Usage

ruler is meant for just simple, top-down testing; there is not need or desire to have a nested trees of "child" conditions, etc. You can build your tests via progamatically, or an array of objects with cmp, path and value properties. The later of the two is ugly, but might be handy if you need to store the "rules" in a database or something.

Build assertions programatically:

var ruler = require('ruler')
  , assert = require('assert');

var engine = ruler()
  .is('name.first', 'john')
  .not('name.last', 'buzz')
  .contains('company', 'red')
  .gte('age', 21);

var result = engine.test({
  name: {
    first: 'john',
    last: 'doe'
  },
  age: 21,
  company: 'redventures'
});

assert.equal(result, true);

Pass in an arrray of key/value rules:

var ruler = require('ruler')
  , assert = require('assert');

var obj = {
  name: {
    first: 'john',
    last: 'doe'
  },
  age: 21,
  company: 'redventures'
};

var filters = [
  { cmp: 'is', path: 'name.first', value: 'john' },
  { cmp: 'not', path: 'name.last', value: 'buzz' },
  { cmp: 'contains', path: 'company', value: 'red' },
  { cmp: 'gte', path: 'age', value: 21 }
];

var engine = ruler(filters);
var result = engine.test(obj);

assert.equal(result, true);

Composition

Rules sets can be created seperately for their own use, but then composed together in order to create more complex rules without having to create duplicated rules.

var ruler = require('ruler')
  , assert = require('assert');

var names = ruler()
  .is('name.first', 'john')
  .not('name.last', 'buzz')

var info = ruler()
  .contains('company', 'red')
  .gte('age', 21);

var all = names.and(info);

var result = all.test({
  name: {
    first: 'john',
    last: 'doe'
  },
  age: 21,
  company: 'redventures'
});

assert.equal(result, true);

Plugins

You can plugin custom "comparators" if the baked in one's do not fit your needs.

function doubled(actual, total){
  return (actual * 2) == total;
}

var engine = ruler()
  .use(doubled, 'price', 40);

engine.test({
  price: 20
});

TODO

  • optimize, every() is probably slow.
  • "works" with component, but uses bunch of ES5 stuff...
  • more built-in comparators?

License

Copyright 2012 Red Ventures

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with the License. You may obtain a copy of the License in the LICENSE file, or at:

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.