Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

interface

Package Overview
Dependencies
Maintainers
2
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

interface - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

11

index.js

@@ -33,2 +33,13 @@ function createInterface () {

createInterface.create = createInterface
/**
*
* support old way of creating interface
* Ex:
*
* const MyInterface = new Interface('...', '...')
* or
* const MyInterface = Interface('...', '...')
*/
module.exports = createInterface

8

package.json
{
"name": "interface",
"version": "1.0.0",
"version": "1.1.0",
"description": "Enforce an interface on classes",

@@ -9,2 +9,3 @@ "main": "index.js",

"chai": "^3.5.0",
"coveralls": "^2.11.16",
"eslint": "^3.15.0",

@@ -18,4 +19,5 @@ "eslint-config-standard": "^6.2.1",

"scripts": {
"test": "nyc mocha",
"lint": "eslint ."
"test": "nyc --reporter=lcov mocha",
"lint": "eslint .",
"coveralls": "cat ./coverage/lcov.info | coveralls"
},

@@ -22,0 +24,0 @@ "author": "Charlie Duong",

# Interface-js
[![Build Status](https://travis-ci.org/charlieduong94/interface-js.svg?branch=master)](https://travis-ci.org/charlieduong94/interface-js)
[![Coverage Status](https://coveralls.io/repos/github/charlieduong94/interface-js/badge.svg)](https://coveralls.io/github/charlieduong94/interface-js)

@@ -11,2 +12,8 @@ Exposes a way to enforce an interface on classes.

```js
const MyInterface = Interface.create('myMethodA', 'myMethodB')
```
or alternatively
```js
const MyInterface = new Interface('myMethodA', 'myMethodB')

@@ -58,3 +65,3 @@ ```

```js
var inherits = require('util').inherits
const inherits = require('util').inherits

@@ -67,2 +74,4 @@ var MyInterface = new Interface('myMethodA', 'myMethodB', 'myMethodC')

inherits(MyClass, MyInterface)
MyClass.prototype.myMethodA = function () {

@@ -69,0 +78,0 @@ // implementation

@@ -9,120 +9,109 @@ var expect = require('chai').expect

var instance
var MyInterface
it('should not allow an interface to be instantiated', function () {
var MyInterface = new Interface('doWork', 'doMoreWork')
try {
instance = new MyInterface()
throw new Error('Interface was instantiated')
} catch (err) {
expect(err.message).to.equal('Cannot create an instance of Interface')
}
})
function generateTestSuite () {
it('should not allow an interface to be instantiated', function () {
try {
instance = new MyInterface()
throw new Error('Interface was instantiated')
} catch (err) {
expect(err.message).to.equal('Cannot create an instance of Interface')
}
})
it('should throw an error for a class not implementing an interface', function () {
var MyInterface = new Interface('method')
it('should throw an error message showing all unimplemented methods', function () {
function MyClass () {
MyInterface.call(this)
}
function MyClass () {
MyInterface.call(this)
}
try {
instance = new MyClass()
throw new Error('MyClass was able to be instantiated')
} catch (err) {
expect(err.message).to.equal('The following function(s) need to be implemented for class MyClass: methodA, methodB, methodC')
}
})
try {
instance = new MyClass()
throw new Error('MyClass was able to be instantiated')
} catch (err) {
expect(err.message).to.equal('The following function(s) need to be implemented for class MyClass: method')
}
})
it('should not throw an error when instantiating a class enforcing all given interfaces', function () {
function MyClass () {
MyInterface.call(this)
}
it('should throw an error message showing all unimplemented methods', function () {
var MyInterface = new Interface('methodA', 'methodB', 'methodC')
MyClass.prototype.methodA = function () {}
MyClass.prototype.methodB = function () {}
MyClass.prototype.methodC = function () {}
function MyClass () {
MyInterface.call(this)
}
try {
instance = new MyClass()
throw new Error('MyClass was able to be instantiated')
} catch (err) {
expect(err.message).to.equal('The following function(s) need to be implemented for class MyClass: methodA, methodB, methodC')
}
})
})
it('should not throw an error when instantiating a class enforcing all given interfaces', function () {
var MyInterface = new Interface('methodA', 'methodB')
it('should enforce an interface on subclasses', function () {
function MyClass () {
MyInterface.call(this)
}
function MyClass () {
MyInterface.call(this)
}
MyClass.prototype.methodA = function () {}
MyClass.prototype.methodA = function () {console.log('methodA called')}
MyClass.prototype.methodB = function () {}
function MySubClass () {
MyClass.call(this)
}
instance = new MyClass()
})
inherits(MySubClass, MyClass)
it('should enforce an interface on subclasses', function () {
var MyInterface = new Interface('methodA', 'methodB')
try {
instance = new MySubClass()
throw new Error('MyClass was able to be instantiated')
} catch (err) {
expect(err.message).to.equal('The following function(s) need to be implemented for class MySubClass: methodB, methodC')
}
})
function MyClass () {
MyInterface.call(this)
}
it('should not throw an error for subclasses that implement all functions in the interface', function () {
function MyClass () {
MyInterface.call(this)
}
inherits(MyClass, MyInterface)
MyClass.prototype.methodA = function () {}
MyClass.prototype.methodA = function () {}
function MySubClass () {
MyClass.call(this)
}
function MySubClass () {
MyClass.call(this)
}
inherits(MySubClass, MyClass)
inherits(MySubClass, MyClass)
MySubClass.prototype.methodB = function () {}
MySubClass.prototype.methodC = function () {}
try {
instance = new MySubClass()
throw new Error('MyClass was able to be instantiated')
} catch (err) {
expect(err.message).to.equal('The following function(s) need to be implemented for class MySubClass: methodB')
}
})
it('should enforce the interface on subclasses', function () {
var MyInterface = new Interface('methodA', 'methodB')
function MySubSubClass () {
MyClass.call(this)
}
function MyClass () {
MyInterface.call(this)
}
inherits(MySubSubClass, MySubClass)
instance = new MySubSubClass()
})
}
MyClass.prototype.methodA = function () {}
context('using the supplied "create" method', function () {
before(function () {
MyInterface = Interface.create('methodA', 'methodB', 'methodC')
})
function MySubClass () {
MyClass.call(this)
}
generateTestSuite()
})
inherits(MySubClass, MyClass)
context('calling the exposed function directly', function () {
before(function () {
MyInterface = Interface('methodA', 'methodB', 'methodC')
})
try {
instance = new MySubClass()
throw new Error('MyClass was able to be instantiated')
} catch (err) {
expect(err.message).to.equal('The following function(s) need to be implemented for class MySubClass: methodB')
}
generateTestSuite()
})
it('should not throw an error for subclasses that implement all functions in the interface', function () {
var MyInterface = new Interface('methodA', 'methodB')
context('creating a "new" interface', function () {
before(function () {
MyInterface = new Interface('methodA', 'methodB', 'methodC')
})
function MyClass () {
MyInterface.call(this)
}
MyClass.prototype.methodA = function () {}
function MySubClass () {
MyClass.call(this)
}
inherits(MySubClass, MyClass)
MySubClass.prototype.methodB = function () {}
instance = new MySubClass()
generateTestSuite()
})
})

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc