safe-json-value
Version:
⛑️ JSON serialization should never fail
30 lines (22 loc) • 491 B
JavaScript
export const omitInvalidKey=({parent,key,prop,changes,path})=>{
if(typeof key==="symbol"){
changes.push({
path,
oldValue:prop,
newValue:undefined,
reason:"ignoredSymbolKey"
});
return{prop:undefined,validKey:false}
}
if(!isEnum.call(parent,key)&&!Array.isArray(parent)){
changes.push({
path,
oldValue:prop,
newValue:undefined,
reason:"ignoredNotEnumerable"
});
return{prop:undefined,validKey:false}
}
return{prop,validKey:true}
};
const{propertyIsEnumerable:isEnum}=Object.prototype;