Added Backend
This commit is contained in:
51
backend/src/utils/stringRendering.ts
Normal file
51
backend/src/utils/stringRendering.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
|
||||
export const renderAsCurrency = (value: string | number,currencyString = "€") => {
|
||||
return `${Number(value).toFixed(2).replace(".",",")} ${currencyString}`
|
||||
}
|
||||
|
||||
export const splitStringBySpace = (input:string,maxSplitLength:number,removeLinebreaks = false) => {
|
||||
|
||||
if(removeLinebreaks) {
|
||||
input = input.replaceAll("\n","")
|
||||
}
|
||||
|
||||
let splitStrings: string[] = []
|
||||
|
||||
input.split("\n").forEach(string => {
|
||||
splitStrings.push(string)
|
||||
})
|
||||
|
||||
let returnSplitStrings: string[] = []
|
||||
|
||||
splitStrings.forEach(string => {
|
||||
let regex = / /gi, result, indices = [];
|
||||
while ( (result = regex.exec(string)) ) {
|
||||
indices.push(result.index);
|
||||
}
|
||||
|
||||
let lastIndex = 0
|
||||
|
||||
if(string.length > maxSplitLength) {
|
||||
let tempStrings = []
|
||||
|
||||
for (let i = maxSplitLength; i < string.length; i = i + maxSplitLength) {
|
||||
let nearestIndex = indices.length > 0 ? indices.reduce(function(prev, curr) {
|
||||
return (Math.abs(curr - i) < Math.abs(prev - i) ? curr : prev);
|
||||
}) : i
|
||||
|
||||
tempStrings.push(string.substring(lastIndex,nearestIndex))
|
||||
|
||||
lastIndex = indices.length > 0 ? nearestIndex + 1 : nearestIndex
|
||||
}
|
||||
|
||||
tempStrings.push(string.substring(lastIndex,input.length))
|
||||
|
||||
returnSplitStrings.push(...tempStrings)
|
||||
|
||||
} else {
|
||||
returnSplitStrings.push(string)
|
||||
}
|
||||
})
|
||||
|
||||
return returnSplitStrings
|
||||
}
|
||||
Reference in New Issue
Block a user