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

@yawaramin/dbc

v0.3.0

Published

This is a small library to help you program in a design-by-contract style in JavaScript and ReasonML (i.e. BuckleScript).

Downloads

1

Readme

@yawaramin/dbc

This is a small library to help you program in a design-by-contract style in JavaScript and ReasonML (i.e. BuckleScript).

Rationale

When targeting JavaScript, even when using a safer language like ReasonML, it's nice to have more guarantees about invariants in your programs. Types provide some of those guarantees, but not all. Design-by- contract style allows you to catch problems as soon as possible–right at the start and end of function bodies.

But this note on defensive programming from the excellent Cornell CS3110 course is worth mentioning here:

Sometimes programmers worry unnecessarily that defensive programming will be too expensive—either in terms of the time it costs them to implement the checks initially, or in the run-time costs that will be paid in checking assertions. These concerns are far too often misplaced. The time and money it costs society to repair faults in software suggests that we could all afford to have programs that run a little more slowly.

Usage

For JavaScript, usage is as follows.

contract(body[, pre[, post]])

The body is the body of the function itself, and is required. pre is an array of (array) pairs of string descriptions and boolean preconditions, and is optional. post is a function that takes the final result of the function being contracted, and returns an array of pairs of postcondition descriptions and booleans.

Throws a TypeError at the first precondition or postcondition violation.

pre, post

These are older APIs; you'll probably want to use contract instead.

Example

const {contract} = require('@yawaramin/dbc');

const safeDiv = (num, denom) => contract(
  num / denom,
  /* pre: */ [
    ['num >= denom', num >= denom],
    ['denom !== 0', denom !== 0],
  ],
  /* post: */ result => [
    ['safeDiv(num, denom) * denom === num', result * denom === num],
  ],
);

const makeUser = (name, age) => contract(
  {name, age},
  /* pre: */ [['age >= 13', age >= 13]],
  // post is optional
);

ReasonML/BuckleScript

See the src/Yawaramin__Dbc.mli file for detailed ReasonML documentation on the functions, and src/Test.re for example usage.

ReasonML usage has an extra feature if you need it: all checks can be erased for production use. The idea being that you test thoroughly with all checks turned on during development, and deploy with them turned off (if you need to). See below for the command.

Tips

For best results, don't use this library to check types (including 'x is not null'). I recommend using a typechecker (like ReasonML or TypeScript) to do that. Use pre and post to check invariants that can't easily be expressed as types, like 'weight must be 5 kg minimum'.

Build

Development:

npm run build

Production:

NODE_ENV=production npm run build # this turns off all checks

If you actually decide to keep checks on in production (imho a good thing), you can always keep them on explicitly:

NODE_ENV= npm run build

Test

npm test