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