js-essential-kit
Version:
This library provides a comprehensive set of utility functions for various common tasks, including date calculations, formatting, masking, normalizing data, and validation
51 lines (50 loc) • 1.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizeName = normalizeName;
exports.arrayToStringWithQuotes = arrayToStringWithQuotes;
exports.normalizeString = normalizeString;
/**
* Normalizes a name by capitalizing the first letter of each word.
*
* This function converts the input string to lowercase, splits it into words,
* capitalizes the first letter of each word, and then joins them back together.
* Extra spaces are removed.
*
* @param {string} name - The name string to be normalized.
* @returns {string} - The normalized name.
*
* @example
* // Normalize a name
* console.log(normalizeName('john DOE')); // 'John Doe'
*
* // Normalize a name with extra spaces
* console.log(normalizeName(' alice SMITH ')); // 'Alice Smith'
*/
function normalizeName(name) {
return name
.trim()
.toLowerCase()
.split(/\s+/)
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');
}
/**
* Converts an array of strings into a single string with each item wrapped in quotes.
*
* This function takes an array of strings, wraps each string in double quotes,
* and then joins them with a comma and a space.
*
* @param {string[]} items - The array of strings to be converted.
* @returns {string} - The resulting string with each item wrapped in quotes.
*
* @example
* // Convert an array to a quoted string
* console.log(arrayToStringWithQuotes(['apple', 'banana', 'cherry']));
* // Output: '"apple", "banana", "cherry"'
*/
function arrayToStringWithQuotes(items) {
return items.map((item) => `"${item}"`).join(', ');
}
function normalizeString(str) {
return str.replace(/\u00a0/g, ' ').normalize('NFKC');
}