-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (48 loc) · 1.45 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
function createInterface () {
// retrieved forced function names
var functionNames = Array.prototype.slice.call(arguments)
function Interface () {
var constructor = this.constructor
if (constructor === Interface) {
// Interface is being created on it's own
throw new Error('Cannot create an instance of Interface')
} else {
var prototype = Object.getPrototypeOf(this)
var unimplemented = []
for (var i = 0; i < functionNames.length; i++) {
if (typeof prototype[functionNames[i]] !== 'function') {
unimplemented.push(functionNames[i])
}
}
// throw error if there are unimplemented functions
if (unimplemented.length) {
throw new Error('The following function(s) need to be implemented for class ' +
constructor.name + ': ' + unimplemented.join(', '))
}
}
}
// expose validate function
Interface.isImplementedBy = function (object) {
var prototype = Object.getPrototypeOf(object)
var valid = true
for (var i = 0; i < functionNames.length; i++) {
if (typeof prototype[functionNames[i]] !== 'function') {
valid = false
break
}
}
return valid
}
return Interface
}
createInterface.create = createInterface
/**
*
* support old way of creating interface
* Ex:
*
* const MyInterface = new Interface('...', '...')
* or
* const MyInterface = Interface('...', '...')
*/
module.exports = createInterface