xuxi
Version:
Dynamically utility for combining different types of values into a single value.
2 lines (1 loc) • 1.82 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0}),exports.object=void 0,exports.isPlainObject=isPlainObject,exports.clean=clean,exports.ocx=ocx;function isPlainObject(s){return s!==null&&typeof s=="object"&&!Array.isArray(s)}function baseOcx(...s){const f=new WeakMap;function o(e,r){if(!r)return e;if(isPlainObject(r)){if(f.has(r))return f.get(r);const t={...e};f.set(r,t),e=t}if(Array.isArray(r))return{...e,...baseOcx(...r)};if(typeof r=="function"){const t=r(e);return isPlainObject(t)?o(e,t):{...e,...baseOcx(t)}}return isPlainObject(r)&&Reflect.ownKeys(r).forEach(t=>{const n=r[t];isPlainObject(n)&&isPlainObject(e[t])?e[t]=o(e[t],n):e[t]=n}),e}return s.reduce((e,r)=>o(e,r),{})}function preserveRoot(...s){const f=new WeakMap;function o(e,r){if(!r)return e;if(isPlainObject(r)){if(f.has(r))return f.get(r);const t={...e};f.set(r,t),e=t}if(Array.isArray(r))return{...e,...preserveRoot(...r)};if(typeof r=="function"){const t=r(e);return isPlainObject(t)?o(e,t):{...e,...preserveRoot(t)}}return isPlainObject(r)&&Reflect.ownKeys(r).forEach(t=>{const n=r[t];e[t]===void 0?e[t]=n:isPlainObject(n)&&isPlainObject(e[t])&&(e[t]=o(e[t],n))}),e}return s.reduce((e,r)=>o(e,r),{})}function clean(s,f=[],o=new WeakSet){const e=new Set(f);return o.has(s)?s:(o.add(s),Reflect.ownKeys(s).reduce((r,t)=>{const n=s[t];if(isPlainObject(n)){const u=clean(n,f,o);(Object.keys(u).length>0||typeof t=="symbol")&&(r[t]=u)}else if(Array.isArray(n)){const u=n.map(l=>isPlainObject(l)?clean(l,f,o):l).filter(l=>l&&!(isPlainObject(l)&&Object.keys(l).length===0)||e.has(l));u.length>0&&(r[t]=u)}else(n||e.has(n)||typeof t=="symbol")&&(r[t]=n);return r},{}))}const object=(...s)=>clean(baseOcx(...s),[0]);exports.object=object,object.raw=baseOcx,object.preserve=preserveRoot;function ocx(...s){return clean(baseOcx(...s),[0])}