forked from regularjs/regular-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeindent.js
More file actions
47 lines (45 loc) · 825 Bytes
/
deindent.js
File metadata and controls
47 lines (45 loc) · 825 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
var splitRE = /\r?\n/g
var emptyRE = /^\s*$/
var needFixRE = /^(\r?\n)*[\t\s]/
module.exports = function deindent( str ) {
if (!needFixRE.test( str )) {
return str
}
var lines = str.split( splitRE )
var min = Infinity
var type,
cur,
c
for (var 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( function( line ) {
return line.slice( min )
} ).join( '\n' )
}
function count( line, type ) {
var i = 0
while (line.charAt( i ) === type) {
i++
}
return i
}