Evan You
9 years ago
10 changed files with 56 additions and 114 deletions
@ -0,0 +1,45 @@ |
|||
/* @flow */ |
|||
|
|||
const splitRE = /\r?\n/g |
|||
const emptyRE = /^\s*$/ |
|||
const needFixRE = /^(\r?\n)*[\t\s]/ |
|||
|
|||
export default function deindent (str: string): string { |
|||
if (!needFixRE.test(str)) { |
|||
return str |
|||
} |
|||
const lines = str.split(splitRE) |
|||
let min = Infinity |
|||
let type, cur, c |
|||
for (let i = 0; i < lines.length; i++) { |
|||
var line = lines[i] |
|||
if (!emptyRE.test(line)) { |
|||
if (!type) { |
|||
c = line.charAt(0) |
|||
if (c === ' ' || c === '\t') { |
|||
type = c |
|||
cur = count(line, type) |
|||
if (cur < min) { |
|||
min = cur |
|||
} |
|||
} else { |
|||
return str |
|||
} |
|||
} else { |
|||
cur = count(line, type) |
|||
if (cur < min) { |
|||
min = cur |
|||
} |
|||
} |
|||
} |
|||
} |
|||
return lines.map(line => line.slice(min)).join('\n') |
|||
} |
|||
|
|||
function count (line: string, type: string): number { |
|||
var i = 0 |
|||
while (line.charAt(i) === type) { |
|||
i++ |
|||
} |
|||
return i |
|||
} |
Loading…
Reference in new issue