Use min-indent (#4)
James Kyle authored 6 years ago
Sindre Sorhus committed 6 years ago
0 | 0 | 'use strict'; |
1 | const minIndent = require('min-indent'); | |
2 | ||
1 | 3 | module.exports = str => { |
2 | const match = str.match(/^[ \t]*(?=\S)/gm); | |
4 | const indent = minIndent(str); | |
3 | 5 | |
4 | if (!match) { | |
6 | if (indent === 0) { | |
5 | 7 | return str; |
6 | 8 | } |
7 | 9 | |
8 | // TODO: Use spread operator when targeting Node.js 6 | |
9 | const indent = Math.min.apply(Math, match.map(x => x.length)); | |
10 | 10 | const re = new RegExp(`^[ \\t]{${indent}}`, 'gm'); |
11 | 11 | |
12 | return indent > 0 ? str.replace(re, '') : str; | |
12 | return str.replace(re, ''); | |
13 | 13 | }; |