Skip to content

Instantly share code, notes, and snippets.

@yuler
Created November 28, 2021 09:47
Show Gist options
  • Select an option

  • Save yuler/8cf2bbd0e8a190ac4fedbe6fdc98eb99 to your computer and use it in GitHub Desktop.

Select an option

Save yuler/8cf2bbd0e8a190ac4fedbe6fdc98eb99 to your computer and use it in GitHub Desktop.
Remove indent
function dedent(str) {
str = str.replace(/\r?\n/g, '\n')
let arr = str.match(/^[ \t]*(?=\S)/gm)
let i = 0,
min = 1 / 0,
len = (arr || []).length
for (; i < len; i++) min = Math.min(min, arr[i].length)
return len && min
? str.replace(new RegExp(`^[ \\t]{${min}}`, 'gm'), '')
: str
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment