All files / util/field/parseFieldArray parseFieldArray.js

100% Statements 4/4
100% Branches 3/3
100% Functions 2/2
100% Lines 4/4

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                    20x   28x 15x         28x      
import Field from '../Field';
 
/**
 * Converts an array of strings or field json objects into Field objects
 * @param  {Array<Field | String>} fields An array of either strings or JSON like objects representing Field object properties
 * @param  {Constructor} Type Type of field to create and return
 * @return {Array<Field>} The array of fields
 */
export default function parseFieldArray (fields, Type = Field) {
    // create field objects
    return fields.map((f) => {
        // if we have a string give it a default name
        if (typeof f === 'string') {
            f = {
                name: f
            };
        }
        // add additional props with field constructor
        return new Type(f);
    });
}