1- 'use strict' ;
1+ 'use strict'
22
3- var toString = require ( 'mdast-util-to-string' ) ;
3+ var toString = require ( 'mdast-util-to-string' )
44
5- module . exports = headingRange ;
5+ module . exports = headingRange
66
7- var splice = [ ] . splice ;
7+ var splice = [ ] . splice
88
9- /* Search `node` with `options` and invoke `callback`. */
9+ // Search `node` with `options` and invoke `callback`.
1010function headingRange ( node , options , callback ) {
11- var test = options ;
12- var ignoreFinalDefinitions = false ;
11+ var test = options
12+ var ignoreFinalDefinitions = false
1313
14- /* Object, not regex. */
14+ // Object, not regex.
1515 if ( test && typeof test === 'object' && ! ( 'exec' in test ) ) {
16- ignoreFinalDefinitions = test . ignoreFinalDefinitions === true ;
17- test = test . test ;
16+ ignoreFinalDefinitions = test . ignoreFinalDefinitions === true
17+ test = test . test
1818 }
1919
2020 if ( typeof test === 'string' ) {
21- test = toExpression ( test ) ;
21+ test = toExpression ( test )
2222 }
2323
24- /* Regex */
24+ // Regex
2525 if ( test && 'exec' in test ) {
26- test = wrapExpression ( test ) ;
26+ test = wrapExpression ( test )
2727 }
2828
2929 if ( typeof test !== 'function' ) {
3030 throw new Error (
31- 'Expected `string`, `regexp`, or `function` for `test`, ' +
32- 'not `' + test + '`'
33- ) ;
31+ 'Expected `string`, `regexp`, or `function` for `test`, not `' +
32+ test +
33+ '`'
34+ )
3435 }
3536
36- search ( node , test , ignoreFinalDefinitions , callback ) ;
37+ search ( node , test , ignoreFinalDefinitions , callback )
3738}
3839
39- /* Search a node for heading range. */
40+ // Search a node for heading range.
4041function search ( root , test , skip , callback ) {
41- var index = - 1 ;
42- var children = root . children ;
43- var length = children . length ;
44- var depth = null ;
45- var start = null ;
46- var end = null ;
47- var nodes ;
48- var clean ;
49- var child ;
42+ var index = - 1
43+ var children = root . children
44+ var length = children . length
45+ var depth = null
46+ var start = null
47+ var end = null
48+ var nodes
49+ var clean
50+ var child
5051
5152 while ( ++ index < length ) {
52- child = children [ index ] ;
53+ child = children [ index ]
5354
5455 if ( closing ( child , depth ) ) {
55- end = index ;
56- break ;
56+ end = index
57+ break
5758 }
5859
5960 if ( opening ( child , depth , test ) ) {
60- start = index ;
61- depth = child . depth ;
61+ start = index
62+ depth = child . depth
6263 }
6364 }
6465
6566 if ( start !== null ) {
6667 if ( end === null ) {
67- end = length ;
68+ end = length
6869 }
6970
7071 if ( skip ) {
7172 while ( end > start ) {
72- child = children [ end - 1 ] ;
73+ child = children [ end - 1 ]
7374
7475 if ( ! definition ( child ) ) {
75- break ;
76+ break
7677 }
7778
78- end -- ;
79+ end --
7980 }
8081 }
8182
@@ -88,57 +89,56 @@ function search(root, test, skip, callback) {
8889 start : start ,
8990 end : children [ end ] ? end : null
9091 }
91- ) ;
92+ )
9293
93- clean = [ ] ;
94- index = - 1 ;
95- length = nodes && nodes . length ;
94+ clean = [ ]
95+ index = - 1
96+ length = nodes && nodes . length
9697
97- /* Ensure no empty nodes are inserted. This could
98- * be the case if `end` is in `nodes` but no `end`
99- * node exists. */
98+ // Ensure no empty nodes are inserted. This could be the case if `end` is
99+ // in `nodes` but no `end` node exists.
100100 while ( ++ index < length ) {
101101 if ( nodes [ index ] ) {
102- clean . push ( nodes [ index ] ) ;
102+ clean . push ( nodes [ index ] )
103103 }
104104 }
105105
106106 if ( nodes ) {
107- splice . apply ( children , [ start , end - start + 1 ] . concat ( clean ) ) ;
107+ splice . apply ( children , [ start , end - start + 1 ] . concat ( clean ) )
108108 }
109109 }
110110}
111111
112- /* Transform a string into an applicable expression. */
112+ // Transform a string into an applicable expression.
113113function toExpression ( value ) {
114- return new RegExp ( '^(' + value + ')$' , 'i' ) ;
114+ return new RegExp ( '^(' + value + ')$' , 'i' )
115115}
116116
117- /* Wrap an expression into an assertion function. */
117+ // Wrap an expression into an assertion function.
118118function wrapExpression ( expression ) {
119- return assertion ;
119+ return assertion
120120
121- /* Assert `value` matches the bound `expression`. */
121+ // Assert `value` matches the bound `expression`.
122122 function assertion ( value ) {
123- return expression . test ( value ) ;
123+ return expression . test ( value )
124124 }
125125}
126126
127- /* Check if `node` is a heading. */
127+ // Check if `node` is a heading.
128128function heading ( node ) {
129- return node && node . type === 'heading' ;
129+ return node && node . type === 'heading'
130130}
131131
132- /* Check if `node` is the main heading. */
132+ // Check if `node` is the main heading.
133133function opening ( node , depth , test ) {
134- return depth === null && heading ( node ) && test ( toString ( node ) , node ) ;
134+ return depth === null && heading ( node ) && test ( toString ( node ) , node )
135135}
136136
137- /* Check if `node` is the next heading. */
137+ // Check if `node` is the next heading.
138138function closing ( node , depth ) {
139- return depth && heading ( node ) && node . depth <= depth ;
139+ return depth && heading ( node ) && node . depth <= depth
140140}
141141
142142function definition ( node ) {
143- return node . type === 'definition' || node . type === 'footnoteDefinition' ;
143+ return node . type === 'definition' || node . type === 'footnoteDefinition'
144144}
0 commit comments