Created
May 26, 2023 10:51
-
-
Save honboubao/392b1097d8522a6e774f04f74f4df988 to your computer and use it in GitHub Desktop.
JS Concat Template String Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Template String Tag Funktion, druckt die eingesetzten Werte im Template und den vorangestellten Verbindungsstrings nur aus, wenn der Wert nicht leer ist. | |
* | |
* z.B. | |
* format`${strasse} ${hausnummer} ${stockTuernummer} / ${abgabestelle} / ${anschriftzusatz}` | |
* | |
* => Bambuslichtung 15 / Scheunentor | |
* | |
* wenn nur strasse, hausnummer und abgabestelle angegeben sind | |
* | |
*/ | |
function format(trennzeichen: TemplateStringsArray, ...einsetzungen: unknown[]): string { | |
let result = ''; | |
for (let i = 0; i < trennzeichen.length; i++) { | |
const e = einsetzungen[i]; | |
if (e !== null && e !== undefined && e !== '') { | |
result += trennzeichen[i] + e; | |
} | |
} | |
result = result.replace(/[ \t]+/g, ' '); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment