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

assert-logic

v1.1.0

Published

This repository/project is a template/placeholder for new NPM packages.

Downloads

7

Readme

assert-logic

Downloads Version@npm Version@git CI Docs

This handy tool can be used to implement additional logic into your assertions, either using Chai, Jest, Assert, or plain JavaScript.

Usage

Install the package:

npm install assert-logic --save-dev

Use it in your tests:

const {and, or} = require('assert-logic');
const {expect} = require('chai');

test("Test if a number is even and greater than 10 or less than 7", () => {
    const number = 12;
    and(
        () => expect(number).to.be.an('number'),
        () => expect(number % 2).to.equal(0),
        or(
            () => expect(number).to.be.lessThan(7),
            () => expect(number).to.be.greaterThan(10),
        ),
    ).evaluate();
});

API

Pass/Fail

In the context of this package, a value is considered failed if it is:

  1. a (sync) function that throws an error
  2. a (sync) function that returns a falsy value (except for undefined)
  3. a (async) function that returns a Promise that rejects
  4. a (async) function that returns a Promise that resolves to a falsy value (except for undefined)
  5. a Promise that rejects
  6. a Promise that resolves to a falsy value (except for undefined)
  7. value that is falsy (except for undefined)

In all other cases, the value is considered passed.

Operations

The API contains the following functions: and, or, not, xor, nand, nor, xnor, every, some, and also the evaluate function.

The following ones accept any number of sync/async functions, Promises, or values as arguments.

| Operation | Description | |-----------|---------------------------------------------------------| | and | Passes if all of its arguments pass | | or | Passes if any of its arguments pass | | not | Negates the result of its argument | | xor | Passes if odd number of its arguments passes | | nand | Passes if any of its arguments fails | | nor | Passes if all of its arguments fail | | xnor | Passes if all of its arguments pass or all of them fail |

The every and some functions are used to evaluate an array of values. The values must be the first parameter, but both functions accept a second parameter that is a function that will be called with each value. If the function is not passed as the second parameter, the value will be interpreted as boolean values.

The every function is a shorthand for and with the values as arguments, while the some function is a shorthand for or with the values as arguments.

const {every, some} = require('assert-logic');
const {expect} = require('expect');

const items = [{id: 1, name: 'Item 1', price: 10}, {id: 2, name: 'Item 2', price: -20}, /* ... */];
every(items, item => {
    expect(item.id, "Item should have ID as positive number").toBeGreaterThan(0);
}).evaluate(); // will not throw
some(items, item => {
    expect(item.price, "Item should have a positive price").toBeGreaterThan(0);
}).evaluate(); // will throw

Append

The append function can be used to append additional logic to an existing variadic logic (all except pass).

const {and} = require('assert-logic');

const logic = and(
    () => true,
    () => false,
);
logic.append(() => true).evaluate(); // will throw an error

Evaluation

The evaluate function will evaluate the logic and throw an error if it fails.

The evaluate function does not need to be called if the logic is used as an argument to an assertion, as the assertion will call it automatically.

The evaluate function will return a Promise if any of the arguments are async.

AssertionError

Given the following expression what is expected to fail:

and(
    xor(true, false, true),
    true,
).evaluate()

The error message will look like this:

AssertionError (AND): Expected all expression to pass, but not all did.
Results:
  - AssertionError (XOR): Expected odd number of expressions to pass, but even number did.
    Results:
      - Pass
      - AssertionError (PASS): Expected expression to pass.
        Results:
          - Error: "Failed expression: (boolean false)"
        Expression: (boolean false)
      - Pass
    Expression: |-
      XOR(
        (boolean true)
        (boolean false)
        (boolean true)
      )
  - Pass
Expression: |-
  AND(
    XOR(
      (boolean true)
      (boolean false)
      (boolean true)
    )
    (boolean true)
  )

How NOT to use it

As a replacement for assertions

This package is not meant to be used as a replacement for assertions, but rather as a tool to implement additional logic into your assertions.

With passing the assertions directly

const {and} = require('assert-logic');
const {expect} = require('chai');

test("Test if a number is even and greater than 10 or less than 7", () => {
    const number = 12;
    and(
        expect(number).to.be.an('number'),
        expect(number % 2).to.equal(0),
        or(
            expect(number).to.be.lessThan(7),
            expect(number).to.be.greaterThan(10),
        ),
    ).evaluate();
});

This will not work as expected, as the assertions will be evaluated before the logic is evaluated.

Other

For detailed documentation see the TypeDocs documentation.

This package uses debug for logging, use assert-logic to see debug logs:

DEBUG=assert-logic node my-script.js