everyutil
Version:
A comprehensive library of lightweight, reusable utility functions for JavaScript and TypeScript, designed to streamline common programming tasks such as string manipulation, array processing, date handling, and more.
22 lines (21 loc) • 774 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.centerText = void 0;
/**
* Centers text in a given width, using a specified fill character.
*
* Example: centerText("hi", 5, "-") → "--hi-"
*
* @author @dailker
* @param {string} str - The input string.
* @param {number} width - The total width of the resulting string.
* @param {string} [fillChar=" "] - The character to use for padding.
* @returns {string} The centered string.
*/
function centerText(str, width, fillChar = ' ') {
const total = Math.max(width - str.length, 0);
const left = Math.floor(total / 2);
const right = total - left;
return fillChar.repeat(left) + str + fillChar.repeat(right);
}
exports.centerText = centerText;