/es6-interface

Primary LanguageJavaScriptMIT LicenseMIT

es6-interface

es6-interfac is a Nodejs module that let you build interfaces for es6 classes

Introduction

in today javascript there is no way to declare an interface like other language's as php or java. with es6-interface easily build interfaces , use multi interfaces on the same class and secure your class.

Limitations

works on NodeJS version 6+, works only on modern web browsers, tested only on NodeJS

Installing

npm install es6-interface --save

lets jump to some examples

Examples

first we start with a simple 1 interface implementation

One interface implementation

const Interface = require('es6-interface')
const testInterface1 = new Set(['required1(arg1)']); // required1 is the method we force to implement

class testClass extends Interface(testInterface1) {
  constructor() {
    super()
  }

}

new testClass() // now we will get an error that we need to implement required1(arg1) method

Multi interface implementation

const Interface = require('es6-interface');
const testInterface1 = new Set(['required1(arg1)']);
const testInterface2 = new Set(['required2(arg1,arg2)', 'required3({arg1 , arg2 , arg3})']);

class testClass extends Interface(testInterface1,testInterface2) {
  constructor() {
    super()
  }

}

new testClass() // now we will get an error that we need to implement required1(arg1) required2(arg1,arg2) required3({arg1,arg2,arg3}) methods

Multi interface with class inheritance

const Interface = require('es6-interface');
const testInterface1 = new Set(['required1(arg1)']);
const testInterface2 = new Set(['required2(arg1,arg2)', 'required3({arg1 , arg2 , arg3})']);
const testInterface4 = new Set(['required4({arg1, arg2, arg3}, arg4)']);

class parentClass {
    constructor() {

    }
    
    required4({arg1, arg2, arg3}, arg4) {

    }
}
class testClass extends Interface(testInterface1,testInterface2,testInterface4,parentClass) {
  constructor() {
    super()
  }

}

new testClass() //  we will still get an error for no implement required1(arg1) required2(arg1,arg2) required3({arg1,arg2,arg3}) methods as we get required4 from our parent class

Next development tasks

  • improve performance
  • better error messages

Tests

npm test