Skip to content

Commit 33d1f77

Browse files
committed
Use ESM
1 parent 7d1461c commit 33d1f77

File tree

7 files changed

+107
-135
lines changed

7 files changed

+107
-135
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-is-element.js
7-
hast-util-is-element.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-is-element.js
3-
hast-util-is-element.min.js
4-
*.json
52
*.md

convert.js

Lines changed: 0 additions & 74 deletions
This file was deleted.

index.js

Lines changed: 85 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,107 @@
1-
'use strict'
2-
3-
var convert = require('./convert')
4-
5-
module.exports = isElement
6-
7-
isElement.convert = convert
8-
91
// Check if if `node` is an `element` and whether it passes the given test.
10-
function isElement(node, test, index, parent, context) {
11-
var check = convert(test)
2+
// eslint-disable-next-line max-params
3+
export function isElement(node, test, index, parent, context) {
4+
var check = convertElement(test)
125

136
if (
14-
index != null &&
7+
index !== undefined &&
8+
index !== null &&
159
(typeof index !== 'number' ||
1610
index < 0 ||
1711
index === Number.POSITIVE_INFINITY)
1812
) {
1913
throw new Error('Expected positive finite index for child node')
2014
}
2115

22-
if (parent != null && (!parent.type || !parent.children)) {
16+
if (
17+
parent !== undefined &&
18+
parent !== null &&
19+
(!parent.type || !parent.children)
20+
) {
2321
throw new Error('Expected parent node')
2422
}
2523

2624
if (!node || !node.type || typeof node.type !== 'string') {
2725
return false
2826
}
2927

30-
if ((parent == null) !== (index == null)) {
28+
if (
29+
(parent === undefined || parent === null) !==
30+
(index === undefined || index === null)
31+
) {
3132
throw new Error('Expected both parent and index')
3233
}
3334

3435
return check.call(context, node, index, parent)
3536
}
37+
38+
export function convertElement(test) {
39+
if (test === undefined || test === null) {
40+
return element
41+
}
42+
43+
if (typeof test === 'string') {
44+
return tagNameFactory(test)
45+
}
46+
47+
if (typeof test === 'object') {
48+
return anyFactory(test)
49+
}
50+
51+
if (typeof test === 'function') {
52+
return callFactory(test)
53+
}
54+
55+
throw new Error('Expected function, string, or array as test')
56+
}
57+
58+
function anyFactory(tests) {
59+
var index = -1
60+
var checks = []
61+
62+
while (++index < tests.length) {
63+
checks[index] = convertElement(tests[index])
64+
}
65+
66+
return any
67+
68+
function any(...parameters) {
69+
var index = -1
70+
71+
while (++index < checks.length) {
72+
if (checks[index].call(this, ...parameters)) {
73+
return true
74+
}
75+
}
76+
77+
return false
78+
}
79+
}
80+
81+
// Utility to convert a string a tag name check.
82+
function tagNameFactory(test) {
83+
return tagName
84+
85+
function tagName(node) {
86+
return element(node) && node.tagName === test
87+
}
88+
}
89+
90+
// Utility to convert a function check.
91+
function callFactory(test) {
92+
return call
93+
94+
function call(node, ...parameters) {
95+
return element(node) && Boolean(test.call(this, node, ...parameters))
96+
}
97+
}
98+
99+
// Utility to return true if this is an element.
100+
function element(node) {
101+
return (
102+
node &&
103+
typeof node === 'object' &&
104+
node.type === 'element' &&
105+
typeof node.tagName === 'string'
106+
)
107+
}

package.json

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,25 @@
2323
"contributors": [
2424
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
2525
],
26+
"sideEffects": false,
27+
"type": "module",
28+
"main": "index.js",
2629
"files": [
27-
"convert.js",
2830
"index.js"
2931
],
3032
"devDependencies": {
31-
"browserify": "^17.0.0",
32-
"nyc": "^15.0.0",
33+
"c8": "^7.0.0",
3334
"prettier": "^2.0.0",
3435
"remark-cli": "^9.0.0",
3536
"remark-preset-wooorm": "^8.0.0",
3637
"tape": "^5.0.0",
37-
"tinyify": "^3.0.0",
3838
"xo": "^0.38.0"
3939
},
4040
"scripts": {
4141
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
42-
"build-bundle": "browserify . -s hastUtilIsElement -o hast-util-is-element.js",
43-
"build-mangle": "browserify . -s hastUtilIsElement -o hast-util-is-element.min.js -p tinyify",
44-
"build": "npm run build-bundle && npm run build-mangle",
45-
"test-api": "node test",
46-
"test-coverage": "nyc --reporter lcov tape test.js",
47-
"test": "npm run format && npm run build && npm run test-coverage"
42+
"test-api": "node test.js",
43+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
44+
"test": "npm run format && npm run test-coverage"
4845
},
4946
"prettier": {
5047
"tabWidth": 2,
@@ -56,30 +53,11 @@
5653
},
5754
"xo": {
5855
"prettier": true,
59-
"esnext": false,
60-
"ignores": [
61-
"hast-util-is-element.js"
62-
],
6356
"rules": {
64-
"eqeqeq": [
65-
"error",
66-
"always",
67-
{
68-
"null": "ignore"
69-
}
70-
],
71-
"max-params": "off",
72-
"no-eq-null": "off",
73-
"unicorn/prefer-type-error": "off",
74-
"unicorn/prefer-reflect-apply": "off"
57+
"no-var": "off",
58+
"prefer-arrow-callback": "off"
7559
}
7660
},
77-
"nyc": {
78-
"check-coverage": true,
79-
"lines": 100,
80-
"functions": 100,
81-
"branches": 100
82-
},
8361
"remarkConfig": {
8462
"plugins": [
8563
"preset-wooorm"

readme.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
## Install
1515

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

1821
```sh
@@ -22,17 +25,20 @@ npm install hast-util-is-element
2225
## Use
2326

2427
```js
25-
var is = require('hast-util-is-element')
28+
import {isElement} from 'hast-util-is-element'
2629

27-
is({type: 'text', value: 'foo'}) // => false
30+
isElement({type: 'text', value: 'foo'}) // => false
2831

29-
is({type: 'element', tagName: 'a'}, 'a') // => true
32+
isElement({type: 'element', tagName: 'a'}, 'a') // => true
3033

31-
is({type: 'element', tagName: 'a'}, ['a', 'area']) // => true
34+
isElement({type: 'element', tagName: 'a'}, ['a', 'area']) // => true
3235
```
3336

3437
## API
3538

39+
This package exports the following identifiers: `isElement`, `convertElement`.
40+
There is no default export.
41+
3642
### `isElement(node[, test[, index, parent[, context]]])`
3743

3844
Check if the given value is a (certain) [*element*][element].
@@ -71,7 +77,7 @@ A `node` that is not a node, or not an element, does not throw.
7177

7278
`boolean?` — Whether `element` matches.
7379

74-
### `isElement.convert(test)`
80+
### `convertElement(test)`
7581

7682
Create a test function from `test`, that can later be called with a `node`,
7783
`index`, and `parent`.
@@ -81,8 +87,6 @@ where something else passes a compatible test.
8187
The created function is slightly faster because it expects valid input only.
8288
Therefore, passing invalid input, yields unexpected results.
8389

84-
Can also be accessed with `require('hast-util-is-element/convert')`.
85-
8690
## Security
8791

8892
`hast-util-is-element` does not change the syntax tree so there are no openings

test.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var isElement = require('.')
1+
import test from 'tape'
2+
import {isElement} from './index.js'
53

64
test('isElement', function (t) {
75
t.equal(isElement(), false, 'should return `false` without node')

0 commit comments

Comments
 (0)