Comparing version 1.2.0 to 1.2.1
21
index.js
@@ -15,7 +15,7 @@ function createInterface () { | ||
functionNames.forEach(function (functionName) { | ||
if (typeof prototype[functionName] !== 'function') { | ||
unimplemented.push(functionName) | ||
for (var i = 0; i < functionNames.length; i++) { | ||
if (typeof prototype[functionNames[i]] !== 'function') { | ||
unimplemented.push(functionNames[i]) | ||
} | ||
}) | ||
} | ||
@@ -30,14 +30,15 @@ // throw error if there are unimplemented functions | ||
// expose valiate function | ||
// expose validate function | ||
Interface.isImplementedBy = function (object) { | ||
var prototype = Object.getPrototypeOf(object) | ||
var valid = true | ||
var length = functionNames.length | ||
while (length--) { | ||
if (typeof prototype[functionNames[length]] !== 'function') { | ||
return false | ||
for (var i = 0; i < functionNames.length; i++) { | ||
if (typeof prototype[functionNames[i]] !== 'function') { | ||
valid = false | ||
break | ||
} | ||
} | ||
return true | ||
return valid | ||
} | ||
@@ -44,0 +45,0 @@ |
{ | ||
"name": "interface", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Enforce an interface on classes", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -44,3 +44,3 @@ # Interface-js | ||
```js | ||
let instance = new MyClass() | ||
const instance = new MyClass() | ||
// throws a new error with the message: | ||
@@ -63,3 +63,3 @@ // 'The following function(s) need to be implemented for class MyClass: myMethodB' | ||
let instance = new MySubClass() | ||
const instance = new MySubClass() | ||
// still throws an error with the message: | ||
@@ -74,3 +74,3 @@ // 'The following function(s) need to be implemented for class MyClass: myMethodB' | ||
var MyInterface = new Interface('myMethodA', 'myMethodB', 'myMethodC') | ||
const MyInterface = new Interface('myMethodA', 'myMethodB', 'myMethodC') | ||
@@ -102,1 +102,26 @@ function MyClass () { | ||
``` | ||
You can also enforce that arbitrary objects match an interface by using | ||
the `isImplementedBy` method. | ||
```js | ||
const MyInterface = new Interface('myMethod') | ||
class MyClass { | ||
myMethod () { | ||
// some implementation | ||
} | ||
} | ||
class MyOtherClass { | ||
myOtherMethod () { | ||
// some implementation | ||
} | ||
} | ||
const instanceA = new MyClass() | ||
const instanceB = new MyOtherClass() | ||
MyInterface.isImplementedBy(instanceA) // returns true | ||
MyInterface.isImplementedBy(instanceB) // returns false | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9096
153
124