* Fetches a node from the tree by its address. * * @param node - A FormKitNode | FormKitNode * @param _context - A FormKitContext | FormKitContext * @param locator - A string or FormKitAddress | FormKitAddress to find in the tree. * * @returns A {@link FormKitNode | For
( node: FormKitNode, _context: FormKitContext, locator: string | FormKitAddress )
| 2796 | * @internal |
| 2797 | */ |
| 2798 | function getNode( |
| 2799 | node: FormKitNode, |
| 2800 | _context: FormKitContext, |
| 2801 | locator: string | FormKitAddress |
| 2802 | ): FormKitNode | undefined { |
| 2803 | const address = |
| 2804 | typeof locator === 'string' ? locator.split(node.config.delimiter) : locator |
| 2805 | if (!address.length) return undefined |
| 2806 | const first = address[0] |
| 2807 | let pointer: FormKitNode | null | undefined = node.parent |
| 2808 | if (!pointer) { |
| 2809 | // This address names the root node, remove it to get child name: |
| 2810 | if (String(address[0]) === String(node.name)) address.shift() |
| 2811 | // All root nodes start at themselves ultimately: |
| 2812 | pointer = node |
| 2813 | } |
| 2814 | // Any addresses starting with $parent should discard it |
| 2815 | if (first === '$parent') address.shift() |
| 2816 | while (pointer && address.length) { |
| 2817 | const name = address.shift() as string | number |
| 2818 | switch (name) { |
| 2819 | case '$root': |
| 2820 | pointer = node.root |
| 2821 | break |
| 2822 | case '$parent': |
| 2823 | pointer = pointer.parent |
| 2824 | break |
| 2825 | case '$self': |
| 2826 | pointer = node |
| 2827 | break |
| 2828 | default: |
| 2829 | pointer = |
| 2830 | (pointer.children.find( |
| 2831 | (c) => !('__FKP' in c) && String(c.name) === String(name) |
| 2832 | ) as FormKitNode | undefined) || select(pointer, name) |
| 2833 | } |
| 2834 | } |
| 2835 | return pointer || undefined |
| 2836 | } |
| 2837 | |
| 2838 | /** |
| 2839 | * Perform selections on a subtree using the address "selector" methods. |