Skip to content

Commit 131ee3d

Browse files
committed
Use ESM
1 parent d321ae7 commit 131ee3d

14 files changed

+179
-409
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-
unist-util-is.js
7-
unist-util-is.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-
unist-util-is.js
3-
unist-util-is.min.js
4-
*.json
52
*.md

convert.d.ts

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

convert.js

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

index.d.ts

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,46 @@
22

33
import {Node, Parent} from 'unist'
44

5-
declare namespace unistUtilIs {
6-
/**
7-
* Check that type property matches expectation for a node
8-
*
9-
* @typeParam T type of node that passes test
10-
*/
11-
type TestType<T extends Node> = T['type']
5+
/**
6+
* Check that type property matches expectation for a node
7+
*
8+
* @typeParam T type of node that passes test
9+
*/
10+
export type TestType<T extends Node> = T['type']
1211

13-
/**
14-
* Check that some attributes on a node are matched
15-
*
16-
* @typeParam T type of node that passes test
17-
*/
18-
type TestObject<T extends Node> = Partial<T>
12+
/**
13+
* Check that some attributes on a node are matched
14+
*
15+
* @typeParam T type of node that passes test
16+
*/
17+
export type TestObject<T extends Node> = Partial<T>
1918

20-
/**
21-
* Check if a node passes a test
22-
*
23-
* @param node node to check
24-
* @param index index of node in parent
25-
* @param parent parent of node
26-
* @typeParam T type of node that passes test
27-
* @returns true if type T is found, false otherwise
28-
*/
29-
type TestFunction<T extends Node> = (
30-
node: unknown,
31-
index?: number,
32-
parent?: Parent
33-
) => node is T
19+
/**
20+
* Check if a node passes a test
21+
*
22+
* @param node node to check
23+
* @param index index of node in parent
24+
* @param parent parent of node
25+
* @typeParam T type of node that passes test
26+
* @returns true if type T is found, false otherwise
27+
*/
28+
export type TestFunction<T extends Node> = (
29+
node: unknown,
30+
index?: number,
31+
parent?: Parent
32+
) => node is T
3433

35-
/**
36-
* Union of all the types of tests
37-
*
38-
* @typeParam T type of node that passes test
39-
*/
40-
type Test<T extends Node> =
41-
| TestType<T>
42-
| TestObject<T>
43-
| TestFunction<T>
44-
| null
45-
| undefined
46-
}
34+
/**
35+
* Union of all the types of tests
36+
*
37+
* @typeParam T type of node that passes test
38+
*/
39+
export type Test<T extends Node> =
40+
| TestType<T>
41+
| TestObject<T>
42+
| TestFunction<T>
43+
| null
44+
| undefined
4745

4846
/**
4947
* Unist utility to check if a node passes a test.
@@ -60,12 +58,12 @@ declare namespace unistUtilIs {
6058
* @typeParam T type that node is compared with
6159
* @returns Whether test passed and `node` is a `Node` (object with `type` set to non-empty `string`).
6260
*/
63-
declare function unistUtilIs<T extends Node>(
61+
export declare function is<T extends Node>(
6462
node: unknown,
65-
test?: unistUtilIs.Test<T> | Array<unistUtilIs.Test<any>>,
63+
test?: Test<T> | Array<Test<any>>,
6664
index?: number,
6765
parent?: Parent,
6866
context?: any
6967
): node is T
7068

71-
export = unistUtilIs
69+
export declare function convert<T extends Node>(test: Test<T>): TestFunction<T>

index.js

Lines changed: 90 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,109 @@
1-
'use strict'
2-
3-
var convert = require('./convert')
4-
5-
module.exports = is
6-
7-
is.convert = convert
8-
91
// Assert if `test` passes for `node`.
102
// When a `parent` node is known the `index` of node should also be given.
11-
function is(node, test, index, parent, context) {
3+
// eslint-disable-next-line max-params
4+
export function is(node, test, index, parent, context) {
125
var check = convert(test)
136

147
if (
15-
index != null &&
16-
(typeof index !== 'number' || index < 0 || index === Infinity)
8+
index !== undefined &&
9+
index !== null &&
10+
(typeof index !== 'number' ||
11+
index < 0 ||
12+
index === Number.POSITIVE_INFINITY)
1713
) {
1814
throw new Error('Expected positive finite index')
1915
}
2016

21-
if (parent != null && (!is(parent) || !parent.children)) {
17+
if (
18+
parent !== undefined &&
19+
parent !== null &&
20+
(!is(parent) || !parent.children)
21+
) {
2222
throw new Error('Expected parent node')
2323
}
2424

25-
if ((parent == null) !== (index == null)) {
25+
if (
26+
(parent === undefined || parent === null) !==
27+
(index === undefined || index === null)
28+
) {
2629
throw new Error('Expected both parent and index')
2730
}
2831

2932
return node && node.type && typeof node.type === 'string'
3033
? Boolean(check.call(context, node, index, parent))
3134
: false
3235
}
36+
37+
export function convert(test) {
38+
if (test === undefined || test === null) {
39+
return ok
40+
}
41+
42+
if (typeof test === 'string') {
43+
return typeFactory(test)
44+
}
45+
46+
if (typeof test === 'object') {
47+
return 'length' in test ? anyFactory(test) : allFactory(test)
48+
}
49+
50+
if (typeof test === 'function') {
51+
return test
52+
}
53+
54+
throw new Error('Expected function, string, or object as test')
55+
}
56+
57+
// Utility to assert each property in `test` is represented in `node`, and each
58+
// values are strictly equal.
59+
function allFactory(test) {
60+
return all
61+
62+
function all(node) {
63+
var key
64+
65+
for (key in test) {
66+
if (node[key] !== test[key]) return false
67+
}
68+
69+
return true
70+
}
71+
}
72+
73+
function anyFactory(tests) {
74+
var checks = []
75+
var index = -1
76+
77+
while (++index < tests.length) {
78+
checks[index] = convert(tests[index])
79+
}
80+
81+
return any
82+
83+
function any(...parameters) {
84+
var index = -1
85+
86+
while (++index < checks.length) {
87+
if (checks[index].call(this, ...parameters)) {
88+
return true
89+
}
90+
}
91+
92+
return false
93+
}
94+
}
95+
96+
// Utility to convert a string into a function which checks a given node’s type
97+
// for said string.
98+
function typeFactory(test) {
99+
return type
100+
101+
function type(node) {
102+
return Boolean(node && node.type === test)
103+
}
104+
}
105+
106+
// Utility to return true.
107+
function ok() {
108+
return true
109+
}

0 commit comments

Comments
 (0)