Skip to content

Commit 1dfbda9

Browse files
committed
Use ESM
1 parent 642852b commit 1dfbda9

File tree

6 files changed

+42
-48
lines changed

6 files changed

+42
-48
lines changed

Diff for: .gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
.DS_Store
22
*.log
3-
.nyc_output/
43
coverage/
54
node_modules/
65
yarn.lock

Diff for: .prettierignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
coverage/
2-
*.json
32
*.md

Diff for: index.js

+12-16
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
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
96
var 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

Diff for: package.json

+13-17
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,30 @@
2323
"contributors": [
2424
"Titus Wormer <[email protected]> (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-
"unist-util-is": "^4.0.0",
32-
"unist-util-visit-parents": "^3.0.0"
33+
"escape-string-regexp": "^5.0.0",
34+
"unist-util-is": "^5.0.0",
35+
"unist-util-visit-parents": "^4.0.0"
3336
},
3437
"devDependencies": {
35-
"nyc": "^15.0.0",
38+
"c8": "^7.0.0",
3639
"prettier": "^2.0.0",
3740
"remark-cli": "^9.0.0",
3841
"remark-preset-wooorm": "^8.0.0",
3942
"tape": "^5.0.0",
40-
"unist-builder": "^2.0.0",
41-
"xo": "^0.38.0"
43+
"unist-builder": "^3.0.0",
44+
"xo": "^0.39.0"
4245
},
4346
"scripts": {
4447
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
45-
"test-api": "node test",
46-
"test-coverage": "nyc --reporter lcov tape test.js",
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",
4750
"test": "npm run format && npm run test-coverage"
4851
},
4952
"prettier": {
@@ -56,18 +59,11 @@
5659
},
5760
"xo": {
5861
"prettier": true,
59-
"esnext": false,
6062
"rules": {
61-
"unicorn/prefer-type-error": "off",
62-
"guard-for-in": "off"
63+
"no-var": "off",
64+
"prefer-arrow-callback": "off"
6365
}
6466
},
65-
"nyc": {
66-
"check-coverage": true,
67-
"lines": 100,
68-
"functions": 100,
69-
"branches": 100
70-
},
7167
"remarkConfig": {
7268
"plugins": [
7369
"preset-wooorm"

Diff for: readme.md

+9-3
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 mdast-util-find-and-replace
2124
## Use
2225

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

2831
var tree = u('paragraph', [
2932
u('text', 'Some '),
@@ -65,6 +68,9 @@ paragraph[8]
6568

6669
## API
6770

71+
This package exports the following identifiers: `findAndReplace`.
72+
There is no default export.
73+
6874
### `findAndReplace(tree, find[, replace][, options])`
6975

7076
Find and replace text in [**mdast**][mdast] [*tree*][tree]s.

Diff for: test.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var u = require('unist-builder')
5-
var findAndReplace = require('.')
1+
import test from 'tape'
2+
import {u} from 'unist-builder'
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 u('link', [u('text', 'importance')])
127125
},
128126
importance: 'something else'
@@ -166,13 +164,13 @@ test('findAndReplace', function (t) {
166164
findAndReplace(
167165
u('paragraph', [u('text', 'Some emphasis, importance, and code.')]),
168166
{
169-
importance: function (value) {
167+
importance(value) {
170168
return u('strong', [u('text', value)])
171169
},
172-
code: function (value) {
170+
code(value) {
173171
return u('inlineCode', value)
174172
},
175-
emphasis: function (value) {
173+
emphasis(value) {
176174
return u('emphasis', [u('text', value)])
177175
}
178176
}

0 commit comments

Comments
 (0)