All files editorTypes.js

73.91% Statements 17/23
50% Branches 5/10
100% Functions 7/7
73.68% Lines 14/19
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      2x 2x   1x     1x             1x 1x 1x               1x 1x           1x 1x 1x 1x                   1x          
import sqlFormatter from 'sql-formatter'
import formatHtml from 'pretty'
 
const parse = value => value
const stringify = value => (value ?? '').toString()
 
export const editorTypes = {
  sql: {
    mode: 'text/x-sql',
    format: value => sqlFormatter.format(value),
    parse,
    stringify
  },
  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 {
        return value
      }
    },
    parse: value => {
      try {
        Eif (typeof value === 'string') return JSON.parse(value)
      } catch {
        return value
      }
    },
    stringify: value => {
      Eif (value) {
        Iif (typeof value === 'string') return value
        try {
          return JSON.stringify(value, null, 2)
        } catch {
          return value
        }
      }
      return ''
    }
  },
  html: {
    mode: 'text/html',
    format: value => formatHtml(value),
    parse,
    stringify
  }
}