Last active
October 18, 2020 12:29
-
-
Save jniac/3d3a14fa5bccce7baf8b14485c16c3ee 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
const removeHeadingWhitespaces = str => { | |
const lines = str.split('\n') | |
while (lines.length > 0 && /^\s*$/.test(lines[0])) { | |
lines.shift() | |
} | |
while (lines.length > 0 && /^\s*$/.test(lines[lines.length - 1])) { | |
lines.pop() | |
} | |
const length = lines[0].match(/^\s*/)[0].length | |
const re = new RegExp(`^\\s{0,${length}}`) | |
return lines.map(line => line.replace(re, '')).join('\n') | |
} | |
export { removeHeadingWhitespaces } | |
export default removeHeadingWhitespaces |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
from (any string beginnings with whitespaces)
returns