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'
84
5+ var own = { } . hasOwnProperty
96var splice = [ ] . splice
107
11- function findAndReplace ( tree , find , replace , options ) {
8+ export function findAndReplace ( tree , find , replace , options ) {
129 var settings
1310 var schema
1411
@@ -47,18 +44,15 @@ function findAndReplace(tree, find, replace, options) {
4744
4845 while ( match ) {
4946 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 } )
5448
5549 if ( value !== false ) {
5650 if ( start !== position ) {
5751 nodes . push ( { type : 'text' , value : node . value . slice ( start , position ) } )
5852 }
5953
6054 if ( typeof value === 'string' && value . length > 0 ) {
61- value = { type : 'text' , value : value }
55+ value = { type : 'text' , value}
6256 }
6357
6458 if ( value ) {
@@ -111,7 +105,7 @@ function search(tree, settings, handler) {
111105 var ignored = convert ( settings . ignore || [ ] )
112106 var result = [ ]
113107
114- visit ( tree , 'text' , visitor )
108+ visitParents ( tree , 'text' , visitor )
115109
116110 return result
117111
@@ -146,7 +140,7 @@ function toPairs(schema) {
146140 var index
147141
148142 if ( typeof schema !== 'object' ) {
149- throw new Error ( 'Expected array or object as schema' )
143+ throw new TypeError ( 'Expected array or object as schema' )
150144 }
151145
152146 if ( 'length' in schema ) {
@@ -160,7 +154,9 @@ function toPairs(schema) {
160154 }
161155 } else {
162156 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+ }
164160 }
165161 }
166162
0 commit comments