nope/lib/helpers/stringMethods.ts

136 lines
2.9 KiB
TypeScript
Raw Normal View History

2020-11-06 08:10:30 +00:00
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2020-11-06 08:54:30
* @modify date 2020-11-06 08:54:31
* @desc [description]
*/
/**
* Replaces all Chars in a String
* @param str base string
* @param value the value which should be replaced
* @param replacement the value which is used as replacement
*/
2021-12-04 07:25:26 +00:00
export function replaceAll(
str: string,
value: string,
replacement: string
): string {
return str.split(value).join(replacement);
}
/**
2021-12-04 07:25:26 +00:00
* Function to Pad a String.
* @param num
* @param size
* @param maxLength
*/
export function padString(
num: number,
size: number,
maxLength = false
): string {
let _size = size;
2021-11-10 11:28:48 +00:00
if (typeof maxLength === "boolean" && maxLength) {
_size = Math.ceil(Math.log10(size));
}
let s = num + "";
while (s.length < _size) s = "0" + s;
return s;
}
/**
2021-12-04 07:25:26 +00:00
* Inserts a String in the String
* @param str base string
* @param index index where the content should be inserted
* @param content the content to insert
*/
export function insert(str: string, index: number, content: string): string {
if (index > 0) {
return str.substring(0, index) + content + str.substring(index, str.length);
} else {
return content + str;
}
}
/**
* Function to Camelize a String
* @param str The String,
* @param char A, used to determine "new words"
*/
2021-11-10 11:28:48 +00:00
export function camelize(str: string, char = "_"): string {
2021-12-04 07:25:26 +00:00
return replaceAll(str, char, " ")
.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
return index == 0 ? word.toLowerCase() : word.toUpperCase();
})
.replace(/\s+/g, "");
2021-11-10 11:28:48 +00:00
}
2021-11-12 07:57:03 +00:00
/**
* Helper to limit the string to a specific length. the rest is reduced by the limitChars
*
* @author M.Karkowski
* @export
* @param {string} str The string to work with
* @param {number} length The max length including the limitChars
* @param {string} [limitChars="..."] The chars which should be used to express limiting
* @return {*} {{
2021-12-04 07:25:26 +00:00
* isLimited: boolean,
2021-11-12 07:57:03 +00:00
* original: string,
* limited: string,
* }}
*/
2021-12-04 07:25:26 +00:00
export function limitString(
str: string,
length: number,
limitChars = "..."
): {
isLimited: boolean;
original: string;
limited: string;
2021-11-10 11:28:48 +00:00
} {
if (str.length > length) {
return {
isLimited: true,
original: str,
2021-12-04 07:25:26 +00:00
limited: str.slice(0, length - limitChars.length) + limitChars,
2021-11-10 11:28:48 +00:00
};
} else {
return {
isLimited: false,
original: str,
2021-12-04 07:25:26 +00:00
limited: str,
2021-11-10 11:28:48 +00:00
};
}
2021-12-04 07:25:26 +00:00
}
2021-12-23 10:48:06 +00:00
/**
* Helper to insert new lines after a given amount of time.
* @param str
* @param maxLength
* @returns
*/
export function insertNewLines(str: string, maxLength= 100){
// now we try to split the string
const splitted = str.split(" ");
const ret: string[] = [];
let length = 0;
for (const word of splitted){
console.log("test")
length += word.length + 1;
ret.push(word);
if (length > maxLength){
ret.push("\n");
length = 0;
}
}
return ret;
}