All files editorTypes.js

73.91% Statements 17/23
50% Branches 6/12
100% Functions 9/9
76.19% Lines 16/21
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      1x     1x 1x 1x         1x 1x 1x               1x 1x           1x 1x 1x 1x                   1x 1x 1x      
import sqlFormatter from 'sql-formatter'
import formatHtml from 'pretty'
 
export const editorTypes = {
  sql: {
    mode: 'text/x-sql',
    format: value => sqlFormatter.format(value),
    parse: value => value,
    toString: value => (value || '').toString()
  },
  json: {
    mode: 'application/json',
    format: value => {
      try {
        Eif (typeof value === 'string') {
          return JSON.stringify(JSON.parse(value), null, 2)
        }
        return JSON.stringify(value, null, 2)
      } catch (e) {
        return value
      }
    },
    parse: value => {
      try {
        Eif (typeof value === 'string') return JSON.parse(value)
      } catch (e) {
        return value
      }
    },
    toString: value => {
      Eif (value) {
        Iif (typeof value === 'string') return value
        try {
          return JSON.stringify(value, null, 2)
        } catch (e) {
          return value
        }
      }
      return ''
    }
  },
  html: {
    mode: 'text/html',
    format: value => formatHtml(value),
    parse: value => value,
    toString: value => (value || '').toString()
  }
}