All files / frontend/src/utils findFieldByPath.ts

100% Statements 18/18
100% Branches 10/10
100% Functions 5/5
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59                            10x 10x 10x     10x 49x 18x   14x 10x       6x               3x               5x                 11x 10x 10x 10x    
import type { Field } from '@quiqr/types';
 
/**
 * Find a field by its nested path (e.g., "author.address" or "blocks[0].button").
 *
 * Array indices are stripped during schema traversal because the static field schema
 * doesn't replicate per-item — "blocks[0].button" traverses the same schema as "blocks.button".
 *
 * Returns undefined when:
 * - A path segment doesn't match any field key
 * - The field only exists in dynamically loaded partials (dynFormSearchKey), not the static schema
 */
export function findFieldByPath(fields: Field[], path: string): Field | undefined {
  // Strip array indices: "content_blocks[0].button" -> "content_blocks.button"
  const normalizedPath = path.replace(/\[\d+\]/g, '');
  const segments = normalizedPath.split('.');
  let currentFields = fields;
  let result: Field | undefined;
 
  for (const segment of segments) {
    result = currentFields.find((f) => f.key === segment);
    if (!result) return undefined;
 
    if ('fields' in result && Array.isArray(result.fields)) {
      currentFields = result.fields as Field[];
    }
  }
 
  return result;
}
 
/**
 * Given a nestPath, determine the top-level field key.
 * Handles both "field.sub" and "field[0].sub" forms.
 */
export function getTopLevelKey(nestPath: string): string {
  return nestPath.split(/[\[.]/)[0];
}
 
/**
 * Returns true if the nestPath contains array indices,
 * indicating it passes through an accordion item.
 */
export function pathHasArrayIndex(path: string): boolean {
  return /\[\d+\]/.test(path);
}
 
/**
 * Parse the array index from a path relative to a parent accordion key.
 * e.g., parentKey="content_blocks", nestPath="content_blocks[2].button" → 2
 * Returns null if the nestPath is not a direct child of the given parent key.
 */
export function getSubTargetIndex(nestPath: string | undefined, fieldPath: string): number | null {
  if (!nestPath) return null;
  const escaped = fieldPath.replace(/[.[\](){}*+?\\^$|]/g, '\\$&');
  const match = nestPath.match(new RegExp(`^${escaped}\\[(\\d+)\\]`));
  return match ? parseInt(match[1], 10) : null;
}