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

contracts-js

v0.6.3

Published

A contract library for JavaScript

Downloads

97

Readme

Contracts.js

Contracts.js is a contract library for JavaScript that allows you to specify invariants between parts of your code and have them checked at runtime for violations. It's heavily inspired by the contract system found in Racket and tracks blame correctly for higher-order values.

For example, you can specify the that following function takes two arguments, one that is an object with a string name field and the other that is an array filled with objects that have a loc number field, and returns a string.

import @ from "contracts.js"

@ ({name: Str}, [...{loc: Num}]) -> Str
function calcAverageLoc(person, locArr) {
    var sum = locArr.reduce(function (l1, l2) {
        return l1.loc + l2.loc;
    });
    return "Average lines of code for " +
           person.name + " was " +
           sum / locArr.length;
}

If you call the function with a bad argument:

var typoPerson = {nam: "Bob"};
calcAverageLoc(typoPerson, [{loc: 1000}, {loc: 789}, {loc: 9001}]);

you will get a helpful error message pin pointing what went wrong:

You can play around with this and other examples on the homepage.

Installation

Uses sweet.js which you can install via npm:

npm install -g sweet.js
npm install contracts-js

Using

At the top of your file you will need to use some special syntax to import contracts.js:

import @ from "contracts.js"

// rest of your code goes here...

This looks like ES6 modules but it's not really and will work with whatever module system you are using (if any). See here for details.

Compile your JavaScript file with sweet.js using the contracts.js module:

sjs --module contracts-js/macros -o output.js input.js

Then run your output.js file in any JavaScript environment. Some features of contracts.js (eg. proxied objects and arrays) require ES6 features which not every JavaScript engine supports right now (any recent version of Firefox is fine along with node.js/V8 with the --harmony flag enabled).

Documentation

Contracts.js is documented here.

Related Work

An initial stab at adding good contract syntax via macros to JavaScript was done in sweet-contracts.

rho-contracts is a contract library for vanilla JavaScript in the same style as contracts.js (in the sense that both project trace their design inspiration to the higher-order contracts in Racket). While contracts.js can work in vanilla JS (using the guard wrapper function), rho-contracts probably has better ergonomics when used as a library with no special syntax support.

TreatJS is another contract library for JavaScript.