Skip to content

Instantly share code, notes, and snippets.

@normanzb
Created April 30, 2025 23:09
Show Gist options
  • Save normanzb/0ddd55e864597f5a31298d283f099f43 to your computer and use it in GitHub Desktop.
Save normanzb/0ddd55e864597f5a31298d283f099f43 to your computer and use it in GitHub Desktop.
helper function to trim spaces or line breaks from template
/**
* 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