safe-json-value
Version:
⛑️ JSON serialization should never fail
68 lines (45 loc) • 862 B
JavaScript
import isPlainObj from"is-plain-obj";
import{transformProp}from"./prop.js";
export const recurseObject=({
object,
changes,
ancestors,
path,
size,
maxSize,
recurse
})=>{
const newObject=getNewObject(object);
let state={empty:true,size};
for(const key of Reflect.ownKeys(object)){
state=transformProp({
parent:object,
changes,
ancestors,
path,
maxSize,
key,
type:"objectProp",
empty:state.empty,
size:state.size,
recurse
});
if(state.value!==undefined){
newObject[key]=state.value
}
}
addClassChange({object,newObject,changes,path});
return{value:newObject,size:state.size}
};
const getNewObject=(object)=>
Object.getPrototypeOf(object)===null?Object.create(null):{};
const addClassChange=({object,newObject,changes,path})=>{
if(!isPlainObj(object)){
changes.push({
path,
oldValue:object,
newValue:newObject,
reason:"unresolvedClass"
})
}
};