nope/lib/helpers/stringMethods.ts

179 lines
4.1 KiB
TypeScript
Raw Normal View History

2020-11-06 08:10:30 +00:00
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @desc [description]
*/
/**
* Helper Function to varify the given string.
* Removes every char which doesn't match a variable name.
* (a-z, A-Z, 0-9).
*
* If `str` starts with an invalid char, (like a number),
* an underscore is added.
* @param {string} str the Stirng
* @returns {string} the adapted name
*/
export function varifyString(str: string): string {
// Regex to test if the first char is correct.
const firstCharCorrect = new RegExp("^[_a-zA-Z]", "i");
const ret = str.replaceAll(new RegExp("[^a-zA-Z0-9]", "g"), "_");
if (firstCharCorrect.test(ret)) {
return ret;
}
return "_" + ret;
}
/**
* Replaces all Chars in a String
* @param str base string
* @param value the value which should be replaced, Or an array containing the value and replacer
* @param replacement the value which is used as replacement
*/
2021-12-04 07:25:26 +00:00
export function replaceAll(
str: string,
value: string | Array<[string, string] | string>,
replacement: string = null
2021-12-04 07:25:26 +00:00
): string {
if (Array.isArray(value)) {
for (const data of value) {
if (Array.isArray(data)) {
str = replaceAll(str, data[0], data[1]);
} else if (replacement !== null) {
str = replaceAll(str, data, replacement);
} else {
throw Error(
"Please provide an arry containing a replace or an in replacement."
);
}
}
return str;
}
if (replacement === null) {
throw Error("Please provide an arry or an in replacement.");
}
return str.split(value).join(replacement);
}
/**
2021-12-04 07:25:26 +00:00
* Function to Pad a String.
2022-08-02 20:31:48 +00:00
* @param num The Number to pad
* @param size the amount of zeros to add
* @param maxLength The max length of the number.
2021-12-04 07:25:26 +00:00
*/
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.
2021-12-23 10:49:15 +00:00
* @param str
* @param maxLength
* @returns
2021-12-23 10:48:06 +00:00
*/
2021-12-23 10:49:15 +00:00
export function insertNewLines(str: string, maxLength = 100) {
// now we try to split the string
2021-12-23 10:48:06 +00:00
const splitted = str.split(" ");
const ret: string[] = [];
let length = 0;
2021-12-23 10:49:15 +00:00
for (const word of splitted) {
console.log("test");
2021-12-23 10:48:06 +00:00
length += word.length + 1;
ret.push(word);
2021-12-23 10:49:15 +00:00
if (length > maxLength) {
2021-12-23 10:48:06 +00:00
ret.push("\n");
length = 0;
}
}
return ret;
}