-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathinterface.js
More file actions
155 lines (141 loc) · 3.6 KB
/
interface.js
File metadata and controls
155 lines (141 loc) · 3.6 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
String.prototype.removeAllSpaces = function () {
return this.replace(/\s+/g, '');
};
const getClassMethodNames = (obj) => {
let methods = new Set();
while ((obj = Reflect.getPrototypeOf(obj))) {
let keys = Reflect.ownKeys(obj);
keys.forEach((k) => methods.add(k));
}
return methods;
};
const getArgs = (func) => {
let args = null;
switch (typeof func) {
case 'function': {
args = func
.toString()
.removeAllSpaces()
.match(/\(\s*([^)]*?)\s*\)/);
break;
}
case 'string': {
args = func.removeAllSpaces().match(/\(\s*([^)]*?)\s*\)/);
break;
}
default: {
throw new Error('func must be of type function or string');
}
}
if (args === null || !args[1]) {
return '';
}
args = args[1];
return args
.split(/\s*,\s*/)
.map((arg) => {
return arg
.replace(/\/\*.*\*\//, '')
.replace(/=.*/, '')
.trim();
})
.filter((arg) => {
return arg;
});
};
class DummyClassForExtendsIfNoClassWasSendToAvoidError {}
function getMethodsFromObject(obj) {
return Object.keys(obj)
.filter((funcName) => typeof obj[funcName] === 'function')
.map((funcName) => {
const funcStr = obj[funcName].toString().removeAllSpaces();
if (funcStr.includes('function')) {
return (
funcName +
funcStr.substr(0, funcStr.lastIndexOf('{')).replace('function', '')
);
} else if (funcStr.includes('(')) {
if (funcStr.includes('=')) {
// fn: () => {}
return (
funcName +
funcStr.substr(funcStr.indexOf('('), funcStr.indexOf('='))
);
} else {
// fn() {}
return (
funcName +
funcStr.substr(funcStr.indexOf('('), funcStr.lastIndexOf('{'))
);
}
} else {
return funcName + '(' + funcStr.substr(0, funcStr.indexOf('=')) + ')';
}
});
}
const container = function () {
if (container.arguments.length === 0) {
throw new Error('you need to supply at least one interface');
}
let arr = [];
const argLength = container.arguments.length;
let Class = null;
for (let i in container.arguments) {
if (
container.arguments[i] instanceof Set ||
Array.isArray(container.arguments[i])
) {
arr = [...arr, ...container.arguments[i]];
} else if (
typeof container.arguments[i] === 'object' &&
container.arguments[i] !== null
) {
arr = [...arr, ...getMethodsFromObject(container.arguments[i])];
} else if (
parseInt(i) + 1 === argLength &&
typeof container.arguments[i] === 'function'
) {
Class = container.arguments[i];
} else {
throw new Error('interfaces can be an Array Set or Object');
}
}
const requiredMethods = new Set(arr);
if (Class === null) {
Class = DummyClassForExtendsIfNoClassWasSendToAvoidError;
}
class Interface extends Class {
constructor(options) {
super(options);
const ClassMethods = getClassMethodNames(this);
let errors = '';
requiredMethods.forEach((key) => {
let methodNameIndex = key.indexOf('(');
let methodName =
methodNameIndex > -1 ? key.substr(0, methodNameIndex) : key;
methodName = methodName.trim();
if (
ClassMethods.has(methodName) === false ||
getArgs(this[methodName]).toString() !== getArgs(key).toString()
) {
errors += `${methodName}(${getArgs(key)})`.replace(/\s+/g, '') + ' ';
}
});
if (errors) {
throw new Error(
this.constructor.name + ' must implement ' + errors + 'methods'
);
}
}
}
return Interface;
};
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = container;
} else if (typeof define === 'function' && define.amd) {
define([], function () {
return container;
});
} else {
window.es6Interface = container;
}