Created
August 29, 2016 15:54
-
-
Save davemackintosh/7efcccaaa3a8704be61257b623f757ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict" | |
const {symbols} = require("./recommended") | |
class Interface { | |
constructor() { | |
// Somewhere to keep the errors. | |
const errors = new Set() | |
// Get the symbolised field with our | |
// interface requirements in it. | |
const required = this[symbols.INTERFACE] | |
// Get an object that represents our class. | |
const class_funcs = Object.getOwnPropertyNames(Object.getPrototypeOf(this)) | |
const class_as_object = {} | |
class_funcs.forEach(func => class_as_object[func] = this[func].length) | |
// Validate the consuming class. | |
Object.keys(required).forEach(member => { | |
if (!class_as_object.hasOwnProperty(member)) { | |
errors.add(`Missing function "${member}", should take ${required[member]} arguments`) | |
} | |
else if (required[member] !== class_as_object[member]) { | |
errors.add(`Function "${member}" should take ${required[member]} arguments not ${class_as_object[member]}`) | |
} | |
}) | |
// Check for errors. | |
if (errors.size > 0) { | |
console.log(`${this.constructor.name} appears to be broken:`) | |
throw Array.from(errors).join("\n") | |
} | |
} | |
} | |
module.exports = Interface |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment