Skip to content

Instantly share code, notes, and snippets.

@jniac
Last active October 18, 2020 12:29
Show Gist options
  • Save jniac/3d3a14fa5bccce7baf8b14485c16c3ee to your computer and use it in GitHub Desktop.
Save jniac/3d3a14fa5bccce7baf8b14485c16c3ee to your computer and use it in GitHub Desktop.
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
@jniac
Copy link
Author

jniac commented Oct 18, 2020

from (any string beginnings with whitespaces)

      #foo {
          color: red;
      }

returns

#foo {
    color: red;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment