1
- 'use strict'
2
-
3
- var visit = require ( 'unist-util-visit-parents' )
4
- var convert = require ( 'hast-util-is-element/convert' )
5
- var escape = require ( 'escape-string-regexp' )
6
-
7
- var defaultIgnore = [ 'title' , 'script' , 'style' , 'svg' , 'math' ]
1
+ import { visitParents } from 'unist-util-visit-parents'
2
+ import { convertElement } from 'hast-util-is-element'
3
+ import escape from 'escape-string-regexp'
8
4
9
5
var splice = [ ] . splice
6
+ var own = { } . hasOwnProperty
10
7
11
- module . exports = findAndReplace
12
-
13
- findAndReplace . ignore = defaultIgnore
8
+ export const defaultIgnore = [ 'title' , 'script' , 'style' , 'svg' , 'math' ]
14
9
15
- function findAndReplace ( tree , find , replace , options ) {
10
+ export function findAndReplace ( tree , find , replace , options ) {
16
11
var settings
17
12
var schema
18
13
@@ -51,18 +46,15 @@ function findAndReplace(tree, find, replace, options) {
51
46
52
47
while ( match ) {
53
48
position = match . index
54
- value = replace . apply (
55
- null ,
56
- [ ] . concat ( match , { index : match . index , input : match . input } )
57
- )
49
+ value = replace ( ...match , { index : match . index , input : match . input } )
58
50
59
51
if ( value !== false ) {
60
52
if ( start !== position ) {
61
53
nodes . push ( { type : 'text' , value : node . value . slice ( start , position ) } )
62
54
}
63
55
64
56
if ( typeof value === 'string' && value . length > 0 ) {
65
- value = { type : 'text' , value : value }
57
+ value = { type : 'text' , value}
66
58
}
67
59
68
60
if ( value ) {
@@ -112,10 +104,10 @@ function findAndReplace(tree, find, replace, options) {
112
104
}
113
105
114
106
function search ( tree , options , handler ) {
115
- var ignored = convert ( options . ignore || defaultIgnore )
107
+ var ignored = convertElement ( options . ignore || defaultIgnore )
116
108
var result = [ ]
117
109
118
- visit ( tree , 'text' , visitor )
110
+ visitParents ( tree , 'text' , visitor )
119
111
120
112
return result
121
113
@@ -150,7 +142,7 @@ function toPairs(schema) {
150
142
var index
151
143
152
144
if ( typeof schema !== 'object' ) {
153
- throw new Error ( 'Expected array or object as schema' )
145
+ throw new TypeError ( 'Expected array or object as schema' )
154
146
}
155
147
156
148
if ( 'length' in schema ) {
@@ -164,7 +156,9 @@ function toPairs(schema) {
164
156
}
165
157
} else {
166
158
for ( key in schema ) {
167
- result . push ( [ toExpression ( key ) , toFunction ( schema [ key ] ) ] )
159
+ if ( own . call ( schema , key ) ) {
160
+ result . push ( [ toExpression ( key ) , toFunction ( schema [ key ] ) ] )
161
+ }
168
162
}
169
163
}
170
164
0 commit comments