.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / unist-util-is / readme.md
1 # unist-util-is
2
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]
10
11 [**unist**][unist] utility to check if a node passes a test.
12
13 ## Install
14
15 [npm][]:
16
17 ```sh
18 npm install unist-util-is
19 ```
20
21 ## Usage
22
23 ```js
24 var is = require('unist-util-is')
25
26 var node = {type: 'strong'}
27 var parent = {type: 'paragraph', children: [node]}
28
29 function test(node, n) {
30   return n === 5
31 }
32
33 is() // => false
34 is({children: []}) // => false
35 is(node) // => true
36 is(node, 'strong') // => true
37 is(node, 'emphasis') // => false
38
39 is(node, node) // => true
40 is(parent, {type: 'paragraph'}) // => true
41 is(parent, {type: 'strong'}) // => false
42
43 is(node, test) // => false
44 is(node, test, 4, parent) // => false
45 is(node, test, 5, parent) // => true
46 ```
47
48 ## API
49
50 ### `is(node[, test[, index, parent[, context]]])`
51
52 ###### Parameters
53
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
64
65 ###### Returns
66
67 `boolean` — Whether `test` passed *and* `node` is a [`Node`][node] (object with
68 `type` set to a non-empty `string`).
69
70 #### `function test(node[, index, parent])`
71
72 ###### Parameters
73
74 *   `node` ([`Node`][node]) — Node to check
75 *   `index` (`number?`) — [Index][] of `node` in `parent`
76 *   `parent` ([`Node?`][node]) — [Parent][] of `node`
77
78 ###### Context
79
80 `*` — The to `is` given `context`.
81
82 ###### Returns
83
84 `boolean?` — Whether `node` matches.
85
86 ### `is.convert(test)`
87
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.
92
93 Can also be accessed with `require('unist-util-is/convert')`.
94
95 For example:
96
97 ```js
98 var u = require('unist-builder')
99 var convert = require('unist-util-is/convert')
100
101 var test = convert('leaf')
102
103 var tree = u('tree', [
104   u('node', [u('leaf', '1')]),
105   u('leaf', '2'),
106   u('node', [u('leaf', '3'), u('leaf', '4')]),
107   u('leaf', '5')
108 ])
109
110 var leafs = tree.children.filter((child, index) => test(child, index, tree))
111
112 console.log(leafs)
113 ```
114
115 Yields:
116
117 ```js
118 [({type: 'leaf', value: '2'}, {type: 'leaf', value: '5'})]
119 ```
120
121 ## Related
122
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
139
140 ## Contribute
141
142 See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
143 started.
144 See [`support.md`][support] for ways to get help.
145
146 This project has a [Code of Conduct][coc].
147 By interacting with this repository, organisation, or community you agree to
148 abide by its terms.
149
150 ## License
151
152 [MIT][license] © [Titus Wormer][author]
153
154 <!-- Definitions -->
155
156 [build-badge]: https://img.shields.io/travis/syntax-tree/unist-util-is.svg
157
158 [build]: https://travis-ci.org/syntax-tree/unist-util-is
159
160 [coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-is.svg
161
162 [coverage]: https://codecov.io/github/syntax-tree/unist-util-is
163
164 [downloads-badge]: https://img.shields.io/npm/dm/unist-util-is.svg
165
166 [downloads]: https://www.npmjs.com/package/unist-util-is
167
168 [size-badge]: https://img.shields.io/bundlephobia/minzip/unist-util-is.svg
169
170 [size]: https://bundlephobia.com/result?p=unist-util-is
171
172 [sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg
173
174 [backers-badge]: https://opencollective.com/unified/backers/badge.svg
175
176 [collective]: https://opencollective.com/unified
177
178 [chat-badge]: https://img.shields.io/badge/join%20the%20community-on%20spectrum-7b16ff.svg
179
180 [chat]: https://spectrum.chat/unified/syntax-tree
181
182 [npm]: https://docs.npmjs.com/cli/install
183
184 [license]: license
185
186 [author]: https://wooorm.com
187
188 [contributing]: https://github.com/syntax-tree/.github/blob/master/contributing.md
189
190 [support]: https://github.com/syntax-tree/.github/blob/master/support.md
191
192 [coc]: https://github.com/syntax-tree/.github/blob/master/code-of-conduct.md
193
194 [unist]: https://github.com/syntax-tree/unist
195
196 [node]: https://github.com/syntax-tree/unist#node
197
198 [parent]: https://github.com/syntax-tree/unist#parent-1
199
200 [index]: https://github.com/syntax-tree/unist#index
201
202 [test]: #function-testnode-index-parent