Plato on Github
Report Home
node_modules\atropa-wtf\src\atropa-wtf.js
Maintainability
65.81
Lines of code
160
Difficulty
32.36
Estimated Errors
0.91
Function weight
By Complexity
By SLOC
/// <reference path="../docs/vsdoc/OpenLayersAll.js"/> /*jslint node: true */ var atropa = require('atropa-header'); atropa.regex = require('atropa-regex').regex; atropa.string = require('atropa-string').string; atropa.setAsOptionalArg = require('atropa-setAsOptionalArg').setAsOptionalArg; /*jslint indent: 4, maxerr: 50, white: true, browser: true, devel: true, plusplus: true, regexp: true, vars: true */ /*global atropa */ // end header (function () { 'use strict'; atropa.requires('wtf', function () { var supported = true; [ atropa.regex, atropa.string.countWords, atropa.setAsOptionalArg ].forEach(function (prerequisite) { if (prerequisite === undefined) { supported = false; } }); return supported; }); }()); (function () { 'use strict'; atropa.requires('wtfHtmlElement', function () { var supported = true; [window].forEach(function (prerequisite) { if (prerequisite === undefined) { supported = false; } }); return supported; }); }()); /** * Container for all Glorious WTFifier related functions and such. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @namespace Container for all Glorious WTFifier related functions and such. * @requires atropa.regex * @requires atropa.wtf.dictionary */ atropa.wtf = {}; /** * The Glorious WTFification Dictionary: Turning Shit * Into Polished Turds. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130110 */ atropa.wtf.dictionary = require('./atropa-wtf-dictionary.json'); /** * Accepts plain text input and Gloriously WTFifies it. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130110 * @param {String} target The text to WTFify. * @param {Boolean} outputHTML Specifies if you want the output * in HTML format. If false, will output plain text. Defaults * to false. * @return {String} Returns Genuine WTFified text. */ atropa.wtf.wtfify = function (target, outputHTML) { 'use strict'; atropa.supportCheck('wtf'); var regexValue, replacementText, oldWord, wtfCount, wordCount, ret, word; if (true !== outputHTML) { outputHTML = false; } ret = {}; wtfCount = 0; target = target.trim(); wordCount = atropa.string.countWords(target); if (true === outputHTML) { target = target.replace(/(\. ?){2,}/gi, '<span style="color : brown ;"> [shit taco] </span>'); target = '<p> ' + target.replace(/(\r\n|\r|\n)/g, ' <br/> ') + ' </p>'; } else { target = target.replace(/(\. ?){2,}/gi, ' [shit taco] '); } /** * Accepts plain text input and Gloriously WTFifies it. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130112 * @methodOf atropa.wtf.wtfify- * @private * @param {String} m First matched pattern in string searched. * @param {String} sub1 First matched subpattern in string searched. * @param {String} sub2 Second matched subpattern in string searched. */ /*jslint unparam: true*/ replacementText = function (m, sub1, sub2) { wtfCount++; sub1 = atropa.setAsOptionalArg('', sub1); sub2 = atropa.setAsOptionalArg('', sub2); var out; if (true === outputHTML) { out = '<span style="color : red ;">' + sub1 + atropa.wtf.dictionary[word] + sub2 + '</span>'; } else { out = sub1 + atropa.wtf.dictionary[word] + sub2; } return out; }; /*jslint unparam: false*/ // word is defined in the containing scope and // is not global, jshint is wrong for (word in atropa.wtf.dictionary) { if (atropa.wtf.dictionary.hasOwnProperty(word)) { oldWord = atropa.regex.appendPrefixesAndSuffixes(word); regexValue = new RegExp(oldWord, 'gi'); target = target.replace(regexValue, replacementText); } } ret.wtfCount = wtfCount; ret.wordCount = wordCount; ret.score = wtfCount / wordCount; ret.txt = target; return ret; }; /** * WTFifies the <code>textContent</code> or <code>value</code> of the * given element and replaces the element's innerHTML with a pre block * containing the results of WTFification. * @param {HTMLElement} elementReference A reference to an HTML Element. * @returns {HTMLElement} Returns the given element after wtfification. * @version 20130313 */ atropa.wtf.htmlElement = function (elementReference) { 'use strict'; atropa.supportCheck('wtfHtmlElement'); var wtfified, txt; elementReference.innerHTML = elementReference.innerHTML.replace(/<br>(\s+)?(\r\n|\r|\n)?/g, '\r\n'); txt = elementReference.value || elementReference.textContent; wtfified = atropa.wtf.wtfify(txt, true); elementReference.innerHTML = '<pre style="color:black; background:white; white-space:pre-wrap;">' + wtfified.txt + '</pre>'; return elementReference; }; while (atropa.data.requirements.length > 0) { atropa.data.requirements.pop()(); } module.exports = atropa;