52 lines
1.4 KiB
TypeScript
52 lines
1.4 KiB
TypeScript
|
/**
|
||
|
* 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
|
||
|
*/
|
||
|
export function replaceAll(str: string, value: string, replacement: string): string {
|
||
|
return str.split(value).join(replacement);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Function to Pad a String.
|
||
|
* @param num
|
||
|
* @param size
|
||
|
* @param maxLength
|
||
|
*/
|
||
|
export function padString(num: number, size: number, maxLength = false) {
|
||
|
|
||
|
let _size = size;
|
||
|
if (typeof maxLength === 'boolean' && maxLength) {
|
||
|
_size = Math.ceil(Math.log10(size));
|
||
|
}
|
||
|
|
||
|
let s = num + "";
|
||
|
while (s.length < _size) s = "0" + s;
|
||
|
return s;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 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"
|
||
|
*/
|
||
|
export function camelize(str: string, char = '_') {
|
||
|
return replaceAll(str, char, ' ').replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
|
||
|
return index == 0 ? word.toLowerCase() : word.toUpperCase();
|
||
|
}).replace(/\s+/g, '');
|
||
|
}
|