Skip to content

Commit

Permalink
Update strings.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
knuijver authored Apr 5, 2024
1 parent a0e0e21 commit dc016e0
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/common/strings.ts
Original file line number Diff line number Diff line change
@@ -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) =>
Expand Down

0 comments on commit dc016e0

Please sign in to comment.