All files editorTypes.js

86.96% Statements 20/23
70% Branches 7/10
100% Functions 7/7
84.21% Lines 16/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      3x 3x   3x     1x             2x 2x 2x               2x 2x   1x       5x 3x 1x 1x         2x         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 => {
      if (value) {
        if (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
  }
}