From d6af2b9616edb3b2afa5de3a189bb2953c410364 Mon Sep 17 00:00:00 2001 From: Ruben Knuijver Date: Fri, 5 Apr 2024 16:17:54 +0200 Subject: [PATCH] Update strings.ts (#8) --- src/common/strings.ts | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/common/strings.ts b/src/common/strings.ts index 9848f68..49cef6d 100644 --- a/src/common/strings.ts +++ b/src/common/strings.ts @@ -1,13 +1,39 @@ +/** + * Make the first char upper case. + * @param str input string + * @returns string with at least first char in upper case. + */ +export function capitalize(str: string) { + return str.charAt(0).toUpperCase() + str.slice(1); +} + +/** + * Join strings as a space separated string. + * @param strings Input strings + * @returns spaces separated string + */ +export function join(...strings: (string | undefined)[]) { + return strings.filter(Boolean).join(' '); +} + export const leadingZeros = ( val: number | string, maxLength: number = 2, fillString: string = "0" ) => String(val).padStart(maxLength, fillString); +/** + * Make a camel case string from a sentence. + * @param s string is a sentence to turn into camelCase + * @returns camelCase text + */ export const toCamel = (s: string) => { - return s.replace(/([-_][a-z])/gi, ($1) => { - return $1.toUpperCase().replace("-", "").replace("_", ""); - }); + return ( + s.charAt(0).toLowerCase() + + s.slice(1).replace(/([-_ ][a-zA-Z])/gi, ($1) => { + return $1.toUpperCase().replace(/[-_ ]/gi, ''); + }) + ); }; export const camelToSnakeCase = (str: string) =>