|
1 | 1 | import flatten from 'lodash/flatten'; |
| 2 | +import entries from 'object.entries'; |
2 | 3 | import isSubset from 'is-subset'; |
3 | 4 | import functionName from 'function.prototype.name'; |
4 | 5 | import { nodeHasProperty } from './Utils'; |
@@ -39,11 +40,11 @@ export function treeFilter(tree, fn) { |
39 | 40 | * To support sibling selectors we need to be able to find |
40 | 41 | * the siblings of a node. The easiest way to do that is find |
41 | 42 | * the parent of the node and access its children. |
42 | | - * |
| 43 | + * |
43 | 44 | * This would be unneeded if the RST spec included sibling pointers |
44 | 45 | * such as node.nextSibling and node.prevSibling |
45 | | - * @param {*} root |
46 | | - * @param {*} targetNode |
| 46 | + * @param {*} root |
| 47 | + * @param {*} targetNode |
47 | 48 | */ |
48 | 49 | export function findParentNode(root, targetNode) { |
49 | 50 | const results = treeFilter( |
@@ -98,12 +99,21 @@ export function nodeHasId(node, id) { |
98 | 99 |
|
99 | 100 | export { nodeHasProperty }; |
100 | 101 |
|
| 102 | +const CAN_NEVER_MATCH = {}; |
| 103 | +function replaceUndefined(v) { |
| 104 | + return typeof v !== 'undefined' ? v : CAN_NEVER_MATCH; |
| 105 | +} |
| 106 | +function replaceUndefinedValues(obj) { |
| 107 | + return entries(obj) |
| 108 | + .reduce((acc, [k, v]) => ({ ...acc, [k]: replaceUndefined(v) }), {}); |
| 109 | +} |
| 110 | + |
101 | 111 | export function nodeMatchesObjectProps(node, props) { |
102 | | - return isSubset(propsOfNode(node), props); |
| 112 | + return isSubset(propsOfNode(node), replaceUndefinedValues(props)); |
103 | 113 | } |
104 | 114 |
|
105 | 115 | export function getTextFromNode(node) { |
106 | | - if (node === null || node === undefined) { |
| 116 | + if (node == null) { |
107 | 117 | return ''; |
108 | 118 | } |
109 | 119 |
|
|
0 commit comments