/* Strings.js - A library of useful string functions for JavaScript 2021 - 2025 by Mercury Thirteen, Creative Commons Attribution 4.0 International License 2025-07-26 - Moved the strings used by StringRandomGenerate() to a set of constants 2025-01-17 - Added .splitList(), which is basically an adaptation of my old WordSplit() function. 2025-01-16 - Added .replaceAll() 2024-11-15 - Fixed a slight bug in the .insertEvery() function where the main string length was not being recalculated on every pass, leading to the entire string not being processed 2024-11-13 - Added .insertAt() and .insertEvery() 2024-10-30 - Certain characters were not getting escaped in the .trimLeft and .trimRight functions; they have been modified to not use regular expressions. 2022-08-12 - Added a proper description comment header to all functions - Added GetElementTextWidth() - Added StringRandomGenerate() - Capitalized StringLengthMakeStart() and StringLengthMakeEnd() - Improved change log formatting 2021-12-19 - Added stringLengthMakeStart() and stringLengthMakeEnd() 2021-12-14 - Initial release */ // define the needed konstants kStringListCharsLowerCase = "abcdefghijklmnopqrstuvwxyz"; kStringListCharsUpperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; kStringListNumbers = "0123456789"; kStringListSymbols = "!@#$%^&*()-_=+[]{}|\;:'<>,.?/~`"; kStringRandomGenerateLowercase = 1; kStringRandomGenerateUppercase = 2; kStringRandomGenerateNumbers = 4; kStringRandomGenerateSymbols = 8; function GetElementTextWidth(element) { /* Returns the width in pixels of the innerText of the specified element. Input: element The element whose innerText will be measured. Output: The width in pixels of the element's innerText. */ // create a temporary element tempText = document.createElement("span"); document.body.appendChild(tempText); // set up the temporary element we just created with key attributes of the specified element tempText.innerText = element.innerText; tempText.style.font = element.style.font; tempText.style.fontSize = element.textSize; tempText.style.height = 'auto'; tempText.style.position = 'absolute'; tempText.style.whiteSpace = 'no-wrap'; tempText.style.width = 'auto'; // calculate the width of the text formattedWidth = Math.ceil(tempText.clientWidth); // remove the temporary element and return! document.body.removeChild(tempText); return formattedWidth; } function StringLengthMakeStart(inputString, targetLength) { /* Returns inputString either truncated or padded (from the beginning) to the length specified. Input: inputString The string which will be truncated or padded. If truncated, "..." will be added to the end of the string. targetLength The length to which inputString will be truncated or padded. Output: String The original string padded or truncated to targetLength. */ if (inputString.length > targetLength) { return inputString.slice(0, targetLength - 3) + '...'; }else{ return inputString.padStart(targetLength); } } function StringLengthMakeEnd(inputString, targetLength) { /* Returns inputString either truncated or padded (from the end) to the length specified. Input: inputString The string which will be truncated or padded. If truncated, "..." will be added to the end of the string. targetLength The length to which inputString will be truncated or padded. Output: String The original string padded or truncated to targetLength. */ if (inputString.length > targetLength) { return inputString.slice(0, targetLength - 3) + '...'; }else{ return inputString.padEnd(targetLength); } } function StringRandomGenerate(length, options) { /* Returns a string of random characters according to the options specified. Input: length The length to which the random string will be built options Options for building the random string: kStringRandomGenerateLowercase Include lower case letters in the output string kStringRandomGenerateUppercase Include upper case letters in the output string kStringRandomGenerateNumbers Include numbers in the output string kStringRandomGenerateSymbols Include symbols in the output string Output: String A string containing random characters as specified */ var sourceString = "", randomString = ""; // build the string from which the random string will be built if (options & kStringRandomGenerateLowercase) sourceString = sourceString + kStringListCharsLowerCase; if (options & kStringRandomGenerateUppercase) sourceString = sourceString + kStringListCharsUpperCase; if (options & kStringRandomGenerateNumbers) sourceString = sourceString + kStringListNumbers; if (options & kStringRandomGenerateSymbols) sourceString = sourceString + kStringListSymbols; // loop through length iterations and built the string for (index = 0; index