|
1 | | -/** |
2 | | - * @author Titus Wormer |
3 | | - * @copyright 2015 Titus Wormer |
4 | | - * @license MIT |
5 | | - * @module unist:util:is |
6 | | - * @fileoverview Utility to check if a node passes a test. |
7 | | - */ |
8 | | - |
9 | 1 | 'use strict'; |
10 | 2 |
|
11 | | -/* eslint-env commonjs */ |
12 | 3 | /* eslint-disable max-params */ |
13 | 4 |
|
14 | 5 | /* Expose. */ |
15 | 6 | module.exports = is; |
16 | 7 |
|
17 | | -/** |
18 | | - * Test. |
19 | | - * |
20 | | - * @typedef {Function} is~test |
21 | | - * @param {Node} node - Node to test. |
22 | | - * @param {number} index - Position of `node` in `parent`. |
23 | | - * @param {Node} parent - Parent of `node`. |
24 | | - * @return {boolean?} - Whether this iteration passes. |
25 | | - */ |
26 | | - |
27 | | -/** |
28 | | - * Utility to return true. |
29 | | - * |
30 | | - * @type {is~test} |
31 | | - */ |
32 | | -function first() { |
33 | | - return true; |
34 | | -} |
35 | | - |
36 | | -/** |
37 | | - * Utility to convert a string into a function which checks |
38 | | - * a given node’s type for said string. |
39 | | - * |
40 | | - * @param {string} test - Node type to test. |
41 | | - * @return {is~test} - Tester. |
42 | | - */ |
43 | | -function typeFactory(test) { |
44 | | - return function (node) { |
45 | | - return Boolean(node && node.type === test); |
46 | | - }; |
47 | | -} |
48 | | - |
49 | | -/** |
50 | | - * Utility assert each property in `test` is represented |
51 | | - * in `node`, and each values are strictly equal. |
52 | | - * |
53 | | - * @param {Object} test - Spec. |
54 | | - * @return {is~test} - Tester. |
55 | | - */ |
56 | | -function matchesFactory(test) { |
57 | | - return function (node) { |
58 | | - var key; |
59 | | - |
60 | | - for (key in test) { |
61 | | - if (node[key] !== test[key]) { |
62 | | - return false; |
63 | | - } |
64 | | - } |
65 | | - |
66 | | - return true; |
67 | | - }; |
68 | | -} |
69 | | - |
70 | | -/** |
71 | | - * Assert if `test` passes for `node`. |
72 | | - * When a `parent` node is known the `index` of node |
73 | | - * |
74 | | - * @param {(string|Node|is~test)?} test - Tester. |
75 | | - * @param {Node} node - Node to test. |
76 | | - * @param {number?} [index] - Position of `node` in `parent`. |
77 | | - * @param {Node?} [parent] - Parent of `node`. |
78 | | - * @param {*} [context] - Context to invoke `test` with. |
79 | | - * @return {boolean} - Whether `test` passes. |
80 | | - */ |
| 8 | +/* Assert if `test` passes for `node`. |
| 9 | + * When a `parent` node is known the `index` of node */ |
81 | 10 | function is(test, node, index, parent, context) { |
82 | 11 | var hasParent = parent !== null && parent !== undefined; |
83 | 12 | var hasIndex = index !== null && index !== undefined; |
@@ -113,3 +42,36 @@ function is(test, node, index, parent, context) { |
113 | 42 |
|
114 | 43 | return Boolean(test.call(context, node, index, parent)); |
115 | 44 | } |
| 45 | + |
| 46 | +/* Utility assert each property in `test` is represented |
| 47 | + * in `node`, and each values are strictly equal. */ |
| 48 | +function matchesFactory(test) { |
| 49 | + return matches; |
| 50 | + |
| 51 | + function matches(node) { |
| 52 | + var key; |
| 53 | + |
| 54 | + for (key in test) { |
| 55 | + if (node[key] !== test[key]) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return true; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/* Utility to convert a string into a function which checks |
| 65 | + * a given node’s type for said string. */ |
| 66 | +function typeFactory(test) { |
| 67 | + return type; |
| 68 | + |
| 69 | + function type(node) { |
| 70 | + return Boolean(node && node.type === test); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/* Utility to return true. */ |
| 75 | +function first() { |
| 76 | + return true; |
| 77 | +} |
0 commit comments