All files / datamodel/src/converter auto-resolver.js

100% Statements 8/8
100% Branches 11/11
100% Functions 1/1
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 20 21 22 23 24 25 26 27 28 29 30                              115x 2x 113x 2x 111x 108x   3x     112x        
import FlatJSON from './flat-json';
import DSVArr from './dsv-arr';
import DSVStr from './dsv-str';
import { isArray, isObject, isString } from '../utils';
 
/**
 * Parses the input data and detect the format automatically.
 *
 * @param {string|Array} data - The input data.
 * @param {Object} options - An optional config specific to data format.
 * @return {Array.<Object>} Returns an array of headers and column major data.
 */
function Auto (data, options) {
    let converter;
 
    if (isString(data)) {
        converter = DSVStr;
    } else if (isArray(data) && isArray(data[0])) {
        converter = DSVArr;
    } else if (isArray(data) && (data.length === 0 || isObject(data[0]))) {
        converter = FlatJSON;
    } else {
        throw new Error('Couldn\'t detect the data format');
    }
 
    return converter(data, options);
}
 
export default Auto;