All files / datamodel/src field-creator.js

88.24% Statements 15/17
84.62% Branches 11/13
100% Functions 5/5
86.67% Lines 13/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 60                          498x   245x   10x   235x       253x       2x       251x                           142x   142x 30x     142x 499x     498x        
import { FieldType, DimensionSubtype } from './enums';
import { Measure, Categorical, DateTime, DiscreteMeasure } from './fields';
 
/**
 * Creates a field instance according to the provided data and schema.
 *
 * @todo Add logic for GEO dimension subtype.
 *
 * @param {Array} data - The field data array.
 * @param {Object} schema - The field schema object.
 * @return {Field} Returns the newly created field instance.
 */
function createUnitField (data, schema) {
    switch (schema.type) {
    case FieldType.MEASURE:
        switch (schema.subtype) {
        case 'discrete':
            return new DiscreteMeasure(schema.name, data, schema, schema.bins);
        default:
            return new Measure(schema.name, data, schema);
        }
    case FieldType.DIMENSION:
    default:
        switch (schema.subtype) {
        case DimensionSubtype.CATEGORICAL:
            return new Categorical(schema.name, data, schema);
        case DimensionSubtype.TEMPORAL:
            return new DateTime(schema.name, data, schema);
        case DimensionSubtype.GEO:
            return new Categorical(schema.name, data, schema);
        default:
            return new Categorical(schema.name, data, schema);
        }
    }
}
 
/**
 * Creates the field instances with input data and schema.
 *
 * @param {Array} dataColumn - The data array for fields.
 * @param {Array} schema - The schema array for fields.
 * @param {Array} headers - The array of header names.
 * @return {Array.<Field>} Returns an array of newly created field instances.
 */
function createFields (dataColumn, schema, headers) {
    const headersObj = {};
 
    if (!(headers && headers.length)) {
        headers = schema.map(item => item.name);
    }
 
    headers.forEach((header, i) => {
        headersObj[header] = i;
    });
 
    return schema.map(item => createUnitField(dataColumn[headersObj[item.name]], item));
}
 
export default createFields;