Skip to content

Instantly share code, notes, and snippets.

@momo-lab
Created July 31, 2024 01:30
Show Gist options
  • Save momo-lab/5bfec28b7fe93b43b28f4e69649d1da1 to your computer and use it in GitHub Desktop.
Save momo-lab/5bfec28b7fe93b43b28f4e69649d1da1 to your computer and use it in GitHub Desktop.
// 複数行テキストの余分なインデントを除去する。
String.prototype.dedent = function() {
const lines = this.split("\n");
const indent = lines
.map(line => line.match(/^\s+(?=[^\s])/)?.[0].length)
.filter(n => n)
.reduce((a, b) => Math.min(a, b));
const re = new RegExp(`^\\s{${indent}}`);
return lines
.map(line => line.replace(re, ""))
.join("\n")
.trim();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment