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 🙏

© 2025 – Pkg Stats / Ryan Hefner

js-contracts

v1.1.1

Published

C# inspired Code Contracts

Readme

js-contracts

C# inspired code contracts. Using typescript decorators we can have postcondition and preconditions

Installation

npm install js-contracts --save

Usage

PLEASE NOTE

This package relies on use of decorators. For this package to work, you need to enable experimentalDecorators option in your tsconfig.json

This packages relies on decorators to implement post and pre conditions. Although some functions do not require decorators usage, so you can use some functionality of this package in functional code as well.

Methods that rely on usage of post and pre conditions require method and Contract Condition to have same signatures. If you are using data from 'this' then you need to pass calling class as a last argument. Take a look in examples section for more details about this case.

Setup

Setup contracts with Contract.setSettings first. You can use default settings without setup. Contracts export Log and ContractFailedError classes for you to extend and define in settings.

Contract Usage Examples

Assert

testMethod(argument) {
  Contract.Assert((argument) => argument !== null, 'fail message')(argument);
}

Exists

testMethod(argument) {
  const collection = [1,2,3];
  Contract.Exists(collection, (item) => item !== null, 'fail message');
}

ForAll

testMethod(argument) {
  const collection = [1,2,3];
  Contract.ForAll(collection, (item) => item === null, 'fail message');
}

Requires

This function requires same amount of arguments as in calling method. If you are using 'this', pass class as a last argument.

class Test {
  @Contract.Requires((argument) => argument !== null, 'fail message');
  testMethod(argument) {
    // do something
  }
}

Class properties usage

class Test {
  constructor() {
    super();

    this.classProperty = null;
  }

  @Contract.Requires((argument, TestClass: Test) => argument !== null && TestClass.classProperty !== null, 'fail message');
  testMethod(argument) {
    // do something
  }
}

Ensures

This function requires same amount of arguments as in calling method. If you are using 'this', pass class as a last argument. Additionally you could use function OldValue, OldValueByPath and Result within this function.

Basic example

class Test {
  @Contract.Ensures((argument) => argument !== null, 'fail message');
  testMethod(argument) {
    // do something
  }
}

OldValue example. OldValue parses the function to cache all arguments first. Path to 'argument' is determined automatically.

class Test {
  @Contract.Ensures((argument) => argument === Contract.OldValue(argument), 'fail message');
  testMethod(argument) {
    argument = 'new value';
    return argument;
  }
}

OldValueByPath example. OldValueByPath parses the function to cache all arguments first. Path to 'argument' is passed by you.

class Test {
  constructor() {
    super();

    this.classProperty = null;
  }

  @Contract.Ensures((argument, TestClass: Test) => argument === Contract.OldValueByPath('TestClass.classProperty'), 'fail message');
  testMethod(argument) {
    argument = 'new value';
    return argument;
  }
}

ContractResult example. ContractResult caches function result.

class Test {
  constructor() {
    super();

    this.classProperty = null;
  }

  @Contract.Ensures((argument, TestClass: Test) => Contract.ContractResult() === Contract.OldValueByPath('TestClass.classProperty'), 'fail message');
  testMethod(argument) {
    argument = 'new value';
    return argument;
  }
}