3 [![Build][build-badge]][build]
4 [![Coverage][coverage-badge]][coverage]
5 [![Downloads][downloads-badge]][downloads]
6 [![Size][size-badge]][size]
7 [![Sponsors][sponsors-badge]][collective]
8 [![Backers][backers-badge]][collective]
9 [![Chat][chat-badge]][chat]
11 [**unist**][unist] utility to check if a node passes a test.
18 npm install unist-util-is
24 var is = require('unist-util-is')
26 var node = {type: 'strong'}
27 var parent = {type: 'paragraph', children: [node]}
29 function test(node, n) {
34 is({children: []}) // => false
36 is(node, 'strong') // => true
37 is(node, 'emphasis') // => false
39 is(node, node) // => true
40 is(parent, {type: 'paragraph'}) // => true
41 is(parent, {type: 'strong'}) // => false
43 is(node, test) // => false
44 is(node, test, 4, parent) // => false
45 is(node, test, 5, parent) // => true
50 ### `is(node[, test[, index, parent[, context]]])`
54 * `node` ([`Node`][node]) — Node to check.
55 * `test` ([`Function`][test], `string`, `Object`, or `Array.<Test>`, optional)
56 — When not given, checks if `node` is a [`Node`][node].
57 When `string`, works like passing `node => node.type === test`.
58 When `array`, checks if any one of the subtests pass.
59 When `object`, checks that all keys in `test` are in `node`,
60 and that they have strictly equal values
61 * `index` (`number`, optional) — [Index][] of `node` in `parent`
62 * `parent` ([`Node`][node], optional) — [Parent][] of `node`
63 * `context` (`*`, optional) — Context object to invoke `test` with
67 `boolean` — Whether `test` passed *and* `node` is a [`Node`][node] (object with
68 `type` set to a non-empty `string`).
70 #### `function test(node[, index, parent])`
74 * `node` ([`Node`][node]) — Node to check
75 * `index` (`number?`) — [Index][] of `node` in `parent`
76 * `parent` ([`Node?`][node]) — [Parent][] of `node`
80 `*` — The to `is` given `context`.
84 `boolean?` — Whether `node` matches.
86 ### `is.convert(test)`
88 Create a test function from `test`, that can later be called with a `node`,
89 `index`, and `parent`.
90 Useful if you’re going to test many nodes, for example when creating a utility
91 where something else passes an is-compatible test.
93 Can also be accessed with `require('unist-util-is/convert')`.
98 var u = require('unist-builder')
99 var convert = require('unist-util-is/convert')
101 var test = convert('leaf')
103 var tree = u('tree', [
104 u('node', [u('leaf', '1')]),
106 u('node', [u('leaf', '3'), u('leaf', '4')]),
110 var leafs = tree.children.filter((child, index) => test(child, index, tree))
118 [({type: 'leaf', value: '2'}, {type: 'leaf', value: '5'})]
123 * [`unist-util-find-after`](https://github.com/syntax-tree/unist-util-find-after)
124 — Find a node after another node
125 * [`unist-util-find-before`](https://github.com/syntax-tree/unist-util-find-before)
126 — Find a node before another node
127 * [`unist-util-find-all-after`](https://github.com/syntax-tree/unist-util-find-all-after)
128 — Find all nodes after another node
129 * [`unist-util-find-all-before`](https://github.com/syntax-tree/unist-util-find-all-before)
130 — Find all nodes before another node
131 * [`unist-util-find-all-between`](https://github.com/mrzmmr/unist-util-find-all-between)
132 — Find all nodes between two nodes
133 * [`unist-util-find`](https://github.com/blahah/unist-util-find)
134 — Find nodes matching a predicate
135 * [`unist-util-filter`](https://github.com/eush77/unist-util-filter)
136 — Create a new tree with nodes that pass a check
137 * [`unist-util-remove`](https://github.com/eush77/unist-util-remove)
138 — Remove nodes from tree
142 See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
144 See [`support.md`][support] for ways to get help.
146 This project has a [Code of Conduct][coc].
147 By interacting with this repository, organisation, or community you agree to
152 [MIT][license] © [Titus Wormer][author]
156 [build-badge]: https://img.shields.io/travis/syntax-tree/unist-util-is.svg
158 [build]: https://travis-ci.org/syntax-tree/unist-util-is
160 [coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-is.svg
162 [coverage]: https://codecov.io/github/syntax-tree/unist-util-is
164 [downloads-badge]: https://img.shields.io/npm/dm/unist-util-is.svg
166 [downloads]: https://www.npmjs.com/package/unist-util-is
168 [size-badge]: https://img.shields.io/bundlephobia/minzip/unist-util-is.svg
170 [size]: https://bundlephobia.com/result?p=unist-util-is
172 [sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg
174 [backers-badge]: https://opencollective.com/unified/backers/badge.svg
176 [collective]: https://opencollective.com/unified
178 [chat-badge]: https://img.shields.io/badge/join%20the%20community-on%20spectrum-7b16ff.svg
180 [chat]: https://spectrum.chat/unified/syntax-tree
182 [npm]: https://docs.npmjs.com/cli/install
186 [author]: https://wooorm.com
188 [contributing]: https://github.com/syntax-tree/.github/blob/master/contributing.md
190 [support]: https://github.com/syntax-tree/.github/blob/master/support.md
192 [coc]: https://github.com/syntax-tree/.github/blob/master/code-of-conduct.md
194 [unist]: https://github.com/syntax-tree/unist
196 [node]: https://github.com/syntax-tree/unist#node
198 [parent]: https://github.com/syntax-tree/unist#parent-1
200 [index]: https://github.com/syntax-tree/unist#index
202 [test]: #function-testnode-index-parent