StructureJS
0.13.0A class based utility library for building modular and scalable web platform applications. Features opt-in classes and utilities which provide a solid foundation and toolset to build your next project.
(function (factory) { if (typeof module === 'object' && typeof module.exports === 'object') { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === 'function' && define.amd) { define(["require", "exports"], factory); } })(function (require, exports) { "use strict"; /** * The StringUtil... * * @class StringUtil * @module StructureJS * @submodule util * @author Robert S. (www.codeBelt.com) * @static */ var StringUtil = (function () { function StringUtil() { throw new Error('[StringUtil] Do not instantiate the StringUtil class because it is a static class.'); } /** * Gets the extension name off the string being passed in. * * @method getExtension * @param filename {string} * @param withDot {boolean} If you want the period to be included in the extension name. * @returns {string} * @public * @static * @example * StringUtil.getExtension('file.exe'); * // 'exe' * * StringUtil.getExtension('file.exe', true); * // '.exe' */ StringUtil.getExtension = function (filename, withDot) { if (withDot === void 0) { withDot = false; } var num = (withDot === true) ? 0 : 1; return filename.slice(filename.lastIndexOf('.') + num, filename.length); }; /** * Converts a string to a sentence case string. * * @method toSentence * @param str {string} * @param [separator] {string} Can be any string you want to use as a separator. * @returns {string} * @public * @static * @example * StringUtil.toSentence("liveDown_by-the.River"); * // 'live down by the river' * * StringUtil.toSentence("liveDown_by-the.River", '-'); * // 'live-down-by-the-river' * * StringUtil.toSentence("liveDown_by-the.River", '_'); * // 'live_down_by_the_river' * * StringUtil.toSentence("liveDown_by-the.River", '/'); * // 'live/down/by/the/river' */ StringUtil.toSentence = function (str, separator) { if (separator === void 0) { separator = ' '; } return String(str) .replace(/(\d)/g, '$1 ') .replace(/([a-z](?=[A-Z]))/g, '$1 ') .replace(/[^a-zA-Z0-9 ]/g, ' ') .replace(/\s{2,}/g, ' ') .replace(/^ | $/g, '') .toLowerCase() .replace(/\s+/g, separator); }; /** * Converts a string to a camel case string. * * @method toCamelCase * @param str {string} * @returns {string} * @public * @static * @example * StringUtil.toCamelCase("liveDown_by-the.River"); * // 'liveDownByTheRiver' */ StringUtil.toCamelCase = function (str) { return StringUtil.toSentence(str) .replace(/ (\w)/g, function (_, $1) { return $1.toUpperCase(); }); }; /** * Converts a hyphen string to a pascal case string. * * @method toPascalCase * @param str {string} * @returns {string} * @public * @static * @example * StringUtil.toPascalCase("liveDown_by-the.River"); * // 'LiveDownByTheRiver' */ StringUtil.toPascalCase = function (str) { return StringUtil.toCamelCase(str) .replace(/^[a-zA-Z]/, function (a, b, c) { return a.toUpperCase(); }); }; /** * Converts a string to a constant case string. * * @method toConstantCase * @param str {string} * @returns {string} * @public * @static * @example * StringUtil.toConstantCase("liveDown_by-the.River"); * // 'LIVE_DOWN_BY_THE_RIVER' */ StringUtil.toConstantCase = function (str) { return StringUtil.toSentence(str, '_') .toUpperCase(); }; /** * Creates a universally unique identifier. * * @method createUUID * @returns {string} * @public * @static * @example * StringUtil.createUUID(); * // 'a95d7134-3342-4001-bcea-cc0371b70dec' */ StringUtil.createUUID = function () { var uuid = ('xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx').replace(/[xy]/g, function (c) { var r = Math.random() * 16 | 0; var v = (c == 'x') ? r : (r & 0x3 | 0x8); return v.toString(16); }); return uuid; }; /** * Converts a query string to an object. * * @method queryStringToObject * @param queryString {string} * @param [useParseFloat=false] {boolean} If true converts strings to numbers. * @returns {Object|Null} * @public * @static * @example * StringUtil.queryStringToObject('?name=Robert&age=23&gender=male'); * // {name: 'Robert', age: '23', gender: 'male'} * * StringUtil.queryStringToObject('?name=Robert&age=23&gender=male', true); * // {name: 'Robert', age: 23, gender: 'male'} */ StringUtil.queryStringToObject = function (queryString, useParseFloat) { if (useParseFloat === void 0) { useParseFloat = false; } var params = {}; var temp = null; var str = queryString.substring(queryString.indexOf('?') + 1); if (str === '') { return null; } // Split into key/value pairs var queries = str.split('&'); // Convert the array of strings into an object var len = queries.length; for (var i = 0; i < len; i++) { temp = queries[i].split('='); params[temp[0]] = (useParseFloat === true && isNaN(parseFloat(temp[1])) === false) ? parseFloat(temp[1]) : temp[1]; } return params; }; /** * Remove all whitespace from the string passed in. * * @method removeAllWhitespace * @param str {string} * @returns {string} * @public * @static * @example * let str = ' a b c d e f g '; * StringUtil.removeAllWhitespace(str); * // 'abcdefg' */ StringUtil.removeAllWhitespace = function (str) { return str.replace(/\s+/g, ''); }; /** * Remove leading and trailing whitespace. * * @method removeLeadingTrailingWhitespace * @param str {string} * @returns {string} * @public * @static * @example * let str = ' a b c d e f g '; * StringUtil.removeLeadingTrailingWhitespace(str); * // 'a b c d e f g' */ StringUtil.removeLeadingTrailingWhitespace = function (str) { return str.replace(/(^\s+|\s+$)/g, ''); }; /** * * @method truncate * @param text {string} * @param length {int} * @param indicator {string} * @returns {string} * @public * @static * @example * StringUtil.truncate('Robert is cool and he likes bruschetta.', 14)); * // 'Robert is cool...' * * StringUtil.truncate('Robert is cool and he likes bruschetta.', 14, '!!!')); * // 'Robert is cool!!!' */ StringUtil.truncate = function (text, length, indicator) { if (indicator === void 0) { indicator = '...'; } if (text.length <= length) { return text; } else { return text.substr(0, length) + indicator; } }; /** * Replaces each format item in a specified string with the text equivalent of a corresponding object's value. * * @method format * @returns {string} * @param str {string} * @param ...rest {Array.<any>} * @public * @static * @example * StringUtil.format('Robert is {0}. Very {0} and {1}!', 'cool', 'smart'); * // 'Robert is cool. Very cool and smart!' */ StringUtil.format = function (str) { var rest = []; for (var _i = 1; _i < arguments.length; _i++) { rest[_i - 1] = arguments[_i]; } var length = rest.length; var value = str; for (var i = 0; i < length; i++) { var reg = new RegExp('\\{' + i + '\\}', 'gm'); value = value.replace(reg, rest[i]); } return value; }; /** * Updates a value in the query string by its key name. * * @method paramReplace * @param queryString * @param name * @param value * @returns {string|void} * @example * StringUtil.paramReplace('?name=Robert&age=23&gender=male', 'gender', 'female'); * // '?name=Robert&age=23&gender=female' */ StringUtil.paramReplace = function (queryString, name, value) { // Find the param with regex // Grab the first character in the returned string (should be ? or &) // Replace our href string with our new value, passing on the name and delimiter var re = new RegExp('[\\?&]' + name + '=([^&#]*)'); var delimiter = re.exec(queryString)[0].charAt(0); return queryString.replace(re, delimiter + name + '=' + value); }; return StringUtil; }()); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = StringUtil; });