diff --git a/lib/interface.js b/lib/interface.js index 1ae2ce3..2ca594d 100644 --- a/lib/interface.js +++ b/lib/interface.js @@ -42,38 +42,20 @@ var util = require( './util' ), */ module.exports = function() { - var def = {}, - name = '', - type = typeof arguments[ 0 ] + var type = typeof arguments[ 0 ] + result = null ; switch ( type ) { // anonymous interface case 'object': - def = arguments[ 0 ]; - - // ensure we have the proper number of arguments (if they passed in - // too many, it may signify that they don't know what they're doing, - // and likely they're not getting the result they're looking for) - if ( arguments.length > 1 ) - { - throw Error( - "Expecting one argument for Interface definition; " + - arguments.length + " given." - ); - } - + result = createAnonymousInterface.apply( null, arguments ); break; // named class case 'string': - name = arguments[ 0 ]; - def = arguments[ 1 ]; - - // add the name to the definition - def.__name = name; - + result = createNamedInterface.apply( null, arguments ); break; default: @@ -84,7 +66,7 @@ module.exports = function() ); } - return extend( def ); + return result; }; @@ -127,6 +109,47 @@ module.exports.isInterface = function( obj ) function Interface() {} +/** + * Creates a new anonymous Interface from the given interface definition + * + * @param {Object} def interface definition + * + * @return {Interface} new anonymous interface + */ +function createAnonymousInterface( def ) +{ + // ensure we have the proper number of arguments (if they passed in + // too many, it may signify that they don't know what they're doing, + // and likely they're not getting the result they're looking for) + if ( arguments.length > 1 ) + { + throw Error( + "Expecting one argument for Interface definition; " + + arguments.length + " given." + ); + } + + return extend( def ); +} + + +/** + * Creates a new named interface from the given interface definition + * + * @param {string} name interface name + * @param {Object} def interface definition + * + * @return {Interface} new named interface + */ +function createNamedInterface( name, def ) +{ + // add the name to the definition + def.__name = name; + + return extend( def ); +} + + var extend = ( function( extending ) { return function extend()