({
data,
path,
paths = [],
}: {
data: Record<string, unknown>;
path: string;
paths?: string[];
})
| 108 | } |
| 109 | |
| 110 | export function expandPath({ |
| 111 | data, |
| 112 | path, |
| 113 | paths = [], |
| 114 | }: { |
| 115 | data: Record<string, unknown>; |
| 116 | path: string; |
| 117 | paths?: string[]; |
| 118 | }) { |
| 119 | if (path.endsWith('.*')) { |
| 120 | path = path + '.'; |
| 121 | } |
| 122 | |
| 123 | const sep = '.*.'; |
| 124 | const parts = path.split(sep); |
| 125 | if (parts.length === 1) { |
| 126 | paths.push(path); |
| 127 | } else { |
| 128 | const partialPath = parts[0]; |
| 129 | const value = get(data, partialPath); |
| 130 | |
| 131 | if (Array.isArray(value)) { |
| 132 | value.forEach((_, index) => { |
| 133 | expandPath({ |
| 134 | data, |
| 135 | path: trimEnd(`${partialPath}.${index}.${parts.slice(1).join(sep)}`, '.'), |
| 136 | paths, |
| 137 | }); |
| 138 | }); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | return paths; |
| 143 | } |
| 144 | |
| 145 | // Allow `fields.` prefix in placeholder to override built in replacements |
| 146 | // like "slug" and "year" with values from fields of the same name. |
no test coverage detected