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

@cowwoc/requirements

v4.0.8

Published

A fluent API for enforcing design contracts with automatic message generation.

Downloads

1,019

Readme

npm version build-status

Requirements API

API Changelog java

A fluent API for enforcing design contracts with automatic message generation:

✔️ Easy to use
✔️ Fast
✔️ Production-ready

To get started, add this dependency:

npm install --save @cowwoc/[email protected]

or pnpm:

pnpm add @cowwoc/[email protected]

Usage Example

import {requireThatString} from "@cowwoc/requirements";

class Cake
{
	private bitesTaken = 0;
	private piecesLeft;

	public constructor(piecesLeft: number)
	{
		requireThat(piecesLeft, "piecesLeft").isPositive();
		this.piecesLeft = piecesLeft;
	}

	public eat(): number
	{
		++bitesTaken;
		assertThat(bitesTaken, "bitesTaken").isNotNegative().elseThrow();

		piecesLeft -= ThreadLocalRandom.current().nextInt(5);

		assertThat(piecesLeft, "piecesLeft").isNotNegative().elseThrow();
		return piecesLeft;
	}

	public getFailures(): String[]
	{
		return checkIf(bitesTaken, "bitesTaken").isNotNegative().
			and(checkIf(piecesLeft, "piecesLeft").isGreaterThan(3)).
			elseGetMessages();
	}
}

If you violate a precondition:

const cake = new Cake(-1000);

You'll get:

RangeError: "piecesLeft" must be positive.
actual: -1000

If you violate a class invariant:

const cake = new Cake(1_000_000);
while (true)
	cake.eat();

You'll get:

lang.AssertionError: "bitesTaken" may not be negative.
actual: -128

If you violate a postcondition:

const cake = new Cake(100);
while (true)
	cake.eat();

You'll get:

AssertionError: "piecesLeft" may not be negative.
actual: -4

If you violate multiple conditions at once:

const cake = new Cake(1);
cake.bitesTaken = -1;
cake.piecesLeft = 2;
const failures = [];
for (const failure of cake.getFailures())
	failures.add(failure);
console.log(failures.join("\n\n"));

You'll get:

"bitesTaken" may not be negative.
actual: -1

"piecesLeft" must be greater than 3.
actual: 2

Features

This library offers the following features:

Entry Points

Designed for discovery using your favorite IDE's auto-complete feature. The main entry points are:

See the API documentation for more details.

Best practices

  • Use checkIf().elseGetMessages() to return failure messages without throwing an exception. This is the fastest validation approach, ideal for web services.
  • To enhance the clarity of failure messages, you should provide parameter names, even when they are optional. In other words, favor assert that(value, name) over assert that(value).

Related Projects

  • http://chaijs.com/
  • https://github.com/dsheiko/bycontract
  • https://github.com/muroc/offensive.js

License

Code licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0

Icons made by Flat Icons from www.flaticon.com is licensed by CC 3.0 BY