Plato on Github
Report Home
node_modules\atropa-string\src\atropa-string.js
Maintainability
73.45
Lines of code
289
Difficulty
35.70
Estimated Errors
1.18
Function weight
By Complexity
By SLOC
/// <reference path="../../docs/vsdoc/OpenLayersAll.js"/> var atropa = require('atropa-header'); atropa.regex = require('atropa-regex').regex; atropa.arrays = require('atropa-arrays').arrays; /*jslint indent: 4, maxerr: 50, white: true, browser: true, devel: true, plusplus: true, regexp: true */ /*global atropa */ // end header /** * A few utilities for manipulating strings. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @namespace A few utilities for manipulating strings. * @requires atropa.regex.patterns */ atropa.string = {}; /** * Replaces repeated words and phrases with a single word or phrase. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130701 * @param {String} string The string to remove repeated words from. * @returns {String} Returns the given string with repeated words and * phrases removed. */ atropa.string.removeRepeatedWord = function removeRepeatedWord (string) { "use strict"; return string.replace(atropa.regex.patterns.repeatedWords, '$1'); }; /** * Creates paragraph breaks at every occurrence of two consecutive line breaks. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130701 * @param {String} string The string to insert paragraph tags into. * @returns {String} Returns the given string with paragraph breaks inserted. */ atropa.string.lineBreaksToParagraphTags = function lineBreaksToParagraphTags (string) { "use strict"; var out = string.replace(atropa.regex.patterns.paragraphBreaks, '</p><p>'); out = '<p>' + out.trim() + '</p>'; out = out.replace(/\s+<\/(p|br)>/g, '</$1>'); return out; }; /** * Creates break tags at every line break. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130701 * @param {String} string The string to insert break tags into. * @returns {String} Returns the given string with break tags inserted. */ atropa.string.lineBreaksToBreakTags = function lineBreaksToBreakTags (string) { "use strict"; return string.replace(atropa.regex.patterns.lineBreaks, '<br>'); }; /** * Normalizes line breaks to `\n`. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130701 * @param {String} string The string to normalize. * @returns {String} Returns the given string with normalized line breaks. */ atropa.string.normalizeEol = function normalizeEol (string) { "use strict"; return string.replace(atropa.regex.patterns.lineBreaks, '\n'); }; /** * Converts the first character of a given string to * uppercase. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20120909 * @param {String} string The string for which you want the * first letter to be in upper case. * @returns {String} The given string with it's first letter capitalized. */ atropa.string.ucFirst = function ucFirst(string) { "use strict"; string = string.charAt(0).toUpperCase() + string.slice(1); return string; }; /** * Converts the given string to camel case. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130823 * @param {String} string The string to camelize. * @returns {String} The camelized string. * @example * atropa.string.camelize('get it together'); * // returns "getItTogether" */ atropa.string.camelize = function camelize (str) { "use strict"; var arr, out; arr = str.split(' '); out = arr.shift(); arr = arr.map(function (item) { return atropa.string.ucFirst(item); }); out += arr.join(''); return out; }; /** * Counts words. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130313 * @param {String} someText Plain text. * @return {Number} Returns the count of words in someText. */ atropa.string.countWords = function countWords(someText) { "use strict"; var wordCount, re, len = 0; if(someText !== undefined && someText !== null) { someText = someText.trim(); if(someText !== '') { wordCount = 0; re = /\s+/gi; wordCount = someText.split(re); len = wordCount.length; } } return len; }; /** * Converts end of line markers into whatever you want. * Automatically detects any of \r\n, \n, or \r and * replaces it with the user specified EOL marker. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @param {String} text The text you want processed. * @param {String} newEOL The replacement for the current EOL marks. * @returns {String} Returns the processed text. */ atropa.string.convertEol = function convertEOL(text, newEOL) { 'use strict'; return text.replace(atropa.regex.patterns.lineBreaks, newEOL); }; /** * Removes a quantity of leading spaces specified by offset. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @param {String} text The text to process. * @param {Number} offset The amount of spaces you want removed * from the beginning of the text. * @returns Returns the processed text. */ atropa.string.offsetWhiteSpace = function offsetWhiteSpace(text, offset) { 'use strict'; var regx; regx = new RegExp('^ {' + offset + '}'); text = text.replace(regx, ''); return text; }; /** * Converts all tabs in leading whitespace into four spaces. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @param {String} text The text to process * @returns {String} Returns the processed text. */ atropa.string.normalizeWhiteSpacePrefix = function normalizeWhiteSpacePrefix( text ) { 'use strict'; var prefix = text.match(/^\s*/); if(prefix) { prefix = prefix[0]; prefix = prefix.replace(/\t/g, ' '); text = text.replace(/^\s*/, prefix); } return text; }; /** * Converts all tabs into four spaces. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @param {String} text The text to process * @returns {String} Returns the processed text. */ atropa.string.normalizeWhiteSpace = function normalizeWhiteSpace(text) { 'use strict'; text = text.replace(/\t/g, ' '); return text; }; /** * Counts the number of leading space or tab characters but not both. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @param {String} text The text to analyze. * @returns {Number} Returns the quantity of leading spaces or tabs. */ atropa.string.getOffset = function getOffset(text) { 'use strict'; var offset = 0, leadingChar = text.charAt(0); if( leadingChar === ' ' || leadingChar === '\t') { while(text.charAt(offset) === leadingChar && offset < text.length) { offset++; } } return offset; }; /** * Breaks a string into an array of words. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130118 * @param {String} text The text to analyze. * @returns {Array} Returns an array of the words in * the given text. * @requires atropa.arrays.removeEmptyElements */ atropa.string.getWords = function (text) { "use strict"; var out = []; function invalidChars(element) { var matched = /^[\-'’`]+$/.test(element); // invert the result of test. throw out elements that match. return !matched; } out = atropa.arrays.removeEmptyElements( text.split(/[^A-Za-z\-'’`]+/gi) ); out = out.filter(invalidChars); return out; }; /** * Escapes <code>CDATA</code> sections in text * so that the text may be embedded into a * <code>CDATA</code> section. This should be run * on any text which may contain the string * <code>]]></code> since said string will effectively * end the <code>CDATA</code> section prematurely. * @author <a href="mailto:matthewkastor@gmail.com"> * Matthew Christopher Kastor-Inare III </a><br /> * ☭ Hial Atropa!! ☭ * @version 20130118 * @param {String} text The text containing * <code>CDATA</code> sections to escape. * @returns {Array} Returns a string with escaped * <code>CDATA</code> sections. * @see <a href="http://en.wikipedia.org/wiki/CDATA#Nesting"> * http://en.wikipedia.org/wiki/CDATA#Nesting</a> * @see <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=98168"> * https://bugzilla.mozilla.org/show_bug.cgi?id=98168</a> */ atropa.string.escapeCdata = function escapeCdata(text) { "use strict"; return String(text).replace(/\]\]>/g, ']]]]><![CDATA[>'); }; while(atropa.data.requirements.length > 0) { atropa.data.requirements.pop()(); } module.exports = atropa;