@@ -36,7 +36,7 @@ import {visitParents} from 'unist-util-visit-parents'
36
36
import { convertElement } from 'hast-util-is-element'
37
37
import escape from 'escape-string-regexp'
38
38
39
- var own = { } . hasOwnProperty
39
+ const own = { } . hasOwnProperty
40
40
41
41
export const defaultIgnore = [ 'title' , 'script' , 'style' , 'svg' , 'math' ]
42
42
@@ -48,9 +48,9 @@ export const defaultIgnore = ['title', 'script', 'style', 'svg', 'math']
48
48
*/
49
49
export function findAndReplace ( tree , find , replace , options ) {
50
50
/** @type {Options } */
51
- var settings
51
+ let settings
52
52
/** @type {FindAndReplaceSchema|FindAndReplaceList } */
53
- var schema
53
+ let schema
54
54
55
55
if ( typeof find === 'string' || find instanceof RegExp ) {
56
56
// @ts -expect-error don’t expect options twice.
@@ -66,9 +66,9 @@ export function findAndReplace(tree, find, replace, options) {
66
66
settings = { }
67
67
}
68
68
69
- var ignored = convertElement ( settings . ignore || defaultIgnore )
70
- var pairs = toPairs ( schema )
71
- var pairIndex = - 1
69
+ const ignored = convertElement ( settings . ignore || defaultIgnore )
70
+ const pairs = toPairs ( schema )
71
+ let pairIndex = - 1
72
72
73
73
while ( ++ pairIndex < pairs . length ) {
74
74
visitParents ( tree , 'text' , visitor )
@@ -78,15 +78,12 @@ export function findAndReplace(tree, find, replace, options) {
78
78
79
79
/** @type {import('unist-util-visit-parents').Visitor<Text> } */
80
80
function visitor ( node , parents ) {
81
- var index = - 1
81
+ let index = - 1
82
82
/** @type {Parent } */
83
- var parent
84
- /** @type {Parent } */
85
- var grandparent
83
+ let grandparent
86
84
87
85
while ( ++ index < parents . length ) {
88
- // @ts -expect-error hast vs. unist parent.
89
- parent = parents [ index ]
86
+ const parent = parents [ index ]
90
87
91
88
if (
92
89
ignored (
@@ -99,6 +96,7 @@ export function findAndReplace(tree, find, replace, options) {
99
96
return
100
97
}
101
98
99
+ // @ts -expect-error hast vs. unist parent.
102
100
grandparent = parent
103
101
}
104
102
@@ -111,27 +109,23 @@ export function findAndReplace(tree, find, replace, options) {
111
109
* @returns {VisitorResult }
112
110
*/
113
111
function handler ( node , parent ) {
114
- var find = pairs [ pairIndex ] [ 0 ]
115
- var replace = pairs [ pairIndex ] [ 1 ]
112
+ const find = pairs [ pairIndex ] [ 0 ]
113
+ const replace = pairs [ pairIndex ] [ 1 ]
114
+ let start = 0
115
+ let index = parent . children . indexOf ( node )
116
116
/** @type {Array.<Content> } */
117
- var nodes = [ ]
118
- var start = 0
119
- var index = parent . children . indexOf ( node )
117
+ let nodes = [ ]
120
118
/** @type {number } */
121
- var position
122
- /** @type {RegExpMatchArray } */
123
- var match
124
- /** @type {Array.<Content>|Content|string|false|undefined|null } */
125
- var value
119
+ let position
126
120
127
121
find . lastIndex = 0
128
122
129
- match = find . exec ( node . value )
123
+ let match = find . exec ( node . value )
130
124
131
125
while ( match ) {
132
126
position = match . index
133
127
// @ts -expect-error this is perfectly fine, typescript.
134
- value = replace ( ...match , { index : match . index , input : match . input } )
128
+ let value = replace ( ...match , { index : match . index , input : match . input } )
135
129
136
130
if ( typeof value === 'string' && value . length > 0 ) {
137
131
value = { type : 'text' , value}
@@ -176,24 +170,26 @@ export function findAndReplace(tree, find, replace, options) {
176
170
* @returns {Pairs }
177
171
*/
178
172
function toPairs ( schema ) {
179
- var index = - 1
180
173
/** @type {Pairs } */
181
- var result = [ ]
182
- /** @type {string } */
183
- var key
174
+ const result = [ ]
184
175
185
176
if ( typeof schema !== 'object' ) {
186
177
throw new TypeError ( 'Expected array or object as schema' )
187
178
}
188
179
189
180
if ( Array . isArray ( schema ) ) {
181
+ let index = - 1
182
+
190
183
while ( ++ index < schema . length ) {
191
184
result . push ( [
192
185
toExpression ( schema [ index ] [ 0 ] ) ,
193
186
toFunction ( schema [ index ] [ 1 ] )
194
187
] )
195
188
}
196
189
} else {
190
+ /** @type {string } */
191
+ let key
192
+
197
193
for ( key in schema ) {
198
194
if ( own . call ( schema , key ) ) {
199
195
result . push ( [ toExpression ( key ) , toFunction ( schema [ key ] ) ] )
@@ -217,11 +213,5 @@ function toExpression(find) {
217
213
* @returns {ReplaceFunction }
218
214
*/
219
215
function toFunction ( replace ) {
220
- return typeof replace === 'function' ? replace : returner
221
-
222
- /** @type {ReplaceFunction } */
223
- function returner ( ) {
224
- // @ts -expect-error it’s a string.
225
- return replace
226
- }
216
+ return typeof replace === 'function' ? replace : ( ) => replace
227
217
}
0 commit comments