safe-json-value
Version:
⛑️ JSON serialization should never fail
38 lines (24 loc) • 618 B
JavaScript
import{safeGetChangeProp}from"./get.js";
export const addNotArrayIndexChanges=(array,changes,path)=>{
if(!Array.isArray(array)){
return
}
const arrayProps=getArrayProps(array.length);
for(const key of Reflect.ownKeys(array)){
if(!arrayProps.has(key)){
changes.push({
path:[...path,key],
oldValue:safeGetChangeProp({parent:array,key}),
newValue:undefined,
reason:"ignoredArrayProperty"
})
}
}
};
const getArrayProps=(length)=>{
const indices=Array.from({length},getArrayIndex);
const arrayProps=new Set(indices);
arrayProps.add("length");
return arrayProps
};
const getArrayIndex=(_,index)=>String(index);