| 1 |
import {Form, Group, isGroup, Node} from '@givewp/forms/types'; |
| 2 |
import {mapGroup, reduceGroup, walkGroup} from './groups'; |
| 3 |
|
| 4 |
/** |
| 5 |
* Receives the form data as provided directly from the server and mutates it to be ready for use by the React application |
| 6 |
* |
| 7 |
* @since 3.0.0 |
| 8 |
*/ |
| 9 |
export default function prepareFormData(form: Form) { |
| 10 |
form.walkNodes = walkGroupNodes.bind(form); |
| 11 |
form.mapNodes = mapGroupNodes.bind(form); |
| 12 |
form.reduceNodes = reduceGroupNodes.bind(form); |
| 13 |
|
| 14 |
form.walkNodes((node: Group) => { |
| 15 |
node.walkNodes = walkGroupNodes.bind(node); |
| 16 |
node.mapNodes = mapGroupNodes.bind(node); |
| 17 |
node.reduceNodes = reduceGroupNodes.bind(node); |
| 18 |
}, isGroup); |
| 19 |
} |
| 20 |
|
| 21 |
function walkGroupNodes(this: Group, callback: (node: Node) => void, filter?: (node: Node) => boolean) { |
| 22 |
walkGroup(this, callback, filter); |
| 23 |
} |
| 24 |
|
| 25 |
function mapGroupNodes(this: Group, callback: (node: Node) => void, filter?: (node: Node) => boolean) { |
| 26 |
return mapGroup(this, callback, filter); |
| 27 |
} |
| 28 |
|
| 29 |
function reduceGroupNodes( |
| 30 |
this: Group, |
| 31 |
callback: (accumulator: any, node: Node) => any, |
| 32 |
initialValue: any, |
| 33 |
filter?: (node: Node) => boolean |
| 34 |
) { |
| 35 |
return reduceGroup(this, callback, initialValue, filter); |
| 36 |
} |
| 37 |
|