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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@ayapapa-npm/contracts-js

v0.3.0

Published

A lightweight Design by Contract library for JavaScript.

Downloads

893

Readme

CI

contracts-js

A lightweight Design by Contract library for JavaScript. Provides runtime contract checks based on Design by Contract principles. All check functions return the evaluated condition itself, so they can be used directly in control flow when exception throwing is suppressed.

Installation

Install from npm registry

Available now.

npm install @ayapapa-npm/contracts-js

Install directly from GitHub Release:

Available now. Check for the latest version before installation: https://github.com/ayapapa/contracts-js/releases

npm install https://github.com/ayapapa/contracts-js/releases/download/X.Y.Z/ayapapa-npm-contracts-js-X.Y.Z.tgz

If you see the error:

npm error code EALLOWREMOTE
npm error Fetching packages of type "remote" have been disabled

allow remote packages:

npm config set allow-remote all

Then run the install command again:

npm install https://github.com/ayapapa/contracts-js/releases/download/X.Y.Z/ayapapa-npm-contracts-js-X.Y.Z.tgz

Contract Types

contracts-js provides runtime checks based on Design by Contract principles.

The library provides four types of contracts:

| Contract | Purpose | When to use | Responsibility | | ----------- | ---------------------- | ---------------------------------------------------------- | ----------------------- | | REQUIRE | Precondition | Check conditions before execution starts | Caller | | VERIFY | Intermediate condition | Check assumptions or intermediate results during execution | Internal process | | ENSURE | Postcondition | Check conditions after execution completes | Function | | INVARIANT | State consistency | Check conditions that must remain valid over time | Object / Data structure |

Quick Guide

REQUIRE - "Can this operation start?"

Use REQUIRE to validate conditions that must be satisfied before calling a function.

Examples:

  • Function arguments are valid.
  • Required objects exist.
  • Required external conditions are available.
Contracts.REQUIRE(
  user !== null,
  'User is required'
);

VERIFY - "Is the current processing state valid?"

Use VERIFY to check intermediate assumptions or temporary states during execution.

Examples:

  • Intermediate calculation results.
  • Internal processing states.
  • Temporary assumptions.

Do not use VERIFY for input validation. Use REQUIRE for conditions required before execution.

Contracts.VERIFY(
  result >= 0,
  'Intermediate result must not be negative'
);

ENSURE - "Did the operation complete correctly?"

Use ENSURE to verify guarantees provided by a function after execution.

Examples:

  • Return values are valid.
  • State changes completed correctly.
  • Processing results satisfy expected conditions.
Contracts.ENSURE(
  result !== null,
  'Result must be available'
);

INVARIANT - "Is the object still valid?"

Use INVARIANT to verify conditions that represent the internal consistency of an object or data structure.

Examples:

  • Internal values remain consistent.
  • Object state rules are maintained.
  • Data structure integrity is protected.
Contracts.INVARIANT(
  balance >= 0,
  'Balance cannot be negative'
);

Usage

import { Contracts } from '@ayapapa-npm/contracts-js';
// or:
// import Contracts from '@ayapapa-npm/contracts-js';

// CommonJS:
// const { Contracts } = require('@ayapapa-npm/contracts-js');

// Enable debug mode
Contracts.setConfig({ debug: true });


// REQUIRE: Check a precondition
function divide(a, b) {
  Contracts.REQUIRE(
    b !== 0,
    'Divisor cannot be zero'
  );

  return a / b;
}


// ENSURE: Check a postcondition
function getPositiveNumber(x) {
  Contracts.REQUIRE(
    x > 0,
    'Input must be positive'
  );

  const result = x * 2;

  Contracts.ENSURE(
    result > 0,
    'Result must be positive'
  );

  return result;
}


// INVARIANT: Check object state consistency
class BankAccount {
  constructor(balance) {
    this.balance = balance;
  }

  withdraw(amount) {
    Contracts.REQUIRE(
      amount >= 0,
      'Amount must not be negative'
    );

    this.balance -= amount;

    Contracts.INVARIANT(
      this.balance >= 0,
      'Balance cannot be negative'
    );
  }
}


// VERIFY: Check an intermediate condition
function processAccount(account) {
  const calculatedBalance = calculateBalance(account);

  Contracts.VERIFY(
    calculatedBalance >= 0,
    'Intermediate balance is invalid'
  );

  return calculatedBalance;
}

XXX_DEBUG() performs the same action as XXX() in debug mode; otherwise, no action.