Created
July 31, 2024 01:30
-
-
Save momo-lab/5bfec28b7fe93b43b28f4e69649d1da1 to your computer and use it in GitHub Desktop.
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
// 複数行テキストの余分なインデントを除去する。 | |
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