UNPKG

@js-utility/object

Version:

Comprehensive utility library for advanced object manipulation in JavaScript: deep get/set, clone, merge, flatten, and more. Ideal for Node.js and browser projects as a lightweight lodash alternative.

2 lines (1 loc) 6.1 kB
function g(e,f){let t=new WeakMap,i=new WeakMap;function o(r,s){if(r===s||typeof r=="number"&&typeof s=="number"&&isNaN(r)&&isNaN(s))return!0;if(r==null||s==null)return r===s;if(typeof r!="object"||typeof s!="object"||Object.getPrototypeOf(r)!==Object.getPrototypeOf(s))return!1;if(t.has(r))return t.get(r)===s;if(i.has(s))return i.get(s)===r;if(t.set(r,s),i.set(s,r),Array.isArray(r)){if(!Array.isArray(s)||r.length!==s.length)return!1;for(let a=0;a<r.length;a++)if(!o(r[a],s[a]))return!1;return!0}if(r instanceof Date&&s instanceof Date)return r.getTime()===s.getTime();if(r instanceof RegExp&&s instanceof RegExp)return r.toString()===s.toString();if(r instanceof Map&&s instanceof Map){if(r.size!==s.size)return!1;for(let[a,d]of r)if(!s.has(a)||!o(d,s.get(a)))return!1;return!0}if(r instanceof Set&&s instanceof Set){if(r.size!==s.size)return!1;for(let a of r)if(!s.has(a))return!1;return!0}let u=Object.keys(r),y=Object.keys(s);if(u.length!==y.length)return!1;for(let a of u)if(!y.includes(a)||!o(r[a],s[a]))return!1;return!0}return o(e,f)}function R(e){return e==null?!1:((e instanceof Number||e instanceof String||e instanceof Boolean)&&(e=e.valueOf()),typeof e=="string"?e.trim().length>0:typeof e=="boolean"?!0:typeof e=="number"?!isNaN(e):e instanceof Date?!isNaN(e.getTime()):Array.isArray(e)?e.length>0:e instanceof Map||e instanceof Set?e.size>0:typeof e=="object"?Object.keys(e).length>0:!0)}function b(e){return!R(e)}function A(e){if(Array.isArray(e))return e;let f=/([^[.\]]+)|\[(\d+|\*)\]/g,t=[];for(let i of e.matchAll(f))i[1]?t.push(i[1]):i[2]&&t.push(i[2]==="*"?"*":Number(i[2]));return t}function O(e,f,t){let{resolveRefs:i,maxRefVisits:o=5,default:r}=t||{};if(b(f)||b(e))return r;let s=new Map,u=A(f),y=n=>{if(!i)return n;if(typeof n=="string"&&e[n]){let p=s.get(n)||0;return p>=o?void 0:(s.set(n,p+1),y(e[n]))}if(Array.isArray(n)){let p=[];for(let c of n){let l=y(c);l!==void 0&&(Array.isArray(l)?p.push(...l):p.push(l))}return p}return n},a=(n,p)=>{if(n=y(n),p>=u.length)return n;let c=u[p];if(Array.isArray(n)){let l=[];if(c==="*"){for(let h of n){let m=a(h,p+1);m!=null&&(Array.isArray(m)?l.push(...m):l.push(m))}return l}else{if(typeof c=="number")return a(n[c],p+1);for(let h of n){let m=a(h?.[c],p+1);m!=null&&(Array.isArray(m)?l.push(...m):l.push(m))}return l}}if(typeof n=="object"&&n!==null)return a(n[c],p+1)};return a(e,0)??r}var w=O;var N=e=>e&&structuredClone(e),C=(e,f)=>e?Object.prototype.hasOwnProperty.call(e,f):!1,$=(e,f=1)=>e.reduce((t,i)=>(i?.split(".")?.reduce((o,r,s,{length:u})=>(s===u-1?o[r]=f:(typeof o[r]!="object"||o[r]===null)&&(o[r]={}),o[r]),t),t),{}),E=(e,f="")=>{let t=[];for(let i in e){let o=f?`${f}.${i}`:i;typeof e[i]=="object"&&e[i]!==null?t=t.concat(E(e[i],o)):t.push(o)}return t},_=e=>(e||[]).filter((f,t)=>!e.some((i,o)=>o<t&&g(f,i))),K=_,X=(e,f)=>e===f?!0:e?.length!==f?.length?!1:!!e?.every((t,i)=>g(t,f?.[i])),Y=(e,f)=>(e||[]).filter((t,i)=>f?.some((o,r)=>g(t,o))),F=(e,f)=>(e.sort((t,i)=>{let o=0;for(let r of f){let s=O(t,r.key),u=O(i,r.key),y=r.priority_prefix&&s?.toString()?.startsWith(r.priority_prefix),a=r.priority_prefix&&u?.toString()?.startsWith(r.priority_prefix);if(y&&!a)return-1;if(!y&&a)return 1;if(o=(s??"").toString().localeCompare(u??"",void 0,{numeric:!0,sensitivity:"base"}),r.order===-1&&(o*=-1),o!==0)return o}return 0}),e);function G(e,f){return e.reduce((t,i)=>{let o=O(i,f)?.toString()??"";return t[o]||(t[o]=[]),t[o].push(i),t},{})}function S(e,f){if(!e)return e;if(Array.isArray(e))return e.map(o=>S(o,f)).filter(Boolean);function t(o,r){if(!o||!r?.length||typeof o!="object")return;let[s,...u]=r;u.length===0?Array.isArray(o)&&typeof s=="number"?o.splice(s,1):delete o[s]:s in o&&t(o[s],u)}let i=N(e);return f.forEach(o=>{t(i,A(o))}),i}function D(e,f,t,i){if(e==null)return;if(Array.isArray(e))return e.forEach(n=>D(n,f,t,i));let{resolveRefs:o,maxRefVisits:r=5,set_forcefully:s}=i||{},u=A(f),y=new Map,a=n=>{if(!o)return n;if(typeof n=="string"&&e[n]){let p=y.get(n)||0;return p>=r?void 0:(y.set(n,p+1),a(e[n]))}return n},d=(n,p)=>{let c=u[p];if(p===u.length-1){if(c==="*"){if(Array.isArray(n))for(let l=0;l<n.length;l++)n[l]=t}else(typeof c=="number"&&Array.isArray(n)||typeof n=="object"&&n!==null)&&(n[c]=t);return}if(c==="*"){if(Array.isArray(n))for(let l=0;l<n.length;l++){let h=a(n[l]);h==null&&(n[l]=typeof u[p+1]=="number"?[]:{}),d(n[l],p+1)}}else{if(typeof n!="object"||n===null)return;c in n||(n[c]=typeof u[p+1]=="number"?[]:{});let l=a(n[c]);if(l==null&&(n[c]=typeof u[p+1]=="number"?[]:{}),s){if(Array.isArray(n[c]))return n[c].forEach((h,m)=>{typeof h!="object"&&(n[c][m]={}),d(n[c][m],p+1)});typeof n[c]!="object"?n[c]={}:(u[p+1]==="*"||typeof u[p+1]=="number")&&(n[c]=[])}d(n[c],p+1)}};d(e,0)}var P=D;function T(e,f){if(Array.isArray(e))return e.map(r=>T(r,f)).filter(Boolean);let t=(r,s,u=[])=>{if(s.length===0)return[{path:u,value:r}];let[y,...a]=s;return y==="*"?Array.isArray(r)?r.flatMap((d,n)=>t(d,a,[...u,n])):[]:r&&y in r?t(r[y],a,[...u,y]):[]},i=f.flatMap(r=>t(e,A(r))),o=Array.isArray(e)?[]:{};for(let{path:r,value:s}of i)P(o,r,s);return o}var te=(e,f)=>b(e)||b(f)||typeof e!="object"||typeof f!="object"?e:Array.isArray(e)&&Array.isArray(f)?e.filter((t,i)=>!f.some((o,r)=>g(t,o))):Object.keys(e).reduce((t,i)=>(g(e[i],f[i])||(t[i]=e[i]),t),{});var se=(e,f=null)=>{if(e==null)return f;if(typeof e!="string")return e;let t=e.trim();if(t==="")return f;if(t==="null")return null;if(t==="undefined")return;if(t==="true")return!0;if(t==="false")return!1;let i=Number(t);if(!isNaN(i))return i;try{return JSON.parse(t)}catch{return t}};var k=(e,...f)=>{if(!e||!f?.length)return e;let t=i=>i&&typeof i=="object"&&!Array.isArray(i);return f.reduce((i,o)=>(o&&Object.keys(o).forEach(r=>{let s=i[r],u=o[r];t(s)&&t(u)?i[r]=k(s,u):i[r]=u}),i),e)};export{X as arrayEquals,N as cloneDeep,D as deepAssign,g as deepEqual,O as deepExtract,S as deepOmit,te as difference,E as fromNestedJson,w as getDeep,G as groupBy,C as hasProperty,Y as intersect,R as isNotNull,b as isNull,se as jsonify,k as mergeObjs,T as pickDeep,_ as removeDuplicates,P as setDeep,F as sort,$ as toNestedJson,K as union};