.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / unist-util-is / index.js
1 'use strict'
2
3 var convert = require('./convert')
4
5 module.exports = is
6
7 is.convert = convert
8
9 // Assert if `test` passes for `node`.
10 // When a `parent` node is known the `index` of node should also be given.
11 // eslint-disable-next-line max-params
12 function is(node, test, index, parent, context) {
13   var hasParent = parent !== null && parent !== undefined
14   var hasIndex = index !== null && index !== undefined
15   var check = convert(test)
16
17   if (
18     hasIndex &&
19     (typeof index !== 'number' || index < 0 || index === Infinity)
20   ) {
21     throw new Error('Expected positive finite index or child node')
22   }
23
24   if (hasParent && (!is(parent) || !parent.children)) {
25     throw new Error('Expected parent node')
26   }
27
28   if (!node || !node.type || typeof node.type !== 'string') {
29     return false
30   }
31
32   if (hasParent !== hasIndex) {
33     throw new Error('Expected both parent and index')
34   }
35
36   return Boolean(check.call(context, node, index, parent))
37 }