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

denuto

v0.1.2

Published

Design by contract for JavaScript

Downloads

12

Readme

Denuto

Denuto enables design by contract programming in JavaScript. It provides four different ways to specify contracts:

  • contract - a higher order function,
  • invariant - a class decorator,
  • requires - a property decorator,
  • ensures - a property decorator

Denuto throws run-time errors when contracts are violated in development. Design by contract can provide a number of advantages:

  • document behaviour inline with the defintition,
  • reduce sanity check tests,
  • reduce need for defensive programming,
  • encourage developers to think about the domain

However, it is not the solution for everything. It is not a replacement for testing, contracts can only specify general rules, not specific cases. It also tends to work best when dealing with domain logic. For example, it is more effective for specifying invariants in state, than the contract for a rendering function.

NOTE: Similary to core-decorators, this library makes use of stage-0 decorators. The spec has changed since moving to stage-2, and transpilers are yet to implement the changes. The implementation of this library will likely have to change, but hopefully not the interface. Regardless, this project will not go to 1.0 until then. I recommend locking your dependency to PATCH.

Getting Started

Install via NPM:

npm install --save denuto

Using:

import { contract, invariant, requires, ensures } from 'denuto'

const fn = () => {}
const contractedFn = contract({ pre: () => true, post: () => true })(fn)

@invariant(self => self.on || !self.on && self.speed === 0)
class Car {
  constructor() {
    this.on = false
    this.speed = 0
  }

  @requires(self => !self.on)
  turnOn() {
    this.on = true
  }

  @requires(self => self.on)
  @ensures((self, old) => self.speed > old.speed)
  accelerate() {
    this.speed += 5
  }

  @ensures((self, old) => self.speed < old.speed)
  break() {
    this.speed -= 0
  }

  @requires(self => self.on && self.speed === 0)
  turnOff() {
    this.on = false
  }
}

Reference

contract(conditions)

Returns a higher-order function that can be applied to existing functions to add pre and post condition checking.

  • conditions (Object)
    • [pre(...args)] (function): If specified, will be called before contracted function is executed with the arguments to the contracted function. If pre returns false, then a precondition error will be thrown.
    • [post(result, args, oldArgs)] (function): If specified, will be called after contracted function is executed with the result, the arguments after execution, and the arguments before execution. If post returns false, then a postcondition error will be thrown.

@invariant(condition)

A class decorator for specifying conditions that must remain true at all times.

  • condition(self) (function): Will be called with a copy of the object before and after every property of the class is accessed. If condition returns false, then a invariant error will be thrown. Note: invariant checking is applied for all properties declared on the class or initialised in the constructor, and not for properties added later.

@requires(condition)

A property decorator for specifying preconditions on access to a property. Can be applied to value, method, getter or setter properties.

  • condition(self, args) (function): Will be called with a copy of the object and arguments before the property is accessed. If condition returns false, then a precondition error will be thrown.

@ensures(condition)

A property decorator for specifying postconditions on access to a property. Can be applied to value, method, getter or setter properties.

  • condition(self, old, result, args, oldArgs) (function): Will be called with the object after execution, a copy of the object before execution, the returned value, the arguments after execution and a copy of the arguments before execution. If condition returns false, then a postcondition error will be thrown.

In Production

Due to conditions not being checked whilst running in production, it also makes sense to strip out as much of Denuto as possible to reduce on build sizes. All of Denuto's checking is wrapped in:

if (process.env.NODE_ENV !== 'production') {
  // do checking
}

You can use Webpack's DefinePlugin or Rollup's rollup-plugin-replace, to replace instances of 'process.env.NODE_ENV' with 'production'. This should result in almost all of Denuto being stripped out.

License

MIT