Plato on Github
Report Home
node_modules\atropa-random\src\atropa-random.js
Maintainability
60.48
Lines of code
207
Difficulty
36.36
Estimated Errors
0.79
Function weight
By Complexity
By SLOC
/// <reference path="../docs/vsdoc/OpenLayersAll.js"/> var atropa = require('atropa-header'); /*jslint indent: 4, maxerr: 50, white: true, browser: true, devel: true, plusplus: true, regexp: true */ /*global atropa */ // end header /** * Provides random strings and numbers. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @namespace Provides random strings and numbers. */ atropa.random = {}; /** * Gives you a random string whose length and characters you specify. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @param {Number} stringLength This is the length of the string. * @param {String} characterClass Optional. May be one of: * numeric, caps, lower, alpha, alphanumeric, punctuation, vowel, consonant * This is the type of characters you want returned to you. Defaults to * alphanumeric. * @return {String} A random string of specified length and composition. */ atropa.random.string = function randomString(stringLength, characterClass) { 'use strict'; var numeric, vowel, consonant, lower, caps, alpha, alphanumeric, punctuation, chars, string_length, randomstring, i, character; numeric = '0123456789'; vowel = 'aeiouy'; consonant = 'bcdfghjklmnpqrstvwxz'; lower = vowel + consonant; caps = lower.toUpperCase(); alpha = caps + lower; alphanumeric = numeric + caps + lower; punctuation = '.?!'; randomstring = ''; switch (characterClass) { case 'numeric': chars = numeric; break; case 'caps': chars = caps; break; case 'lower': chars = lower; break; case 'alpha': chars = alpha; break; case 'alphanumeric': chars = alphanumeric; break; case 'punctuation': chars = punctuation; break; case 'vowel': chars = vowel; break; case 'consonant': chars = consonant; break; default: chars = alphanumeric; break; } if (stringLength === undefined) { string_length = 4; } else { string_length = stringLength; } for (i = 0; i < string_length; i++) { character = Math.floor(Math.random() * chars.length); randomstring += chars[character]; } return randomstring; }; /** * Generates a random number between the specified min and max value. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @param {Number} min The lowest number you want returned * @param {Number} max The highest number you want returned * @returns {Number} A random number within the specified range. */ atropa.random.integer = function randomInteger(min, max) { 'use strict'; return Math.floor(Math.random() * (max - min + 1) + min); }; /** * Get a random property name from the given object. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @param {Object} obj The object to select a random * property name from. * @return {String} A random property name from the * given object. */ atropa.random.getPropertyName = function (obj) { "use strict"; var arr; arr = Object.keys(obj); return arr[atropa.random.getArrayKey(arr)]; }; /** * Get a random key from the given array. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @param {Array} arr The array to select a random * key from. The keys of the array must be contiguous. * @return {Number} A random integer between 0 and * <code>arr.length</code> */ atropa.random.getArrayKey = function (arr) { "use strict"; return Math.floor(Math.random() * arr.length); }; /** * Get a random value from the given array. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @param {Array} arr The array to select a random * value from. The keys of the array must be contiguous. * @return {Mixed} A random value from the given array. */ atropa.random.getArrayValue = function (arr) { "use strict"; return arr[atropa.random.getArrayKey(arr)]; }; /** * Remove a random element from the given array. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @param {Array} arr The array to remove a random * element from. The keys of the array must be contiguous. * @return {Mixed} A random value from the given array. */ atropa.random.pullArrayElement = function (arr) { "use strict"; var k, d; k = atropa.random.getArrayKey(arr); d = arr[k]; arr.splice(k, 1); return d; }; /** * Remove a random property from the given object. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @param {Object} obj The object to remove a random * property from. * @return {Mixed} A random value from the given object. */ atropa.random.pullProperty = function (obj) { "use strict"; var pName, objData; pName = atropa.random.getPropertyName(obj); objData = obj[pName]; delete obj[pName]; return objData; }; while(atropa.data.requirements.length > 0) { atropa.data.requirements.pop()(); } module.exports = atropa;