Plato on Github
Report Home
node_modules\atropa-Babbler\src\atropa-Babbler.js
Maintainability
71.13
Lines of code
297
Difficulty
27.94
Estimated Errors
0.80
Function weight
By Complexity
By SLOC
/// <reference path="../docs/vsdoc/OpenLayersAll.js"/> var atropa = require('atropa-header'); atropa.random = require('atropa-random').random; atropa.string = require('atropa-string').string; /*jslint indent: 4, maxerr: 50, white: true, browser: true, devel: true, plusplus: true, regexp: true */ /*global atropa */ // end header atropa.requires( 'Babbler', function () { "use strict"; var supported = true; [ atropa.random.integer, atropa.string.ucFirst, atropa.random.string ].forEach(function (prerequisite) { if(prerequisite === undefined) { supported = false; } }); return supported; } ); /** * This class represents a babbler. The babbler * produces lorum ipsum text, to user specifications. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130313 * @class This class represents a babbler * @param {Number} wrdCount The amount of "words" you would like * the babbler to produce. * @returns {Babbler} Returns a babbler. * @requires atropa.random.integer * @requires atropa.string.ucFirst * @requires atropa.random.string * @example * var babbler = new atropa.Babbler(30); * // resets the word count * babbler.resetWordCount(10) * console.log(babbler.getWordCount()); * * // displays a 10 word sentence of nonsense words. * console.log(babbler.generateBabble(10)); * // displays a 3 word sentence * console.log(babbler.generateBabble(3)); * * // displays the user stored or last generated babble * console.log(babbler.getBabble()); * * // clears the stored babble * babbler.resetBabble(); * console.log(babbler.getBabble()); * * // sets the babble * babbler.setBabble('here be gibberish '); * console.log(babbler.getBabble()); * * // append more gibberish to the current babble * babbler.setBabble(babbler.getBabble() + babbler.generateBabble(5)); * console.log(babbler.getBabble()); * * // generate a sentence * babbler.resetWordCount(10); * console.log(babbler.generateSentence(5, 20)); * * // generate random punctuation * console.log(babbler.punctuate()); * * // generate a word * console.log(babbler.generateWord(3,7)); * console.log(babbler.generateWord(7,10)); */ atropa.Babbler = function Babbler(wrdCount) { 'use strict'; var my = this, babble = '', wordCount = 0; /** * Sets the word count. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130313 * @methodOf atropa.Babbler# * @param {Number} wrdCount The amount of "words" which you want the * babbler to produce. * @returns {Number} Returns the set word count for this babbler. */ this.setWordCount = function (wrdCount) { if (typeof wrdCount !== 'number') { wordCount = 250; } else { wordCount = wrdCount; } return wordCount; }; /** * Resets the word count for this babbler. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130313 * @methodOf atropa.Babbler# * @param {Number} wordCount The amount of "words" you would like * to set for this babbler. * @returns {Number} Returns the set word count for this babbler. */ this.resetWordCount = function resetWordCount(wordCount) { my.setWordCount(wordCount); return wordCount; }; /** * Gets the current word count. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @methodOf atropa.Babbler# * @returns {Number} Returns the word count for this babbler. */ this.getWordCount = function getWordCount() { return wordCount; }; /** * Generates a word with a specified length. Lowers the word count by one. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @methodOf atropa.Babbler# * @param {Number} stringMin the shortest word, in characters. * @param {Number} stringMax The longest word, in characters. * @returns {String} Returns a random string of characters * within the specified range of length. * @requires atropa.random.integer * @requires atropa.random.string */ this.generateWord = function generateWord(stringMin, stringMax) { var wordLength, word; wordLength = atropa.random.integer(stringMin, stringMax); word = atropa.random.string(wordLength, 'lower'); wordCount--; return word; }; /** * Provides random punctuation. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @methodOf atropa.Babbler# * @returns {String} Returns a random punctuation * character ( . ! or ? ). * @requires atropa.random.string */ this.punctuate = function punctuate() { var punctuation; punctuation = atropa.random.string(1, 'punctuation'); return punctuation; }; /** * Generates a sentence of specified length in words. The quantity * of words in the generated sentence will be between the minimum * and maximum set, with the maximum capped at the current words * count. The word count will be lowered by the * quantity of words in the generated sentence. If the word count * is 0 then there will be no words in the sentence. If the word * count is 3 then the maximum possible number of words in the * sentence will be three. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130313 * @methodOf atropa.Babbler# * @param {Number} sentenceMin The shortest sentence, in words, * you would like returned. * @param {Number} sentenceMax The longest sentence, in words, * you would like returned. * @returns {String} Returns a "sentence" within the specified * range of length. * @requires atropa.random.integer * @requires atropa.string.ucFirst */ this.generateSentence = function generateSentence( sentenceMin, sentenceMax ) { var word, sentenceLength, sentence; sentenceLength = atropa.random.integer(sentenceMin, sentenceMax); sentence = ''; if (sentenceLength > wordCount) { sentenceLength = wordCount; } for (sentenceLength; sentenceLength > 0; sentenceLength--) { if (wordCount > 0) { word = my.generateWord(4, 12); sentence += ' ' + word; } else { sentenceLength = 0; } } sentence += my.punctuate(); return atropa.string.ucFirst(sentence.trim()); }; /** * Sets the babble. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130313 * @methodOf atropa.Babbler# * @param {String} babbleString Specified babble to set. * @returns {String} Returns the stored babble. */ this.setBabble = function setBabble(babbleString) { if (typeof babbleString === 'string') { babble = babbleString; } else { my.resetBabble(); } return babble; }; /** * Clears the stored babble. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @methodOf atropa.Babbler# * @returns {String} Returns the stored babble. */ this.resetBabble = function resetBabble() { babble = ''; return babble; }; /** * Gets the last generated babble. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @methodOf atropa.Babbler# * @returns {String} Returns the stored babble. */ this.getBabble = function getBabble() { return babble; }; /** * Generates babble to a user specified length in words. * The word count will be zero after this and the stored * babble will be set to the generated babble. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @methodOf atropa.Babbler# * @param {Number} wordsCt The desired word count for the * generated babble. * @returns {String} Returns babble of specified length in words. * @see atropa.Babbler#getWordCount */ this.generateBabble = function generateBabble(wordsCt) { my.resetBabble(); my.resetWordCount(wordsCt); for (wordCount; wordCount > 0; babble += ' ') { my.setBabble(babble + my.generateSentence(5, 20)); } return babble; }; atropa.supportCheck('Babbler'); this.resetWordCount(wrdCount); }; while(atropa.data.requirements.length > 0) { atropa.data.requirements.pop()(); } module.exports = atropa;