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

es6-interfaces

v1.0.4

Published

Implementation of Interfaces on JS

Downloads

2

Readme

ES6 Interfaces

Implementation of Interfaces on ES6 JS

This code can be build on NodeJS v6.17.1 and up but you've to use v14.0.0 or higher to run the node tests.

The idea of this code is to use for implement S.O.L.I.D in Javascript, specifically the Interface segregation principle and Dependency inversion principle. Some examples of implementation are provided here.

How to use

How to load the module

For ES6 import() you can use

import { Interface } from 'es6-interfaces';

and with require()

const { Interface } = require('es6-interfaces');

or

const Interface = require('es6-interfaces').Interface;

Interface definition

// Interface class
class IContainer {
  static method(target) {
    return target;
  }
}

// Implemtation class
class Container {
  static method(param) {
    return param + 1;
  }
}

IContainer is the interface definition class. Is very common in OOP using the name of the implementation class prefixed with a capital 'i'. method() is the interface for the method in the implementation class. The parameter target is the function method() of the implementation class. In this case the interface is a simple passthru from the interface to the inplementation class.

// Interface class
class IContainer {
  static method(target) {
    return param => target(String(param));
  }
}

In this portion of code the interface for method() is passing an arrow funtion instead of the target just to adquire the parameter and transform it to string before pass it to the target function.

// Interface class
class IContainer {
  static method(target) {
    return param => String(target(param));
  }
}

This one is very similar to the previous but instead of change the parameter to string is change the return of the target function.

The same behavior is applied to the properties.

// Interface class
class IContainer {
  static value = String;
}

// Implemtation class
class Container {
  static value = 0;
}

On this example value will be converted to String by the interface side. In this case target is the value of the variable. The interface can be used to type validation, transformation or sustitution.

NOTE: If the method/property of the implementation class is not defined on the interface class, this interface will not publish this method/property. In other words this method will be undefined for this interface

Interface implementation

const container = new Interface(Container, IContainer);

This interface implements the interface definition class IContainer with the implementation class Container.

To understand this the execution will be on this way container.method() = IContainer.method() -> Container.method()

const container = new Interface(Container, [IContainer1, IContainer2]);

The Interface can use more than one interface class to extend the interface.

const containerA = new Interface(Container, [IContainer1, IContainer2]);

const containerB = new Interface(Container, [IContainer1, IContainer3]);

Or you can use differents interface classes with the same implemtation class for interface segregation.

Check the examples for more information.