Repository URL to install this package:
|
Version:
7.0.0-alpha.3 ▾
|
import { isNil, isObj, isString, isValidJSON, isStringPrimitive } from 'exotic'
import { stringify } from 'chain-able-lego'
import { defineFinal } from 'chain-able-deps'
import { isURIEncoded, parseJSON } from './stringMagic'
import { ObjWithValuesSerialized } from '../typings'
/**
* @param parsed @mutates
* @note these are not if-else
*/
export function parseObjWithSerializedValues(
parsed: ObjWithValuesSerialized
): void {
Object.keys(parsed).forEach(key => {
// remove undefined
if (isNil(parsed[key]) === true) {
delete parsed[key]
}
if (isStringPrimitive(parsed[key]) === false) {
return
}
// @note - typescript isn't recognizing the `if isString` above
if (isURIEncoded(parsed[key]) === true) {
parsed[key] = decodeURIComponent(parsed[key] as string)
}
if (isValidJSON(parsed[key] as string) === true) {
parsed[key] = parseJSON(parsed[key])
/**
* make it serializable
*
* @todo @@perf the `toString` is probably not needed
*/
if (isObj(parsed[key])) {
const toJSON = () => stringify(parsed[key] as any)
defineFinal(parsed[key] as any, 'toString', toJSON)
defineFinal(parsed[key] as any, Symbol.toPrimitive, toJSON)
}
}
})
}