Skip to content

Instantly share code, notes, and snippets.

@Abhinav1217
Created June 2, 2024 20:00
Show Gist options
  • Save Abhinav1217/ae9cc84486ca9b3789fe5f7fd380d36d to your computer and use it in GitHub Desktop.
Save Abhinav1217/ae9cc84486ca9b3789fe5f7fd380d36d to your computer and use it in GitHub Desktop.
A collection of utility functions for string manipulation.
/**
* StringUtil.ts
*
* A collection of utility functions for string manipulation.
*
* Attribution: Custom implementations inspired by common JavaScript practices.
*
* ## Contents:
* - `trimWhitespace(str)`: Removes leading and trailing whitespace from a string.
* - `toCamelCase(str)`: Converts a string to camelCase.
* - `splitIntoWords(str)`: Splits a string into an array of words.
* - `capitalizeFirstLetter(str)`: Capitalizes the first letter of a string.
* - `ensureLength(str, minLength)`: Ensures a string has at least a minimum length by padding with spaces.
* - `leftPad(str, targetLength, padChar)`: Pads the start of a string with a specified character until it reaches a certain length.
* - `removeSpaces(str)`: Removes all spaces from a string.
* - `startsWithVowel(str)`: Checks if a string starts with a vowel.
* - `endsWithConsonant(str)`: Checks if a string ends with a consonant.
*/
/**
* Removes leading and trailing whitespace from a string.
*
* @param {string} str - The input string.
* @returns {string} The trimmed string.
*/
export function trimWhitespace(str: string): string {
return str.trim();
}
/**
* Converts a string to camelCase.
*
* @param {string} str - The input string.
* @returns {string} The camelCased string.
*/
export function toCamelCase(str: string): string {
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
return index === 0 ? word.toLowerCase() : word.toUpperCase();
}).replace(/\s+/g, '');
}
/**
* Splits a string into an array of words.
*
* @param {string} str - The input string.
* @returns {string[]} An array of words.
*/
export function splitIntoWords(str: string): string[] {
return str.split(/\s+/);
}
/**
* Capitalizes the first letter of a string.
*
* @param {string} str - The input string.
* @returns {string} The capitalized string.
*/
export function capitalizeFirstLetter(str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1);
}
/**
* Ensures a string has at least a minimum length by padding with spaces.
*
* @param {string} str - The input string.
* @param {number} minLength - The minimum length the string should have.
* @returns {string} The padded string.
*/
export function ensureLength(str: string, minLength: number): string {
return str.padEnd(minLength, ' ');
}
/**
* Pads the start of a string with a specified character until it reaches a certain length.
*
* @param {string} str - The input string.
* @param {number} targetLength - The target length of the string after padding.
* @param {string} padChar - The character to pad the string with.
* @returns {string} The padded string.
*/
export function leftPad(str: string, targetLength: number, padChar: string = ' '): string {
return str.padStart(targetLength, padChar);
}
/**
* Removes all spaces from a string.
*
* @param {string} str - The input string.
* @returns {string} The string without spaces.
*/
export function removeSpaces(str: string): string {
return str.replace(/\s+/g, '');
}
/**
* Checks if a string starts with a vowel.
*
* @param {string} str - The input string.
* @returns {boolean} True if the string starts with a vowel, false otherwise.
*/
export function startsWithVowel(str: string): boolean {
return ['a', 'e', 'i', 'o', 'u'].some(vowel => str[0].toLowerCase() === vowel);
}
/**
* Checks if a string ends with a consonant.
*
* @param {string} str - The input string.
* @returns {boolean} True if the string ends with a consonant, false otherwise.
*/
export function endsWithConsonant(str: string): boolean {
return !['a', 'e', 'i', 'o', 'u'].includes(str[str.length - 1].toLowerCase());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment