Skip to content

Commit 5f2ca6e

Browse files
committed
Use ESM
1 parent c99427b commit 5f2ca6e

File tree

6 files changed

+47
-67
lines changed

6 files changed

+47
-67
lines changed

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
.DS_Store
22
*.log
3-
.nyc_output/
43
coverage/
54
node_modules/
6-
hast-util-find-and-replace.js
7-
hast-util-find-and-replace.min.js
85
yarn.lock

.prettierignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
coverage/
2-
hast-util-find-and-replace.js
3-
hast-util-find-and-replace.min.js
4-
*.json
52
*.md

index.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
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'
84

95
var splice = [].splice
6+
var own = {}.hasOwnProperty
107

11-
module.exports = findAndReplace
12-
13-
findAndReplace.ignore = defaultIgnore
8+
export const defaultIgnore = ['title', 'script', 'style', 'svg', 'math']
149

15-
function findAndReplace(tree, find, replace, options) {
10+
export function findAndReplace(tree, find, replace, options) {
1611
var settings
1712
var schema
1813

@@ -51,18 +46,15 @@ function findAndReplace(tree, find, replace, options) {
5146

5247
while (match) {
5348
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})
5850

5951
if (value !== false) {
6052
if (start !== position) {
6153
nodes.push({type: 'text', value: node.value.slice(start, position)})
6254
}
6355

6456
if (typeof value === 'string' && value.length > 0) {
65-
value = {type: 'text', value: value}
57+
value = {type: 'text', value}
6658
}
6759

6860
if (value) {
@@ -112,10 +104,10 @@ function findAndReplace(tree, find, replace, options) {
112104
}
113105

114106
function search(tree, options, handler) {
115-
var ignored = convert(options.ignore || defaultIgnore)
107+
var ignored = convertElement(options.ignore || defaultIgnore)
116108
var result = []
117109

118-
visit(tree, 'text', visitor)
110+
visitParents(tree, 'text', visitor)
119111

120112
return result
121113

@@ -150,7 +142,7 @@ function toPairs(schema) {
150142
var index
151143

152144
if (typeof schema !== 'object') {
153-
throw new Error('Expected array or object as schema')
145+
throw new TypeError('Expected array or object as schema')
154146
}
155147

156148
if ('length' in schema) {
@@ -164,7 +156,9 @@ function toPairs(schema) {
164156
}
165157
} else {
166158
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+
}
168162
}
169163
}
170164

package.json

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,31 @@
2323
"contributors": [
2424
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
2525
],
26+
"sideEffects": false,
27+
"type": "module",
28+
"main": "index.js",
2629
"files": [
2730
"index.js"
2831
],
2932
"dependencies": {
30-
"escape-string-regexp": "^4.0.0",
31-
"hast-util-is-element": "^1.1.0",
32-
"unist-util-visit-parents": "^3.0.0"
33+
"escape-string-regexp": "^5.0.0",
34+
"hast-util-is-element": "^2.0.0",
35+
"unist-util-visit-parents": "^4.0.0"
3336
},
3437
"devDependencies": {
35-
"browserify": "^17.0.0",
36-
"hastscript": "^6.0.0",
37-
"nyc": "^15.0.0",
38+
"c8": "^7.0.0",
39+
"hastscript": "^7.0.0",
3840
"prettier": "^2.0.0",
3941
"remark-cli": "^9.0.0",
4042
"remark-preset-wooorm": "^8.0.0",
4143
"tape": "^5.0.0",
42-
"tinyify": "^3.0.0",
43-
"xo": "^0.38.0"
44+
"xo": "^0.39.0"
4445
},
4546
"scripts": {
4647
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
47-
"build-bundle": "browserify . -s hastUtilFindAndReplace -o hast-util-find-and-replace.js",
48-
"build-mangle": "browserify . -s hastUtilFindAndReplace -o hast-util-find-and-replace.min.js -p tinyify",
49-
"build": "npm run build-bundle && npm run build-mangle",
50-
"test-api": "node test",
51-
"test-coverage": "nyc --reporter lcov tape test.js",
52-
"test": "npm run format && npm run build && npm run test-coverage"
48+
"test-api": "node test.js",
49+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
50+
"test": "npm run format && npm run test-coverage"
5351
},
5452
"prettier": {
5553
"tabWidth": 2,
@@ -61,20 +59,10 @@
6159
},
6260
"xo": {
6361
"prettier": true,
64-
"esnext": false,
6562
"rules": {
66-
"unicorn/prefer-type-error": "off",
67-
"guard-for-in": "off"
68-
},
69-
"ignores": [
70-
"hast-util-find-and-replace.js"
71-
]
72-
},
73-
"nyc": {
74-
"check-coverage": true,
75-
"lines": 100,
76-
"functions": 100,
77-
"branches": 100
63+
"no-var": "off",
64+
"prefer-arrow-callback": "off"
65+
}
7866
},
7967
"remarkConfig": {
8068
"plugins": [

readme.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
## Install
1414

15+
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
16+
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
17+
1518
[npm][]:
1619

1720
```sh
@@ -21,9 +24,9 @@ npm install hast-util-find-and-replace
2124
## Use
2225

2326
```js
24-
var h = require('hastscript')
25-
var inspect = require('unist-util-inspect')
26-
var findAndReplace = require('hast-util-find-and-replace')
27+
import {h} from 'hastscript'
28+
import {inspect} from 'unist-util-inspect'
29+
import {findAndReplace} from 'hast-util-find-and-replace'
2730

2831
var tree = h('p', [
2932
'Some ',
@@ -69,6 +72,9 @@ element[9] [tagName="p"]
6972

7073
## API
7174

75+
This package exports the following identifiers: `findAndReplace`, `defaultIgnore`.
76+
There is no default export.
77+
7278
### `findAndReplace(tree, find[, replace][, options])`
7379

7480
Find and replace text in a [**hast**][hast] [*tree*][tree].
@@ -103,7 +109,7 @@ Partial matches are not supported.
103109
* `options.ignore` (`Test`, default: `['title', 'script', 'style', 'svg',
104110
'math']`)
105111
— Any [`hast-util-is-element`][test] compatible test.
106-
The default list can be accessed at `findAndReplace.ignore`
112+
The default list is exported as `defaultIgnore`
107113

108114
###### Returns
109115

test.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var h = require('hastscript')
5-
var findAndReplace = require('.')
1+
import test from 'tape'
2+
import {h} from 'hastscript'
3+
import {findAndReplace} from './index.js'
64

75
test('findAndReplace', function (t) {
86
t.throws(
97
function () {
108
findAndReplace(create(), true)
119
},
12-
/^Error: Expected array or object as schema$/,
10+
/^TypeError: Expected array or object as schema$/,
1311
'should throw on invalid search and replaces'
1412
)
1513

@@ -122,7 +120,7 @@ test('findAndReplace', function (t) {
122120

123121
t.deepEqual(
124122
findAndReplace(create(), {
125-
emphasis: function () {
123+
emphasis() {
126124
return h('a', 'importance')
127125
},
128126
importance: 'something else'
@@ -174,13 +172,13 @@ test('findAndReplace', function (t) {
174172

175173
t.deepEqual(
176174
findAndReplace(h('p', 'Some emphasis, importance, and code.'), {
177-
importance: function (match) {
175+
importance(match) {
178176
return h('strong', match)
179177
},
180-
code: function (match) {
178+
code(match) {
181179
return h('code', match)
182180
},
183-
emphasis: function (match) {
181+
emphasis(match) {
184182
return h('em', match)
185183
}
186184
}),

0 commit comments

Comments
 (0)