-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (70 loc) · 1.78 KB
/
index.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const multipleBlankLines = /\n{3,}/g;
const singleBlankLine = "\n\n";
module.exports = function sievery( sourceData, targets ) {
if (!sourceData) {
throw new TypeError('sourceData must not be empty')
}
if (typeof sourceData !== 'string') {
throw new TypeError('sourceData must be a string')
}
const lines = sourceData.split( '\n' )
const codes = new Array( targets.length )
const normalMode = -1
const targetMode = targets.length
let excludeMode = 0
let mode = normalMode
const targetMarkers = targets.map( target => ( {
line: lineMarker( target ),
start: startMarker( target ),
end: endMarker( target )
} ) )
targetMarkers.forEach( ( target, index ) => {
const excludes = targetMarkers.filter( item => item !== target )
const code = []
lines.forEach( line => {
if ( mode === excludeMode ) {
if ( excludes[ excludeMode ].end.test( line ) ) {
mode = normalMode
}
return
}
if ( mode === targetMode ) {
if ( target.end.test( line ) ) {
mode = normalMode
return
}
code.push( line )
return
}
if ( target.line.test( line ) ) {
code.push( line.replace( target.line, '' ) )
return
}
if ( target.start.test( line ) ) {
mode = targetMode
return
}
for ( let i = 0; i < excludes.length; i++ ) {
if ( excludes[ i ].line.test( line ) ) {
return
}
if ( excludes[ i ].start.test( line ) ) {
mode = excludeMode = i
return
}
}
code.push( line )
} )
codes[ index ] = code.join( '\n' ).replace( multipleBlankLines, singleBlankLine )
} )
return codes
}
function lineMarker( token ) {
return new RegExp( ` // ${token}$` )
}
function startMarker( token ) {
return new RegExp( `// ${token}/$` )
}
function endMarker( token ) {
return new RegExp( `// /${token}$` )
}