-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
1 lines (1 loc) · 1022 Bytes
/
index.js
1
let a=Array.isArray,b=(a)=>'object'==typeof a&&null!==a,c=(b)=>Object.keys(a(b)?[...b]:b),d=module.exports=(e,f=[])=>{let g,h,i=(a,b)=>{for(g=f.length;g>(b?1:0);)a=a[f[--g]];return b?void(a[f[--g]]=b):a},j=i(e),k=(d=e)=>b(d)?c(d).reduce((c,e)=>(g=d[e],c[e]=a(g)?g.map(k):b(g)?k(g):g,c),a(d)?[]:{}):d,l=()=>{throw new Error(`${a(j)?'arr':'obj'} immutable`)},m=(a)=>(...b)=>{let c=/reverse|sort|splice|fill|copyWithin/.test(a),f=/filter|map|concat|slice/.test(a),h=f?e:k(),j=i(h)[a](...b);return g=c||f?d(j):j,/push|pop|shift|unshift/.test(a)?[g,d(h)]:g};if(!b(e))throw new Error('expect arr|obj');else if(!j)return j;return f.length||(e=c(e).reduce((a,c)=>(a[c]=b(j[c])?d(e,[...f,c]):j[c],a),a(e)?[]:{})),new Proxy(j,{get(b,c){return g=i(e),{$set(a,b){return h=k(),g=i(h),g[a]=b,d(h)},$unset(b){return(h=k(),a(j)&&!(b%1)&&0<=b)?(g=[...h.slice(0,b),...h.slice(b+1)],f.length?(i(h,g),d(h)):d(g)):(delete i(h)[b],d(h))}}[c]||g[c]&&({}.hasOwnProperty.call(g,c)?g[c]:m(c))},defineProperty:l,deleteProperty:l,setPrototypeOf:l})};