All files / src/helpers discernFileFormatter.js

100% Statements 2/2
100% Branches 0/0
100% Functions 0/0
100% Lines 2/2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24                              153x 153x              
import discernFormat from './discernFormat'
import formatters from '../formatters/index'
 
/**
 * Returns a formatter that will format json data to file type specified by the extension in `filePath`. Used internally by {@link writeData} and {@link writeDataSync}.
 *
 * @function discernFileFormatter
 * @param {String} filePath Input file path
 * @returns {Function} A formatter function that will write the extension format
 *
 * @example
 * var formatter = io.discernFileFormatter('path/to/data.tsv')
 * var csv = formatter(json)
 */
export default function discernFileFormatter (filePath) {
  var format = discernFormat(filePath)
  var formatter = formatters[format]
  // If we don't have a parser for this format, return as text
  if (typeof formatter === 'undefined') {
    formatter = formatters['txt']
  }
  return formatter
}