-
-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy path3-wrapper.js
More file actions
24 lines (20 loc) · 655 Bytes
/
3-wrapper.js
File metadata and controls
24 lines (20 loc) · 655 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
'use strict';
const contract = (fn, ...types) => (...args) => {
args.forEach((arg, index) => {
const typeName = types[index].name.toLowerCase();
const argType = typeof arg;
if (typeName !== argType) {
throw new TypeError(`Argument type expected:
${typeName}, got: ${argType}`);
}
});
const result = fn(...args);
const expectedResultType = types[types.length - 1].name.toLowerCase();
const resultType = typeof result;
if (expectedResultType !== resultType) {
throw new TypeError(`Result type expected:
${expectedResultType}, got: ${resultType}`);
}
return result;
};
module.exports = { contract };