Newer
Older
percord / src / util / util / String.ts
@Emma [it/its]@Rory& Emma [it/its]@Rory& on 27 Jul 2025 1 KB WIP logo/terminal stuff
/*
	Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
	Copyright (C) 2023 Spacebar and Spacebar Contributors
	
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Affero General Public License as published
	by the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Affero General Public License for more details.
	
	You should have received a copy of the GNU Affero General Public License
	along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

import { SPECIAL_CHAR } from "./Regex";

export function trimSpecial(str?: string): string {
	if (!str) return "";
	return str.replace(SPECIAL_CHAR, "").trim();
}

/**
 * Capitalizes the first letter of a string.
 * @param str The string to capitalize.
 * @returns The capitalized string.
 */
export function capitalize(str: string): string {
	if (!str) return "";
	return str.charAt(0).toUpperCase() + str.slice(1);
}

export function centerString(str: string, len: number): string {
	const pad = len - str.length;
	const padLeft = Math.floor(pad / 2) + str.length;
	return str.padStart(padLeft).padEnd(len);
}