You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
easily could change to use code like just-safe-set, then it will handle nested objects/arrays. Working from the just-safe-set code
Changes
new function name/export name: omit and dropped the value param
obj[lastProp] = value becomes delete obj[lastProp]
if used on array, delete obj[lastProp] puts in null, so check for array and handle with obj.splice(parseInt(lastProp), 1)
module.exports=omit;/*var obj = {a: 3, b: {c:5, d:7}, e:[1,2,3]};omit(obj, 'a'); // {b: {c:5, d:7}, e:[1,2,3]};omit(obj, 'b.c'); // {a:3, b: {d:7}, e:[1,2,3]};omit(obj, 'e.1'); // {a: 3, b: {c:5, d:7}, e:[1,3]};*/functionomit(obj,propsArg){varprops,lastProp;if(Array.isArray(propsArg)){props=propsArg.slice(0);}if(typeofpropsArg=='string'){props=propsArg.split('.');}if(typeofpropsArg=='symbol'){props=[propsArg];}if(!Array.isArray(props)){thrownewError('props arg must be an array, a string or a symbol');}lastProp=props.pop();if(!lastProp){returnfalse;}prototypeCheck(lastProp);varthisProp;while((thisProp=props.shift())){prototypeCheck(thisProp);if(typeofobj[thisProp]=='undefined'){obj[thisProp]={};}obj=obj[thisProp];if(!obj||typeofobj!='object'){returnfalse;}}if(Array.isArray(obj)){obj.splice(parseInt(lastProp),1);}else{deleteobj[lastProp];}returntrue;}functionprototypeCheck(prop){// coercion is intentional to catch prop values like `['__proto__']`if(prop=='__proto__'||prop=='constructor'||prop=='prototype'){thrownewError('setting of prototype values not supported');}}
The text was updated successfully, but these errors were encountered:
I will take a stab at it. Will be my first PR. Should it be a new function renamed as "just-safe-omit" to match up with just-safe-[get/set]? As you know both safe functions dig deep in nesting. Seems like the other functions (like just-pick) are all aimed at root level object and array methods? Happy to leave it name just-omit if you think thats better.
easily could change to use code like just-safe-set, then it will handle nested objects/arrays. Working from the just-safe-set code
Changes
omit
and dropped thevalue
paramobj[lastProp] = value
becomesdelete obj[lastProp]
delete obj[lastProp]
puts in null, so check for array and handle withobj.splice(parseInt(lastProp), 1)
The text was updated successfully, but these errors were encountered: