Repository URL to install this package:
|
Version:
7.1.2-patch-delete ▾
|
import { isStringPrimitive, isArray, isObj, isJSON } from 'exotic'
const store = new WeakMap()
function wipeUndefined(x) {
if (isArray(x)) {
return x.map(wipeUndefined)
} else if (isObj(x)) {
if (store.has(x)) {
return x
}
store.set(x, true)
Object.keys(x).forEach(key => {
if (x[key] === undefined || x[key] === null) {
// delete x[key]
x[key] = ''
} else if (isStringPrimitive(x[key])) {
// to stringify +1?
if (isJSON(x[key])) {
x[key] = JSON.parse(x[key])
}
}
})
return x
} else {
return x
}
}
export function toStringy(x: string | any): string {
if (x === null) {
return ''
} else if (x === undefined) {
return ''
} else if (isStringPrimitive(x)) {
return x.replace(/\\"/g, '"')
} else {
return JSON.stringify(wipeUndefined(x)).replace(/\\"/g, '"')
}
}