reflect-deep
Version:
Deep property manipulation utilities with robust cloning, path traversal, and property access for JavaScript/TypeScript objects.
2 lines (1 loc) • 3.94 kB
JavaScript
const e=Object.create,t=Object.prototype.valueOf,r=Reflect.get,n=Reflect.set,f=Reflect.has,i=Reflect.ownKeys,o=Reflect.getPrototypeOf,c=Reflect.deleteProperty,a=Reflect.defineProperty,u=Array.isArray,s=Array.from,l=ArrayBuffer.isView,y=ArrayBuffer.prototype.slice,d=Date,w=RegExp,p=Map,h=Set,_=WeakMap,b=WeakSet,A=WeakRef,g=DataView,R=Number,B=String,k=Boolean,v="undefined"!=typeof BigInt?BigInt:void 0,S=Promise,j="undefined"!=typeof SharedArrayBuffer?SharedArrayBuffer:void 0,M="undefined"!=typeof Buffer?Buffer:void 0,x=null==M?void 0:M.from,E=Set.prototype.add,m=Set.prototype.forEach,N=Map.prototype.set,P=Map.prototype.forEach,$=TypeError;function D(e){return("object"!=typeof e||null===e)&&"function"!=typeof e}function O(e,t){if(D(t))throw new $(`__NAME__.${e} called with non-object target: ${t}`)}function I(e,t,r){if(D(t))throw new $(`__NAME__.${e} called with non-object target: ${t}`);if(!Array.isArray(r))throw new $(`__NAME__.${e} called with non-array keys`);if(0===r.length)throw new $(`__NAME__.${e} called with empty array of keys`)}function W(c,a){if("object"!=typeof a||null===a)return a;if(c.has(a))return c.get(a);if(a instanceof R||a instanceof B||a instanceof k||v&&a instanceof v){const r=t.call(a);if(a instanceof R)return new R(r);if(a instanceof B)return new B(r);if(a instanceof k)return new k(r);if(v&&a instanceof v)return e(v.prototype,{[Symbol.toPrimitive]:{value:()=>r,enumerable:!1,configurable:!1}})}if(u(a)){const e=[];c.set(a,e);for(let t=0;t<a.length;t++)t in a&&(e[t]=W(c,a[t]));return e}if(a instanceof p){const e=new p;return c.set(a,e),P.call(a,(t,r)=>{N.call(e,W(c,r),W(c,t))}),e}if(a instanceof h){const e=new h;return c.set(a,e),m.call(a,t=>{E.call(e,W(c,t))}),e}if(a instanceof d)return new d(a);if(a instanceof w)return new w(a.source,a.flags);if(a instanceof _)return a;if(a instanceof b)return a;if(a instanceof A)return new A(W(c,a.deref()));if(a instanceof S)return a;if(j&&a instanceof j)return a;if(l(a)){const e=a.constructor;if(a instanceof g){const e=y.call(a.buffer,a.byteOffset,a.byteOffset+a.byteLength);return new g(e)}return new e(a)}if(a instanceof ArrayBuffer)return y.call(a,0);if(M&&a instanceof M)return x(a);const s=e(o(a));c.set(a,s);const $=i(a);for(let e=0;e<$.length;e++){if(!f(a,$[e]))continue;const t=r(a,$[e]);n(s,$[e],W(c,t))}return s}const K=Symbol("NOT_PROVIDED");class T extends(null){constructor(){throw new $("ReflectDeep is not a constructor")}static has(e,t){I("has",e,t);let n=e;for(let e=0;e<t.length-1;e++){if(!f(n,t[e]))return!1;if(n=r(n,t[e]),D(n))return!1}return f(n,t[t.length-1])}static get(e,t,n=K){I("get",e,t);let i=e;for(let e=0;e<t.length-1;e++){if(!f(i,t[e]))return;if(i=r(i,t[e]),D(i))return}return n===K?r(i,t[t.length-1]):r(i,t[t.length-1],n)}static set(e,t,i,o=K){I("set",e,t);let c=e;for(let e=0;e<t.length-1;e++){if(!f(c,t[e])&&!n(c,t[e],{}))return!1;if(c=r(c,t[e]),D(c))return!1}return o===K?n(c,t[t.length-1],i):n(c,t[t.length-1],i,o)}static reach(e,t,n=K){I("reach",e,t);let i=e;for(let e=0;e<t.length;e++){if(!f(i,t[e]))return{value:i,index:e-1,reached:!1};if(e===t.length-1){return{value:n===K?r(i,t[e]):r(i,t[e],n),index:e,reached:!0}}if(i=r(i,t[e]),D(i))return{value:i,index:e,reached:!1}}return{value:i,index:-1,reached:!1}}static clone(e){return W(new _,e)}static deleteProperty(e,t){I("deleteProperty",e,t);let n=e;for(let e=0;e<t.length-1;e++){if(!f(n,t[e]))return!0;if(n=r(n,t[e]),D(n))return!1}return c(n,t[t.length-1])}static defineProperty(e,t,i){I("defineProperty",e,t);let o=e;for(let e=0;e<t.length-1;e++){if(!f(o,t[e])&&!n(o,t[e],{}))return!1;if(o=r(o,t[e]),D(o))return!1}return a(o,t[t.length-1],i)}static keys(e){O("keys",e);const t=new h(i(e));let r=e;for(;;){if(r=o(r),!r)return s(t);{const e=i(r);for(let r=0;r<e.length;r++)E.call(t,e[r])}}}static groupedKeys(e){O("groupedKeys",e);const t=[{keys:i(e),object:e}];let r=o(e);for(;;){if(!r)return t;t.push({object:r,keys:i(r)}),r=o(r)}}}export{T as ReflectDeep};