Created
April 30, 2025 23:09
-
-
Save normanzb/0ddd55e864597f5a31298d283f099f43 to your computer and use it in GitHub Desktop.
helper function to trim spaces or line breaks from template
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
/** | |
* helper function to trim spaces or line breaks from the template | |
* @param template | |
* @returns | |
*/ | |
function trimTemplate(template: string) { | |
const trimmed = template.replace(/^\s*/, "").replace(/\s*$/, ""); | |
const lines = trimmed.split("\n"); | |
const indent = /^(\s*)/.exec(lines[0])?.[1]; | |
if (!indent) { | |
return trimmed; | |
} | |
return lines | |
.map((line) => line.replace(new RegExp(`^${indent}`), "")) | |
.join("\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment