1
- const blockC = require ( './region-matchers/block-c' ) ;
2
- const html = require ( './region-matchers/html' ) ;
3
- const inlineC = require ( './region-matchers/inline-c' ) ;
1
+ import { blockC } from './region-matchers/block-c' ;
2
+ import { html } from './region-matchers/html' ;
3
+ import { inlineC } from './region-matchers/inline-c' ;
4
4
5
- export type Region = { lines : string [ ] , open : boolean } ;
6
- export type RegionMap = { [ regionName : string ] : Region } ;
5
+ export type Region = { lines : string [ ] , open : boolean } ;
6
+ export type RegionMap = { [ regionName : string ] : Region } ;
7
7
8
8
export function regionParser ( contents : string , fileType : string ) {
9
- return regionParserImpl ( contents , fileType ) ;
9
+ return regionParserImpl ( contents , fileType ) ;
10
10
}
11
11
12
12
/**
@@ -15,113 +15,115 @@ export function regionParser(contents: string, fileType: string) {
15
15
* @returns {contents: string, regions: {[regionName: string]: string} }
16
16
*/
17
17
function regionParserImpl ( contents : string , fileType : string )
18
- : { contents : string , regions : { [ regionName : string ] : string } } {
19
- const regionMatchers : { [ fileType : string ] : { [ region : string ] : RegExp } } = {
20
- ts : inlineC ,
21
- js : inlineC ,
22
- es6 : inlineC ,
23
- html : html ,
24
- css : blockC ,
25
- json : inlineC ,
26
- 'json.annotated' : inlineC
27
- } ;
28
- const regionMatcher = regionMatchers [ fileType ] ;
29
- const openRegions : string [ ] = [ ] ;
30
- const regionMap : RegionMap = { } ;
18
+ : { contents : string , regions : { [ regionName : string ] : string } } {
19
+ const regionMatchers : { [ fileType : string ] : { [ region : string ] : RegExp } } = {
20
+ ts : inlineC ,
21
+ js : inlineC ,
22
+ es6 : inlineC ,
23
+ html : html ,
24
+ css : blockC ,
25
+ json : inlineC ,
26
+ 'json.annotated' : inlineC
27
+ } ;
28
+ const regionMatcher = regionMatchers [ fileType ] ;
29
+ const openRegions : string [ ] = [ ] ;
30
+ const regionMap : RegionMap = { } ;
31
31
32
- if ( regionMatcher ) {
33
- const lines = contents . split ( / \r ? \n / ) . filter ( ( line ) => {
34
- // debugger;
35
- const startRegion = line . match ( regionMatcher . regionStartMatcher ) ;
36
- const endRegion = line . match ( regionMatcher . regionEndMatcher ) ;
32
+ if ( regionMatcher ) {
33
+ const lines = contents . split ( / \r ? \n / ) . filter ( ( line ) => {
34
+ // debugger;
35
+ const startRegion = line . match ( regionMatcher . regionStartMatcher ) ;
36
+ const endRegion = line . match ( regionMatcher . regionEndMatcher ) ;
37
37
38
- // start region processing
39
- if ( startRegion ) {
40
- // open up the specified region
41
- const regionNames = getRegionNames ( startRegion [ 1 ] ) ;
42
- if ( regionNames . length === 0 ) {
43
- regionNames . push ( '' ) ;
44
- }
45
- regionNames . forEach ( regionName => {
46
- const region = regionMap [ regionName ] ;
47
- if ( region ) {
48
- if ( region . open ) {
49
- throw new Error (
50
- `Tried to open a region, named "${ regionName } ", that is already open`
51
- ) ;
52
- }
53
- region . open = true ;
54
- } else {
55
- regionMap [ regionName ] = { lines : [ ] , open : true } ;
56
- }
57
- openRegions . push ( regionName ) ;
58
- } ) ;
38
+ // start region processing
39
+ if ( startRegion ) {
40
+ // open up the specified region
41
+ const regionNames = getRegionNames ( startRegion [ 1 ] ) ;
42
+ if ( regionNames . length === 0 ) {
43
+ regionNames . push ( '' ) ;
44
+ }
45
+ regionNames . forEach ( regionName => {
46
+ const region = regionMap [ regionName ] ;
47
+ if ( region ) {
48
+ if ( region . open ) {
49
+ throw new Error (
50
+ `Tried to open a region, named "${ regionName } ", that is already open`
51
+ ) ;
52
+ }
53
+ region . open = true ;
54
+ } else {
55
+ regionMap [ regionName ] = { lines : [ ] , open : true } ;
56
+ }
57
+ openRegions . push ( regionName ) ;
58
+ } ) ;
59
59
60
- // end region processing
61
- } else if ( endRegion ) {
62
- if ( openRegions . length === 0 ) {
63
- throw new Error ( 'Tried to close a region when none are open' ) ;
64
- }
65
- // close down the specified region (or most recent if no name is given)
66
- const regionNames = getRegionNames ( endRegion [ 1 ] ) ;
67
- if ( regionNames . length === 0 ) {
68
- regionNames . push ( openRegions [ openRegions . length - 1 ] ) ;
69
- }
60
+ // end region processing
61
+ } else if ( endRegion ) {
62
+ if ( openRegions . length === 0 ) {
63
+ throw new Error ( 'Tried to close a region when none are open' ) ;
64
+ }
65
+ // close down the specified region (or most recent if no name is given)
66
+ const regionNames = getRegionNames ( endRegion [ 1 ] ) ;
67
+ if ( regionNames . length === 0 ) {
68
+ regionNames . push ( openRegions [ openRegions . length - 1 ] ) ;
69
+ }
70
70
71
- regionNames . forEach ( regionName => {
72
- const region = regionMap [ regionName ] ;
73
- if ( ! region || ! region . open ) {
74
- throw new Error (
75
- `Tried to close a region, named "${ regionName } ", that is not open` ) ;
76
- }
77
- region . open = false ;
78
- removeLast ( openRegions , regionName ) ;
79
- } ) ;
71
+ regionNames . forEach ( regionName => {
72
+ const region = regionMap [ regionName ] ;
73
+ if ( ! region || ! region . open ) {
74
+ throw new Error (
75
+ `Tried to close a region, named "${ regionName } ", that is not open` ) ;
76
+ }
77
+ region . open = false ;
78
+ removeLast ( openRegions , regionName ) ;
79
+ } ) ;
80
80
81
- } else {
82
- openRegions . forEach ( regionName => regionMap [ regionName ] . lines . push ( line ) ) ;
83
- // do not filter out this line from the content
84
- return true ;
85
- }
81
+ } else {
82
+ openRegions . forEach ( regionName => regionMap [ regionName ] . lines . push ( line ) ) ;
83
+ // do not filter out this line from the content
84
+ return true ;
85
+ }
86
86
87
- // this line contained an annotation so let's filter it out
88
- return false ;
89
- } ) ;
90
- if ( ! regionMap [ '' ] ) {
91
- regionMap [ '' ] = { lines, open : false } ;
92
- }
93
- return {
94
- contents : lines . join ( '\n' ) ,
95
- regions : mapObject ( regionMap , ( regionName : string , region : Region ) =>
96
- leftAlign ( region . lines ) . join ( '\n' ) )
97
- } ;
98
- } else {
99
- return { contents, regions : { } } ;
87
+ // this line contained an annotation so let's filter it out
88
+ return false ;
89
+ } ) ;
90
+ if ( ! regionMap [ '' ] ) {
91
+ regionMap [ '' ] = { lines, open : false } ;
100
92
}
93
+ return {
94
+ contents : lines . join ( '\n' ) ,
95
+ regions : mapObject ( regionMap , ( regionName : string , region : Region ) =>
96
+ leftAlign ( region . lines ) . join ( '\n' ) )
97
+ } ;
98
+ } else {
99
+ return { contents, regions : { } } ;
100
+ }
101
101
}
102
102
103
103
function mapObject ( obj : RegionMap , mapper : ( regionName : string , region : Region ) => string ) {
104
- const mappedObj : { [ regionName : string ] : string } = { } ;
105
- Object . keys ( obj ) . forEach ( ( key : string ) => { mappedObj [ key ] = mapper ( key , obj [ key ] ) ; } ) ;
106
- return mappedObj ;
104
+ const mappedObj : { [ regionName : string ] : string } = { } ;
105
+ Object . keys ( obj ) . forEach ( ( key : string ) => {
106
+ mappedObj [ key ] = mapper ( key , obj [ key ] ) ;
107
+ } ) ;
108
+ return mappedObj ;
107
109
}
108
110
109
111
function getRegionNames ( input : string ) : string [ ] {
110
- return ( input . trim ( ) === '' ) ? [ ] : input . split ( ',' ) . map ( name => name . trim ( ) ) ;
112
+ return ( input . trim ( ) === '' ) ? [ ] : input . split ( ',' ) . map ( name => name . trim ( ) ) ;
111
113
}
112
114
113
115
function removeLast ( array : string [ ] , item : string ) {
114
- const index = array . lastIndexOf ( item ) ;
115
- array . splice ( index , 1 ) ;
116
+ const index = array . lastIndexOf ( item ) ;
117
+ array . splice ( index , 1 ) ;
116
118
}
117
119
118
120
function leftAlign ( lines : string [ ] ) : string [ ] {
119
- let indent = Number . MAX_VALUE ;
120
- lines . forEach ( line => {
121
- const lineIndent = line . search ( / \S / ) ;
122
- if ( lineIndent !== - 1 ) {
123
- indent = Math . min ( lineIndent , indent ) ;
124
- }
125
- } ) ;
126
- return lines . map ( line => line . substr ( indent ) ) ;
121
+ let indent = Number . MAX_VALUE ;
122
+ lines . forEach ( line => {
123
+ const lineIndent = line . search ( / \S / ) ;
124
+ if ( lineIndent !== - 1 ) {
125
+ indent = Math . min ( lineIndent , indent ) ;
126
+ }
127
+ } ) ;
128
+ return lines . map ( line => line . substr ( indent ) ) ;
127
129
}
0 commit comments