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-es6

v3.0.0

Published

Force your ES6 classes to conform to an interface!

Downloads

7

Readme

ES6 Contracts

Build Status Coverage Status

Have you ever wished that JavaScript implemented interfaces like Java, C#, or other statically typed languages?

ES6 Contracts provides interface validation for ES6 classes. This project was developed against Node.js v8 but is tested against Node.js >= 4. It should run just fine in the browser as well.

Usage

const Interface = require('contracts-es6');

// Note the empty function bodies
class TestInterface {
  method1() { }
  method2() { }
  method3WithParams(foo, bar, baz) { }
}

// Alternatively, this can be implemented as
// class TestStrictImpl extends Interface(TestInterface, Interface.STRICT);
class TestStrictImpl extends Interface.StrictInterface(TestInterface) {
  constructor() {
    super(); // super call required
    console.log('Implemented!');
  }
  method1() {
    // Implementation here
  }
  method2() {
    // Implementation here
  }
  method3WithParams(foo, bar, baz) {
    // Implementation here
  }
}

const impl = new TestStrictImpl();
// output: 'Implemented!'

If you fail to satisfy the interface, an ImplementationError will be thrown explaining the error.

You can access a list of all the implementation issues from the ImplementationError.

const Interface = require('contracts-es6');

class TestInterface {
  method1() { }
  method2() { }
}

class TestImpl extends Interface.StrictInterface(TestInterface) { }

try {
  new TestImpl();
} catch (err) {
  console.log(err.message);
  console.log(err.errors);
}

/*
output:
The provided implementation does not fully implement this interface.

[ 'TestImpl must implement `method1` with the following signature: `method1()`.',
  'TestImpl must implement `method2` with the following signature: `method2()`.' ]
*/

You can also validate that your implementation fulfills its contract before instantiation by using the check function.

const Interface = require('contracts-es6');

class TestInterface {
  method1() { }
  method2() { }
}

class TestImpl extends Interface.StrictInterface(TestInterface) { }

class TestCompleteImpl extends Interface.StrictInterface(TestInterface) { 
  method1() { }
  method2() { }
}

console.log(`TestImpl: ${Interface.check(TestImpl)}`);
console.log(`TestCompleteImpl: ${Interface.check(TestCompleteImpl)}`);

/*
output: 
TestImpl: false
TestCompleteImpl: true
*/

The interface module exports two different interface types: Interface.StrictInterface and Interface.LooseInterface. See Loose Mode for Loose Mode conventions. It also exports the general Interface type which has the Interface(klass: interface-object, mode: integer) signature. The Interface type defaults to Strict mode if you don't specify a mode argument. Currently there are two exported mode constants, Interface.STRICT and Interface.LOOSE which makes Interface's declaration a bit more legible.

Strict Mode

Strict Mode is the traditional interface pattern. Impl classes must implement all of the methods specified in the interface.

Loose Mode

Loose Mode bypasses argument checks for a given interface. JavaScript developers tend to play fast and loose with implementation method signatures, preferring to only specify used arguments. ESLint includes a rule (no-unused-vars) that enforces this concept. To use Strict Mode you'll need to disable it and a few other rules (see ESLint.).

Caveats

Because this is Javascript we're talking about, deriving meaningful information about returned values is nearly impossible. Consequently, ES6 Contracts doesn't validate return types, only method implementation and signature.

ESLint

Conventions in this library differ with some well-established ESLint rules. To make sure that use of Interface passes ESLint checks you should disable the following rules:

/* eslint-disable-rule class-methods-use-this, no-empty-function, no-unused-vars */

or

{
    "rules": {
        "class-methods-use-this": "off",
        "no-empty-function": "off",
        "no-unused-vars": ["error", { "args": "none" }],
    }
}