mongoose-schema-parser
Version:
Parse mongoose schema to JSON
42 lines (31 loc) • 782 B
JavaScript
const { hasOwnProperty } = require('./utils')
const outputFormats = {
JS: 1,
JSON: 2
}
const getOutputFormat = (formatName) => {
const name = formatName.toUpperCase()
if (formatName && !hasOwnProperty(outputFormats, name)) {
throw new Error(`${formatName} is not a valid output format name`)
}
return outputFormats[name]
}
const getOutputFormats = () => outputFormats
const format = (data, formatName = 'js') => {
const formatId = getOutputFormat(formatName)
let formatedData
switch (formatId) {
case outputFormats.JS:
formatedData = data
break
case outputFormats.JSON:
formatedData = JSON.stringify(data, null, 2)
break
}
return formatedData
}
module.exports = {
format,
getOutputFormat,
getOutputFormats
}