Last active
September 16, 2016 11:26
-
-
Save bas080/28612d69d121aa5e7b7824beab28d42b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /** | |
| * Extend a constructor/type with new methods | |
| * | |
| * @param {Constructor} type - reference to a constructor | |
| * @param {Constructor} protocolType - reference to a constructor | |
| * @param {object} properties - which stores the method/property names | |
| * and values | |
| * | |
| * @returns {Constructor} reference which equals the type Constructor | |
| */ | |
| function extendType(type, protocolType, properties) { | |
| // check if required protocol methods are all defined | |
| let notDefined = Object.getOwnPropertyNames(protocolType.prototype).filter(prop => { | |
| return !Boolean(properties[prop]); | |
| }); | |
| if (notDefined.length > 0) { | |
| throw `missing methods for type extensions of ${type.name} with ${protocolType.name}: ${notDefined.join(' ')}`; | |
| } | |
| Object.keys(properties).forEach(property => { | |
| let value = properties[property]; | |
| if (type.prototype[property]) { | |
| throw `${property} alread defined on ${type.name} prototype`; | |
| } | |
| type.prototype[property] = value; | |
| }); | |
| return type; | |
| } | |
| /** | |
| * Creates static methods on the protocol Constructor | |
| * | |
| * @param {Constructor} which represents a protocol | |
| * | |
| * @returns {Constructor} same | |
| */ | |
| function generateProtocolMethods(protocol) { | |
| return Object.getOwnPropertyNames(protocol.prototype).forEach(prop => { | |
| protocol[prop] = (instance, ...args) => { | |
| return instance[prop].apply(instance, args); | |
| }; | |
| }); | |
| return protocol; | |
| } | |
| module.exports = { | |
| extendType, | |
| generateProtocolMethods | |
| }; | |
This file contains hidden or 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
| const extendType = require('./protocol').extendType; | |
| const protocolFunctions = require('./protocol').protocolFunctions; | |
| class Fileable { | |
| read(){} | |
| write(){} | |
| } | |
| generateProtocolMethods(Fileable); | |
| class File { | |
| } | |
| extendType(File, Fileable, { | |
| read(item) { | |
| return 'i can read'; | |
| }, | |
| write(items) { | |
| return 'i can write'; | |
| } | |
| }); | |
| const f = new File(); | |
| console.log(f.read()); | |
| console.log(f.write()); | |
| console.log('and now for the static methods'); | |
| console.log(Fileable.read(f)); | |
| console.log(Fileable.write(f)); | |
| module.exports = File; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment