All files / lib/dynamic-form-input/dynamic-form-file dynamic-form-file-helpers.ts

100% Statements 9/9
100% Branches 13/13
100% Functions 3/3
100% Lines 8/8

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              1x 13x 17x 7x 10x 6x   11x     7x  
import { DynamicFormFileUpload } from './dynamic-form-file';
 
export interface FileItem {
  key: string;
  file: File;
}
 
const extractFilesFromObject = (value: any, path: string = '', files: FileItem[] = []): FileItem[] =>
  Object.entries(value || {}).reduce((result, [key, entry]) => {
    if (entry instanceof DynamicFormFileUpload) {
      result.push({ key: path ? `${path}.${key}` : key, file: entry.file });
    } else if (Array.isArray(entry) || typeof entry === 'object') {
      return extractFilesFromObject(entry, path ? `${path}.${key}` : key, result);
    }
    return result;
  }, files);
 
export const extractFiles = (value: any): FileItem[] => extractFilesFromObject(value);