@magic/core
Version:
@magic core. generate static pages and serverless lambdas. ~5kb client boilerplate.
33 lines (28 loc) • 752 B
JavaScript
import is from '@magic/types'
export const stringifyObject = (obj, indent = '') => {
if (indent === false) {
indent = ''
} else {
indent += ' '
}
if (is.string(obj)) {
if (obj.includes("'")) {
obj = `"${obj}"`
} else {
obj = `'${obj}'`
}
} else if (is.array(obj)) {
obj = `[${obj.map(o => stringifyObject(o, indent)).join(',')}]`
} else if (is.fn(obj)) {
obj = obj.toString()
} else if (is.regex(obj)) {
obj = obj.toString()
} else if (is.obj(obj)) {
const str = Object.entries(obj)
.sort(([a], [b]) => (a > b ? 1 : -1))
.map(([k, o]) => `${indent}'${k}': ${stringifyObject(o, indent)}`)
.join(',\n')
obj = `${indent}{\n${str}\n${indent}}\n`
}
return obj
}