| 1 |
import {Field, Group, isField, isGroup, Node} from '@givewp/forms/types'; |
| 2 |
|
| 3 |
/** |
| 4 |
* Finds the first node with a given name within a collection of nodes |
| 5 |
* |
| 6 |
* @since 3.0.0 |
| 7 |
*/ |
| 8 |
export function findNode(name: string, nodes: Node[]): Node { |
| 9 |
let node; |
| 10 |
for (let index = 0; index < nodes.length; index++) { |
| 11 |
node = nodes[index]; |
| 12 |
|
| 13 |
if (node.name === name) { |
| 14 |
return node; |
| 15 |
} else if (isGroup(node)) { |
| 16 |
const nestedNode = findNode(name, node.nodes); |
| 17 |
if (nestedNode !== null) { |
| 18 |
return nestedNode; |
| 19 |
} |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
return null; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Walks through a group's nodes and calls a callback for each node. If a filter is provided the callback only fires for |
| 28 |
* nodes which pass the filter. |
| 29 |
* |
| 30 |
* @since 3.0.0 |
| 31 |
*/ |
| 32 |
export function walkGroup(group: Group, callback: (node: Node) => void, filter?: (node: Node) => boolean) { |
| 33 |
walkNodes(group.nodes, callback, filter); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Maps through a Group's nodes and calls a callback for each node. If a filter is provided the callback only fires for |
| 38 |
* passing nodes. |
| 39 |
* |
| 40 |
* @since 3.0.0 |
| 41 |
*/ |
| 42 |
export function mapGroup(group: Group, callback: (node: Node) => unknown, filter?: (node: Node) => boolean) { |
| 43 |
return mapNodes(group.nodes, callback, filter); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Reduces a Group's nodes into a single value. If a filter is provided the callback only fires for passing nodes. |
| 48 |
* @since 3.0.0 |
| 49 |
*/ |
| 50 |
export function reduceGroup( |
| 51 |
group: Group, |
| 52 |
callback: (accumulator: unknown, node: Node) => unknown, |
| 53 |
initialValue: unknown, |
| 54 |
filter?: (node: Node) => boolean |
| 55 |
) { |
| 56 |
return reduceNodes(group.nodes, callback, initialValue, filter); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Walks through a collection of nodes and calls a callback for each node. If a filter is provided the callback only fires for passing nodes. |
| 61 |
* |
| 62 |
* @since 3.0.0 |
| 63 |
*/ |
| 64 |
export function walkNodes(nodes: Node[], callback: (node: Node) => void, filter?: (node: Node) => boolean) { |
| 65 |
nodes.forEach((node) => { |
| 66 |
if (!filter || filter(node)) { |
| 67 |
callback(node); |
| 68 |
} |
| 69 |
|
| 70 |
if (isGroup(node)) { |
| 71 |
walkNodes(node.nodes, callback, filter); |
| 72 |
} |
| 73 |
}); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Maps a collection of nodes to a new array of values. If a filter is provided the callback only fires for nodes which |
| 78 |
* pass. |
| 79 |
* |
| 80 |
* @since 3.0.0 |
| 81 |
*/ |
| 82 |
export function mapNodes<Type>( |
| 83 |
nodes: Node[], |
| 84 |
callback: (node: Node) => Type, |
| 85 |
filter?: (node: Node) => boolean |
| 86 |
): Type[] { |
| 87 |
let mappedValues: Array<Type> = []; |
| 88 |
|
| 89 |
walkNodes( |
| 90 |
nodes, |
| 91 |
(node) => { |
| 92 |
mappedValues.push(callback(node)); |
| 93 |
}, |
| 94 |
filter |
| 95 |
); |
| 96 |
|
| 97 |
return mappedValues; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Reduces an array of nodes to a single value. If the filter is provided, only nodes which pass the filter will be used. |
| 102 |
* |
| 103 |
* @since 3.0.0 |
| 104 |
*/ |
| 105 |
export function reduceNodes<Type>( |
| 106 |
nodes: Node[], |
| 107 |
callback: (accumulator: Type, node: Node) => Type, |
| 108 |
initialValue: Type, |
| 109 |
filter?: (node: Node) => boolean |
| 110 |
): Type { |
| 111 |
let accumulator = initialValue; |
| 112 |
|
| 113 |
walkNodes( |
| 114 |
nodes, |
| 115 |
(node) => { |
| 116 |
accumulator = callback(accumulator, node); |
| 117 |
}, |
| 118 |
filter |
| 119 |
); |
| 120 |
|
| 121 |
return accumulator; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Walks through an array of nodes, limited by fields, and calls a callback for each field. |
| 126 |
* |
| 127 |
* @since 3.0.0 |
| 128 |
*/ |
| 129 |
export function walkFields(nodes: Node[], callback: (field: Field) => void) { |
| 130 |
walkNodes(nodes, callback, isField); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Reduces an array of nodes, limited by its fields, into a single value. |
| 135 |
* |
| 136 |
* @since 3.0.0 |
| 137 |
*/ |
| 138 |
export function reduceFields<Type>( |
| 139 |
nodes: Parameters<typeof reduceNodes>[0], |
| 140 |
callback: (accumulator: Type, field: Field) => Type, |
| 141 |
initialValue: Type |
| 142 |
) { |
| 143 |
return reduceNodes(nodes, callback, initialValue, isField); |
| 144 |
} |
| 145 |
|