|
1 | | -'use strict' |
2 | | - |
3 | | -var convert = require('./convert') |
4 | | - |
5 | | -module.exports = isElement |
6 | | - |
7 | | -isElement.convert = convert |
8 | | - |
9 | 1 | // 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) |
12 | 5 |
|
13 | 6 | if ( |
14 | | - index != null && |
| 7 | + index !== undefined && |
| 8 | + index !== null && |
15 | 9 | (typeof index !== 'number' || |
16 | 10 | index < 0 || |
17 | 11 | index === Number.POSITIVE_INFINITY) |
18 | 12 | ) { |
19 | 13 | throw new Error('Expected positive finite index for child node') |
20 | 14 | } |
21 | 15 |
|
22 | | - if (parent != null && (!parent.type || !parent.children)) { |
| 16 | + if ( |
| 17 | + parent !== undefined && |
| 18 | + parent !== null && |
| 19 | + (!parent.type || !parent.children) |
| 20 | + ) { |
23 | 21 | throw new Error('Expected parent node') |
24 | 22 | } |
25 | 23 |
|
26 | 24 | if (!node || !node.type || typeof node.type !== 'string') { |
27 | 25 | return false |
28 | 26 | } |
29 | 27 |
|
30 | | - if ((parent == null) !== (index == null)) { |
| 28 | + if ( |
| 29 | + (parent === undefined || parent === null) !== |
| 30 | + (index === undefined || index === null) |
| 31 | + ) { |
31 | 32 | throw new Error('Expected both parent and index') |
32 | 33 | } |
33 | 34 |
|
34 | 35 | return check.call(context, node, index, parent) |
35 | 36 | } |
| 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 | +} |
0 commit comments