Plato on Github
Report Home
node_modules\atropa-ArgsInfo\src\atropa-ArgsInfo.js
Maintainability
64.86
Lines of code
229
Difficulty
20.86
Estimated Errors
0.61
Function weight
By Complexity
By SLOC
/// <reference path="../docs/vsdoc/OpenLayersAll.js"/> /*jslint node: true */ var atropa = require('atropa-header'); atropa.inquire = require('atropa-inquire').inquire; atropa.arrays = require('atropa-arrays').arrays; atropa.customErrors = require('atropa-customErrors').customErrors; /*jslint indent: 4, maxerr: 50, white: true, browser: true, devel: true, plusplus: true, regexp: true */ /*global atropa */ // end header /** * This represents a filter for arguments based on type. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130221 * @class This represents a filter for arguments based on type. * @returns {ArgsInfo} Returns an ArgsInfo filter. * @requires atropa.arrays.match * @example * function myClassyConstructor(takes, a, few, args) { * var expectedArgTypes, checker; * * expectedArgTypes = {}; * expectedArgTypes.requestWithMessage = * ['string', 'string', 'string', 'function']; * expectedArgTypes.requestNullMessage = * ['string', 'string', 'object', 'function']; * * checker = new atropa.ArgsInfo(); * checker.setExpectedArgTypes(expectedArgTypes); * * try { * * // Check the supplied arguments pseudo array's argument types * // if the pattern of types in arguments matches one of the * // patterns set on expectedArgTypes then the matching pattern * // will be returned. Otherwise, an error will be thrown. * * checker.checkArgTypes(arguments); * } catch (e) { * * // Invalid argument types supplied. Handle * // the error or bail. * * } * * // the arguments supplied will be of the proper type * // your function can go ahead and do things with them * } */ atropa.ArgsInfo = function ArgsInfo() { 'use strict'; var expectedArgTypes, checkArgs, that; /** * Holds the proper reference to <code>this</code> * for private functions. * @type This * @private * @fieldOf atropa.ArgsInfo- */ that = this; /** * Holds the expected argument types object. * @private * @type Expected Arg Types * @fieldOf atropa.ArgsInfo- */ expectedArgTypes = {}; /** * Sets the expected argument types. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @methodOf atropa.ArgsInfo# * @param {Expected Arg Types} typesObj An object containing information * about the types of arguments you expect. Specifically, the object should * look like the example. * @example * // typesObj is expected to be of the form: * * var typesObj = { * "namedArgumentTypesArray" : ["string", "function", "number"], * "namedAlternateArgumentTypesArray" : ["object", "function", "number"] * }; * * // You may use as many named arrays as you wish and checkArgTypes will * // test for a match to at least one of the provided named arrays. * @throws {atropa.customErrors.InvalidArgumentTypesError} Throws an error if the * typesObj can not be used to set the expected argument types. */ this.setExpectedArgTypes = function setExpectedArgTypes(typesObj) { var error, names; error = false; if(atropa.inquire.isObjectNotNull(typesObj)) { names = Object.keys(typesObj); if (names.length > 0) { expectedArgTypes = typesObj; } else { error = true; } } else { error = true; } if(error) { throw new atropa.customErrors.InvalidArgumentTypesError( 'typesObj is expected to be of the form: var typesObj = ' + '{ "namedArgumentTypesArray" : ' + ' ["string", "function", "number"], ' + '"namedAlternateArgumentTypesArray" : ' + ' ["object", "function", "number"] }; ' + 'You may use as many named arrays as you wish and' + 'checkArgTypes will test for a match to at least one of the ' + 'provided named arrays.' ); } }; /** * Gets the types of arguments. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @methodOf atropa.ArgsInfo# * @param {arguments} args An arguments object, or anything you want to * check the type of. * @returns {Array} Returns an array of the types of arguments passed in. */ this.getArgTypes = function getArgTypes(args) { var x, argTypes; argTypes = []; for (x in args) { if (args.hasOwnProperty(x)) { argTypes.push(typeof(args[x])); } } return argTypes; }; /** * Compares the expected arguments types to the * received arguments types. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @private * @methodOf atropa.ArgsInfo- * @param {Array} expectedTypesArray An array taken from the user * created argument types object. * @param {arguments} args an arguments object. * @returns {Boolean} Returns true if the expected types match for type * and are in the same order as the received types. * @requires atropa.arrays.match */ checkArgs = function checkArgs(expectedTypesArray, args) { var types; types = {}; types.expected = expectedTypesArray; types.received = that.getArgTypes(args); return atropa.arrays.match(types.expected, types.received); }; /** * Checks the given arguments object against the expected * arguments types. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @methodOf atropa.ArgsInfo# * @param {arguments} args An arguments object * @returns {String} The user assigned key which matches the * arguments supplied, or throws an error. * @throws {atropa.customErrors.InvalidArgumentTypesError} Throws an error if no matching * pattern of argument types can be found for <code>args</code> * @see atropa.ArgsInfo#setExpectedArgTypes */ this.checkArgTypes = function checkArgTypes(args) { var expectedTypes; if (Object.keys(expectedArgTypes).length < 1) { throw new atropa.customErrors.InvalidArgumentTypesError( 'Expected argument types is not set. Use ' + 'setExpectedArgTypes(typesObj) to set. typesObj is an ' + 'object whose properties are arrays of strings representing ' + 'the typeof(argument) for each argument, in the exact order ' + 'in which they will be given to the function. Using multiple ' + 'properties it is possible to define alternative acceptable ' + 'argument type sets. Use getArgTypes(arguments) as a ' + 'convenient way of getting the array you want to hard code ' + 'in for validation. Example: var typesObj = ' + '{ "messageIncluded" : ["string", "function", "number"], ' + '"messageNotIncluded" : ["object", "function", "number"] };' ); } for (expectedTypes in expectedArgTypes) { if (expectedArgTypes.hasOwnProperty(expectedTypes)) { if (checkArgs(expectedArgTypes[expectedTypes], args)) { return expectedTypes; } } } throw new atropa.customErrors.InvalidArgumentTypesError( 'invalid argument type @ atropa.ArgsInfo.checkArgTypes'); }; }; while(atropa.data.requirements.length > 0) { atropa.data.requirements.pop()(); } module.exports = atropa;