JSFiddle - React, Tailwind, and code Playground
by akl_gate4com
JavaScript
/**
Code copyright Dustin Diaz and Ross Harmes, Pro JavaScript Design Patterns.
Modified by Andreas Klein, Gate4 GmbH
**/
// Constructor.
var Interface = function (name, methods) {
if (arguments.length != 2) {
throw new Error("Interface constructor called with " + arguments.length + "arguments, but expected exactly 2.");
}
this.name = name;
this.methods = [];
for (var property in methods) {
if (methods.hasOwnProperty(property)) {
if (typeof methods[property] !== 'function') {
throw new Error("Interface constructor expects method names to be passed in as a function.");
}
var signature = /(.+)\((.*)\)/g.exec(methods[property].toString().replace(/(\s)|(\/\*.*\*\/)|(\/\/[\w\s\']*)|(\<![\-\-\s\w\>\/]*\>)/g,''));
var interfaceMethodParameter = signature[2].match(/([a-zA-Z_][\w]*)/g);
interfaceMethodParameter = interfaceMethodParameter ? interfaceMethodParameter.length : 0;
this.methods.push({"method":property, "parameter":interfaceMethodParameter});
}
}
};
// Static class method.
Interface.ensureImplements = function (object) {
if (arguments.length < 2) {
throw new Error("Function Interface.ensureImplements called with " + arguments.length + "arguments, but expected at least 2.");
}
for (var i = 1, len = arguments.length; i < len; i++) {
var interface = arguments[i];
if (interface.constructor !== Interface) {
throw new Error("Function Interface.ensureImplements expects arguments two and above to be instances of Interface.");
}
for (var j = 0, methodsLen = interface.methods.length; j < methodsLen; j++) {
var method = interface.methods[j].method;
var interfaceMethodParameter = interface.methods[j].parameter;
if (!object[method] || typeof object[method] !== 'function') {
...