-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathprocessNodes.js
More file actions
29 lines (23 loc) · 862 Bytes
/
processNodes.js
File metadata and controls
29 lines (23 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import convertNodeToElement from './convertNodeToElement';
/**
* Processes the nodes generated by htmlparser2 and convert them all into React elements
*
* @param {Object[]} nodes List of nodes to process
* @param {Function} transform Transform function to optionally apply to nodes
* @returns {React.Element[]} The list of processed React elements
*/
export default function processNodes(nodes, transform) {
return nodes
.map((node, index) => {
// return the result of the transform function if applicable
let transformed;
if (typeof transform === 'function') {
transformed = transform(node, index);
if (transformed === null || !!transformed) {
return transformed;
}
}
// otherwise convert the node as standard
return convertNodeToElement(node, index, transform);
});
}