@fluixi/utils 1.0.0-alpha.83 → 1.0.0-alpha.84
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +2858 -1
- package/dist/index.mjs +2838 -1
- package/dist/lib/array/advanced.cjs +2227 -1
- package/dist/lib/array/advanced.mjs +2204 -1
- package/dist/lib/array/array.cjs +57 -1
- package/dist/lib/array/array.mjs +36 -1
- package/dist/lib/array/index.cjs +2344 -1
- package/dist/lib/array/index.mjs +2322 -1
- package/dist/lib/array/utils.cjs +103 -1
- package/dist/lib/array/utils.mjs +82 -1
- package/dist/lib/color/color.cjs +2190 -1
- package/dist/lib/color/color.mjs +2167 -1
- package/dist/lib/color/constants.cjs +380 -1
- package/dist/lib/color/constants.mjs +359 -1
- package/dist/lib/color/conversions.cjs +629 -1
- package/dist/lib/color/conversions.mjs +608 -1
- package/dist/lib/color/helpers.cjs +2534 -1
- package/dist/lib/color/helpers.mjs +2511 -1
- package/dist/lib/color/index.cjs +2641 -1
- package/dist/lib/color/index.mjs +2619 -1
- package/dist/lib/color/types.cjs +18 -1
- package/dist/lib/color/utils.cjs +962 -1
- package/dist/lib/color/utils.mjs +939 -1
- package/dist/lib/compare/comparators.cjs +171 -1
- package/dist/lib/compare/comparators.mjs +150 -1
- package/dist/lib/compare/index.cjs +172 -1
- package/dist/lib/compare/index.mjs +152 -1
- package/dist/lib/crypto/hash.cjs +83 -1
- package/dist/lib/crypto/hash.mjs +62 -1
- package/dist/lib/crypto/index.cjs +85 -1
- package/dist/lib/crypto/index.mjs +62 -1
- package/dist/lib/dom/events.cjs +40 -1
- package/dist/lib/dom/events.mjs +19 -1
- package/dist/lib/dom/index.cjs +78 -1
- package/dist/lib/dom/index.mjs +56 -1
- package/dist/lib/dom/utils.cjs +57 -1
- package/dist/lib/dom/utils.mjs +36 -1
- package/dist/lib/functions/functions.cjs +366 -1
- package/dist/lib/functions/functions.mjs +345 -1
- package/dist/lib/functions/index.cjs +369 -1
- package/dist/lib/functions/index.mjs +347 -1
- package/dist/lib/functions/types.cjs +18 -1
- package/dist/lib/helpers.cjs +78 -1
- package/dist/lib/helpers.mjs +59 -1
- package/dist/lib/index.cjs +2857 -1
- package/dist/lib/index.mjs +2836 -1
- package/dist/lib/maths/constants.cjs +70 -1
- package/dist/lib/maths/constants.mjs +50 -1
- package/dist/lib/maths/fn/advanced.cjs +231 -1
- package/dist/lib/maths/fn/advanced.mjs +208 -1
- package/dist/lib/maths/fn/basic.cjs +212 -1
- package/dist/lib/maths/fn/basic.mjs +191 -1
- package/dist/lib/maths/fn/complex.cjs +268 -1
- package/dist/lib/maths/fn/complex.mjs +247 -1
- package/dist/lib/maths/fn/index.cjs +877 -1
- package/dist/lib/maths/fn/index.mjs +855 -1
- package/dist/lib/maths/fn/trigo.cjs +179 -1
- package/dist/lib/maths/fn/trigo.mjs +156 -1
- package/dist/lib/maths/fn/vector.cjs +728 -1
- package/dist/lib/maths/fn/vector.mjs +707 -1
- package/dist/lib/maths/formatter.cjs +61 -1
- package/dist/lib/maths/formatter.mjs +40 -1
- package/dist/lib/maths/guards.cjs +164 -1
- package/dist/lib/maths/guards.mjs +144 -1
- package/dist/lib/maths/index.cjs +1152 -1
- package/dist/lib/maths/index.mjs +1130 -1
- package/dist/lib/maths/parser.cjs +75 -1
- package/dist/lib/maths/parser.mjs +54 -1
- package/dist/lib/maths/random.cjs +49 -1
- package/dist/lib/maths/random.mjs +28 -1
- package/dist/lib/maths/types/advanced.cjs +18 -1
- package/dist/lib/maths/types/basic.cjs +18 -1
- package/dist/lib/maths/types/index.cjs +19 -1
- package/dist/lib/maths/types/index.mjs +1 -0
- package/dist/lib/maths/types/negative.cjs +18 -1
- package/dist/lib/maths/types/operations.cjs +18 -1
- package/dist/lib/object/clone.cjs +88 -1
- package/dist/lib/object/clone.mjs +65 -1
- package/dist/lib/object/diff.cjs +573 -1
- package/dist/lib/object/diff.mjs +550 -1
- package/dist/lib/object/getter-setter.cjs +2351 -1
- package/dist/lib/object/getter-setter.mjs +2330 -1
- package/dist/lib/object/index.cjs +3956 -6
- package/dist/lib/object/index.mjs +3936 -6
- package/dist/lib/object/merge.cjs +1029 -1
- package/dist/lib/object/merge.mjs +1006 -1
- package/dist/lib/object/object.cjs +280 -1
- package/dist/lib/object/object.mjs +257 -1
- package/dist/lib/object/omit.cjs +2022 -1
- package/dist/lib/object/omit.mjs +1999 -1
- package/dist/lib/object/reconcile.cjs +126 -1
- package/dist/lib/object/reconcile.mjs +105 -1
- package/dist/lib/object/selector.cjs +303 -1
- package/dist/lib/object/selector.mjs +282 -1
- package/dist/lib/object/sort.cjs +209 -1
- package/dist/lib/object/sort.mjs +186 -1
- package/dist/lib/object/transform.cjs +2211 -1
- package/dist/lib/object/transform.mjs +2190 -1
- package/dist/lib/object/types.cjs +18 -1
- package/dist/lib/object/utils.cjs +326 -6
- package/dist/lib/object/utils.mjs +305 -6
- package/dist/lib/primitive/boolean/boolean.cjs +407 -1
- package/dist/lib/primitive/boolean/boolean.mjs +386 -1
- package/dist/lib/primitive/boolean/index.cjs +382 -1
- package/dist/lib/primitive/boolean/index.mjs +359 -1
- package/dist/lib/primitive/boolean/types.cjs +18 -1
- package/dist/lib/primitive/date/date.cjs +1126 -1
- package/dist/lib/primitive/date/date.mjs +1105 -1
- package/dist/lib/primitive/date/index.cjs +1128 -1
- package/dist/lib/primitive/date/index.mjs +1105 -1
- package/dist/lib/primitive/date/types.cjs +18 -1
- package/dist/lib/primitive/index.cjs +2338 -5
- package/dist/lib/primitive/index.mjs +2316 -5
- package/dist/lib/primitive/string/index.cjs +875 -5
- package/dist/lib/primitive/string/index.mjs +852 -5
- package/dist/lib/primitive/string/parser-2.cjs +543 -8
- package/dist/lib/primitive/string/parser-2.mjs +522 -8
- package/dist/lib/primitive/string/parser.cjs +236 -3
- package/dist/lib/primitive/string/parser.mjs +215 -3
- package/dist/lib/primitive/string/string.cjs +926 -5
- package/dist/lib/primitive/string/string.mjs +903 -5
- package/dist/lib/primitive/string/types.cjs +18 -1
- package/dist/lib/primitive/types.cjs +18 -1
- package/dist/lib/url/index.cjs +327 -1
- package/dist/lib/url/index.mjs +304 -1
- package/dist/lib/url/parser.cjs +325 -1
- package/dist/lib/url/parser.mjs +304 -1
- package/dist/lib/url/types.cjs +18 -1
- package/package.json +5 -5
|
@@ -1,6 +1,3956 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
${r(l)}}`:`{${d.join(",")}}`}}},s=o(e,0);return s===void 0?void 0:s}var K=(e,t)=>{if(e==null||typeof e!="object")return e;if(e instanceof Date)return new Date(e.getTime());if(e instanceof RegExp)return new RegExp(e.source,e.flags);if(e instanceof Map){let n=new Map;return e.forEach((r,i)=>{(!t||!t.some(a=>a.toString()===i.toString()))&&n.set(i,K(r))}),n}if(e instanceof Set){let n=new Set;return e.forEach(r=>{n.add(K(r))}),n}if(Array.isArray(e))return Array.from(e).map(n=>K(n));if(T(e)){let n={};for(let i in e)Object.prototype.hasOwnProperty.call(e,i)&&(!t||!t.some(a=>a.toString()===i.toString()))&&(n[i]=K(e[i]));let r=Object.getOwnPropertySymbols(e);for(let i of r)(!t||!t.some(a=>a.toString()===i.toString()))&&(n[i]=K(e[i]));return n}return e};var L=(e,t=!1)=>{if(Array.isArray(e))return e.map(r=>L(r,t));if(!T(e))return{};let n={};for(let r in e){let i=e[r];typeof i=="string"||typeof i=="number"||typeof i=="symbol"?n[i]=r:T(i)?t&&(n[r]=L(i,t)):Array.isArray(i)&&t&&(n[r]=i.map(a=>L(a,t)))}return n},Je=(e,t)=>{if(Array.isArray(e))return e.filter(t);if(!T(e))return{};let n={};for(let r in e)t(e[r],r)&&(n[r]=e[r]);return n},Ae=(e,t=!1)=>{if(!T(e))return{};let n={};for(let r in e){let i=e[r];if(i!=null)if(t&&T(i)){let a=Ae(i,!0);Object.keys(a).length>0&&(n[r]=a)}else n[r]=i}return n},qe=(e,t)=>{if(Array.isArray(e))return Array.isArray(t)?[...e,...t]:[...e,t];let n=K(e);for(let r in t)n[r]===void 0&&(n[r]=t[r]);return n},Ze=e=>T(e)?Object.keys(e).length:0,Qe=(e,t)=>{if(Array.isArray(e))return e.map(t);if(!T(e))return{};let n={};for(let r in e){let[i,a]=t(r,e[r]);n[i]=a}return n},Ge=(e,t)=>Array.isArray(e)?e.reduce((n,r)=>{let i=r[t];if(i==null)return n;let a=String(i);return n[a]||(n[a]=[]),n[a].push(r),n},{}):{},He=e=>{if(!T(e))return"";let t=[],n=(r,i)=>{i!=null&&(Array.isArray(i)?i.forEach(a=>{t.push(`${encodeURIComponent(r)}=${encodeURIComponent(a)}`)}):t.push(`${encodeURIComponent(r)}=${encodeURIComponent(i)}`))};for(let r in e)n(r,e[r]);return t.join("&")},Xe=e=>{if(!e||typeof e!="string")return{};let t=e.startsWith("?")?e.slice(1):e;if(!t)return{};let n={},r=t.split("&");for(let i of r){let[a,o]=i.split("=").map(decodeURIComponent);a&&(a in n?Array.isArray(n[a])?n[a].push(o||""):n[a]=[n[a],o||""]:n[a]=o||"")}return n},te=e=>{if(e==null||typeof e!="object")return e;Object.freeze(e);for(let n in e){let r=e[n];r!=null&&typeof r=="object"&&te(r)}let t=Object.getOwnPropertySymbols(e);for(let n of t){let r=e[n];r!=null&&typeof r=="object"&&te(r)}return e},Ye=e=>e!=null&&typeof e=="object"&&!Array.isArray(e);var Te=(e,t)=>e===t,j=(e,t)=>{if(e.length!==t.length)return!1;for(let n=0;n<e.length;n++)if(Array.isArray(e[n])&&Array.isArray(t[n])){if(!j(e[n],t[n]))return!1}else if(T(e[n])&&T(t[n])){if(!me(e[n],t[n]))return!1}else if(e[n]!==t[n])return!1;return!0},me=(e,t)=>{let n=Object.keys(e),r=Object.keys(t);if(n.length!==r.length)return!1;for(let i of n){if(!(i in t))return!1;let a=e[i],o=t[i];if(Array.isArray(a)&&Array.isArray(o)){if(!j(a,o))return!1}else if(T(a)&&T(o)){if(!me(a,o))return!1}else if(a!==o)return!1}return!0},z=(e,t,n={})=>{let{includeDeleted:r=!1,includeAdded:i=!0,deepArrayComparison:a=!0,equalityFn:o=Te,preserveStructure:s=!1}=n;if(e==null&&t==null)return{};if(e==null)return i?{...t}:{};if(t==null){if(r){let y={};for(let c in e)y[c]=void 0;return y}return{}}if(!T(e)||!T(t))return o(e,t)?{}:t;let f={},l=new Set;for(let y in t){l.add(y);let c=e[y],u=t[y];if(!(y in e)){i&&(f[y]=u);continue}if(Array.isArray(c)&&Array.isArray(u)){a?j(c,u)?s&&(f[y]=void 0):f[y]=u:c!==u?f[y]=u:s&&(f[y]=void 0);continue}if(T(c)&&T(u)){let d=z(c,u,n);Object.keys(d).length>0?f[y]=d:s&&(f[y]=void 0);continue}o(c,u)?s&&(f[y]=void 0):f[y]=u}if(r)for(let y in e)l.has(y)||(f[y]=void 0);return f},U=(e,t,n={})=>{let r=[],i=[],a=[],o={},{includeDeleted:s=!1,includeAdded:f=!0,deepArrayComparison:l=!0,equalityFn:y=Te}=n;if(Array.isArray(e)&&Array.isArray(t)){let d=Math.max(e.length,t.length),p=[],A=!1;for(let g=0;g<d;g++){let m=`[${g}]`;if(g>=e.length)r.push(m),p[g]=t[g],A=!0;else if(g>=t.length)i.push(m),A=!0;else{let x=e[g],h=t[g];T(x)&&T(h)||Array.isArray(x)&&Array.isArray(h)?U(x,h,n).hasChanges?(a.push(m),p[g]=h,A=!0):p[g]=x:y(x,h)?p[g]=x:(a.push(m),p[g]=h,A=!0)}}return{changes:A?p:{},added:r,deleted:i,modified:a,hasChanges:A||i.length>0}}let c=(d,p,A="")=>{let g={added:[],deleted:[],modified:[]};if(Array.isArray(d)&&Array.isArray(p)){let m=Math.max(d.length,p.length);for(let x=0;x<m;x++){let h=A?`${A}[${x}]`:`[${x}]`;if(x>=d.length)g.added.push(h);else if(x>=p.length)g.deleted.push(h);else{let S=d[x],b=p[x];if(T(S)&&T(b)){let O=c(S,b,h);g.added.push(...O.added),g.deleted.push(...O.deleted),g.modified.push(...O.modified)}else if(Array.isArray(S)&&Array.isArray(b)){let O=c(S,b,h);g.added.push(...O.added),g.deleted.push(...O.deleted),g.modified.push(...O.modified)}else S!==b&&g.modified.push(h)}}return g}if(T(d)&&T(p)){let m=new Set([...Object.keys(d||{}),...Object.keys(p||{})]);for(let x of m){let h=d?.[x],S=p?.[x],b=A?`${A}.${x}`:x;if(!(x in(d||{})))g.added.push(b);else if(!(x in(p||{})))g.deleted.push(b);else if(Array.isArray(h)&&Array.isArray(S)){if(!j(h,S)){let O=c(h,S,b);g.added.push(...O.added),g.deleted.push(...O.deleted),g.modified.push(...O.modified)}}else if(T(h)&&T(S)){let O=c(h,S,b);g.added.push(...O.added),g.deleted.push(...O.deleted),g.modified.push(...O.modified)}else h!==S&&g.modified.push(b)}}return g};if(e==null&&t==null)return{changes:{},added:r,deleted:i,modified:a,hasChanges:!1};if(e==null){if(f&&t)for(let d in t)r.push(d),o[d]=t[d];return{changes:o,added:r,deleted:i,modified:a,hasChanges:r.length>0}}if(t==null){if(s)for(let d in e)i.push(d),o[d]=void 0;return{changes:o,added:r,deleted:i,modified:a,hasChanges:i.length>0}}let u=new Set;for(let d in t){u.add(d);let p=e[d],A=t[d];if(!(d in e)){f&&(r.push(d),o[d]=A);continue}let g=!1;if(Array.isArray(p)&&Array.isArray(A)){if(g=l?!j(p,A):p!==A,g){let m=c(p,A,d);r.push(...m.added),i.push(...m.deleted),a.push(...m.modified),o[d]=A}}else if(T(p)&&T(A)){let m=z(p,A,n);if(g=Object.keys(m).length>0,g){let x=c(p,A,d);r.push(...x.added),i.push(...x.deleted),a.push(...x.modified),o[d]=m}}else g=!y(p,A),g&&(a.push(d),o[d]=A)}for(let d in e)u.has(d)||(i.push(d),o[d]=void 0);return{changes:o,added:r,deleted:i,modified:a,hasChanges:r.length>0||i.length>0||a.length>0}},et=(e,t,n={})=>{let r=z(e,t,n);return Object.keys(r).length>0};function tt(e){return!!(e&&(e.diff||e.changes)&&(e.modified||e.deleted||e.hasChanges))}var k=(e,t,n)=>{if(!tt(t)){let g=t,m=n||!1;if(Array.isArray(e)&&Array.isArray(g)){let h=m?e:[...e];g.length<h.length&&(h.length=g.length);for(let S=0;S<g.length;S++){let b=g[S];b!==void 0&&(T(h[S])&&T(b)||Array.isArray(h[S])&&Array.isArray(b)?h[S]=k(h[S],b,m):h[S]=b)}return h}if(!T(e))return g;if(!T(g))return e;let x=m?e:{...e};for(let h in g){let S=g[h];S===void 0?delete x[h]:Array.isArray(x[h])&&Array.isArray(S)||T(x[h])&&T(S)?x[h]=k(x[h],S,m):x[h]=S}return x}let{diff:r,added:i=[],deleted:a=[],modified:o=[],changes:s,keepRef:f}=t,l=n!==void 0?n:f,y=s||r;if(!y)return e;let c=new Set(i),u=new Set(a),d=new Set(o),p=g=>{if(i.length===0&&a.length===0&&o.length===0)return!0;let m=g.split(/[.\[\]]+/).filter(Boolean);for(let x=0;x<=m.length;x++){let h=m.slice(0,x).join("."),S=m.slice(0,x).map((b,O)=>/^\d+$/.test(b)?`[${b}]`:O>0?`.${b}`:b).join("");if(c.has(h)||c.has(S)||u.has(h)||u.has(S)||d.has(h)||d.has(S))return!0}return!1};if(Array.isArray(e)&&Array.isArray(y)){let g=l?e:[...e];g.length=y.length;for(let m=0;m<y.length;m++){let x=`[${m}]`,h=y[m];h!==void 0&&p(x)&&(T(g[m])&&T(h)||Array.isArray(g[m])&&Array.isArray(h)?g[m]=k(g[m],h,l):g[m]=h)}return g}if(!T(e))return y;if(!T(y))return e;let A=l?e:{...e};for(let g of a)g.includes(".")||g.includes("[")||p(g)&&delete A[g];for(let g in y){let m=y[g];p(g)&&(m===void 0?delete A[g]:Array.isArray(A[g])&&Array.isArray(m)||T(A[g])&&T(m)?A[g]=k(A[g],m,l):A[g]=m)}return A},nt=(e,t,n={})=>{let r=[],i=(a,o,s="")=>{if(a===o)return;if(Array.isArray(a)&&Array.isArray(o)){let l=Math.max(a.length,o.length);for(let y=0;y<l;y++){let c=s?`${s}[${y}]`:`[${y}]`;if(y>=a.length)r.push(c);else if(y>=o.length)r.push(c);else{let u=a[y],d=o[y];T(u)&&T(d)||Array.isArray(u)&&Array.isArray(d)?i(u,d,c):u!==d&&r.push(c)}}return}if(!T(a)||!T(o)){s&&r.push(s);return}let f=new Set([...Object.keys(a||{}),...Object.keys(o||{})]);for(let l of f){let y=a?.[l],c=o?.[l],u=s?`${s}.${l}`:l;!(l in(a||{}))||!(l in(o||{}))?r.push(u):Array.isArray(y)&&Array.isArray(c)||T(y)&&T(c)?i(y,c,u):y!==c&&r.push(u)}};return i(e,t),r};var v=(e,t={})=>{let{type:n="auto",throwOnError:r=!1}=t;if(!e||typeof e!="string")return[];let i=[],a="",o=!1,s=!1,f="",l="",y=0,u=n==="auto"?(p=>p.includes("..")?"descendant":p.includes("[?(")?"filter":p.includes("[:")||p.includes("[0:")?"slice":p.includes("[,")||p.includes("[0,1]")?"union":p.includes("[*]")||p.includes(".*.")?"wildcard":p.includes("[]")?"array-wildcard":p.includes("[")&&p.includes("]")?"bracket":"dot")(e):n,d=p=>{let A=p.trim();if(A==="*"||A==="")return{type:"wildcard",value:A===""?"[]":"*",raw:A===""?"[]":p};if(A==="[]")return{type:"wildcard",value:"[]",raw:p};if(/^-?\d+$/.test(A))return{type:"index",value:parseInt(A,10),raw:p};if(A.includes(":")){let g=A.split(":").map(h=>h.trim()),m=g[0]===""?0:parseInt(g[0],10),x=g[1]===""?1/0:parseInt(g[1],10);if(isNaN(m)||isNaN(x)){if(r)throw new Error(`Invalid slice notation: ${p}`);return{type:"wildcard",value:"*",raw:p}}return{type:"slice",value:[m,x],raw:p}}if(A.includes(",")){let g=A.split(",").map(m=>{let x=parseInt(m.trim(),10);return isNaN(x)?-1:x}).filter(m=>m>=0);if(g.length===0){if(r)throw new Error(`Invalid union notation: ${p}`);return{type:"wildcard",value:"*",raw:p}}return{type:"union",value:g,raw:p}}if(A.startsWith("?(")&&A.endsWith(")")){let g=A.slice(2,-1);try{return{type:"filter",value:xe(g),raw:p}}catch(m){if(r)throw m;return{type:"wildcard",value:"*",raw:p}}}return A.startsWith('"')&&A.endsWith('"')||A.startsWith("'")&&A.endsWith("'")?{type:"property",value:A.slice(1,-1),raw:p}:{type:"property",value:A,raw:p}};for(let p=0;p<e.length;p++){let A=e[p],g=e[p+1];if((A==='"'||A==="'")&&!s){s=!0,f=A;continue}else if(A===f&&s){s=!1,f="",A==='"'||A==="'"||(o?l+=A:a+=A);continue}if(s){o?l+=A:a+=A;continue}if(A==="["&&!o){a&&(i.push({type:"property",value:a,raw:a}),a=""),o=!0,l="";continue}if(A==="]"&&o&&!s){o=!1;let x=d(l);i.push(x);continue}let m=i.length>0?i[i.length-1]:null;if(A==="."&&!o&&!s){if(g==="."){let x=e.split("..")[0];i.push({type:"property",value:x,raw:x}),i.push({type:"descendant",value:"..",raw:".."}),p++}else a&&(a==="*"?i.push({type:"wildcard",value:a,raw:a}):i.push({type:"property",value:a,raw:a}),a="");continue}if(o)l+=A;else if(m&&m.type!=="descendant")a+=A;else{let x=e.split(m?.raw)[1];x!==void 0?a=x:a+=A}}return a&&i.push({type:"property",value:a,raw:a}),i.map(p=>{if(p.type==="property"&&typeof p.value=="string"&&/^-?\d+$/.test(p.value)){let A=parseInt(p.value,10);if(!isNaN(A))return{...p,type:"index",value:A}}return p})},xe=e=>{let t=e.replace(/@\.([a-zA-Z_$][\w$]*)/g,(n,r)=>`item.${r}`).replace(/@/g,"item");try{return new Function("item",`return ${t}`)}catch{throw new Error(`Invalid filter expression: ${e}`)}},E=e=>{let t=[];for(let n=0;n<e.length;n++){let r=e[n],i=n>0?e[n-1]:null;switch(r.type){case"property":/[\.\[\]\s]/.test(String(r.value))?t.push(`["${r.value}"]`):(i&&i.type!=="descendant"&&t.push("."),t.push(String(r.value)));break;case"index":t.push(`[${r.value}]`);break;case"wildcard":r.value==="[]"?t.push("[]"):t.push("[*]");break;case"slice":let[o,s]=r.value;t.push(`[${o}:${s===1/0?"":s}]`);break;case"union":let f=r.value;t.push(`[${f.join(",")}]`);break;case"filter":t.push(`[?(${r.raw?.slice(2,-1)||""})]`);break;case"descendant":t.push("..");break}}return t.join("").replace(/\.\[/g,"[")},he=(e,t={})=>{if(Array.isArray(e))return e.map(n=>he(n,t));if(!/^[a-zA-Z]/.test(e))return{valid:!1,error:"Selector starts with '.'",type:D(e)};if(!/[a-zA-Z]$/.test(e))return{valid:!1,error:"Selector end with '.'",type:D(e)};try{let n=v(e,{throwOnError:!0}),r=D(e);return t.allowedTypes&&!t.allowedTypes.includes(r)?{valid:!1,error:`Selector type ${r} not allowed. Allowed: ${t.allowedTypes.join(", ")}`,type:r}:{valid:!0,type:D(e)}}catch(n){return{valid:!1,error:n.message}}},D=e=>e.includes("..")?"descendant":e.includes("[?(")?"filter":e.includes("[:")||e.includes("[0:")?"slice":/\[[^\]]*,[^\]]*\]/.test(e)?"union":e.includes("[*]")||e.includes(".*.")?"wildcard":e.includes("[]")?"array-wildcard":e.includes("[")&&e.includes("]")?"bracket":"dot",rt=(e,t)=>e.replace(/\{(\w+)\}/g,(n,r)=>r in t?String(t[r]):`{${r}}`);function ne(...e){if(e.length===0)return{};let t=e[0],n=Array.isArray(t);for(let i of e)if(i&&Array.isArray(i)!==n)throw new Error("Cannot mix arrays and objects in deepMerge");let r=n?[]:{};for(let i of e)if(i!=null)if(n){let a=r,o=i;for(let s of o)a.push(K(s))}else{if(!w(i))continue;let a=r,o=i;for(let f in o)if(Object.prototype.hasOwnProperty.call(o,f)){let l=o[f],y=a[f];y===void 0?a[f]=K(l):Array.isArray(l)&&Array.isArray(y)?a[f]=[...y,...l.map(c=>K(c))]:w(l)&&w(y)?a[f]=ne(y,l):a[f]=K(l)}let s=Object.getOwnPropertySymbols(o);for(let f of s){let l=o[f],y=a[f];y===void 0?a[f]=K(l):Array.isArray(l)&&Array.isArray(y)?a[f]=[...y,...l.map(c=>K(c))]:w(l)&&w(y)?a[f]=ne(y,l):a[f]=K(l)}}return r}function st(e,t,n){let{key:r,override:i=!0}=n||{};if(!r)return Array.from(new Set([...e,...t]));let a=new Map;for(let o of e)a.set(o[r],o);for(let o of t){let s=a.get(o[r]);s?a.set(o[r],i?{...s,...o}:{...o,...s}):a.set(o[r],o)}return Array.from(a.values())}function it(e,t){for(let n in t){let r=e[n],i=t[n],a=U(r,i);Array.isArray(r)&&Array.isArray(i)?(r.length=0,r.push(...i)):typeof r=="object"&&typeof i=="object"&&r&&i&&a.hasChanges?k(r,i):e[n]=i}}function C(e,...t){if(t.length===0)return K(e);let n={preserveTarget:!1,strict:!1},r=t,i=t[t.length-1];i&&typeof i=="object"&&("preserveTarget"in i||"strict"in i)&&(n={...n,...i},r=t.slice(0,-1));let a=n.preserveTarget?e:K(e),o=Array.isArray(a);for(let s of r)if(s&&Array.isArray(s)!==o)throw new Error("Cannot mix arrays and objects in deepMergeWithOptions");for(let s of r)if(s!=null)if(o){let f=a,l=s;if(n.strict)for(let y=0;y<f.length&&y<l.length;y++)w(f[y])&&w(l[y])?f[y]=C(f[y],l[y],n):typeof f[y]==typeof l[y]&&(f[y]=K(l[y]));else it(f,l)}else{if(!w(s))continue;let f=a,l=s;for(let c in l){if(!Object.prototype.hasOwnProperty.call(l,c))continue;let u=l[c];if(n.strict&&!(c in f))continue;let d=f[c];d===void 0?f[c]=K(u):Array.isArray(u)&&Array.isArray(d)?n.strict?f[c]=C(d,u,n):f[c]=[...d,...u.map(p=>K(p))]:w(u)&&w(d)?f[c]=C(d,u,n):f[c]=K(u)}let y=Object.getOwnPropertySymbols(l);for(let c of y){if(n.strict&&!(c in f))continue;let u=l[c],d=f[c];d===void 0?f[c]=K(u):Array.isArray(u)&&Array.isArray(d)?n.strict?f[c]=C(d,u,n):f[c]=[...d,...u.map(p=>K(p))]:w(u)&&w(d)?f[c]=C(d,u,n):f[c]=K(u)}}return a}function J(e,...t){return C(e,...t,{preserveTarget:!0})}function at(e,...t){let n=!1,r=t;return t.length>0&&typeof t[t.length-1]=="boolean"&&(n=t[t.length-1],r=t.slice(0,-1)),C(e,...r,{preserveTarget:n,strict:!0})}var re=(e,t,n)=>{let r=v(t),i=e;for(let a=0;a<r.length;a++){let o=r[a],s=a===r.length-1;switch(o.type){case"property":if(s)i[String(o.value)]=n;else{if(!i[String(o.value)]){let l=r[a+1];i[String(o.value)]=l.type==="index"||l.type==="slice"||l.type==="union"||l.type==="wildcard"?[]:{}}i=i[String(o.value)]}break;case"index":Array.isArray(i)||(i=[]);let f=o.value;if(s){if(f>=i.length)for(let l=i.length;l<=f;l++)i.push(void 0);i[f]=n}else{if(!i[f]){let l=r[a+1];i[f]=l.type==="index"||l.type==="slice"||l.type==="union"||l.type==="wildcard"?[]:{}}i=i[f]}break;case"wildcard":if(Array.isArray(i))if(s)for(let l=0;l<i.length;l++)i[l]=n;else{let l=r[a+1];for(let y=0;y<i.length;y++)i[y]||(i[y]=l.type==="index"||l.type==="slice"||l.type==="union"||l.type==="wildcard"?[]:{});i.length>0&&(i=i[0])}break}}};var Se=require("@fluixi/utils/primitive"),Oe=require("@fluixi/utils/object"),ot=require("@fluixi/utils/array"),lt=require("@fluixi/utils/functions");function Ke(e){let t=typeof e;return e===null||t==="string"||t==="number"||t==="boolean"||t==="symbol"||t==="bigint"||t==="undefined"||(0,Se.isDate)(e)}function P(e,t,n={}){let{preserveStructure:r,multiple:i,throwOnError:a=!1,defaultValue:o,maxDepth:s=20}=n;if(!e||typeof e!="object")return o;if(!t)return e;if(Array.isArray(t))if(r){let y={};for(let c of t){let u=P(e,c,{...n,preserveStructure:!1});u!==void 0&&re(y,c,u)}return y}else return t.map(y=>P(e,y,{...n,preserveStructure:!1}));let f=v(t,{...n,throwOnError:a});if(f.length===0)return e;if(typeof r>"u"||r===!1){let y=$(e,t);return i===!1&&Array.isArray(y)?y[0]:y===void 0&&o!==void 0?o:y}let l=R(e,f,0,s);return l===void 0&&o!==void 0?o:l}function _(e,t,n,r,i){let a=K(e),o=[];if(typeof t<"u"&&o.push(t),typeof n<"u"&&o.push(n),typeof r<"u"&&o.push(r),typeof i<"u"&&o.push(i),Ke(a))return typeof t=="function"?t(a):t;if(o.length===1){if(typeof o[0]=="function")return Ie(a,o[0]);if(Array.isArray(o[0])){if(Array.isArray(a))if(t.length===0)for(let s=0;s<a.length;s++)a.pop();else return U([...a],[...o[0]]).hasChanges&&k(a,[...o[0]],!0),a}else typeof o[0]=="object"&&!Array.isArray(o[0])?typeof a=="object"&&!Array.isArray(a)&&k(a,o[0],!0):a=o[0];return a}if(Array.isArray(a)){if(typeof t=="string"&&(ft(n)||typeof n=="string")&&o.length===3){if(Array.isArray(n)){for(let s=0;s<n.length;s++)V(a,t,r,n[s]);return a}return V(a,t,r,n)}if(typeof t=="string"&&typeof n=="function"&&o.length===2)return V(a,t,n)}if((typeof t=="string"||typeof t=="number")&&(o.length===2||o.length===3))return ke(n)?Z(a,t,n,r):typeof t=="number"?typeof r<"u"?V(a,`[${t}].${n}`,r):V(a,`[${t}]`,n,r):typeof n=="number"&&typeof r<"u"?q(a,t,n,r):Array.isArray(n)&&n.length>0&&n.every(s=>typeof s=="number")?W(a,t,n,r):typeof r<"u"&&typeof n=="function"?Q(a,t,n,r):V(a,t,n,r);if(typeof t=="string"&&typeof n=="number"){if(o.length===3)return q(a,t,n,r);if(o.length===4)return yt(a,t,n,r,i)}if(typeof t=="string"&&Array.isArray(n)){if(o.length===3)return W(a,t,n,r);if(o.length===4)return ct(a,t,n,r,i)}if(typeof t=="string"&&typeof n=="object"&&!Array.isArray(n)&&n!==null){let s=n;if("from"in s||"to"in s){if(o.length===3)return Z(a,t,s,r);if(o.length===4)return we(a,t,s,r,i)}}if(typeof t=="object"&&!Array.isArray(t)&&o.length===1)return k(a,t,!0);if(typeof t=="string"&&typeof n=="object"&&!Array.isArray(n)&&o.length===2)return Pe(a,t,n);if(typeof t=="string"&&typeof n=="function"){if(o.length===3)return Q(a,t,n,r);if(o.length===4)return se(a,t,n,r,i)}if(typeof t=="function"&&typeof n=="string"){if(o.length===3)return ve(a,t,n,r);if(o.length===4)return se(a,n,t,r,i)}if(Array.isArray(t)){if(o.length===2&&typeof n=="object"&&!Array.isArray(n))return ie(a,t,n);if(o.length===2)return ie(a,t,n)}throw new Error(`Invalid set parameters: ${JSON.stringify(o)}`)}function oe(e,t,n,r=!1){let i=v(e);if(i.length===0)return n;let a=i[i.length-1].type==="wildcard",o=i.some(f=>f.type==="wildcard")&&Array.isArray(t),s;if((a||o&&r||o&&i.length===2)&&Array.isArray(t))for(let f=0;f<t.length;f++){let l=typeof n=="function"?n(t[f]):typeof t[f]=="object"?k(t[f],n,!0):n;s||(s=[]),s.push(l)}return!a&&!s&&(s=typeof n=="function"?n(t):n),s}function ke(e){return typeof e=="object"&&e!==null&&"from"in e&&"to"in e&&typeof e.from=="number"&&typeof e.to=="number"&&e.from<e.to&&e.from>=0&&e.to>0}function ft(e){return Array.isArray(e)&&e.every(t=>typeof t=="string")}function V(e,t,n,r){let i=v(t),a=$(e,t,{paths:i,createMissing:!0}),o;r&&typeof n=="function"&&(o=P(a,r));let f=oe(t,r?o:a,n,Array.isArray(e));return r?I(e,`${t}.${r}`,f,{createMissing:!0}):I(e,t,f,{createMissing:!0})}function q(e,t,n,r){let i=$(e,t,{createMissing:!0});if(!Array.isArray(i))throw new Error(`Target at ${t} is not an array ${n}`);let a=structuredClone(i),o=oe(t,a[n],r);if(n<0){let s=i.length+n;if(s<0)throw new Error(`Index ${n} is out of bounds`);return I(e,`${t}[${s}]`,o,{createMissing:!0})}return I(e,`${t}[${n}]`,o,{createMissing:!0})}var yt=(e,t,n,r,i)=>typeof r=="function"?V(e,`${t}[${n}]`,r):typeof r=="string"?V(e,`${t}[${n}]`,i,r):null,W=(e,t,n,r)=>{let i=$(e,t);if(!Array.isArray(i))throw new Error(`Target at ${t} is not an array`);for(let a of n)typeof a=="number"?q(e,t,a,r):typeof a=="string"&&V(e,t,r,a);return e},ct=(e,t,n,r,i)=>W(e,t,n,o=>{let s=typeof i=="function"?i(o?$(o,r):void 0):i,f=o?{...o}:{};return I(f,r,s),f});function Z(e,t,n,r){let i=$(e,t);if(!Array.isArray(i))throw new Error(`Target at ${t} is not an array`);let a=n.from??0,o=n.to??i.length-1,s=n.by??1,f=[];for(let l=a;l<=o;l+=s)l>=0&&l<i.length&&f.push(l);return W(e,t,f,r)}var we=(e,t,n,r,i)=>Z(e,t,n,o=>{let s=typeof i=="function"?i(o?$(o,r):void 0):i,f=o?{...o}:{};return I(f,r,s),f});function ut(e,t){return Object.assign(e,t)}function Q(e,t,n,r){let i=$(e,t);if(!Array.isArray(i))throw new Error(`Target at ${t} is not an array`);let a=[],o=[...i];return i.forEach((s,f)=>{n(s,f)&&a.push(f)}),W(e,t,a,r)}function ve(e,t,n,r){let i=e;if(!Array.isArray(i))throw new Error(`Target at ${n} is not an array`);let a=[],o=[...i];return i.forEach((s,f)=>{s=I(s,n,r,{createMissing:!0})}),i}function se(e,t,n,r,i){return Q(e,t,n,o=>{let s=typeof i=="function"?i(o?$(o,r):void 0):i,f=o?{...o}:{};return I(f,r,s),f})}function Pe(e,t,n){let r=$(e,t,{createMissing:!0}),i=k(r||{},n,!0);return I(e,t,i,{createMissing:!0})}function ie(e,t,n){if(typeof n=="object"&&!Array.isArray(n)&&n!==null)for(let[r,i]of Object.entries(n))t.includes(r)&&I(e,r,i,{createMissing:!0});else for(let r of t){let i=oe(r,$(e,r),n);I(e,r,i,{createMissing:!0})}return e}function $(e,t,n={}){let{createMissing:r=!1,add:i=!1,debug:a=!1}=n,o=v(t),s=e,f=!1,l="",y=t;if(typeof s=="object"&&!Array.isArray(s)&&y.length>0&&s[y])return s=s[y],s;for(let c=0;c<o.length;c++){let u=o[c],d=o.length>1?c===o.length-1:!1;if(s===null&&!r)return;let p=o[c+1];switch(u.type){case"property":if(s&&typeof s=="object")if(Array.isArray(s)){if(typeof u.value=="number"){let A=u.value;if(r&&A>=s.length)for(let g=s.length;g<=A;g++)s.push(void 0);if(r&&(s[A]===null||s[A]===void 0)&&(!d||f)&&(p?s[A]=p.type==="index"||p.type==="slice"||p.type==="union"||p.type==="wildcard"?[]:{}:s[A]=null,f=!1),s=s[A],p){f=!0;continue}}else i?s.push({[String(u.value)]:null}):s=s.map(A=>A[String(u.value)]);if(p)continue;return be(s)}else{if(s[l]&&l.split(".").length>=2){s=s[l];continue}if(r&&(s[String(u.value)]===null||s[String(u.value)]===void 0)&&(!d||f)&&(p?s[String(u.value)]=p.type==="index"||p.type==="slice"||p.type==="union"||p.type==="wildcard"?[]:{}:s[String(u.value)]=null,f=!1,s=s[String(u.value)],p)){f=!0;continue}if(s?(!s[String(u.value)]&&r&&(s[String(u.value)]=null),s=s[String(u.value)]):s={[String(u.value)]:null},p)continue}else if(r)s={},s[String(u.value)]=p.type==="index"||p.type==="slice"||p.type==="union"||p.type==="wildcard"||n.type==="array"?[]:{},s=s[String(u.value)];else return;break;case"index":case"wildcard":if(Array.isArray(s)){if(u.type==="index"){let A=u.value;if(A<0){s=s[s.length-Math.abs(A)];continue}if(r&&(A>=s.length||s[A]==null||s[A]==null)&&!d){for(let g=s.length;g<=A;g++)s.push(void 0);p&&(s[A]=p.type==="index"||p.type==="slice"||p.type==="union"||p.type==="wildcard"?[]:{})}s=s[A]}else if(!d)continue}else if(r){let A=o[c+1];s=[];let g=u.value;for(let m=0;m<=g;m++)s.push(void 0);s[g]=A.type==="index"||A.type==="slice"||A.type==="union"||A.type==="wildcard"||n.type==="array"?[]:{},s=s[g]}else{if(u.type==="index"&&typeof s=="object"&&s&&s[String(u.value)]!==void 0){s=s[String(u.value)];continue}if(typeof s=="object"){s=Object.values(s);continue}return}break;default:return}}return be(s)}function be(e){return Array.isArray(e)?e.filter(t=>typeof t<"u"):e}var I=(e,t,n,r={})=>{if(t==="[*]")return k(e,n,!0);let{createMissing:i=!0}=r,a=v(t);if(a.length===0)return e;let o=a[a.length-1],s=e;for(let f=0;f<a.length-1;f++){let l=a[f];if(s==null){if(!i)return e;throw new Error(`Cannot navigate through null/undefined at segment ${f}`)}let y=a[f+1],c=y.type==="index"||y.type==="slice"||y.type==="union"||y.type==="wildcard";if(l.type==="property"){if(Array.isArray(s)&&typeof typeof l.value=="number"){let u=l.value;if(u<0)throw new Error(`Negative index ${u} not allowed`);if(u>=s.length&&i)for(let d=s.length;d<=u;d++)s.push(void 0);(s[u]===null||s[u]===void 0)&&i&&(s[u]=c?[]:{}),s=s[u]}else(s[String(l.value)]===null||s[String(l.value)]===void 0)&&i&&(s[String(l.value)]=c?[]:{}),typeof s=="object"&&!Array.isArray(s)&&(s=s[String(l.value)]);if(y)continue;break}else if(l.type==="index"||l.type==="wildcard"){if(!Array.isArray(s)){if(!i)return e;s=[]}if(l.type==="index"){let u=l.value;if(u<0)throw new Error(`Negative index ${u} not allowed`);if(u>=s.length&&i)for(let d=s.length;d<=u;d++)s.push(void 0);(s[u]===null||s[u]===void 0)&&i&&(s[u]=c?[]:void 0),s=s[u]}if(y)continue;break}else throw new Error(`Invalid segment type for set: ${l.type}`)}switch(o.type){case"property":if(Array.isArray(s)&&typeof o.value=="number"){let l=o.value;if(l<0)throw new Error(`Negative index ${l} not allowed`);if(l>=s.length)for(let y=s.length;y<=l;y++)s.push(void 0);Array.isArray(n)?s[l]=k(structuredClone(s[l]),n[l],!0):s[l]=n}else if(s||(s={}),Array.isArray(s)){let l=t.split("."),y;if(l.length>=2){l.shift();for(let c=0;c<s.length;c++)Array.isArray(n)?(y=ae(l.join("."),n[c]),s[c]=typeof s[c]=="object"?k(s[c],y,!0):y):(y=ae(l.join("."),n),s[c]=typeof s[c]=="object"?k(s[c],y,!0):y)}else y={[String(o.value)]:n}}else Array.isArray(n)&&n.length===0?typeof s[String(o.value)]>"u"?s[String(o.value)]=[]:s[String(o.value)]=[]:s[String(o.value)]=n;break;case"index":case"wildcard":if(!Array.isArray(s))throw new Error("Cannot use index on non-array for final segment");let f=o.value;if(o.type==="index"){if(f<0)throw new Error(`Negative index ${f} not allowed`);if(f>=s.length)for(let l=s.length;l<=f;l++)s.push(void 0);if(Array.isArray(n))if(typeof s[f]==typeof n[f])try{s[f]=k(K(s[f]),n[f],!0)}catch{throw Error("expected on array")}else s[f]=n;else s[f]=n}else if(o.type==="wildcard")if(Array.isArray(n))for(let l=0;l<s.length;l++)typeof s[l]=="object"?s[l]=k(s[l],n[l],!0):s[l]=n[l];else for(let l=0;l<s.length;l++)typeof s[l]=="object"?s[l]=k(s[l],n,!0):s[l]=n;break;default:throw new Error(`Invalid final segment type for set: ${o.type}`)}return e};var ae=(e,t)=>{let n=v(e);if(n.length===0)return t;let r=t;for(let i=n.length-1;i>=0;i--){let a=n[i];if(a.type==="property")r={[String(a.value)]:r};else if(a.type==="index"){let o=parseInt(String(a.value)),s=[];s[o]=r,r=s}else a.type==="wildcard"?Array.isArray(r)?r=r:r=[r]:a.type==="slice"?Array.isArray(r)||(r=[r]):a.type==="union"?Array.isArray(r)||(r=[r]):a.type==="filter"?Array.isArray(r)||(r=[r]):a.type==="descendant"&&(r=r)}return r},dt=(e,t,n={})=>P(e,t,{...n,preserveStructure:!1}),Re=(e,t,n={})=>{if(Array.isArray(t))return t.map(i=>Re(e,i,n));let r=P(e,t,{...n,preserveStructure:!1});return Array.isArray(r)?r.length>0?r.every(i=>i!==void 0):!1:r!==void 0},N=(e,t,n={})=>{let{throwOnError:r=!1}=n;if(Array.isArray(t))return t.map(s=>N(e,s,n));let i=v(t,{throwOnError:r});if(i.length===0)return!1;let a=i[i.length-1];if(i.some(s=>s.type==="wildcard"||s.type==="slice"||s.type==="union")){let s=G(e,t,n),f=!1;for(let l of s)pt(e,l)&&(f=!0);return f}let o=e;for(let s=0;s<i.length-1;s++){let f=i[s];if(o==null)return!1;switch(f.type){case"property":o=o[String(f.value)];break;case"index":if(!Array.isArray(o))return!1;let l=f.value;if(l<0||l>=o.length)return!1;o=o[l];break;default:return!1}}if(o==null)return!1;switch(a.type){case"property":if(String(a.value)in o)return delete o[String(a.value)],!0;break;case"index":if(!Array.isArray(o))return!1;let s=a.value;if(s>=0&&s<o.length)return o.splice(s,1),!0;break}return!1},pt=(e,t)=>{let n=v(t);if(n.length===0)return!1;let r=n[n.length-1],i=e;for(let a=0;a<n.length-1;a++){let o=n[a];if(i==null)return!1;switch(o.type){case"property":i=i[String(o.value)];break;case"index":if(!Array.isArray(i))return!1;let s=o.value;if(s<0||s>=i.length)return!1;i=i[s];break;default:return!1}}if(i==null)return!1;switch(r.type){case"property":if(String(r.value)in i)return delete i[String(r.value)],!0;break;case"index":if(!Array.isArray(i))return!1;let a=r.value;if(a>=0&&a<i.length)return i.splice(a,1),!0;break}return!1},G=(e,t,n={})=>{let{format:r="selector",maxDepth:i=20}=n;if(t&&Array.isArray(t)){let s=[];for(let f of t){let l=G(e,f,n);s.push(...l)}return s}if(t){let s=v(t,n);return F(e,s,0,i).map(l=>r==="selector"?l.path:l.stringPath)}let a=[],o=(s,f=[],l=[],y=0)=>{if(!(y>i)){if(f.length>0){let c=E(f);a.push({path:c,stringPath:l.join(".")})}if(s&&typeof s=="object")if(Array.isArray(s))for(let c=0;c<s.length;c++)o(s[c],[...f,{type:"index",value:c,raw:`[${c}]`}],[...l,`[${c}]`],y+1);else for(let c in s)s.hasOwnProperty(c)&&o(s[c],[...f,{type:"property",value:c,raw:c}],[...l,c],y+1)}};return o(e),a.map(s=>r==="selector"?s.path:s.stringPath)},gt=(e,t)=>G(e).filter(r=>{let i=P(e,r);return t(r,i)}),Fe=(e,t=".")=>{let n={},r=(i,a=[])=>{if(i==null)return;let o=R(i,a,0,20);if(o!==void 0){n[E(a)]=o;return}if(Array.isArray(i))for(let s=0;s<i.length;s++){let f=[...a,{type:"index",value:s,raw:`[${s}]`}];r(i[s],f)}else if(T(i)){for(let f in i)if(i.hasOwnProperty(f)){let l=[...a,{type:"property",value:f,raw:f}];r(i[f],l)}let s=Object.getOwnPropertySymbols(i);for(let f of s){let l=[...a,{type:"property",value:f.toString(),raw:f.toString()}];r(i[f],l)}}else{let s=E(a);n[s]=i}};return r(e),n},At=e=>{let t={};for(let n in e)_(t,n,e[n]);return t},Tt=(...e)=>{let t={};for(let n of e){if(!T(n))continue;let r=Fe(n);for(let i in r)_(t,i,r[i])}return t};function F(e,t,n,r,i=[],a=[]){if(n>r)return[];if(t.length===0)return[{value:e,path:E(i),stringPath:a.join(".")}];let o=t[0],s=t.slice(1),f=[];if(o.type==="descendant"){if(f.push(...F(e,s,n+1,r,i,a)),e&&typeof e=="object")if(Array.isArray(e))for(let l=0;l<e.length;l++)f.push(...F(e[l],t,n+1,r,[...i,{type:"index",value:l,raw:`[${l}]`}],[...a,`[${l}]`]));else for(let l in e)e.hasOwnProperty(l)&&f.push(...F(e[l],t,n+1,r,[...i,{type:"property",value:l,raw:l}],[...a,l]));return f}switch(o.type){case"property":if(e&&typeof e=="object"&&String(o.value)in e){let l=e[String(o.value)];f.push(...F(l,s,n+1,r,[...i,o],[...a,String(o.value)]))}break;case"index":if(Array.isArray(e)){let l=o.value;l>=0&&l<e.length&&f.push(...F(e[l],s,n+1,r,[...i,o],[...a,`[${l}]`]))}else if(e&&typeof e=="object"){let l=String(o.value);l in e&&f.push(...F(e[l],s,n+1,r,[...i,{...o,type:"property"}],[...a,l]))}break;case"wildcard":if(e&&typeof e=="object")if(Array.isArray(e))for(let l=0;l<e.length;l++)f.push(...F(e[l],s,n+1,r,[...i,{type:"index",value:l,raw:`[${l}]`}],[...a,`[${l}]`]));else for(let l in e)e.hasOwnProperty(l)&&f.push(...F(e[l],s,n+1,r,[...i,{type:"property",value:l,raw:l}],[...a,l]));break;case"slice":if(Array.isArray(e)){let[l,y]=o.value,c=Math.max(0,l),u=Math.min(e.length,y===1/0?e.length:y);for(let d=c;d<u;d++)f.push(...F(e[d],s,n+1,r,[...i,{type:"index",value:d,raw:`[${d}]`}],[...a,`[${d}]`]))}break;case"union":if(Array.isArray(e)){let l=o.value;for(let y of l)y>=0&&y<e.length&&f.push(...F(e[y],s,n+1,r,[...i,{type:"index",value:y,raw:`[${y}]`}],[...a,`[${y}]`]))}break;case"filter":if(Array.isArray(e)){let l=o.value;for(let y=0;y<e.length;y++)l(e[y])&&f.push(...F(e[y],s,n+1,r,[...i,{type:"index",value:y,raw:`[${y}]`}],[...a,`[${y}]`]))}break}return f}var mt=(e,t,n)=>{let r=P(e,t),i=n(r);return V(e,t,i)},Ie=(e,t)=>{let n=t(e);return k(e,n,!0)},xt=(e,t)=>{let n=P(e,t);if(n!==void 0)return n===null?"null":Array.isArray(n)?"array":typeof n};function ht(e,t){if(!T(e))return{};let n={...e};for(let r of t)delete n[r];return n}function le(e,t){if(e==null||typeof e!="object")return e;if(Array.isArray(e))return e.map(r=>le(r,t));let n={};for(let[r,i]of Object.entries(e))t.includes(r)||(n[r]=le(i,t));return n}function St(e){return t=>{if(!T(t))return{};let n={...t};for(let r of e)r in n&&delete n[r];return n}}function $e(e,t){if(!T(e))return{};let n={};for(let r in e)Object.prototype.hasOwnProperty.call(e,r)&&!t(e[r],r,e)&&(n[r]=e[r]);return n}function Ot(e){return $e(e,t=>t==null)}function Kt(e,t){if(!T(e))return{};let n={};for(let r of t)r in e&&(n[r]=e[r]);return n}function bt(e,t){if(!T(e))return{};let n={};for(let r in e)Object.prototype.hasOwnProperty.call(e,r)&&t(e[r],r,e)&&(n[r]=e[r]);return n}function kt(e,t,n){if(!T(e))return{};let r=JSON.parse(JSON.stringify(e)),i=Array.isArray(t)?t:[t];for(let o=0;o<i.length;o++){let s=i[o],f=i[o+1];if(!s||s==="")continue;let l=v(s);if(s.includes("*")||s.includes("[")||l.length>1){let u=function(d,p=""){if(T(d)){if(Array.isArray(d)){d.forEach((A,g)=>{u(A,p?`${p}[${g}]`:`[${g}]`)});return}for(let A in d)if(Object.prototype.hasOwnProperty.call(d,A)){let g=p?`${p}.${A}`:A;c.push({fullPath:g,segments:v(g)}),u(d[A],g)}}};var a=u;let y=P(r,s,{preserveStructure:!0});if(y===void 0)continue;let c=[];u(y);for(let{fullPath:d,segments:p}of c){let A=[...p],g=A.pop();if(!g)continue;let m=A.map(O=>O.type==="index"?`[${O.value}]`:O.value).join(".").replace(/\.\[/g,"["),x=P(r,m.length>0?m:d,{preserveStructure:!0}),h=m?x:r;if(!h)continue;let S=String(g.value),b=h[S];if(n){let O=n(b,d,r);if(O===!0)if(g.type==="index"&&Array.isArray(h)){let Y=g.value;Y>=0&&Y<h.length&&h.splice(Y,1)}else N(r,i[o]);else O!==!1&&(h[S]=O)}else if(g.type==="index"&&Array.isArray(h)){let O=g.value;O>=0&&O<h.length&&h.splice(O,1)}else N(r,i[o])}}else{let y=s;if(typeof y=="string"&&y in r)if(n){let c=n(r[y],s,r);c===!0?delete r[y]:c!==!1&&(r[y]=c)}else N(r,i[o])}}return r}function wt(e,t,n){if(!T(e))return{};if(Array.isArray(t)){let a={};for(let o of t){let s=P(e,o,{preserveStructure:!0});if(s!==void 0)if(n){let f=(y,c)=>{if(T(y)){if(Array.isArray(y))return y.map((d,p)=>f(d,`${c}[${p}]`));let u={};for(let d in y)if(Object.prototype.hasOwnProperty.call(y,d)){let p=c?`${c}.${d}`:d;u[d]=f(y[d],p)}return u}return n(y,c,e)},l=f(s,o);J(a,l)}else Object.assign(a,J(a,s))}return a}let r=Array.isArray(t)?t[0]:t,i=P(e,r,{preserveStructure:!0});if(i===void 0)return{};if(n){let a=(o,s)=>{if(T(o)){if(Array.isArray(o))return o.map((l,y)=>a(l,`${s}[${y}]`));let f={};for(let l in o)if(Object.prototype.hasOwnProperty.call(o,l)){let y=s?`${s}.${l}`:l;f[l]=a(o[l],y)}return f}return n(o,s,e)};return a(i,r)}return i}function H(e,t){if(!T(e))return e;if(Array.isArray(e))return e.map(r=>H(r,t));let n={};for(let r in e){if(!Object.prototype.hasOwnProperty.call(e,r))continue;let i=e[r];t(i,r,e)||(T(i)?n[r]=H(i,t):Array.isArray(i)?n[r]=i.map(a=>H(a,t)):n[r]=i)}return n}function vt(e,t){if(!T(e))return{};let n={};for(let r in e)Object.prototype.hasOwnProperty.call(e,r)&&(n[r]=t(e[r],r,e));return n}function Pt(e,t){if(!T(e))return{};let n={};for(let r in e)if(Object.prototype.hasOwnProperty.call(e,r)){let i=t(r,e[r],e);n[i]=e[r]}return n}function fe(e,t={deep:!0}){if(!T(e))return e;if(Array.isArray(e))return t.deep?e.map(r=>fe(r,t)):e;let n={};for(let r in e)if(Object.prototype.hasOwnProperty.call(e,r)){let i=e[r],a=r.replace(/[-_]([a-z])/g,(o,s)=>s.toUpperCase()).replace(/^([A-Z])/,o=>o.toLowerCase());n[a]=t.deep&&T(i)?fe(i,t):i}return n}function ye(e,t={deep:!0}){if(!T(e))return e;if(Array.isArray(e))return t.deep?e.map(r=>ye(r,t)):e;let n={};for(let r in e)if(Object.prototype.hasOwnProperty.call(e,r)){let i=e[r],a=r.replace(/[-]([a-z])/g,(o,s)=>`_${s}`).replace(/([a-z0-9])([A-Z])/g,"$1_$2").toLowerCase();n[a]=t.deep&&T(i)?ye(i,t):i}return n}function ce(e,t={deep:!0}){if(!T(e))return e;if(Array.isArray(e))return t.deep?e.map(r=>ce(r,t)):e;let n={};for(let r in e)if(Object.prototype.hasOwnProperty.call(e,r)){let i=e[r],a=r.replace(/[_]([a-z])/g,(o,s)=>`-${s}`).replace(/([a-z0-9])([A-Z])/g,"$1-$2").toLowerCase();n[a]=t.deep&&T(i)?ce(i,t):i}return n}var Ve=(e,t={})=>{let{delimiter:n=".",includeArrays:r=!1,maxDepth:i=9}=t;if(Array.isArray(e))return e.map(s=>Ve(s,t));let a={},o=(s,f="",l=0)=>{if(l>i){a[f]=s;return}if(!T(s)&&!Array.isArray(s)){f&&(a[f]=s);return}if(Array.isArray(s))for(let y=0;y<s.length;y++){let c=f?`${f}[${y}]`:`[${y}]`;o(s[y],c,l+1)}else{for(let c in s){let u=f?`${f}${n}${c}`:c;o(s[c],u,l+1)}let y=Object.getOwnPropertySymbols(s);for(let c of y){let u=f?`${f}${n}${c.toString()}`:c.toString();o(s[c],u,l+1)}}};return o(e),a};function Ce(e,t={}){let{delimiter:n=".",detectArrays:r=!0}=t,i={};for(let a in e)if(Object.prototype.hasOwnProperty.call(e,a)){let o=e[a],s=a.split(n),f=i;for(let y=0;y<s.length-1;y++){let c=s[y],u=s[y+1],d=r&&/^\d+$/.test(u);if(!(c in f))f[c]=d?[]:{};else if(d&&!Array.isArray(f[c])){let p={...f[c]};f[c]=[],Object.assign(f[c],p)}f=f[c]}let l=s[s.length-1];f[l]=o}if(r){let a=o=>{if(!T(o))return o;if(Array.isArray(o))return o.map(a);let s=Object.keys(o);if(s.length>0&&s.every((y,c)=>String(c)===y))return Object.values(o).map(a);let l={};for(let y in o)Object.prototype.hasOwnProperty.call(o,y)&&(l[y]=a(o[y]));return l};return a(i)}return i}function Rt(e,...t){if(!T(e)||t.length===0)return e;let n={...e};for(let r in n)if(Object.prototype.hasOwnProperty.call(n,r)){let i=n[r];for(let a of t)i=a(i);n[r]=i}return n}function Ft(e,t,n={}){if(!T(e))return e;let{removeUnmapped:r=!1}=n,i={};for(let a in e)Object.prototype.hasOwnProperty.call(e,a)&&(a in t?i[t[a]]=e[a]:r||(i[a]=e[a]));return i}function It(e,t={}){let{transformObject:n=s=>s,transformArray:r=s=>s,transformValue:i=s=>s,maxDepth:a=1/0}=t;function o(s,f=[],l=0){if(l>a)return s;if(Array.isArray(s)){let c=s.map((u,d)=>o(u,[...f,d.toString()],l+1));return r(c,f)}if(T(s)){let c={};for(let u in s)if(Object.prototype.hasOwnProperty.call(s,u)){let d=[...f,u];c[u]=o(s[u],d,l+1),!Array.isArray(s[u])&&!T(s[u])&&(c[u]=i(c[u],u,d))}return n(c,f)}let y=f.length>0?f[f.length-1]:"";return i(s,y,f)}return o(e)}function $t(e,t={}){return Ce(e,t)}function Vt(e,t,n,r={}){if(!T(e))return e;let i=JSON.parse(JSON.stringify(e)),{transformAll:a=!1}=r,o=Array.isArray(t)?t:[t];for(let l of o)if(l.includes("*")||l.includes("[")){let c=function(d,p=""){if(T(d)){if(Array.isArray(d)){d.forEach((A,g)=>{let m=p?`${p}[${g}]`:`[${g}]`;c(A,m)});return}for(let A in d)if(Object.prototype.hasOwnProperty.call(d,A)){let g=d[A],m=p?`${p}.${A}`:A;if(g===null||typeof g!="object"){let x=p?P(i,p):i,h=a?n(g,m,x):n(g,A,x);d[A]=h}else c(g,m)}}};var s=c;let y=P(i,l,{preserveStructure:!0});if(y===void 0)continue;c(y);let u=v(l);if(u.length>0){let d=u[0];d.type==="property"&&(i[String(d.value)]=y[String(d.value)])}}else{let c=function(u,d,p,A){if(p>=d.length)return u;let g=d[p];if(p===d.length-1){let x=p>0?c(u,d,p-1,d.slice(0,p).join(".")):u,h=a?n(x[g],A,x):n(x[g],g,x);return x[g]=h,x}let m=u[g]||{};return u[g]=c(m,d,p+1,A),u};var f=c;let y=l.split(".");c(i,y,0,l)}return i}function Ct(e,t,n,r={}){if(!T(e))return[];let{flatten:i=!0}=r,o=(Array.isArray(t)?t:[t]).map(s=>{let f=P(e,s);return f===void 0?[]:Array.isArray(f)?f.map((l,y)=>{let c=`${s}[${y}]`;return n(l,c,e)}):[n(f,s,e)]});return i?o.flat():o}function Ut(e,t,n,r={}){if(!T(e))return e;let i=JSON.parse(JSON.stringify(e)),{recursive:a=!1}=r,o=Array.isArray(t)?t:[t];for(let s of o){let f=s?P(i,s):i;if(!T(f))continue;let l=(y,c=s)=>{if(!T(y))return y;if(Array.isArray(y))return y.map((d,p)=>T(d)&&!Array.isArray(d)?l(d,`${c}[${p}]`):d);let u={};for(let d in y)if(Object.prototype.hasOwnProperty.call(y,d)){let p=n(d,y[d],c);a&&T(y[d])?u[p]=l(y[d],`${c}.${p}`):u[p]=y[d]}return u};if(Array.isArray(f))for(let y=0;y<f.length;y++)T(f[y])&&!Array.isArray(f[y])&&(f[y]=l(f[y],`${s}[${y}]`));else{let y=l(f);s?_(i,s,y):(Object.keys(i).forEach(c=>{delete i[c]}),Object.assign(i,y))}}return i}function Ue(e){if(e==null||typeof e!="object")return!1;let t=Object.getPrototypeOf(e);return t===Object.prototype||t===null}function M(e,t,n){return e===t?t:Array.isArray(e)?Et(e,Array.isArray(t)?t:[],n):Ue(e)?Nt(e,Ue(t)?t:{},n):e}function Et(e,t,n){let{key:r,keepRef:i}=n,a=t.length,o=e.length,s=new Array(a);for(let c=0;c<a;c++)s[c]=t[c];if(r===!1){if(i){for(let d=0;d<o;d++){let p=M(e[d],s[d],n);p!==s[d]&&(t[d]=p)}return a>o&&t.splice(o),t}let c=o!==a,u=new Array(o);for(let d=0;d<o;d++)u[d]=M(e[d],s[d],n),u[d]!==s[d]&&(c=!0);return c?u:t}let f=new Map;for(let c=0;c<a;c++){let u=s[c];u!=null&&typeof u=="object"&&r in u&&f.set(u[r],u)}let l=new Array(o);for(let c=0;c<o;c++){let u=e[c];u!=null&&typeof u=="object"&&r in u?l[c]=M(u,f.get(u[r]),n):l[c]=M(u,s[c],n)}if(i){for(let c=0;c<o;c++)l[c]!==s[c]&&(t[c]=l[c]);return a>o&&t.splice(o),t}let y=o!==a;for(let c=0;!y&&c<o;c++)l[c]!==s[c]&&(y=!0);return y?l:t}function Nt(e,t,n){let{merge:r,keepRef:i}=n,a=Object.keys(e),o=Object.keys(t),s=Object.create(null);for(let y=0;y<o.length;y++)s[o[y]]=t[o[y]];let f=null,l=()=>(f||(f=i?t:{...s}),f);if(!r)for(let y=0;y<o.length;y++){let c=o[y];c in e||delete l()[c]}for(let y=0;y<a.length;y++){let c=a[y],u=s[c],d=M(e[c],u,n);(d!==u||!(c in s))&&(l()[c]=d)}return f??t}function Mt(e,t){let n={key:t?.key!==void 0?t.key:"id",merge:t?.merge??!1,keepRef:t?.keepRef??!1};return r=>r==null?e:M(e,r,n)}function Ee(e,t){if(e==null)return;let n=e;for(let r of t.split(".")){if(n==null)return;n=n[r]}return n}function Ne(e,t,n,r){let i=e==null,a=t==null;if(i&&a)return 0;if(i)return 1;if(a)return-1;let o=typeof e=="number"&&Number.isNaN(e),s=typeof t=="number"&&Number.isNaN(t);return o&&s?0:o?1:s?-1:e instanceof Date&&t instanceof Date?e.getTime()-t.getTime():typeof e=="boolean"&&typeof t=="boolean"?e===t?0:e?1:-1:typeof e=="number"&&typeof t=="number"?e-t:typeof e=="string"&&typeof t=="string"?e.localeCompare(t,n,r):String(e).localeCompare(String(t),n,r)}function B(e,t){return(Array.isArray(e)?e[t]??"asc":e??"asc")==="desc"?-1:1}function jt(e,t){let{order:n,locale:r,localeOptions:i,nullsLast:a=!0}=t,o=B(n,0);return e.slice().sort((s,f)=>{let l=s==null,y=f==null;return l&&y?0:l?a?1:-1:y?a?-1:1:Ne(s,f,r,i)*o})}function Dt(e,t,n){let{order:r,locale:i,localeOptions:a,nullsLast:o=!0,comparator:s}=n;return s?e.slice().sort(s):e.slice().sort((f,l)=>{for(let y=0;y<t.length;y++){let c=Ee(f,t[y]),u=Ee(l,t[y]),d=c==null,p=u==null;if(d&&p)continue;if(d)return o?1:-1;if(p)return o?-1:1;let A=Ne(c,u,i,a);if(A!==0)return A*B(r,y)}return 0})}function Me(e,t){let{order:n,priorityKeys:r=[],locale:i,localeOptions:a,deep:o}=t,s=B(n,0),f=new Set(r),l=Object.keys(e).filter(u=>!f.has(u));l.sort((u,d)=>u.localeCompare(d,i,a)*s);let y=[...r.filter(u=>u in e),...l],c={};for(let u of y)c[u]=o?ue(e[u],t):e[u];return c}function ue(e,t){if(Array.isArray(e)){let n=e.map(r=>ue(r,t));return je(n,t)}return w(e)?Me(e,t):e}function Wt(e,t){let n=e.length,r=new Set,i=[];for(let a of t)a>=0&&a<n&&!r.has(a)&&(i.push(e[a]),r.add(a));for(let a=0;a<n;a++)r.has(a)||i.push(e[a]);return i}function je(e,t){let{by:n,comparator:r,indices:i,order:a}=t;if(i){let s=Wt(e,i);return B(a,0)===-1?s.reverse():s}if(r)return e.slice().sort(r);if(n){let s=Array.isArray(n)?n:[n];return Dt(e,s,t)}let o=e.find(s=>s!=null);return o!=null&&w(o)?B(a,0)===-1?e.slice().reverse():e.slice():jt(e,t)}function X(e,t={}){let{deep:n}=t;if(Array.isArray(e)){let r=n?e.map(i=>ue(i,t)):e;return je(r,t)}return w(e)?Me(e,t):e}function _t(e,t,n="asc",r={}){return X(e,{...r,by:t,order:n})}function Bt(e,t={}){return X(e,t)}function Lt(e,t,n){let r=e.length;if(r===0)return e.slice();let i=Math.max(0,Math.min(r-1,t)),a=Math.max(0,Math.min(r-1,n));if(i===a)return e.slice();let o=e.slice(),[s]=o.splice(i,1);return o.splice(a,0,s),o}function zt(e,t,n){let r=e.length;if(t<0||t>=r)throw new RangeError(`swap: index ${t} out of range`);if(n<0||n>=r)throw new RangeError(`swap: index ${n} out of range`);if(t===n)return e.slice();let i=e.slice();return i[t]=e[n],i[n]=e[t],i}function Jt(e,t,n="asc"){return X(e,{indices:t,order:n})}
|
|
1
|
+
/*! @fluixi/utils v1.0.0-alpha.84 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/lib/object/index.ts
|
|
22
|
+
var index_exports = {};
|
|
23
|
+
__export(index_exports, {
|
|
24
|
+
ValueOfKey: () => ValueOfKey,
|
|
25
|
+
applyDiff: () => applyDiff,
|
|
26
|
+
camelCaseKeys: () => camelCaseKeys,
|
|
27
|
+
castProps: () => castProps,
|
|
28
|
+
clean: () => clean,
|
|
29
|
+
createFilterFunction: () => createFilterFunction,
|
|
30
|
+
createOmitter: () => createOmitter,
|
|
31
|
+
createSelector: () => createSelector,
|
|
32
|
+
deepClone: () => deepClone,
|
|
33
|
+
deepFreeze: () => deepFreeze,
|
|
34
|
+
deepMerge: () => deepMergeKeepReference,
|
|
35
|
+
deepMergeKeepReference: () => deepMergeKeepReference,
|
|
36
|
+
deepMergeStrict: () => deepMergeStrict,
|
|
37
|
+
deepMergeWithArrays: () => deepMergeWithArrays,
|
|
38
|
+
deepMergeWithOptions: () => deepMergeWithOptions,
|
|
39
|
+
deepOmit: () => deepOmit,
|
|
40
|
+
deepOmitBy: () => deepOmitBy,
|
|
41
|
+
deepTransform: () => deepTransform,
|
|
42
|
+
defaults: () => defaults,
|
|
43
|
+
deletePath: () => deletePath,
|
|
44
|
+
detectSelectorType: () => detectSelectorType,
|
|
45
|
+
diffObject: () => diffObject,
|
|
46
|
+
diffObjectDetailed: () => diffObjectDetailed,
|
|
47
|
+
filter: () => filter,
|
|
48
|
+
findPaths: () => findPaths,
|
|
49
|
+
flatten: () => flatten,
|
|
50
|
+
flattenWithArrays: () => flattenWithArrays,
|
|
51
|
+
fromPath: () => fromPath,
|
|
52
|
+
fromQueryString: () => fromQueryString,
|
|
53
|
+
get: () => get,
|
|
54
|
+
getChangedPaths: () => getChangedPaths,
|
|
55
|
+
getPaths: () => getPaths,
|
|
56
|
+
getTypeAtPath: () => getTypeAtPath,
|
|
57
|
+
groupBy: () => groupBy,
|
|
58
|
+
has: () => has,
|
|
59
|
+
hasObjectChanges: () => hasObjectChanges,
|
|
60
|
+
invert: () => invert,
|
|
61
|
+
isClass: () => isClass,
|
|
62
|
+
isEmptyObject: () => isEmptyObject,
|
|
63
|
+
isNonNullObject: () => isNonNullObject,
|
|
64
|
+
isObject: () => isObject,
|
|
65
|
+
isPlainObject: () => isPlainObject,
|
|
66
|
+
isRangeObject: () => isRangeObject,
|
|
67
|
+
kebabCaseKeys: () => kebabCaseKeys,
|
|
68
|
+
mapKeys: () => mapKeys,
|
|
69
|
+
mapPath: () => mapPath,
|
|
70
|
+
mapValues: () => mapValues,
|
|
71
|
+
merge: () => merge,
|
|
72
|
+
mergeArrays: () => mergeArrays,
|
|
73
|
+
mergeIntoStructure: () => mergeIntoStructure,
|
|
74
|
+
mergeStrict: () => deepMergeStrict,
|
|
75
|
+
mergeUpdateAtPath: () => mergeUpdateAtPath,
|
|
76
|
+
mergeWithOptions: () => deepMergeWithOptions,
|
|
77
|
+
move: () => move,
|
|
78
|
+
omit: () => omit,
|
|
79
|
+
omitBy: () => omitBy,
|
|
80
|
+
omitNil: () => omitNil,
|
|
81
|
+
omitPath: () => omitPath,
|
|
82
|
+
parseSelector: () => parseSelector,
|
|
83
|
+
pick: () => pick,
|
|
84
|
+
pickBy: () => pickBy,
|
|
85
|
+
pickPath: () => pickPath,
|
|
86
|
+
reconcile: () => reconcile,
|
|
87
|
+
renameKeys: () => renameKeys,
|
|
88
|
+
reorder: () => reorder,
|
|
89
|
+
select: () => select,
|
|
90
|
+
set: () => set,
|
|
91
|
+
setArrayValueWithFilter: () => setArrayValueWithFilter,
|
|
92
|
+
setAtPath: () => setAtPath,
|
|
93
|
+
setValueAtIndex: () => setValueAtIndex,
|
|
94
|
+
setValueAtPath: () => setValueAtPath,
|
|
95
|
+
setValueBetweenRange: () => setValueBetweenRange,
|
|
96
|
+
setValueBetweenRangeWithSubPath: () => setValueBetweenRangeWithSubPath,
|
|
97
|
+
setValueWithArraySelectors: () => setValueWithArraySelectors,
|
|
98
|
+
setValueWithFilter: () => setValueWithFilter,
|
|
99
|
+
setValueWithFilterWithSubPath: () => setValueWithFilterWithSubPath,
|
|
100
|
+
size: () => size,
|
|
101
|
+
snakeCaseKeys: () => snakeCaseKeys,
|
|
102
|
+
sort: () => sort,
|
|
103
|
+
sortBy: () => sortBy,
|
|
104
|
+
sortKeys: () => sortKeys,
|
|
105
|
+
stringifySelector: () => stringifySelector,
|
|
106
|
+
structureByKeys: () => structureByKeys,
|
|
107
|
+
swap: () => swap,
|
|
108
|
+
toJSONString: () => toJSONString,
|
|
109
|
+
toQueryString: () => toQueryString,
|
|
110
|
+
transform: () => transform,
|
|
111
|
+
transformKeysAtPath: () => transformKeysAtPath,
|
|
112
|
+
transformPath: () => transformPath,
|
|
113
|
+
transformValues: () => transformValues,
|
|
114
|
+
traverseWithPaths: () => traverseWithPaths,
|
|
115
|
+
unflatten: () => unflatten,
|
|
116
|
+
unflattenWithArrays: () => unflattenWithArrays,
|
|
117
|
+
update: () => update,
|
|
118
|
+
updateWithCallBack: () => updateWithCallBack,
|
|
119
|
+
validateSelector: () => validateSelector
|
|
120
|
+
});
|
|
121
|
+
module.exports = __toCommonJS(index_exports);
|
|
122
|
+
|
|
123
|
+
// src/lib/object/utils.ts
|
|
124
|
+
var isObject = (value) => {
|
|
125
|
+
if (value == null) return false;
|
|
126
|
+
if (Array.isArray(value)) return false;
|
|
127
|
+
if (typeof value === "function") return false;
|
|
128
|
+
if (value instanceof Date) return false;
|
|
129
|
+
if (value instanceof RegExp) return false;
|
|
130
|
+
if (value instanceof Map) return false;
|
|
131
|
+
if (value instanceof Set) return false;
|
|
132
|
+
if (value instanceof Error) return false;
|
|
133
|
+
return typeof value === "object";
|
|
134
|
+
};
|
|
135
|
+
function isPlainObject(v) {
|
|
136
|
+
if (typeof v !== "object" || v === null) return false;
|
|
137
|
+
const proto = Object.getPrototypeOf(v);
|
|
138
|
+
return proto === Object.prototype || proto === null;
|
|
139
|
+
}
|
|
140
|
+
function isClass(obj) {
|
|
141
|
+
return typeof obj === "function" && /^class\s/.test(Function.prototype.toString.call(obj));
|
|
142
|
+
}
|
|
143
|
+
var isEmptyObject = (value) => {
|
|
144
|
+
if (!isObject(value)) return false;
|
|
145
|
+
return Object.keys(value).length === 0;
|
|
146
|
+
};
|
|
147
|
+
var traverseStructure = (current, segments, depth, maxDepth) => {
|
|
148
|
+
if (depth > maxDepth) return void 0;
|
|
149
|
+
if (segments.length === 0) return current;
|
|
150
|
+
const segment = segments[0];
|
|
151
|
+
const remaining = segments.slice(1);
|
|
152
|
+
if (segment.type === "descendant") {
|
|
153
|
+
const result = traverseStructure(current, remaining, depth + 1, maxDepth);
|
|
154
|
+
if (result !== void 0) {
|
|
155
|
+
return result;
|
|
156
|
+
}
|
|
157
|
+
if (current && typeof current === "object") {
|
|
158
|
+
if (Array.isArray(current)) {
|
|
159
|
+
const arrResult = [];
|
|
160
|
+
for (let i = 0; i < current.length; i++) {
|
|
161
|
+
const itemResult = traverseStructure(
|
|
162
|
+
current[i],
|
|
163
|
+
segments,
|
|
164
|
+
depth + 1,
|
|
165
|
+
maxDepth
|
|
166
|
+
);
|
|
167
|
+
if (itemResult !== void 0) {
|
|
168
|
+
arrResult[i] = itemResult;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return arrResult.length > 0 ? arrResult : void 0;
|
|
172
|
+
} else {
|
|
173
|
+
const objResult = {};
|
|
174
|
+
for (const key in current) {
|
|
175
|
+
if (current.hasOwnProperty(key)) {
|
|
176
|
+
const itemResult = traverseStructure(
|
|
177
|
+
current[key],
|
|
178
|
+
segments,
|
|
179
|
+
depth + 1,
|
|
180
|
+
maxDepth
|
|
181
|
+
);
|
|
182
|
+
if (itemResult !== void 0) {
|
|
183
|
+
objResult[key] = itemResult;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return Object.keys(objResult).length > 0 ? objResult : void 0;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return void 0;
|
|
191
|
+
}
|
|
192
|
+
switch (segment.type) {
|
|
193
|
+
case "property":
|
|
194
|
+
if (isObject(current) && segment.value && String(segment.value) in current) {
|
|
195
|
+
const next = current[String(segment.value)];
|
|
196
|
+
const result = traverseStructure(next, remaining, depth + 1, maxDepth);
|
|
197
|
+
if (result !== void 0) {
|
|
198
|
+
return { [String(segment.value)]: result };
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
break;
|
|
202
|
+
case "index":
|
|
203
|
+
if (Array.isArray(current)) {
|
|
204
|
+
const index = segment.value;
|
|
205
|
+
if (index >= 0 && index < current.length) {
|
|
206
|
+
const result = traverseStructure(
|
|
207
|
+
current[index],
|
|
208
|
+
remaining,
|
|
209
|
+
depth + 1,
|
|
210
|
+
maxDepth
|
|
211
|
+
);
|
|
212
|
+
if (result !== void 0) {
|
|
213
|
+
const arr = new Array(current.length).filter(
|
|
214
|
+
(a) => a !== void 0
|
|
215
|
+
);
|
|
216
|
+
arr[index] = result;
|
|
217
|
+
return arr;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
} else if (current && typeof current === "object") {
|
|
221
|
+
const key = String(segment.value);
|
|
222
|
+
if (key in current) {
|
|
223
|
+
const result = traverseStructure(
|
|
224
|
+
current[key],
|
|
225
|
+
remaining,
|
|
226
|
+
depth + 1,
|
|
227
|
+
maxDepth
|
|
228
|
+
);
|
|
229
|
+
if (result !== void 0) {
|
|
230
|
+
return { [key]: result };
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
break;
|
|
235
|
+
case "wildcard":
|
|
236
|
+
if (current && typeof current === "object") {
|
|
237
|
+
if (Array.isArray(current)) {
|
|
238
|
+
const arrResult = [];
|
|
239
|
+
let hasResults = false;
|
|
240
|
+
for (let i = 0; i < current.length; i++) {
|
|
241
|
+
const result = traverseStructure(
|
|
242
|
+
current[i],
|
|
243
|
+
remaining,
|
|
244
|
+
depth + 1,
|
|
245
|
+
maxDepth
|
|
246
|
+
);
|
|
247
|
+
if (result !== void 0) {
|
|
248
|
+
arrResult[i] = result;
|
|
249
|
+
hasResults = true;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return hasResults ? arrResult : void 0;
|
|
253
|
+
} else {
|
|
254
|
+
const objResult = {};
|
|
255
|
+
let hasResults = false;
|
|
256
|
+
for (const key in current) {
|
|
257
|
+
if (current.hasOwnProperty(key)) {
|
|
258
|
+
const result = traverseStructure(
|
|
259
|
+
current[key],
|
|
260
|
+
remaining,
|
|
261
|
+
depth + 1,
|
|
262
|
+
maxDepth
|
|
263
|
+
);
|
|
264
|
+
if (result !== void 0) {
|
|
265
|
+
objResult[key] = result;
|
|
266
|
+
hasResults = true;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return hasResults ? objResult : void 0;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
break;
|
|
274
|
+
case "slice":
|
|
275
|
+
if (Array.isArray(current)) {
|
|
276
|
+
const [start, end] = segment.value;
|
|
277
|
+
const actualStart = Math.max(0, start);
|
|
278
|
+
const actualEnd = Math.min(
|
|
279
|
+
current.length,
|
|
280
|
+
end === Infinity ? current.length : end
|
|
281
|
+
);
|
|
282
|
+
const arrResult = [];
|
|
283
|
+
let hasResults = false;
|
|
284
|
+
for (let i = actualStart; i < actualEnd; i++) {
|
|
285
|
+
const result = traverseStructure(
|
|
286
|
+
current[i],
|
|
287
|
+
remaining,
|
|
288
|
+
depth + 1,
|
|
289
|
+
maxDepth
|
|
290
|
+
);
|
|
291
|
+
if (result !== void 0) {
|
|
292
|
+
arrResult[i] = result;
|
|
293
|
+
hasResults = true;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return hasResults ? arrResult : void 0;
|
|
297
|
+
}
|
|
298
|
+
break;
|
|
299
|
+
case "union":
|
|
300
|
+
if (Array.isArray(current)) {
|
|
301
|
+
const indices = segment.value;
|
|
302
|
+
const arrResult = [];
|
|
303
|
+
let hasResults = false;
|
|
304
|
+
for (const index of indices) {
|
|
305
|
+
if (index >= 0 && index < current.length) {
|
|
306
|
+
const result = traverseStructure(
|
|
307
|
+
current[index],
|
|
308
|
+
remaining,
|
|
309
|
+
depth + 1,
|
|
310
|
+
maxDepth
|
|
311
|
+
);
|
|
312
|
+
if (result !== void 0) {
|
|
313
|
+
arrResult[index] = result;
|
|
314
|
+
hasResults = true;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return hasResults ? arrResult : void 0;
|
|
319
|
+
}
|
|
320
|
+
break;
|
|
321
|
+
case "filter":
|
|
322
|
+
if (Array.isArray(current)) {
|
|
323
|
+
const filterFn = segment.value;
|
|
324
|
+
const arrResult = [];
|
|
325
|
+
let hasResults = false;
|
|
326
|
+
for (let i = 0; i < current.length; i++) {
|
|
327
|
+
if (filterFn(current[i])) {
|
|
328
|
+
const result = traverseStructure(
|
|
329
|
+
current[i],
|
|
330
|
+
remaining,
|
|
331
|
+
depth + 1,
|
|
332
|
+
maxDepth
|
|
333
|
+
);
|
|
334
|
+
if (result !== void 0) {
|
|
335
|
+
arrResult[i] = result;
|
|
336
|
+
hasResults = true;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
return hasResults ? arrResult : void 0;
|
|
341
|
+
}
|
|
342
|
+
break;
|
|
343
|
+
}
|
|
344
|
+
return void 0;
|
|
345
|
+
};
|
|
346
|
+
function toJSONString(value, space = 0) {
|
|
347
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
348
|
+
const gap = (n) => space ? " ".repeat(n) : "";
|
|
349
|
+
const nl = (n) => space ? `
|
|
350
|
+
${" ".repeat(n)}` : "";
|
|
351
|
+
const esc = (s) => s.replace(/[\u0000-\u001F"\\]/g, (c) => {
|
|
352
|
+
switch (c) {
|
|
353
|
+
case '"':
|
|
354
|
+
return '\\"';
|
|
355
|
+
case "\\":
|
|
356
|
+
return "\\\\";
|
|
357
|
+
case "\b":
|
|
358
|
+
return "\\b";
|
|
359
|
+
case "\f":
|
|
360
|
+
return "\\f";
|
|
361
|
+
case "\n":
|
|
362
|
+
return "\\n";
|
|
363
|
+
case "\r":
|
|
364
|
+
return "\\r";
|
|
365
|
+
case " ":
|
|
366
|
+
return "\\t";
|
|
367
|
+
default:
|
|
368
|
+
const code = c.charCodeAt(0).toString(16).padStart(4, "0");
|
|
369
|
+
return `\\u${code}`;
|
|
370
|
+
}
|
|
371
|
+
});
|
|
372
|
+
const ser = (v, indent) => {
|
|
373
|
+
if (v === null) return "null";
|
|
374
|
+
const t = typeof v;
|
|
375
|
+
if (t === "number") return Number.isFinite(v) ? String(v) : "null";
|
|
376
|
+
if (t === "boolean") return v ? "true" : "false";
|
|
377
|
+
if (t === "string") {
|
|
378
|
+
}
|
|
379
|
+
if (t === "bigint") return `"${String(v)}"`;
|
|
380
|
+
if (t === "function" || t === "undefined" || t === "symbol")
|
|
381
|
+
return void 0;
|
|
382
|
+
if (Array.isArray(v)) {
|
|
383
|
+
if (seen.has(v))
|
|
384
|
+
throw new TypeError("Converting circular structure to JSON");
|
|
385
|
+
seen.add(v);
|
|
386
|
+
const next = indent + space;
|
|
387
|
+
const parts = v.map((x) => {
|
|
388
|
+
const s = ser(x, next);
|
|
389
|
+
return s === void 0 ? "null" : s;
|
|
390
|
+
});
|
|
391
|
+
seen.delete(v);
|
|
392
|
+
if (!space) return `[${parts.join(",")}]`;
|
|
393
|
+
return `[${nl(next)}${parts.join(`,${nl(next)}`)}${nl(indent)}]`;
|
|
394
|
+
}
|
|
395
|
+
if (t === "object") {
|
|
396
|
+
if (seen.has(v))
|
|
397
|
+
throw new TypeError("Converting circular structure to JSON");
|
|
398
|
+
seen.add(v);
|
|
399
|
+
const keys = Object.keys(v);
|
|
400
|
+
const next = indent + space;
|
|
401
|
+
const parts = [];
|
|
402
|
+
for (const k of keys) {
|
|
403
|
+
const s = ser(v[k], next);
|
|
404
|
+
if (s !== void 0) {
|
|
405
|
+
if (!space) parts.push(`"${esc(k)}":${s}`);
|
|
406
|
+
else parts.push(`${gap(next)}"${esc(k)}": ${s}`);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
seen.delete(v);
|
|
410
|
+
if (!space) return `{${parts.join(",")}}`;
|
|
411
|
+
return `{
|
|
412
|
+
${parts.join(",\n")}
|
|
413
|
+
${gap(indent)}}`;
|
|
414
|
+
}
|
|
415
|
+
return void 0;
|
|
416
|
+
};
|
|
417
|
+
const out = ser(value, 0);
|
|
418
|
+
return out === void 0 ? void 0 : out;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// src/lib/object/clone.ts
|
|
422
|
+
var deepClone = (obj, skipKeys) => {
|
|
423
|
+
if (obj == null || typeof obj !== "object") {
|
|
424
|
+
return obj;
|
|
425
|
+
}
|
|
426
|
+
if (obj instanceof Date) {
|
|
427
|
+
return new Date(obj.getTime());
|
|
428
|
+
}
|
|
429
|
+
if (obj instanceof RegExp) {
|
|
430
|
+
return new RegExp(obj.source, obj.flags);
|
|
431
|
+
}
|
|
432
|
+
if (obj instanceof Map) {
|
|
433
|
+
const clone = /* @__PURE__ */ new Map();
|
|
434
|
+
obj.forEach((value, key) => {
|
|
435
|
+
if (!skipKeys || !skipKeys.some((k) => k.toString() === key.toString())) {
|
|
436
|
+
clone.set(key, deepClone(value));
|
|
437
|
+
}
|
|
438
|
+
});
|
|
439
|
+
return clone;
|
|
440
|
+
}
|
|
441
|
+
if (obj instanceof Set) {
|
|
442
|
+
const clone = /* @__PURE__ */ new Set();
|
|
443
|
+
obj.forEach((value) => {
|
|
444
|
+
clone.add(deepClone(value));
|
|
445
|
+
});
|
|
446
|
+
return clone;
|
|
447
|
+
}
|
|
448
|
+
if (Array.isArray(obj)) {
|
|
449
|
+
return Array.from(obj).map((item) => deepClone(item));
|
|
450
|
+
}
|
|
451
|
+
if (isObject(obj)) {
|
|
452
|
+
const clone = {};
|
|
453
|
+
for (const key in obj) {
|
|
454
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
455
|
+
if (!skipKeys || !skipKeys.some((k) => k.toString() === key.toString())) {
|
|
456
|
+
clone[key] = deepClone(obj[key]);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
const symbols = Object.getOwnPropertySymbols(obj);
|
|
461
|
+
for (const symbol of symbols) {
|
|
462
|
+
if (!skipKeys || !skipKeys.some((s) => s.toString() === symbol.toString())) {
|
|
463
|
+
clone[symbol] = deepClone(obj[symbol]);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
return clone;
|
|
467
|
+
}
|
|
468
|
+
return obj;
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
// src/lib/object/object.ts
|
|
472
|
+
var invert = (obj, deep = false) => {
|
|
473
|
+
if (Array.isArray(obj)) {
|
|
474
|
+
return obj.map((item) => invert(item, deep));
|
|
475
|
+
}
|
|
476
|
+
if (!isObject(obj)) {
|
|
477
|
+
return {};
|
|
478
|
+
}
|
|
479
|
+
const result = {};
|
|
480
|
+
for (const key in obj) {
|
|
481
|
+
let value = obj[key];
|
|
482
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "symbol") {
|
|
483
|
+
result[value] = key;
|
|
484
|
+
} else if (isObject(value)) {
|
|
485
|
+
if (deep) {
|
|
486
|
+
result[key] = invert(value, deep);
|
|
487
|
+
}
|
|
488
|
+
} else if (Array.isArray(value)) {
|
|
489
|
+
if (deep) {
|
|
490
|
+
result[key] = value.map((item) => invert(item, deep));
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
return result;
|
|
495
|
+
};
|
|
496
|
+
var filter = (obj, predicate) => {
|
|
497
|
+
if (Array.isArray(obj)) {
|
|
498
|
+
return obj.filter(predicate);
|
|
499
|
+
}
|
|
500
|
+
if (!isObject(obj)) {
|
|
501
|
+
return {};
|
|
502
|
+
}
|
|
503
|
+
const result = {};
|
|
504
|
+
for (const key in obj) {
|
|
505
|
+
if (predicate(obj[key], key)) {
|
|
506
|
+
result[key] = obj[key];
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
return result;
|
|
510
|
+
};
|
|
511
|
+
var clean = (obj, deep = false) => {
|
|
512
|
+
if (!isObject(obj)) {
|
|
513
|
+
return {};
|
|
514
|
+
}
|
|
515
|
+
const result = {};
|
|
516
|
+
for (const key in obj) {
|
|
517
|
+
const value = obj[key];
|
|
518
|
+
if (value != null) {
|
|
519
|
+
if (deep && isObject(value)) {
|
|
520
|
+
const cleaned = clean(value, true);
|
|
521
|
+
if (Object.keys(cleaned).length > 0) {
|
|
522
|
+
result[key] = cleaned;
|
|
523
|
+
}
|
|
524
|
+
} else {
|
|
525
|
+
result[key] = value;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
return result;
|
|
530
|
+
};
|
|
531
|
+
var defaults = (current, overrides) => {
|
|
532
|
+
if (Array.isArray(current)) {
|
|
533
|
+
if (Array.isArray(overrides)) {
|
|
534
|
+
return [...current, ...overrides];
|
|
535
|
+
}
|
|
536
|
+
return [...current, overrides];
|
|
537
|
+
}
|
|
538
|
+
const result = deepClone(current);
|
|
539
|
+
for (const key in overrides) {
|
|
540
|
+
if (result[key] === void 0) {
|
|
541
|
+
result[key] = overrides[key];
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
return result;
|
|
545
|
+
};
|
|
546
|
+
var size = (obj) => {
|
|
547
|
+
if (!isObject(obj)) {
|
|
548
|
+
return 0;
|
|
549
|
+
}
|
|
550
|
+
return Object.keys(obj).length;
|
|
551
|
+
};
|
|
552
|
+
var transform = (obj, transformer) => {
|
|
553
|
+
if (Array.isArray(obj)) {
|
|
554
|
+
return obj.map(transformer);
|
|
555
|
+
}
|
|
556
|
+
if (!isObject(obj)) {
|
|
557
|
+
return {};
|
|
558
|
+
}
|
|
559
|
+
const result = {};
|
|
560
|
+
for (const key in obj) {
|
|
561
|
+
const [newKey, newValue] = transformer(key, obj[key]);
|
|
562
|
+
result[newKey] = newValue;
|
|
563
|
+
}
|
|
564
|
+
return result;
|
|
565
|
+
};
|
|
566
|
+
var groupBy = (array, key) => {
|
|
567
|
+
if (!Array.isArray(array)) {
|
|
568
|
+
return {};
|
|
569
|
+
}
|
|
570
|
+
return array.reduce((groups, item) => {
|
|
571
|
+
const groupKey = item[key];
|
|
572
|
+
if (groupKey == null) {
|
|
573
|
+
return groups;
|
|
574
|
+
}
|
|
575
|
+
const keyStr = String(groupKey);
|
|
576
|
+
if (!groups[keyStr]) {
|
|
577
|
+
groups[keyStr] = [];
|
|
578
|
+
}
|
|
579
|
+
groups[keyStr].push(item);
|
|
580
|
+
return groups;
|
|
581
|
+
}, {});
|
|
582
|
+
};
|
|
583
|
+
var toQueryString = (obj) => {
|
|
584
|
+
if (!isObject(obj)) {
|
|
585
|
+
return "";
|
|
586
|
+
}
|
|
587
|
+
const params = [];
|
|
588
|
+
const addParam = (key, value) => {
|
|
589
|
+
if (value == null) return;
|
|
590
|
+
if (Array.isArray(value)) {
|
|
591
|
+
value.forEach((item) => {
|
|
592
|
+
params.push(`${encodeURIComponent(key)}=${encodeURIComponent(item)}`);
|
|
593
|
+
});
|
|
594
|
+
} else {
|
|
595
|
+
params.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
|
|
596
|
+
}
|
|
597
|
+
};
|
|
598
|
+
for (const key in obj) {
|
|
599
|
+
addParam(key, obj[key]);
|
|
600
|
+
}
|
|
601
|
+
return params.join("&");
|
|
602
|
+
};
|
|
603
|
+
var fromQueryString = (queryString) => {
|
|
604
|
+
if (!queryString || typeof queryString !== "string") {
|
|
605
|
+
return {};
|
|
606
|
+
}
|
|
607
|
+
const str = queryString.startsWith("?") ? queryString.slice(1) : queryString;
|
|
608
|
+
if (!str) {
|
|
609
|
+
return {};
|
|
610
|
+
}
|
|
611
|
+
const result = {};
|
|
612
|
+
const pairs = str.split("&");
|
|
613
|
+
for (const pair of pairs) {
|
|
614
|
+
const [key, value] = pair.split("=").map(decodeURIComponent);
|
|
615
|
+
if (key) {
|
|
616
|
+
if (key in result) {
|
|
617
|
+
if (Array.isArray(result[key])) {
|
|
618
|
+
result[key].push(value || "");
|
|
619
|
+
} else {
|
|
620
|
+
result[key] = [result[key], value || ""];
|
|
621
|
+
}
|
|
622
|
+
} else {
|
|
623
|
+
result[key] = value || "";
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
return result;
|
|
628
|
+
};
|
|
629
|
+
var deepFreeze = (obj) => {
|
|
630
|
+
if (obj == null || typeof obj !== "object") {
|
|
631
|
+
return obj;
|
|
632
|
+
}
|
|
633
|
+
Object.freeze(obj);
|
|
634
|
+
for (const key in obj) {
|
|
635
|
+
const value = obj[key];
|
|
636
|
+
if (value != null && typeof value === "object") {
|
|
637
|
+
deepFreeze(value);
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
const symbols = Object.getOwnPropertySymbols(obj);
|
|
641
|
+
for (const symbol of symbols) {
|
|
642
|
+
const value = obj[symbol];
|
|
643
|
+
if (value != null && typeof value === "object") {
|
|
644
|
+
deepFreeze(value);
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
return obj;
|
|
648
|
+
};
|
|
649
|
+
var isNonNullObject = (value) => {
|
|
650
|
+
return value != null && typeof value === "object" && !Array.isArray(value);
|
|
651
|
+
};
|
|
652
|
+
|
|
653
|
+
// src/lib/object/diff.ts
|
|
654
|
+
var defaultEqualityFn = (a, b) => a === b;
|
|
655
|
+
var deepArrayEqual = (arr1, arr2) => {
|
|
656
|
+
if (arr1.length !== arr2.length) return false;
|
|
657
|
+
for (let i = 0; i < arr1.length; i++) {
|
|
658
|
+
if (Array.isArray(arr1[i]) && Array.isArray(arr2[i])) {
|
|
659
|
+
if (!deepArrayEqual(arr1[i], arr2[i])) return false;
|
|
660
|
+
} else if (isObject(arr1[i]) && isObject(arr2[i])) {
|
|
661
|
+
if (!deepObjectEqual(arr1[i], arr2[i])) return false;
|
|
662
|
+
} else if (arr1[i] !== arr2[i]) {
|
|
663
|
+
return false;
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
return true;
|
|
667
|
+
};
|
|
668
|
+
var deepObjectEqual = (obj1, obj2) => {
|
|
669
|
+
const keys1 = Object.keys(obj1);
|
|
670
|
+
const keys2 = Object.keys(obj2);
|
|
671
|
+
if (keys1.length !== keys2.length) return false;
|
|
672
|
+
for (const key of keys1) {
|
|
673
|
+
if (!(key in obj2)) return false;
|
|
674
|
+
const val1 = obj1[key];
|
|
675
|
+
const val2 = obj2[key];
|
|
676
|
+
if (Array.isArray(val1) && Array.isArray(val2)) {
|
|
677
|
+
if (!deepArrayEqual(val1, val2)) return false;
|
|
678
|
+
} else if (isObject(val1) && isObject(val2)) {
|
|
679
|
+
if (!deepObjectEqual(val1, val2)) return false;
|
|
680
|
+
} else if (val1 !== val2) {
|
|
681
|
+
return false;
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
return true;
|
|
685
|
+
};
|
|
686
|
+
var diffObject = (oldObj, newObj, options = {}) => {
|
|
687
|
+
const {
|
|
688
|
+
includeDeleted = false,
|
|
689
|
+
includeAdded = true,
|
|
690
|
+
deepArrayComparison = true,
|
|
691
|
+
equalityFn = defaultEqualityFn,
|
|
692
|
+
preserveStructure = false
|
|
693
|
+
} = options;
|
|
694
|
+
if (oldObj == null && newObj == null) {
|
|
695
|
+
return {};
|
|
696
|
+
}
|
|
697
|
+
if (oldObj == null) {
|
|
698
|
+
return includeAdded ? { ...newObj } : {};
|
|
699
|
+
}
|
|
700
|
+
if (newObj == null) {
|
|
701
|
+
if (includeDeleted) {
|
|
702
|
+
const result2 = {};
|
|
703
|
+
for (const key in oldObj) {
|
|
704
|
+
result2[key] = void 0;
|
|
705
|
+
}
|
|
706
|
+
return result2;
|
|
707
|
+
}
|
|
708
|
+
return {};
|
|
709
|
+
}
|
|
710
|
+
if (!isObject(oldObj) || !isObject(newObj)) {
|
|
711
|
+
return equalityFn(oldObj, newObj) ? {} : newObj;
|
|
712
|
+
}
|
|
713
|
+
const result = {};
|
|
714
|
+
const processedKeys = /* @__PURE__ */ new Set();
|
|
715
|
+
for (const key in newObj) {
|
|
716
|
+
processedKeys.add(key);
|
|
717
|
+
const oldValue = oldObj[key];
|
|
718
|
+
const newValue = newObj[key];
|
|
719
|
+
if (!(key in oldObj)) {
|
|
720
|
+
if (includeAdded) {
|
|
721
|
+
result[key] = newValue;
|
|
722
|
+
}
|
|
723
|
+
continue;
|
|
724
|
+
}
|
|
725
|
+
if (Array.isArray(oldValue) && Array.isArray(newValue)) {
|
|
726
|
+
if (deepArrayComparison) {
|
|
727
|
+
if (!deepArrayEqual(oldValue, newValue)) {
|
|
728
|
+
result[key] = newValue;
|
|
729
|
+
} else if (preserveStructure) {
|
|
730
|
+
result[key] = void 0;
|
|
731
|
+
}
|
|
732
|
+
} else {
|
|
733
|
+
if (oldValue !== newValue) {
|
|
734
|
+
result[key] = newValue;
|
|
735
|
+
} else if (preserveStructure) {
|
|
736
|
+
result[key] = void 0;
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
continue;
|
|
740
|
+
}
|
|
741
|
+
if (isObject(oldValue) && isObject(newValue)) {
|
|
742
|
+
const nestedDiff = diffObject(oldValue, newValue, options);
|
|
743
|
+
if (Object.keys(nestedDiff).length > 0) {
|
|
744
|
+
result[key] = nestedDiff;
|
|
745
|
+
} else if (preserveStructure) {
|
|
746
|
+
result[key] = void 0;
|
|
747
|
+
}
|
|
748
|
+
continue;
|
|
749
|
+
}
|
|
750
|
+
if (!equalityFn(oldValue, newValue)) {
|
|
751
|
+
result[key] = newValue;
|
|
752
|
+
} else if (preserveStructure) {
|
|
753
|
+
result[key] = void 0;
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
if (includeDeleted) {
|
|
757
|
+
for (const key in oldObj) {
|
|
758
|
+
if (!processedKeys.has(key)) {
|
|
759
|
+
result[key] = void 0;
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
return result;
|
|
764
|
+
};
|
|
765
|
+
var diffObjectDetailed = (oldObj, newObj, options = {}) => {
|
|
766
|
+
const added = [];
|
|
767
|
+
const deleted = [];
|
|
768
|
+
const modified = [];
|
|
769
|
+
const changes = {};
|
|
770
|
+
const {
|
|
771
|
+
includeDeleted = false,
|
|
772
|
+
includeAdded = true,
|
|
773
|
+
deepArrayComparison = true,
|
|
774
|
+
equalityFn = defaultEqualityFn
|
|
775
|
+
} = options;
|
|
776
|
+
if (Array.isArray(oldObj) && Array.isArray(newObj)) {
|
|
777
|
+
const maxLength = Math.max(oldObj.length, newObj.length);
|
|
778
|
+
const arrayChanges = [];
|
|
779
|
+
let hasArrayChanges = false;
|
|
780
|
+
for (let i = 0; i < maxLength; i++) {
|
|
781
|
+
const indexPath = `[${i}]`;
|
|
782
|
+
if (i >= oldObj.length) {
|
|
783
|
+
added.push(indexPath);
|
|
784
|
+
arrayChanges[i] = newObj[i];
|
|
785
|
+
hasArrayChanges = true;
|
|
786
|
+
} else if (i >= newObj.length) {
|
|
787
|
+
deleted.push(indexPath);
|
|
788
|
+
hasArrayChanges = true;
|
|
789
|
+
} else {
|
|
790
|
+
const oldElement = oldObj[i];
|
|
791
|
+
const newElement = newObj[i];
|
|
792
|
+
if (isObject(oldElement) && isObject(newElement)) {
|
|
793
|
+
const nestedDiff = diffObjectDetailed(
|
|
794
|
+
oldElement,
|
|
795
|
+
newElement,
|
|
796
|
+
options
|
|
797
|
+
);
|
|
798
|
+
if (nestedDiff.hasChanges) {
|
|
799
|
+
modified.push(indexPath);
|
|
800
|
+
arrayChanges[i] = newElement;
|
|
801
|
+
hasArrayChanges = true;
|
|
802
|
+
} else {
|
|
803
|
+
arrayChanges[i] = oldElement;
|
|
804
|
+
}
|
|
805
|
+
} else if (Array.isArray(oldElement) && Array.isArray(newElement)) {
|
|
806
|
+
const nestedDiff = diffObjectDetailed(
|
|
807
|
+
oldElement,
|
|
808
|
+
newElement,
|
|
809
|
+
options
|
|
810
|
+
);
|
|
811
|
+
if (nestedDiff.hasChanges) {
|
|
812
|
+
modified.push(indexPath);
|
|
813
|
+
arrayChanges[i] = newElement;
|
|
814
|
+
hasArrayChanges = true;
|
|
815
|
+
} else {
|
|
816
|
+
arrayChanges[i] = oldElement;
|
|
817
|
+
}
|
|
818
|
+
} else if (!equalityFn(oldElement, newElement)) {
|
|
819
|
+
modified.push(indexPath);
|
|
820
|
+
arrayChanges[i] = newElement;
|
|
821
|
+
hasArrayChanges = true;
|
|
822
|
+
} else {
|
|
823
|
+
arrayChanges[i] = oldElement;
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
return {
|
|
828
|
+
changes: hasArrayChanges ? arrayChanges : {},
|
|
829
|
+
added,
|
|
830
|
+
deleted,
|
|
831
|
+
modified,
|
|
832
|
+
hasChanges: hasArrayChanges || deleted.length > 0
|
|
833
|
+
};
|
|
834
|
+
}
|
|
835
|
+
const collectDetailedPaths = (old, newVal, currentPath = "") => {
|
|
836
|
+
const result = {
|
|
837
|
+
added: [],
|
|
838
|
+
deleted: [],
|
|
839
|
+
modified: []
|
|
840
|
+
};
|
|
841
|
+
if (Array.isArray(old) && Array.isArray(newVal)) {
|
|
842
|
+
const maxLength = Math.max(old.length, newVal.length);
|
|
843
|
+
for (let i = 0; i < maxLength; i++) {
|
|
844
|
+
const indexPath = currentPath ? `${currentPath}[${i}]` : `[${i}]`;
|
|
845
|
+
if (i >= old.length) {
|
|
846
|
+
result.added.push(indexPath);
|
|
847
|
+
} else if (i >= newVal.length) {
|
|
848
|
+
result.deleted.push(indexPath);
|
|
849
|
+
} else {
|
|
850
|
+
const oldElement = old[i];
|
|
851
|
+
const newElement = newVal[i];
|
|
852
|
+
if (isObject(oldElement) && isObject(newElement)) {
|
|
853
|
+
const nested = collectDetailedPaths(
|
|
854
|
+
oldElement,
|
|
855
|
+
newElement,
|
|
856
|
+
indexPath
|
|
857
|
+
);
|
|
858
|
+
result.added.push(...nested.added);
|
|
859
|
+
result.deleted.push(...nested.deleted);
|
|
860
|
+
result.modified.push(...nested.modified);
|
|
861
|
+
} else if (Array.isArray(oldElement) && Array.isArray(newElement)) {
|
|
862
|
+
const nested = collectDetailedPaths(
|
|
863
|
+
oldElement,
|
|
864
|
+
newElement,
|
|
865
|
+
indexPath
|
|
866
|
+
);
|
|
867
|
+
result.added.push(...nested.added);
|
|
868
|
+
result.deleted.push(...nested.deleted);
|
|
869
|
+
result.modified.push(...nested.modified);
|
|
870
|
+
} else if (oldElement !== newElement) {
|
|
871
|
+
result.modified.push(indexPath);
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
return result;
|
|
876
|
+
}
|
|
877
|
+
if (isObject(old) && isObject(newVal)) {
|
|
878
|
+
const allKeys = /* @__PURE__ */ new Set([
|
|
879
|
+
...Object.keys(old || {}),
|
|
880
|
+
...Object.keys(newVal || {})
|
|
881
|
+
]);
|
|
882
|
+
for (const key of allKeys) {
|
|
883
|
+
const oldValue = old?.[key];
|
|
884
|
+
const newValue = newVal?.[key];
|
|
885
|
+
const path = currentPath ? `${currentPath}.${key}` : key;
|
|
886
|
+
if (!(key in (old || {}))) {
|
|
887
|
+
result.added.push(path);
|
|
888
|
+
} else if (!(key in (newVal || {}))) {
|
|
889
|
+
result.deleted.push(path);
|
|
890
|
+
} else if (Array.isArray(oldValue) && Array.isArray(newValue)) {
|
|
891
|
+
if (!deepArrayEqual(oldValue, newValue)) {
|
|
892
|
+
const nested = collectDetailedPaths(oldValue, newValue, path);
|
|
893
|
+
result.added.push(...nested.added);
|
|
894
|
+
result.deleted.push(...nested.deleted);
|
|
895
|
+
result.modified.push(...nested.modified);
|
|
896
|
+
}
|
|
897
|
+
} else if (isObject(oldValue) && isObject(newValue)) {
|
|
898
|
+
const nested = collectDetailedPaths(oldValue, newValue, path);
|
|
899
|
+
result.added.push(...nested.added);
|
|
900
|
+
result.deleted.push(...nested.deleted);
|
|
901
|
+
result.modified.push(...nested.modified);
|
|
902
|
+
} else if (oldValue !== newValue) {
|
|
903
|
+
result.modified.push(path);
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
return result;
|
|
908
|
+
};
|
|
909
|
+
if (oldObj == null && newObj == null) {
|
|
910
|
+
return { changes: {}, added, deleted, modified, hasChanges: false };
|
|
911
|
+
}
|
|
912
|
+
if (oldObj == null) {
|
|
913
|
+
if (includeAdded && newObj) {
|
|
914
|
+
for (const key in newObj) {
|
|
915
|
+
added.push(key);
|
|
916
|
+
changes[key] = newObj[key];
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
return {
|
|
920
|
+
changes,
|
|
921
|
+
added,
|
|
922
|
+
deleted,
|
|
923
|
+
modified,
|
|
924
|
+
hasChanges: added.length > 0
|
|
925
|
+
};
|
|
926
|
+
}
|
|
927
|
+
if (newObj == null) {
|
|
928
|
+
if (includeDeleted) {
|
|
929
|
+
for (const key in oldObj) {
|
|
930
|
+
deleted.push(key);
|
|
931
|
+
changes[key] = void 0;
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
return {
|
|
935
|
+
changes,
|
|
936
|
+
added,
|
|
937
|
+
deleted,
|
|
938
|
+
modified,
|
|
939
|
+
hasChanges: deleted.length > 0
|
|
940
|
+
};
|
|
941
|
+
}
|
|
942
|
+
const processedKeys = /* @__PURE__ */ new Set();
|
|
943
|
+
for (const key in newObj) {
|
|
944
|
+
processedKeys.add(key);
|
|
945
|
+
const oldValue = oldObj[key];
|
|
946
|
+
const newValue = newObj[key];
|
|
947
|
+
if (!(key in oldObj)) {
|
|
948
|
+
if (includeAdded) {
|
|
949
|
+
added.push(key);
|
|
950
|
+
changes[key] = newValue;
|
|
951
|
+
}
|
|
952
|
+
continue;
|
|
953
|
+
}
|
|
954
|
+
let isDifferent = false;
|
|
955
|
+
if (Array.isArray(oldValue) && Array.isArray(newValue)) {
|
|
956
|
+
isDifferent = deepArrayComparison ? !deepArrayEqual(oldValue, newValue) : oldValue !== newValue;
|
|
957
|
+
if (isDifferent) {
|
|
958
|
+
const paths = collectDetailedPaths(oldValue, newValue, key);
|
|
959
|
+
added.push(...paths.added);
|
|
960
|
+
deleted.push(...paths.deleted);
|
|
961
|
+
modified.push(...paths.modified);
|
|
962
|
+
changes[key] = newValue;
|
|
963
|
+
}
|
|
964
|
+
} else if (isObject(oldValue) && isObject(newValue)) {
|
|
965
|
+
const nestedDiff = diffObject(oldValue, newValue, options);
|
|
966
|
+
isDifferent = Object.keys(nestedDiff).length > 0;
|
|
967
|
+
if (isDifferent) {
|
|
968
|
+
const paths = collectDetailedPaths(oldValue, newValue, key);
|
|
969
|
+
added.push(...paths.added);
|
|
970
|
+
deleted.push(...paths.deleted);
|
|
971
|
+
modified.push(...paths.modified);
|
|
972
|
+
changes[key] = nestedDiff;
|
|
973
|
+
}
|
|
974
|
+
} else {
|
|
975
|
+
isDifferent = !equalityFn(oldValue, newValue);
|
|
976
|
+
if (isDifferent) {
|
|
977
|
+
modified.push(key);
|
|
978
|
+
changes[key] = newValue;
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
for (const key in oldObj) {
|
|
983
|
+
if (!processedKeys.has(key)) {
|
|
984
|
+
deleted.push(key);
|
|
985
|
+
changes[key] = void 0;
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
return {
|
|
989
|
+
changes,
|
|
990
|
+
added,
|
|
991
|
+
deleted,
|
|
992
|
+
modified,
|
|
993
|
+
hasChanges: added.length > 0 || deleted.length > 0 || modified.length > 0
|
|
994
|
+
};
|
|
995
|
+
};
|
|
996
|
+
var hasObjectChanges = (oldObj, newObj, options = {}) => {
|
|
997
|
+
const diff = diffObject(oldObj, newObj, options);
|
|
998
|
+
return Object.keys(diff).length > 0;
|
|
999
|
+
};
|
|
1000
|
+
function isApplyDiff(value) {
|
|
1001
|
+
if (value && (value.diff || value.changes) && (value.modified || value.deleted || value.hasChanges)) {
|
|
1002
|
+
return true;
|
|
1003
|
+
}
|
|
1004
|
+
return false;
|
|
1005
|
+
}
|
|
1006
|
+
var applyDiff = (baseObj, option, keepRefParam) => {
|
|
1007
|
+
if (!isApplyDiff(option)) {
|
|
1008
|
+
const diff2 = option;
|
|
1009
|
+
const shouldKeepRef2 = keepRefParam || false;
|
|
1010
|
+
if (Array.isArray(baseObj) && Array.isArray(diff2)) {
|
|
1011
|
+
const result3 = shouldKeepRef2 ? baseObj : [...baseObj];
|
|
1012
|
+
if (diff2.length < result3.length) {
|
|
1013
|
+
result3.length = diff2.length;
|
|
1014
|
+
}
|
|
1015
|
+
for (let i = 0; i < diff2.length; i++) {
|
|
1016
|
+
const diffValue = diff2[i];
|
|
1017
|
+
if (diffValue === void 0) {
|
|
1018
|
+
continue;
|
|
1019
|
+
}
|
|
1020
|
+
if (isObject(result3[i]) && isObject(diffValue)) {
|
|
1021
|
+
result3[i] = applyDiff(result3[i], diffValue, shouldKeepRef2);
|
|
1022
|
+
} else if (Array.isArray(result3[i]) && Array.isArray(diffValue)) {
|
|
1023
|
+
result3[i] = applyDiff(
|
|
1024
|
+
result3[i],
|
|
1025
|
+
diffValue,
|
|
1026
|
+
shouldKeepRef2
|
|
1027
|
+
);
|
|
1028
|
+
} else {
|
|
1029
|
+
result3[i] = diffValue;
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
return result3;
|
|
1033
|
+
}
|
|
1034
|
+
if (!isObject(baseObj)) return diff2;
|
|
1035
|
+
if (!isObject(diff2)) return baseObj;
|
|
1036
|
+
const result2 = shouldKeepRef2 ? baseObj : { ...baseObj };
|
|
1037
|
+
for (const key in diff2) {
|
|
1038
|
+
const diffValue = diff2[key];
|
|
1039
|
+
if (diffValue === void 0) {
|
|
1040
|
+
delete result2[key];
|
|
1041
|
+
} else if (Array.isArray(result2[key]) && Array.isArray(diffValue)) {
|
|
1042
|
+
result2[key] = applyDiff(result2[key], diffValue, shouldKeepRef2);
|
|
1043
|
+
} else if (isObject(result2[key]) && isObject(diffValue)) {
|
|
1044
|
+
result2[key] = applyDiff(result2[key], diffValue, shouldKeepRef2);
|
|
1045
|
+
} else {
|
|
1046
|
+
result2[key] = diffValue;
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
return result2;
|
|
1050
|
+
}
|
|
1051
|
+
const {
|
|
1052
|
+
diff,
|
|
1053
|
+
added = [],
|
|
1054
|
+
deleted = [],
|
|
1055
|
+
modified = [],
|
|
1056
|
+
changes,
|
|
1057
|
+
keepRef: keepRefOption
|
|
1058
|
+
} = option;
|
|
1059
|
+
const shouldKeepRef = keepRefParam !== void 0 ? keepRefParam : keepRefOption;
|
|
1060
|
+
const diffToApply = changes || diff;
|
|
1061
|
+
if (!diffToApply) return baseObj;
|
|
1062
|
+
const addedSet = new Set(added);
|
|
1063
|
+
const deletedSet = new Set(deleted);
|
|
1064
|
+
const modifiedSet = new Set(modified);
|
|
1065
|
+
const shouldApplyPath = (path) => {
|
|
1066
|
+
if (added.length === 0 && deleted.length === 0 && modified.length === 0) {
|
|
1067
|
+
return true;
|
|
1068
|
+
}
|
|
1069
|
+
const pathParts = path.split(/[.\[\]]+/).filter(Boolean);
|
|
1070
|
+
for (let i = 0; i <= pathParts.length; i++) {
|
|
1071
|
+
const partialPath = pathParts.slice(0, i).join(".");
|
|
1072
|
+
const bracketPath = pathParts.slice(0, i).map((p, idx) => /^\d+$/.test(p) ? `[${p}]` : idx > 0 ? `.${p}` : p).join("");
|
|
1073
|
+
if (addedSet.has(partialPath) || addedSet.has(bracketPath) || deletedSet.has(partialPath) || deletedSet.has(bracketPath) || modifiedSet.has(partialPath) || modifiedSet.has(bracketPath)) {
|
|
1074
|
+
return true;
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
return false;
|
|
1078
|
+
};
|
|
1079
|
+
if (Array.isArray(baseObj) && Array.isArray(diffToApply)) {
|
|
1080
|
+
const result2 = shouldKeepRef ? baseObj : [...baseObj];
|
|
1081
|
+
result2.length = diffToApply.length;
|
|
1082
|
+
for (let i = 0; i < diffToApply.length; i++) {
|
|
1083
|
+
const indexPath = `[${i}]`;
|
|
1084
|
+
const diffValue = diffToApply[i];
|
|
1085
|
+
if (diffValue === void 0) {
|
|
1086
|
+
continue;
|
|
1087
|
+
}
|
|
1088
|
+
if (!shouldApplyPath(indexPath)) {
|
|
1089
|
+
continue;
|
|
1090
|
+
}
|
|
1091
|
+
if (isObject(result2[i]) && isObject(diffValue)) {
|
|
1092
|
+
result2[i] = applyDiff(result2[i], diffValue, shouldKeepRef);
|
|
1093
|
+
} else if (Array.isArray(result2[i]) && Array.isArray(diffValue)) {
|
|
1094
|
+
result2[i] = applyDiff(
|
|
1095
|
+
result2[i],
|
|
1096
|
+
diffValue,
|
|
1097
|
+
shouldKeepRef
|
|
1098
|
+
);
|
|
1099
|
+
} else {
|
|
1100
|
+
result2[i] = diffValue;
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
return result2;
|
|
1104
|
+
}
|
|
1105
|
+
if (!isObject(baseObj)) return diffToApply;
|
|
1106
|
+
if (!isObject(diffToApply)) return baseObj;
|
|
1107
|
+
const result = shouldKeepRef ? baseObj : { ...baseObj };
|
|
1108
|
+
for (const path of deleted) {
|
|
1109
|
+
if (path.includes(".") || path.includes("[")) continue;
|
|
1110
|
+
if (shouldApplyPath(path)) {
|
|
1111
|
+
delete result[path];
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
for (const key in diffToApply) {
|
|
1115
|
+
const diffValue = diffToApply[key];
|
|
1116
|
+
if (!shouldApplyPath(key)) {
|
|
1117
|
+
continue;
|
|
1118
|
+
}
|
|
1119
|
+
if (diffValue === void 0) {
|
|
1120
|
+
delete result[key];
|
|
1121
|
+
} else if (Array.isArray(result[key]) && Array.isArray(diffValue)) {
|
|
1122
|
+
result[key] = applyDiff(result[key], diffValue, shouldKeepRef);
|
|
1123
|
+
} else if (isObject(result[key]) && isObject(diffValue)) {
|
|
1124
|
+
result[key] = applyDiff(result[key], diffValue, shouldKeepRef);
|
|
1125
|
+
} else {
|
|
1126
|
+
result[key] = diffValue;
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
return result;
|
|
1130
|
+
};
|
|
1131
|
+
var getChangedPaths = (oldObj, newObj, options = {}) => {
|
|
1132
|
+
const paths = [];
|
|
1133
|
+
const collectPaths = (old, newVal, currentPath = "") => {
|
|
1134
|
+
if (old === newVal) return;
|
|
1135
|
+
if (Array.isArray(old) && Array.isArray(newVal)) {
|
|
1136
|
+
const maxLength = Math.max(old.length, newVal.length);
|
|
1137
|
+
for (let i = 0; i < maxLength; i++) {
|
|
1138
|
+
const indexPath = currentPath ? `${currentPath}[${i}]` : `[${i}]`;
|
|
1139
|
+
if (i >= old.length) {
|
|
1140
|
+
paths.push(indexPath);
|
|
1141
|
+
} else if (i >= newVal.length) {
|
|
1142
|
+
paths.push(indexPath);
|
|
1143
|
+
} else {
|
|
1144
|
+
const oldElement = old[i];
|
|
1145
|
+
const newElement = newVal[i];
|
|
1146
|
+
if (isObject(oldElement) && isObject(newElement)) {
|
|
1147
|
+
collectPaths(oldElement, newElement, indexPath);
|
|
1148
|
+
} else if (Array.isArray(oldElement) && Array.isArray(newElement)) {
|
|
1149
|
+
collectPaths(oldElement, newElement, indexPath);
|
|
1150
|
+
} else if (oldElement !== newElement) {
|
|
1151
|
+
paths.push(indexPath);
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
return;
|
|
1156
|
+
}
|
|
1157
|
+
if (!isObject(old) || !isObject(newVal)) {
|
|
1158
|
+
if (currentPath) paths.push(currentPath);
|
|
1159
|
+
return;
|
|
1160
|
+
}
|
|
1161
|
+
const allKeys = /* @__PURE__ */ new Set([
|
|
1162
|
+
...Object.keys(old || {}),
|
|
1163
|
+
...Object.keys(newVal || {})
|
|
1164
|
+
]);
|
|
1165
|
+
for (const key of allKeys) {
|
|
1166
|
+
const oldValue = old?.[key];
|
|
1167
|
+
const newValue = newVal?.[key];
|
|
1168
|
+
const path = currentPath ? `${currentPath}.${key}` : key;
|
|
1169
|
+
if (!(key in (old || {})) || !(key in (newVal || {}))) {
|
|
1170
|
+
paths.push(path);
|
|
1171
|
+
} else if (Array.isArray(oldValue) && Array.isArray(newValue)) {
|
|
1172
|
+
collectPaths(oldValue, newValue, path);
|
|
1173
|
+
} else if (isObject(oldValue) && isObject(newValue)) {
|
|
1174
|
+
collectPaths(oldValue, newValue, path);
|
|
1175
|
+
} else if (oldValue !== newValue) {
|
|
1176
|
+
paths.push(path);
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
};
|
|
1180
|
+
collectPaths(oldObj, newObj);
|
|
1181
|
+
return paths;
|
|
1182
|
+
};
|
|
1183
|
+
|
|
1184
|
+
// src/lib/object/selector.ts
|
|
1185
|
+
var parseSelector = (selector, options = {}) => {
|
|
1186
|
+
const { type = "auto", throwOnError = false } = options;
|
|
1187
|
+
if (!selector || typeof selector !== "string") {
|
|
1188
|
+
return [];
|
|
1189
|
+
}
|
|
1190
|
+
const segments = [];
|
|
1191
|
+
let current = "";
|
|
1192
|
+
let inBrackets = false;
|
|
1193
|
+
let inQuotes = false;
|
|
1194
|
+
let quoteChar = "";
|
|
1195
|
+
let bracketContent = "";
|
|
1196
|
+
let bracketDepth = 0;
|
|
1197
|
+
const detectType = (str) => {
|
|
1198
|
+
if (str.includes("..")) return "descendant";
|
|
1199
|
+
if (str.includes("[?(")) return "filter";
|
|
1200
|
+
if (str.includes("[:") || str.includes("[0:")) return "slice";
|
|
1201
|
+
if (str.includes("[,") || str.includes("[0,1]")) return "union";
|
|
1202
|
+
if (str.includes("[*]") || str.includes(".*.")) return "wildcard";
|
|
1203
|
+
if (str.includes("[]")) return "array-wildcard";
|
|
1204
|
+
if (str.includes("[") && str.includes("]")) return "bracket";
|
|
1205
|
+
return "dot";
|
|
1206
|
+
};
|
|
1207
|
+
const actualType = type === "auto" ? detectType(selector) : type;
|
|
1208
|
+
const parseBracketContent = (content) => {
|
|
1209
|
+
const trimmed = content.trim();
|
|
1210
|
+
if (trimmed === "*" || trimmed === "") {
|
|
1211
|
+
return {
|
|
1212
|
+
type: "wildcard",
|
|
1213
|
+
value: trimmed === "" ? "[]" : "*",
|
|
1214
|
+
raw: trimmed === "" ? "[]" : content
|
|
1215
|
+
};
|
|
1216
|
+
}
|
|
1217
|
+
if (trimmed === "[]") {
|
|
1218
|
+
return { type: "wildcard", value: "[]", raw: content };
|
|
1219
|
+
}
|
|
1220
|
+
if (/^-?\d+$/.test(trimmed)) {
|
|
1221
|
+
return { type: "index", value: parseInt(trimmed, 10), raw: content };
|
|
1222
|
+
}
|
|
1223
|
+
if (trimmed.includes(":")) {
|
|
1224
|
+
const parts = trimmed.split(":").map((p) => p.trim());
|
|
1225
|
+
const start = parts[0] === "" ? 0 : parseInt(parts[0], 10);
|
|
1226
|
+
const end = parts[1] === "" ? Infinity : parseInt(parts[1], 10);
|
|
1227
|
+
if (isNaN(start) || isNaN(end)) {
|
|
1228
|
+
if (throwOnError) throw new Error(`Invalid slice notation: ${content}`);
|
|
1229
|
+
return { type: "wildcard", value: "*", raw: content };
|
|
1230
|
+
}
|
|
1231
|
+
return { type: "slice", value: [start, end], raw: content };
|
|
1232
|
+
}
|
|
1233
|
+
if (trimmed.includes(",")) {
|
|
1234
|
+
const indices = trimmed.split(",").map((p) => {
|
|
1235
|
+
const num = parseInt(p.trim(), 10);
|
|
1236
|
+
return isNaN(num) ? -1 : num;
|
|
1237
|
+
}).filter((n) => n >= 0);
|
|
1238
|
+
if (indices.length === 0) {
|
|
1239
|
+
if (throwOnError) throw new Error(`Invalid union notation: ${content}`);
|
|
1240
|
+
return { type: "wildcard", value: "*", raw: content };
|
|
1241
|
+
}
|
|
1242
|
+
return { type: "union", value: indices, raw: content };
|
|
1243
|
+
}
|
|
1244
|
+
if (trimmed.startsWith("?(") && trimmed.endsWith(")")) {
|
|
1245
|
+
const expr = trimmed.slice(2, -1);
|
|
1246
|
+
try {
|
|
1247
|
+
const filterFn = createFilterFunction(expr);
|
|
1248
|
+
return { type: "filter", value: filterFn, raw: content };
|
|
1249
|
+
} catch (error) {
|
|
1250
|
+
if (throwOnError) throw error;
|
|
1251
|
+
return { type: "wildcard", value: "*", raw: content };
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
if (trimmed.startsWith('"') && trimmed.endsWith('"') || trimmed.startsWith("'") && trimmed.endsWith("'")) {
|
|
1255
|
+
return { type: "property", value: trimmed.slice(1, -1), raw: content };
|
|
1256
|
+
}
|
|
1257
|
+
return { type: "property", value: trimmed, raw: content };
|
|
1258
|
+
};
|
|
1259
|
+
for (let i = 0; i < selector.length; i++) {
|
|
1260
|
+
const char = selector[i];
|
|
1261
|
+
const nextChar = selector[i + 1];
|
|
1262
|
+
if ((char === '"' || char === "'") && !inQuotes) {
|
|
1263
|
+
inQuotes = true;
|
|
1264
|
+
quoteChar = char;
|
|
1265
|
+
continue;
|
|
1266
|
+
} else if (char === quoteChar && inQuotes) {
|
|
1267
|
+
inQuotes = false;
|
|
1268
|
+
quoteChar = "";
|
|
1269
|
+
if (!(char === '"' || char === "'")) {
|
|
1270
|
+
if (inBrackets) {
|
|
1271
|
+
bracketContent += char;
|
|
1272
|
+
} else {
|
|
1273
|
+
current += char;
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1276
|
+
continue;
|
|
1277
|
+
}
|
|
1278
|
+
if (inQuotes) {
|
|
1279
|
+
if (inBrackets) {
|
|
1280
|
+
bracketContent += char;
|
|
1281
|
+
} else {
|
|
1282
|
+
current += char;
|
|
1283
|
+
}
|
|
1284
|
+
continue;
|
|
1285
|
+
}
|
|
1286
|
+
if (char === "[" && !inBrackets) {
|
|
1287
|
+
if (current) {
|
|
1288
|
+
segments.push({ type: "property", value: current, raw: current });
|
|
1289
|
+
current = "";
|
|
1290
|
+
}
|
|
1291
|
+
inBrackets = true;
|
|
1292
|
+
bracketContent = "";
|
|
1293
|
+
continue;
|
|
1294
|
+
}
|
|
1295
|
+
if (char === "]" && inBrackets && !inQuotes) {
|
|
1296
|
+
inBrackets = false;
|
|
1297
|
+
const segment = parseBracketContent(bracketContent);
|
|
1298
|
+
segments.push(segment);
|
|
1299
|
+
continue;
|
|
1300
|
+
}
|
|
1301
|
+
let lastDot = segments.length > 0 ? segments[segments.length - 1] : null;
|
|
1302
|
+
if (char === "." && !inBrackets && !inQuotes) {
|
|
1303
|
+
if (nextChar === ".") {
|
|
1304
|
+
const l = selector.split("..")[0];
|
|
1305
|
+
segments.push({ type: "property", value: l, raw: l });
|
|
1306
|
+
segments.push({ type: "descendant", value: "..", raw: ".." });
|
|
1307
|
+
i++;
|
|
1308
|
+
} else if (current) {
|
|
1309
|
+
if (current === "*") {
|
|
1310
|
+
segments.push({ type: "wildcard", value: current, raw: current });
|
|
1311
|
+
} else {
|
|
1312
|
+
segments.push({ type: "property", value: current, raw: current });
|
|
1313
|
+
}
|
|
1314
|
+
current = "";
|
|
1315
|
+
}
|
|
1316
|
+
continue;
|
|
1317
|
+
}
|
|
1318
|
+
if (inBrackets) {
|
|
1319
|
+
bracketContent += char;
|
|
1320
|
+
} else {
|
|
1321
|
+
if (lastDot && lastDot.type !== "descendant") {
|
|
1322
|
+
current += char;
|
|
1323
|
+
} else {
|
|
1324
|
+
const l = selector.split(lastDot?.raw)[1];
|
|
1325
|
+
if (l !== void 0) {
|
|
1326
|
+
current = l;
|
|
1327
|
+
} else {
|
|
1328
|
+
current += char;
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
}
|
|
1333
|
+
if (current) {
|
|
1334
|
+
segments.push({ type: "property", value: current, raw: current });
|
|
1335
|
+
}
|
|
1336
|
+
return segments.map((segment) => {
|
|
1337
|
+
if (segment.type === "property" && typeof segment.value === "string") {
|
|
1338
|
+
if (/^-?\d+$/.test(segment.value)) {
|
|
1339
|
+
const num = parseInt(segment.value, 10);
|
|
1340
|
+
if (!isNaN(num)) {
|
|
1341
|
+
return { ...segment, type: "index", value: num };
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
1344
|
+
}
|
|
1345
|
+
return segment;
|
|
1346
|
+
});
|
|
1347
|
+
};
|
|
1348
|
+
var createFilterFunction = (expr) => {
|
|
1349
|
+
const safeExpr = expr.replace(/@\.([a-zA-Z_$][\w$]*)/g, (_, prop) => `item.${prop}`).replace(/@/g, "item");
|
|
1350
|
+
try {
|
|
1351
|
+
return new Function("item", `return ${safeExpr}`);
|
|
1352
|
+
} catch (error) {
|
|
1353
|
+
throw new Error(`Invalid filter expression: ${expr}`);
|
|
1354
|
+
}
|
|
1355
|
+
};
|
|
1356
|
+
var stringifySelector = (segments) => {
|
|
1357
|
+
const parts = [];
|
|
1358
|
+
for (let i = 0; i < segments.length; i++) {
|
|
1359
|
+
const segment = segments[i];
|
|
1360
|
+
const prevSegment = i > 0 ? segments[i - 1] : null;
|
|
1361
|
+
switch (segment.type) {
|
|
1362
|
+
case "property":
|
|
1363
|
+
const needsQuotes = /[\.\[\]\s]/.test(String(segment.value));
|
|
1364
|
+
if (needsQuotes) {
|
|
1365
|
+
parts.push(`["${segment.value}"]`);
|
|
1366
|
+
} else {
|
|
1367
|
+
if (prevSegment && prevSegment.type !== "descendant") {
|
|
1368
|
+
parts.push(".");
|
|
1369
|
+
}
|
|
1370
|
+
parts.push(String(segment.value));
|
|
1371
|
+
}
|
|
1372
|
+
break;
|
|
1373
|
+
case "index":
|
|
1374
|
+
parts.push(`[${segment.value}]`);
|
|
1375
|
+
break;
|
|
1376
|
+
case "wildcard":
|
|
1377
|
+
if (segment.value === "[]") {
|
|
1378
|
+
parts.push("[]");
|
|
1379
|
+
} else {
|
|
1380
|
+
parts.push("[*]");
|
|
1381
|
+
}
|
|
1382
|
+
break;
|
|
1383
|
+
case "slice":
|
|
1384
|
+
const [start, end] = segment.value;
|
|
1385
|
+
parts.push(`[${start}:${end === Infinity ? "" : end}]`);
|
|
1386
|
+
break;
|
|
1387
|
+
case "union":
|
|
1388
|
+
const indices = segment.value;
|
|
1389
|
+
parts.push(`[${indices.join(",")}]`);
|
|
1390
|
+
break;
|
|
1391
|
+
case "filter":
|
|
1392
|
+
parts.push(`[?(${segment.raw?.slice(2, -1) || ""})]`);
|
|
1393
|
+
break;
|
|
1394
|
+
case "descendant":
|
|
1395
|
+
parts.push("..");
|
|
1396
|
+
break;
|
|
1397
|
+
}
|
|
1398
|
+
}
|
|
1399
|
+
return parts.join("").replace(/\.\[/g, "[");
|
|
1400
|
+
};
|
|
1401
|
+
var validateSelector = (selector, options = {}) => {
|
|
1402
|
+
if (Array.isArray(selector)) {
|
|
1403
|
+
return selector.map(
|
|
1404
|
+
(sel) => validateSelector(sel, options)
|
|
1405
|
+
);
|
|
1406
|
+
}
|
|
1407
|
+
if (!/^[a-zA-Z]/.test(selector)) {
|
|
1408
|
+
return {
|
|
1409
|
+
valid: false,
|
|
1410
|
+
error: `Selector starts with '.'`,
|
|
1411
|
+
type: detectSelectorType(selector)
|
|
1412
|
+
};
|
|
1413
|
+
}
|
|
1414
|
+
if (!/[a-zA-Z]$/.test(selector)) {
|
|
1415
|
+
return {
|
|
1416
|
+
valid: false,
|
|
1417
|
+
error: `Selector end with '.'`,
|
|
1418
|
+
type: detectSelectorType(selector)
|
|
1419
|
+
};
|
|
1420
|
+
}
|
|
1421
|
+
try {
|
|
1422
|
+
const segments = parseSelector(selector, { throwOnError: true });
|
|
1423
|
+
const type = detectSelectorType(selector);
|
|
1424
|
+
if (options.allowedTypes) {
|
|
1425
|
+
if (!options.allowedTypes.includes(type)) {
|
|
1426
|
+
return {
|
|
1427
|
+
valid: false,
|
|
1428
|
+
error: `Selector type ${type} not allowed. Allowed: ${options.allowedTypes.join(
|
|
1429
|
+
", "
|
|
1430
|
+
)}`,
|
|
1431
|
+
type
|
|
1432
|
+
};
|
|
1433
|
+
}
|
|
1434
|
+
}
|
|
1435
|
+
return { valid: true, type: detectSelectorType(selector) };
|
|
1436
|
+
} catch (error) {
|
|
1437
|
+
return { valid: false, error: error.message };
|
|
1438
|
+
}
|
|
1439
|
+
};
|
|
1440
|
+
var detectSelectorType = (selector) => {
|
|
1441
|
+
if (selector.includes("..")) return "descendant";
|
|
1442
|
+
if (selector.includes("[?(")) return "filter";
|
|
1443
|
+
if (selector.includes("[:") || selector.includes("[0:")) return "slice";
|
|
1444
|
+
if (/\[[^\]]*,[^\]]*\]/.test(selector)) return "union";
|
|
1445
|
+
if (selector.includes("[*]") || selector.includes(".*.")) return "wildcard";
|
|
1446
|
+
if (selector.includes("[]")) return "array-wildcard";
|
|
1447
|
+
if (selector.includes("[") && selector.includes("]")) return "bracket";
|
|
1448
|
+
return "dot";
|
|
1449
|
+
};
|
|
1450
|
+
var createSelector = (template, replacements) => {
|
|
1451
|
+
return template.replace(/\{(\w+)\}/g, (_, key) => {
|
|
1452
|
+
if (key in replacements) {
|
|
1453
|
+
return String(replacements[key]);
|
|
1454
|
+
}
|
|
1455
|
+
return `{${key}}`;
|
|
1456
|
+
});
|
|
1457
|
+
};
|
|
1458
|
+
|
|
1459
|
+
// src/lib/object/merge.ts
|
|
1460
|
+
function merge(...sources) {
|
|
1461
|
+
if (sources.length === 0) return {};
|
|
1462
|
+
const firstItem = sources[0];
|
|
1463
|
+
const isArrayMerge = Array.isArray(firstItem);
|
|
1464
|
+
for (const source of sources) {
|
|
1465
|
+
if (source && Array.isArray(source) !== isArrayMerge) {
|
|
1466
|
+
throw new Error("Cannot mix arrays and objects in deepMerge");
|
|
1467
|
+
}
|
|
1468
|
+
}
|
|
1469
|
+
let result = isArrayMerge ? [] : {};
|
|
1470
|
+
for (const source of sources) {
|
|
1471
|
+
if (source == null) continue;
|
|
1472
|
+
if (isArrayMerge) {
|
|
1473
|
+
const resultArray = result;
|
|
1474
|
+
const sourceArray = source;
|
|
1475
|
+
for (const item of sourceArray) {
|
|
1476
|
+
resultArray.push(deepClone(item));
|
|
1477
|
+
}
|
|
1478
|
+
} else {
|
|
1479
|
+
if (!isPlainObject(source)) continue;
|
|
1480
|
+
const resultObj = result;
|
|
1481
|
+
const sourceObj = source;
|
|
1482
|
+
for (const key in sourceObj) {
|
|
1483
|
+
if (Object.prototype.hasOwnProperty.call(sourceObj, key)) {
|
|
1484
|
+
const value = sourceObj[key];
|
|
1485
|
+
const existing = resultObj[key];
|
|
1486
|
+
if (existing === void 0) {
|
|
1487
|
+
resultObj[key] = deepClone(value);
|
|
1488
|
+
} else if (Array.isArray(value) && Array.isArray(existing)) {
|
|
1489
|
+
resultObj[key] = [
|
|
1490
|
+
...existing,
|
|
1491
|
+
...value.map((item) => deepClone(item))
|
|
1492
|
+
];
|
|
1493
|
+
} else if (isPlainObject(value) && isPlainObject(existing)) {
|
|
1494
|
+
resultObj[key] = merge(existing, value);
|
|
1495
|
+
} else {
|
|
1496
|
+
resultObj[key] = deepClone(value);
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
}
|
|
1500
|
+
const symbols = Object.getOwnPropertySymbols(sourceObj);
|
|
1501
|
+
for (const symbol of symbols) {
|
|
1502
|
+
const value = sourceObj[symbol];
|
|
1503
|
+
const existing = resultObj[symbol];
|
|
1504
|
+
if (existing === void 0) {
|
|
1505
|
+
resultObj[symbol] = deepClone(value);
|
|
1506
|
+
} else if (Array.isArray(value) && Array.isArray(existing)) {
|
|
1507
|
+
resultObj[symbol] = [
|
|
1508
|
+
...existing,
|
|
1509
|
+
...value.map((item) => deepClone(item))
|
|
1510
|
+
];
|
|
1511
|
+
} else if (isPlainObject(value) && isPlainObject(existing)) {
|
|
1512
|
+
resultObj[symbol] = merge(existing, value);
|
|
1513
|
+
} else {
|
|
1514
|
+
resultObj[symbol] = deepClone(value);
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
}
|
|
1518
|
+
}
|
|
1519
|
+
return result;
|
|
1520
|
+
}
|
|
1521
|
+
function mergeArrays(arr1, arr2, options) {
|
|
1522
|
+
const { key, override = true } = options || {};
|
|
1523
|
+
if (!key) {
|
|
1524
|
+
return Array.from(/* @__PURE__ */ new Set([...arr1, ...arr2]));
|
|
1525
|
+
}
|
|
1526
|
+
const map = /* @__PURE__ */ new Map();
|
|
1527
|
+
for (const item of arr1) {
|
|
1528
|
+
map.set(item[key], item);
|
|
1529
|
+
}
|
|
1530
|
+
for (const item of arr2) {
|
|
1531
|
+
const existing = map.get(item[key]);
|
|
1532
|
+
if (!existing) {
|
|
1533
|
+
map.set(item[key], item);
|
|
1534
|
+
} else {
|
|
1535
|
+
map.set(
|
|
1536
|
+
item[key],
|
|
1537
|
+
override ? { ...existing, ...item } : { ...item, ...existing }
|
|
1538
|
+
// keep original
|
|
1539
|
+
);
|
|
1540
|
+
}
|
|
1541
|
+
}
|
|
1542
|
+
return Array.from(map.values());
|
|
1543
|
+
}
|
|
1544
|
+
function mergeInPlace(target, source) {
|
|
1545
|
+
for (const key in source) {
|
|
1546
|
+
const tVal = target[key];
|
|
1547
|
+
const sVal = source[key];
|
|
1548
|
+
const diff = diffObjectDetailed(tVal, sVal);
|
|
1549
|
+
if (Array.isArray(tVal) && Array.isArray(sVal)) {
|
|
1550
|
+
tVal.length = 0;
|
|
1551
|
+
tVal.push(...sVal);
|
|
1552
|
+
} else if (typeof tVal === "object" && typeof sVal === "object" && tVal && sVal && diff.hasChanges) {
|
|
1553
|
+
applyDiff(tVal, sVal);
|
|
1554
|
+
} else {
|
|
1555
|
+
target[key] = sVal;
|
|
1556
|
+
}
|
|
1557
|
+
}
|
|
1558
|
+
}
|
|
1559
|
+
function deepMergeWithOptions(target, ...args) {
|
|
1560
|
+
if (args.length === 0) return deepClone(target);
|
|
1561
|
+
let options = { preserveTarget: false, strict: false };
|
|
1562
|
+
let sources = args;
|
|
1563
|
+
const lastArg = args[args.length - 1];
|
|
1564
|
+
if (lastArg && typeof lastArg === "object" && ("preserveTarget" in lastArg || "strict" in lastArg)) {
|
|
1565
|
+
options = { ...options, ...lastArg };
|
|
1566
|
+
sources = args.slice(0, -1);
|
|
1567
|
+
}
|
|
1568
|
+
const result = options.preserveTarget ? target : deepClone(target);
|
|
1569
|
+
const isArrayMerge = Array.isArray(result);
|
|
1570
|
+
for (const source of sources) {
|
|
1571
|
+
if (source && Array.isArray(source) !== isArrayMerge) {
|
|
1572
|
+
throw new Error("Cannot mix arrays and objects in deepMergeWithOptions");
|
|
1573
|
+
}
|
|
1574
|
+
}
|
|
1575
|
+
for (const source of sources) {
|
|
1576
|
+
if (source == null) continue;
|
|
1577
|
+
if (isArrayMerge) {
|
|
1578
|
+
const resultArray = result;
|
|
1579
|
+
const sourceArray = source;
|
|
1580
|
+
if (options.strict) {
|
|
1581
|
+
for (let i = 0; i < resultArray.length && i < sourceArray.length; i++) {
|
|
1582
|
+
if (isPlainObject(resultArray[i]) && isPlainObject(sourceArray[i])) {
|
|
1583
|
+
resultArray[i] = deepMergeWithOptions(
|
|
1584
|
+
resultArray[i],
|
|
1585
|
+
sourceArray[i],
|
|
1586
|
+
options
|
|
1587
|
+
);
|
|
1588
|
+
} else {
|
|
1589
|
+
if (typeof resultArray[i] === typeof sourceArray[i]) {
|
|
1590
|
+
resultArray[i] = deepClone(sourceArray[i]);
|
|
1591
|
+
}
|
|
1592
|
+
}
|
|
1593
|
+
}
|
|
1594
|
+
} else {
|
|
1595
|
+
mergeInPlace(resultArray, sourceArray);
|
|
1596
|
+
}
|
|
1597
|
+
} else {
|
|
1598
|
+
if (!isPlainObject(source)) continue;
|
|
1599
|
+
const resultObj = result;
|
|
1600
|
+
const sourceObj = source;
|
|
1601
|
+
for (const key in sourceObj) {
|
|
1602
|
+
if (!Object.prototype.hasOwnProperty.call(sourceObj, key)) continue;
|
|
1603
|
+
const value = sourceObj[key];
|
|
1604
|
+
if (options.strict && !(key in resultObj)) {
|
|
1605
|
+
continue;
|
|
1606
|
+
}
|
|
1607
|
+
const existing = resultObj[key];
|
|
1608
|
+
if (existing === void 0) {
|
|
1609
|
+
resultObj[key] = deepClone(value);
|
|
1610
|
+
} else if (Array.isArray(value) && Array.isArray(existing)) {
|
|
1611
|
+
if (options.strict) {
|
|
1612
|
+
resultObj[key] = deepMergeWithOptions(existing, value, options);
|
|
1613
|
+
} else {
|
|
1614
|
+
resultObj[key] = [
|
|
1615
|
+
...existing,
|
|
1616
|
+
...value.map((item) => deepClone(item))
|
|
1617
|
+
];
|
|
1618
|
+
}
|
|
1619
|
+
} else if (isPlainObject(value) && isPlainObject(existing)) {
|
|
1620
|
+
resultObj[key] = deepMergeWithOptions(existing, value, options);
|
|
1621
|
+
} else {
|
|
1622
|
+
resultObj[key] = deepClone(value);
|
|
1623
|
+
}
|
|
1624
|
+
}
|
|
1625
|
+
const symbols = Object.getOwnPropertySymbols(sourceObj);
|
|
1626
|
+
for (const symbol of symbols) {
|
|
1627
|
+
if (options.strict && !(symbol in resultObj)) {
|
|
1628
|
+
continue;
|
|
1629
|
+
}
|
|
1630
|
+
const value = sourceObj[symbol];
|
|
1631
|
+
const existing = resultObj[symbol];
|
|
1632
|
+
if (existing === void 0) {
|
|
1633
|
+
resultObj[symbol] = deepClone(value);
|
|
1634
|
+
} else if (Array.isArray(value) && Array.isArray(existing)) {
|
|
1635
|
+
if (options.strict) {
|
|
1636
|
+
resultObj[symbol] = deepMergeWithOptions(
|
|
1637
|
+
existing,
|
|
1638
|
+
value,
|
|
1639
|
+
options
|
|
1640
|
+
);
|
|
1641
|
+
} else {
|
|
1642
|
+
resultObj[symbol] = [
|
|
1643
|
+
...existing,
|
|
1644
|
+
...value.map((item) => deepClone(item))
|
|
1645
|
+
];
|
|
1646
|
+
}
|
|
1647
|
+
} else if (isPlainObject(value) && isPlainObject(existing)) {
|
|
1648
|
+
resultObj[symbol] = deepMergeWithOptions(
|
|
1649
|
+
existing,
|
|
1650
|
+
value,
|
|
1651
|
+
options
|
|
1652
|
+
);
|
|
1653
|
+
} else {
|
|
1654
|
+
resultObj[symbol] = deepClone(value);
|
|
1655
|
+
}
|
|
1656
|
+
}
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
return result;
|
|
1660
|
+
}
|
|
1661
|
+
function deepMergeKeepReference(target, ...sources) {
|
|
1662
|
+
return deepMergeWithOptions(target, ...sources, {
|
|
1663
|
+
preserveTarget: true
|
|
1664
|
+
});
|
|
1665
|
+
}
|
|
1666
|
+
function deepMergeStrict(target, ...args) {
|
|
1667
|
+
let preserveReference = false;
|
|
1668
|
+
let sources = args;
|
|
1669
|
+
if (args.length > 0 && typeof args[args.length - 1] === "boolean") {
|
|
1670
|
+
preserveReference = args[args.length - 1];
|
|
1671
|
+
sources = args.slice(0, -1);
|
|
1672
|
+
}
|
|
1673
|
+
return deepMergeWithOptions(target, ...sources, {
|
|
1674
|
+
preserveTarget: preserveReference,
|
|
1675
|
+
strict: true
|
|
1676
|
+
});
|
|
1677
|
+
}
|
|
1678
|
+
var mergeIntoStructure = (target, selector, value) => {
|
|
1679
|
+
const segments = parseSelector(selector);
|
|
1680
|
+
let current = target;
|
|
1681
|
+
for (let i = 0; i < segments.length; i++) {
|
|
1682
|
+
const segment = segments[i];
|
|
1683
|
+
const isLast = i === segments.length - 1;
|
|
1684
|
+
switch (segment.type) {
|
|
1685
|
+
case "property":
|
|
1686
|
+
if (isLast) {
|
|
1687
|
+
current[String(segment.value)] = value;
|
|
1688
|
+
} else {
|
|
1689
|
+
if (!current[String(segment.value)]) {
|
|
1690
|
+
const nextSegment = segments[i + 1];
|
|
1691
|
+
current[String(segment.value)] = nextSegment.type === "index" || nextSegment.type === "slice" || nextSegment.type === "union" || nextSegment.type === "wildcard" ? [] : {};
|
|
1692
|
+
}
|
|
1693
|
+
current = current[String(segment.value)];
|
|
1694
|
+
}
|
|
1695
|
+
break;
|
|
1696
|
+
case "index":
|
|
1697
|
+
if (!Array.isArray(current)) {
|
|
1698
|
+
current = [];
|
|
1699
|
+
}
|
|
1700
|
+
const index = segment.value;
|
|
1701
|
+
if (isLast) {
|
|
1702
|
+
if (index >= current.length) {
|
|
1703
|
+
for (let j = current.length; j <= index; j++) {
|
|
1704
|
+
current.push(void 0);
|
|
1705
|
+
}
|
|
1706
|
+
}
|
|
1707
|
+
current[index] = value;
|
|
1708
|
+
} else {
|
|
1709
|
+
if (!current[index]) {
|
|
1710
|
+
const nextSegment = segments[i + 1];
|
|
1711
|
+
current[index] = nextSegment.type === "index" || nextSegment.type === "slice" || nextSegment.type === "union" || nextSegment.type === "wildcard" ? [] : {};
|
|
1712
|
+
}
|
|
1713
|
+
current = current[index];
|
|
1714
|
+
}
|
|
1715
|
+
break;
|
|
1716
|
+
case "wildcard":
|
|
1717
|
+
if (Array.isArray(current)) {
|
|
1718
|
+
if (isLast) {
|
|
1719
|
+
for (let j = 0; j < current.length; j++) {
|
|
1720
|
+
current[j] = value;
|
|
1721
|
+
}
|
|
1722
|
+
} else {
|
|
1723
|
+
const nextSegment = segments[i + 1];
|
|
1724
|
+
for (let j = 0; j < current.length; j++) {
|
|
1725
|
+
if (!current[j]) {
|
|
1726
|
+
current[j] = nextSegment.type === "index" || nextSegment.type === "slice" || nextSegment.type === "union" || nextSegment.type === "wildcard" ? [] : {};
|
|
1727
|
+
}
|
|
1728
|
+
}
|
|
1729
|
+
if (current.length > 0) {
|
|
1730
|
+
current = current[0];
|
|
1731
|
+
}
|
|
1732
|
+
}
|
|
1733
|
+
}
|
|
1734
|
+
break;
|
|
1735
|
+
}
|
|
1736
|
+
}
|
|
1737
|
+
};
|
|
1738
|
+
|
|
1739
|
+
// src/lib/helpers.ts
|
|
1740
|
+
var import_primitive = require("@fluixi/utils/primitive");
|
|
1741
|
+
var import_object = require("@fluixi/utils/object");
|
|
1742
|
+
var import_array = require("@fluixi/utils/array");
|
|
1743
|
+
var import_functions = require("@fluixi/utils/functions");
|
|
1744
|
+
function isPrimitive(v) {
|
|
1745
|
+
const type = typeof v;
|
|
1746
|
+
return v === null || type === "string" || type === "number" || type === "boolean" || type === "symbol" || type === "bigint" || type === "undefined" || (0, import_primitive.isDate)(v);
|
|
1747
|
+
}
|
|
1748
|
+
|
|
1749
|
+
// src/lib/object/getter-setter.ts
|
|
1750
|
+
function get(obj, selector, options = {}) {
|
|
1751
|
+
const {
|
|
1752
|
+
preserveStructure,
|
|
1753
|
+
multiple,
|
|
1754
|
+
throwOnError = false,
|
|
1755
|
+
defaultValue,
|
|
1756
|
+
maxDepth = 20
|
|
1757
|
+
} = options;
|
|
1758
|
+
if (!obj || typeof obj !== "object") {
|
|
1759
|
+
return defaultValue;
|
|
1760
|
+
}
|
|
1761
|
+
if (!selector) {
|
|
1762
|
+
return obj;
|
|
1763
|
+
}
|
|
1764
|
+
if (Array.isArray(selector)) {
|
|
1765
|
+
if (preserveStructure) {
|
|
1766
|
+
const result2 = {};
|
|
1767
|
+
for (const sel of selector) {
|
|
1768
|
+
const value = get(obj, sel, { ...options, preserveStructure: false });
|
|
1769
|
+
if (value !== void 0) {
|
|
1770
|
+
mergeIntoStructure(result2, sel, value);
|
|
1771
|
+
}
|
|
1772
|
+
}
|
|
1773
|
+
return result2;
|
|
1774
|
+
} else {
|
|
1775
|
+
return selector.map(
|
|
1776
|
+
(sel) => get(obj, sel, { ...options, preserveStructure: false })
|
|
1777
|
+
);
|
|
1778
|
+
}
|
|
1779
|
+
}
|
|
1780
|
+
const segments = parseSelector(selector, { ...options, throwOnError });
|
|
1781
|
+
if (segments.length === 0) {
|
|
1782
|
+
return obj;
|
|
1783
|
+
}
|
|
1784
|
+
if (typeof preserveStructure === "undefined" || preserveStructure === false) {
|
|
1785
|
+
const v = ValueOfKey(obj, selector);
|
|
1786
|
+
if (multiple === false && Array.isArray(v)) {
|
|
1787
|
+
return v[0];
|
|
1788
|
+
}
|
|
1789
|
+
if (v === void 0 && defaultValue !== void 0) {
|
|
1790
|
+
return defaultValue;
|
|
1791
|
+
}
|
|
1792
|
+
return v;
|
|
1793
|
+
}
|
|
1794
|
+
const result = traverseStructure(obj, segments, 0, maxDepth);
|
|
1795
|
+
if (result === void 0 && defaultValue !== void 0) {
|
|
1796
|
+
return defaultValue;
|
|
1797
|
+
}
|
|
1798
|
+
return result;
|
|
1799
|
+
}
|
|
1800
|
+
function set(_obj, first, second, third, fourth) {
|
|
1801
|
+
let obj = deepClone(_obj);
|
|
1802
|
+
const params = [];
|
|
1803
|
+
if (typeof first !== "undefined") params.push(first);
|
|
1804
|
+
if (typeof second !== "undefined") params.push(second);
|
|
1805
|
+
if (typeof third !== "undefined") params.push(third);
|
|
1806
|
+
if (typeof fourth !== "undefined") params.push(fourth);
|
|
1807
|
+
if (isPrimitive(obj)) {
|
|
1808
|
+
if (typeof first === "function") {
|
|
1809
|
+
return first(obj);
|
|
1810
|
+
}
|
|
1811
|
+
return first;
|
|
1812
|
+
}
|
|
1813
|
+
if (params.length === 1) {
|
|
1814
|
+
if (typeof params[0] === "function") {
|
|
1815
|
+
return updateWithCallBack(obj, params[0]);
|
|
1816
|
+
}
|
|
1817
|
+
if (Array.isArray(params[0])) {
|
|
1818
|
+
if (Array.isArray(obj)) {
|
|
1819
|
+
if (first.length === 0) {
|
|
1820
|
+
for (let i = 0; i < obj.length; i++) {
|
|
1821
|
+
obj.pop();
|
|
1822
|
+
}
|
|
1823
|
+
} else {
|
|
1824
|
+
const diff = diffObjectDetailed([...obj], [...params[0]]);
|
|
1825
|
+
if (diff.hasChanges) {
|
|
1826
|
+
applyDiff(obj, [...params[0]], true);
|
|
1827
|
+
}
|
|
1828
|
+
return obj;
|
|
1829
|
+
}
|
|
1830
|
+
}
|
|
1831
|
+
} else if (typeof params[0] === "object" && !Array.isArray(params[0])) {
|
|
1832
|
+
if (typeof obj === "object" && !Array.isArray(obj)) {
|
|
1833
|
+
applyDiff(obj, params[0], true);
|
|
1834
|
+
}
|
|
1835
|
+
} else {
|
|
1836
|
+
obj = params[0];
|
|
1837
|
+
}
|
|
1838
|
+
return obj;
|
|
1839
|
+
}
|
|
1840
|
+
if (Array.isArray(obj)) {
|
|
1841
|
+
if (typeof first === "string" && (isArrayOfString(second) || typeof second === "string") && params.length === 3) {
|
|
1842
|
+
if (Array.isArray(second)) {
|
|
1843
|
+
for (let i = 0; i < second.length; i++) {
|
|
1844
|
+
setValueAtPath(obj, first, third, second[i]);
|
|
1845
|
+
}
|
|
1846
|
+
return obj;
|
|
1847
|
+
}
|
|
1848
|
+
return setValueAtPath(obj, first, third, second);
|
|
1849
|
+
}
|
|
1850
|
+
if (typeof first === "string" && typeof second === "function" && params.length === 2) {
|
|
1851
|
+
return setValueAtPath(obj, first, second);
|
|
1852
|
+
}
|
|
1853
|
+
}
|
|
1854
|
+
if ((typeof first === "string" || typeof first === "number") && (params.length === 2 || params.length === 3)) {
|
|
1855
|
+
if (isRangeObject(second)) {
|
|
1856
|
+
return setValueBetweenRange(
|
|
1857
|
+
obj,
|
|
1858
|
+
first,
|
|
1859
|
+
second,
|
|
1860
|
+
third
|
|
1861
|
+
);
|
|
1862
|
+
}
|
|
1863
|
+
if (typeof first === "number") {
|
|
1864
|
+
if (typeof third !== "undefined") {
|
|
1865
|
+
return setValueAtPath(obj, `[${first}].${second}`, third);
|
|
1866
|
+
} else {
|
|
1867
|
+
return setValueAtPath(obj, `[${first}]`, second, third);
|
|
1868
|
+
}
|
|
1869
|
+
}
|
|
1870
|
+
if (typeof second === "number" && typeof third !== "undefined") {
|
|
1871
|
+
return setValueAtIndex(obj, first, second, third);
|
|
1872
|
+
}
|
|
1873
|
+
if (Array.isArray(second) && second.length > 0 && second.every((s) => typeof s === "number")) {
|
|
1874
|
+
return setValueWithIndices(obj, first, second, third);
|
|
1875
|
+
}
|
|
1876
|
+
if (typeof third !== "undefined" && typeof second === "function") {
|
|
1877
|
+
return setValueWithFilter(obj, first, second, third);
|
|
1878
|
+
}
|
|
1879
|
+
return setValueAtPath(obj, first, second, third);
|
|
1880
|
+
}
|
|
1881
|
+
if (typeof first === "string" && typeof second === "number") {
|
|
1882
|
+
if (params.length === 3) {
|
|
1883
|
+
return setValueAtIndex(obj, first, second, third);
|
|
1884
|
+
} else if (params.length === 4) {
|
|
1885
|
+
return setIndexWithSubPath(
|
|
1886
|
+
obj,
|
|
1887
|
+
first,
|
|
1888
|
+
second,
|
|
1889
|
+
third,
|
|
1890
|
+
fourth
|
|
1891
|
+
);
|
|
1892
|
+
}
|
|
1893
|
+
}
|
|
1894
|
+
if (typeof first === "string" && Array.isArray(second)) {
|
|
1895
|
+
if (params.length === 3) {
|
|
1896
|
+
return setValueWithIndices(obj, first, second, third);
|
|
1897
|
+
} else if (params.length === 4) {
|
|
1898
|
+
return setValueWithIndicesWithSubPath(
|
|
1899
|
+
obj,
|
|
1900
|
+
first,
|
|
1901
|
+
second,
|
|
1902
|
+
third,
|
|
1903
|
+
fourth
|
|
1904
|
+
);
|
|
1905
|
+
}
|
|
1906
|
+
}
|
|
1907
|
+
if (typeof first === "string" && typeof second === "object" && !Array.isArray(second) && second !== null) {
|
|
1908
|
+
const range = second;
|
|
1909
|
+
if ("from" in range || "to" in range) {
|
|
1910
|
+
if (params.length === 3) {
|
|
1911
|
+
return setValueBetweenRange(obj, first, range, third);
|
|
1912
|
+
} else if (params.length === 4) {
|
|
1913
|
+
return setValueBetweenRangeWithSubPath(
|
|
1914
|
+
obj,
|
|
1915
|
+
first,
|
|
1916
|
+
range,
|
|
1917
|
+
third,
|
|
1918
|
+
fourth
|
|
1919
|
+
);
|
|
1920
|
+
}
|
|
1921
|
+
}
|
|
1922
|
+
}
|
|
1923
|
+
if (typeof first === "object" && !Array.isArray(first) && params.length === 1) {
|
|
1924
|
+
return applyDiff(obj, first, true);
|
|
1925
|
+
}
|
|
1926
|
+
if (typeof first === "string" && typeof second === "object" && !Array.isArray(second) && params.length === 2) {
|
|
1927
|
+
return mergeUpdateAtPath(obj, first, second);
|
|
1928
|
+
}
|
|
1929
|
+
if (typeof first === "string" && typeof second === "function") {
|
|
1930
|
+
if (params.length === 3) {
|
|
1931
|
+
return setValueWithFilter(
|
|
1932
|
+
obj,
|
|
1933
|
+
first,
|
|
1934
|
+
second,
|
|
1935
|
+
third
|
|
1936
|
+
);
|
|
1937
|
+
} else if (params.length === 4) {
|
|
1938
|
+
return setValueWithFilterWithSubPath(
|
|
1939
|
+
obj,
|
|
1940
|
+
first,
|
|
1941
|
+
second,
|
|
1942
|
+
third,
|
|
1943
|
+
fourth
|
|
1944
|
+
);
|
|
1945
|
+
}
|
|
1946
|
+
}
|
|
1947
|
+
if (typeof first === "function" && typeof second === "string") {
|
|
1948
|
+
if (params.length === 3) {
|
|
1949
|
+
return setArrayValueWithFilter(
|
|
1950
|
+
obj,
|
|
1951
|
+
first,
|
|
1952
|
+
second,
|
|
1953
|
+
third
|
|
1954
|
+
);
|
|
1955
|
+
} else if (params.length === 4) {
|
|
1956
|
+
return setValueWithFilterWithSubPath(
|
|
1957
|
+
obj,
|
|
1958
|
+
second,
|
|
1959
|
+
first,
|
|
1960
|
+
third,
|
|
1961
|
+
fourth
|
|
1962
|
+
);
|
|
1963
|
+
}
|
|
1964
|
+
}
|
|
1965
|
+
if (Array.isArray(first)) {
|
|
1966
|
+
if (params.length === 2 && typeof second === "object" && !Array.isArray(second)) {
|
|
1967
|
+
return setValueWithArraySelectors(obj, first, second);
|
|
1968
|
+
} else if (params.length === 2) {
|
|
1969
|
+
return setValueWithArraySelectors(obj, first, second);
|
|
1970
|
+
}
|
|
1971
|
+
}
|
|
1972
|
+
throw new Error(`Invalid set parameters: ${JSON.stringify(params)}`);
|
|
1973
|
+
}
|
|
1974
|
+
function handleFunctionParams(selector, data, value, isArray2 = false) {
|
|
1975
|
+
const paths = parseSelector(selector);
|
|
1976
|
+
if (paths.length === 0) return value;
|
|
1977
|
+
const isWildcard = paths[paths.length - 1].type === "wildcard";
|
|
1978
|
+
const containWildcard = paths.some((path) => path.type === "wildcard") && Array.isArray(data);
|
|
1979
|
+
let finalValue;
|
|
1980
|
+
if (isWildcard || containWildcard && isArray2 || containWildcard && paths.length === 2) {
|
|
1981
|
+
if (Array.isArray(data)) {
|
|
1982
|
+
for (let i = 0; i < data.length; i++) {
|
|
1983
|
+
const v = typeof value === "function" ? value(data[i]) : typeof data[i] === "object" ? applyDiff(data[i], value, true) : value;
|
|
1984
|
+
if (!finalValue) finalValue = [];
|
|
1985
|
+
finalValue.push(v);
|
|
1986
|
+
}
|
|
1987
|
+
}
|
|
1988
|
+
}
|
|
1989
|
+
if (!isWildcard && !finalValue) {
|
|
1990
|
+
finalValue = typeof value === "function" ? value(data) : value;
|
|
1991
|
+
}
|
|
1992
|
+
return finalValue;
|
|
1993
|
+
}
|
|
1994
|
+
function isRangeObject(obj) {
|
|
1995
|
+
return typeof obj === "object" && obj !== null && "from" in obj && "to" in obj && typeof obj.from === "number" && typeof obj.to === "number" && obj.from < obj.to && obj.from >= 0 && obj.to > 0;
|
|
1996
|
+
}
|
|
1997
|
+
function isArrayOfString(value) {
|
|
1998
|
+
return Array.isArray(value) && value.every((item) => typeof item === "string");
|
|
1999
|
+
}
|
|
2000
|
+
function setValueAtPath(obj, selector, value, subPath) {
|
|
2001
|
+
const paths = parseSelector(selector);
|
|
2002
|
+
let target = ValueOfKey(obj, selector, {
|
|
2003
|
+
paths,
|
|
2004
|
+
createMissing: true
|
|
2005
|
+
});
|
|
2006
|
+
let get_value;
|
|
2007
|
+
if (subPath) {
|
|
2008
|
+
if (typeof value === "function") {
|
|
2009
|
+
get_value = get(target, subPath);
|
|
2010
|
+
}
|
|
2011
|
+
}
|
|
2012
|
+
const data = subPath ? get_value : target;
|
|
2013
|
+
const finalValue = handleFunctionParams(
|
|
2014
|
+
selector,
|
|
2015
|
+
data,
|
|
2016
|
+
value,
|
|
2017
|
+
Array.isArray(obj)
|
|
2018
|
+
);
|
|
2019
|
+
if (subPath) {
|
|
2020
|
+
const result = setAtPath(obj, `${selector}.${subPath}`, finalValue, {
|
|
2021
|
+
createMissing: true
|
|
2022
|
+
});
|
|
2023
|
+
return result;
|
|
2024
|
+
} else {
|
|
2025
|
+
const r = setAtPath(obj, selector, finalValue, { createMissing: true });
|
|
2026
|
+
return r;
|
|
2027
|
+
}
|
|
2028
|
+
}
|
|
2029
|
+
function setValueAtIndex(obj, selector, index, value) {
|
|
2030
|
+
const target = ValueOfKey(obj, selector, { createMissing: true });
|
|
2031
|
+
if (!Array.isArray(target)) {
|
|
2032
|
+
throw new Error(`Target at ${selector} is not an array ${index}`);
|
|
2033
|
+
}
|
|
2034
|
+
const target_copy = structuredClone(target);
|
|
2035
|
+
const finalValue = handleFunctionParams(selector, target_copy[index], value);
|
|
2036
|
+
if (index < 0) {
|
|
2037
|
+
const actualIndex = target.length + index;
|
|
2038
|
+
if (actualIndex < 0) {
|
|
2039
|
+
throw new Error(`Index ${index} is out of bounds`);
|
|
2040
|
+
}
|
|
2041
|
+
return setAtPath(obj, `${selector}[${actualIndex}]`, finalValue, {
|
|
2042
|
+
createMissing: true
|
|
2043
|
+
});
|
|
2044
|
+
}
|
|
2045
|
+
return setAtPath(obj, `${selector}[${index}]`, finalValue, {
|
|
2046
|
+
createMissing: true
|
|
2047
|
+
});
|
|
2048
|
+
}
|
|
2049
|
+
var setIndexWithSubPath = (obj, selector, index, subPath, value) => {
|
|
2050
|
+
if (typeof subPath === "function") {
|
|
2051
|
+
return setValueAtPath(obj, `${selector}[${index}]`, subPath);
|
|
2052
|
+
}
|
|
2053
|
+
if (typeof subPath === "string") {
|
|
2054
|
+
return setValueAtPath(obj, `${selector}[${index}]`, value, subPath);
|
|
2055
|
+
}
|
|
2056
|
+
return null;
|
|
2057
|
+
};
|
|
2058
|
+
var setValueWithIndices = (obj, selector, indices, value) => {
|
|
2059
|
+
const target = ValueOfKey(obj, selector);
|
|
2060
|
+
if (!Array.isArray(target)) {
|
|
2061
|
+
throw new Error(`Target at ${selector} is not an array`);
|
|
2062
|
+
}
|
|
2063
|
+
for (const index of indices) {
|
|
2064
|
+
if (typeof index === "number") {
|
|
2065
|
+
setValueAtIndex(obj, selector, index, value);
|
|
2066
|
+
} else if (typeof index === "string") {
|
|
2067
|
+
setValueAtPath(obj, selector, value, index);
|
|
2068
|
+
}
|
|
2069
|
+
}
|
|
2070
|
+
return obj;
|
|
2071
|
+
};
|
|
2072
|
+
var setValueWithIndicesWithSubPath = (obj, selector, indices, subPath, value) => {
|
|
2073
|
+
const updater = (prev) => {
|
|
2074
|
+
const newValue = typeof value === "function" ? value(prev ? ValueOfKey(prev, subPath) : void 0) : value;
|
|
2075
|
+
const result = prev ? { ...prev } : {};
|
|
2076
|
+
setAtPath(result, subPath, newValue);
|
|
2077
|
+
return result;
|
|
2078
|
+
};
|
|
2079
|
+
return setValueWithIndices(obj, selector, indices, updater);
|
|
2080
|
+
};
|
|
2081
|
+
function setValueBetweenRange(obj, selector, range, value) {
|
|
2082
|
+
const target = ValueOfKey(obj, selector);
|
|
2083
|
+
if (!Array.isArray(target)) {
|
|
2084
|
+
throw new Error(`Target at ${selector} is not an array`);
|
|
2085
|
+
}
|
|
2086
|
+
const from = range.from ?? 0;
|
|
2087
|
+
const to = range.to ?? target.length - 1;
|
|
2088
|
+
const by = range.by ?? 1;
|
|
2089
|
+
const indices = [];
|
|
2090
|
+
for (let i = from; i <= to; i += by) {
|
|
2091
|
+
if (i >= 0 && i < target.length) {
|
|
2092
|
+
indices.push(i);
|
|
2093
|
+
}
|
|
2094
|
+
}
|
|
2095
|
+
return setValueWithIndices(obj, selector, indices, value);
|
|
2096
|
+
}
|
|
2097
|
+
var setValueBetweenRangeWithSubPath = (obj, selector, range, subPath, value) => {
|
|
2098
|
+
const updater = (prev) => {
|
|
2099
|
+
const newValue = typeof value === "function" ? value(prev ? ValueOfKey(prev, subPath) : void 0) : value;
|
|
2100
|
+
const result = prev ? { ...prev } : {};
|
|
2101
|
+
setAtPath(result, subPath, newValue);
|
|
2102
|
+
return result;
|
|
2103
|
+
};
|
|
2104
|
+
return setValueBetweenRange(obj, selector, range, updater);
|
|
2105
|
+
};
|
|
2106
|
+
function castProps(arr, props) {
|
|
2107
|
+
return Object.assign(arr, props);
|
|
2108
|
+
}
|
|
2109
|
+
function setValueWithFilter(obj, selector, filterFn, value) {
|
|
2110
|
+
const target = ValueOfKey(obj, selector);
|
|
2111
|
+
if (!Array.isArray(target)) {
|
|
2112
|
+
throw new Error(`Target at ${selector} is not an array`);
|
|
2113
|
+
}
|
|
2114
|
+
const indices = [];
|
|
2115
|
+
const target_copy = [...target];
|
|
2116
|
+
target.forEach((item, index) => {
|
|
2117
|
+
if (filterFn(item, index)) {
|
|
2118
|
+
indices.push(index);
|
|
2119
|
+
}
|
|
2120
|
+
});
|
|
2121
|
+
return setValueWithIndices(obj, selector, indices, value);
|
|
2122
|
+
}
|
|
2123
|
+
function setArrayValueWithFilter(obj, filterFn, selector, value) {
|
|
2124
|
+
let target = obj;
|
|
2125
|
+
if (!Array.isArray(target)) {
|
|
2126
|
+
throw new Error(`Target at ${selector} is not an array`);
|
|
2127
|
+
}
|
|
2128
|
+
const indices = [];
|
|
2129
|
+
const target_copy = [...target];
|
|
2130
|
+
target.forEach((item, index) => {
|
|
2131
|
+
const n = setAtPath(item, selector, value, { createMissing: true });
|
|
2132
|
+
item = n;
|
|
2133
|
+
});
|
|
2134
|
+
return target;
|
|
2135
|
+
}
|
|
2136
|
+
function setValueWithFilterWithSubPath(obj, selector, filterFn, subPath, value) {
|
|
2137
|
+
const updater = (prev) => {
|
|
2138
|
+
const newValue = typeof value === "function" ? value(prev ? ValueOfKey(prev, subPath) : void 0) : value;
|
|
2139
|
+
const result = prev ? { ...prev } : {};
|
|
2140
|
+
setAtPath(result, subPath, newValue);
|
|
2141
|
+
return result;
|
|
2142
|
+
};
|
|
2143
|
+
return setValueWithFilter(obj, selector, filterFn, updater);
|
|
2144
|
+
}
|
|
2145
|
+
function mergeUpdateAtPath(obj, selector, updates) {
|
|
2146
|
+
const current = ValueOfKey(obj, selector, { createMissing: true });
|
|
2147
|
+
const merged = applyDiff(current || {}, updates, true);
|
|
2148
|
+
return setAtPath(obj, selector, merged, { createMissing: true });
|
|
2149
|
+
}
|
|
2150
|
+
function setValueWithArraySelectors(obj, selectors, value) {
|
|
2151
|
+
if (typeof value === "object" && !Array.isArray(value) && value !== null) {
|
|
2152
|
+
for (const [path, val] of Object.entries(value)) {
|
|
2153
|
+
if (selectors.includes(path)) {
|
|
2154
|
+
setAtPath(obj, path, val, { createMissing: true });
|
|
2155
|
+
}
|
|
2156
|
+
}
|
|
2157
|
+
} else {
|
|
2158
|
+
for (const selector of selectors) {
|
|
2159
|
+
const finalValue = handleFunctionParams(
|
|
2160
|
+
selector,
|
|
2161
|
+
ValueOfKey(obj, selector),
|
|
2162
|
+
value
|
|
2163
|
+
);
|
|
2164
|
+
setAtPath(obj, selector, finalValue, { createMissing: true });
|
|
2165
|
+
}
|
|
2166
|
+
}
|
|
2167
|
+
return obj;
|
|
2168
|
+
}
|
|
2169
|
+
function ValueOfKey(obj, selector, options = {}) {
|
|
2170
|
+
const { createMissing = false, add = false, debug = false } = options;
|
|
2171
|
+
const segments = parseSelector(selector);
|
|
2172
|
+
let current = obj;
|
|
2173
|
+
let updateEvenIfNotlast = false;
|
|
2174
|
+
let chained_property = "";
|
|
2175
|
+
const stringify = selector;
|
|
2176
|
+
if (typeof current === "object" && !Array.isArray(current) && stringify.length > 0) {
|
|
2177
|
+
if (current[stringify]) {
|
|
2178
|
+
current = current[stringify];
|
|
2179
|
+
return current;
|
|
2180
|
+
}
|
|
2181
|
+
}
|
|
2182
|
+
for (let i = 0; i < segments.length; i++) {
|
|
2183
|
+
const segment = segments[i];
|
|
2184
|
+
const isLast = segments.length > 1 ? i === segments.length - 1 : false;
|
|
2185
|
+
if (current === null && !createMissing) {
|
|
2186
|
+
return void 0;
|
|
2187
|
+
}
|
|
2188
|
+
const nextSegment = segments[i + 1];
|
|
2189
|
+
switch (segment.type) {
|
|
2190
|
+
case "property":
|
|
2191
|
+
if (current && typeof current === "object") {
|
|
2192
|
+
if (Array.isArray(current)) {
|
|
2193
|
+
if (typeof segment.value === "number") {
|
|
2194
|
+
const index = segment.value;
|
|
2195
|
+
if (createMissing && index >= current.length) {
|
|
2196
|
+
for (let j = current.length; j <= index; j++) {
|
|
2197
|
+
current.push(void 0);
|
|
2198
|
+
}
|
|
2199
|
+
}
|
|
2200
|
+
if (createMissing && (current[index] === null || current[index] === void 0) && (!isLast || updateEvenIfNotlast)) {
|
|
2201
|
+
if (nextSegment) {
|
|
2202
|
+
current[index] = nextSegment.type === "index" || nextSegment.type === "slice" || nextSegment.type === "union" || nextSegment.type === "wildcard" ? [] : {};
|
|
2203
|
+
} else {
|
|
2204
|
+
current[index] = null;
|
|
2205
|
+
}
|
|
2206
|
+
updateEvenIfNotlast = false;
|
|
2207
|
+
}
|
|
2208
|
+
current = current[index];
|
|
2209
|
+
if (nextSegment) {
|
|
2210
|
+
updateEvenIfNotlast = true;
|
|
2211
|
+
continue;
|
|
2212
|
+
}
|
|
2213
|
+
} else if (add) {
|
|
2214
|
+
current.push({
|
|
2215
|
+
[String(segment.value)]: null
|
|
2216
|
+
});
|
|
2217
|
+
} else {
|
|
2218
|
+
current = current.map((item) => item[String(segment.value)]);
|
|
2219
|
+
}
|
|
2220
|
+
if (nextSegment) {
|
|
2221
|
+
continue;
|
|
2222
|
+
}
|
|
2223
|
+
return filterDefined(current);
|
|
2224
|
+
} else {
|
|
2225
|
+
if (current[chained_property] && chained_property.split(".").length >= 2) {
|
|
2226
|
+
current = current[chained_property];
|
|
2227
|
+
continue;
|
|
2228
|
+
}
|
|
2229
|
+
if (createMissing && (current[String(segment.value)] === null || current[String(segment.value)] === void 0) && (!isLast || updateEvenIfNotlast)) {
|
|
2230
|
+
if (nextSegment) {
|
|
2231
|
+
current[String(segment.value)] = nextSegment.type === "index" || nextSegment.type === "slice" || nextSegment.type === "union" || nextSegment.type === "wildcard" ? [] : {};
|
|
2232
|
+
} else {
|
|
2233
|
+
current[String(segment.value)] = null;
|
|
2234
|
+
}
|
|
2235
|
+
updateEvenIfNotlast = false;
|
|
2236
|
+
current = current[String(segment.value)];
|
|
2237
|
+
if (nextSegment) {
|
|
2238
|
+
updateEvenIfNotlast = true;
|
|
2239
|
+
continue;
|
|
2240
|
+
}
|
|
2241
|
+
}
|
|
2242
|
+
if (!current) {
|
|
2243
|
+
current = { [String(segment.value)]: null };
|
|
2244
|
+
} else {
|
|
2245
|
+
if (!current[String(segment.value)] && createMissing) {
|
|
2246
|
+
current[String(segment.value)] = null;
|
|
2247
|
+
}
|
|
2248
|
+
current = current[String(segment.value)];
|
|
2249
|
+
}
|
|
2250
|
+
if (nextSegment) {
|
|
2251
|
+
continue;
|
|
2252
|
+
}
|
|
2253
|
+
}
|
|
2254
|
+
} else if (createMissing) {
|
|
2255
|
+
current = {};
|
|
2256
|
+
current[String(segment.value)] = nextSegment.type === "index" || nextSegment.type === "slice" || nextSegment.type === "union" || nextSegment.type === "wildcard" || options.type === "array" ? [] : {};
|
|
2257
|
+
current = current[String(segment.value)];
|
|
2258
|
+
} else {
|
|
2259
|
+
return void 0;
|
|
2260
|
+
}
|
|
2261
|
+
break;
|
|
2262
|
+
case "index":
|
|
2263
|
+
case "wildcard":
|
|
2264
|
+
if (Array.isArray(current)) {
|
|
2265
|
+
if (segment.type === "index") {
|
|
2266
|
+
const index = segment.value;
|
|
2267
|
+
if (index < 0) {
|
|
2268
|
+
current = current[current.length - Math.abs(index)];
|
|
2269
|
+
continue;
|
|
2270
|
+
}
|
|
2271
|
+
if (createMissing && (index >= current.length || current[index] == null || current[index] == void 0) && !isLast) {
|
|
2272
|
+
for (let j = current.length; j <= index; j++) {
|
|
2273
|
+
current.push(void 0);
|
|
2274
|
+
}
|
|
2275
|
+
if (nextSegment) {
|
|
2276
|
+
current[index] = nextSegment.type === "index" || nextSegment.type === "slice" || nextSegment.type === "union" || nextSegment.type === "wildcard" ? [] : {};
|
|
2277
|
+
}
|
|
2278
|
+
}
|
|
2279
|
+
current = current[index];
|
|
2280
|
+
} else {
|
|
2281
|
+
if (!isLast) {
|
|
2282
|
+
continue;
|
|
2283
|
+
}
|
|
2284
|
+
}
|
|
2285
|
+
} else if (createMissing) {
|
|
2286
|
+
const nextSegment2 = segments[i + 1];
|
|
2287
|
+
current = [];
|
|
2288
|
+
const index = segment.value;
|
|
2289
|
+
for (let j = 0; j <= index; j++) {
|
|
2290
|
+
current.push(void 0);
|
|
2291
|
+
}
|
|
2292
|
+
current[index] = nextSegment2.type === "index" || nextSegment2.type === "slice" || nextSegment2.type === "union" || nextSegment2.type === "wildcard" || options.type === "array" ? [] : {};
|
|
2293
|
+
current = current[index];
|
|
2294
|
+
} else {
|
|
2295
|
+
if (segment.type === "index" && typeof current === "object") {
|
|
2296
|
+
if (current && current[String(segment.value)] !== void 0) {
|
|
2297
|
+
current = current[String(segment.value)];
|
|
2298
|
+
continue;
|
|
2299
|
+
}
|
|
2300
|
+
}
|
|
2301
|
+
if (typeof current === "object") {
|
|
2302
|
+
const values = Object.values(current);
|
|
2303
|
+
current = values;
|
|
2304
|
+
continue;
|
|
2305
|
+
}
|
|
2306
|
+
return void 0;
|
|
2307
|
+
}
|
|
2308
|
+
break;
|
|
2309
|
+
default:
|
|
2310
|
+
return void 0;
|
|
2311
|
+
}
|
|
2312
|
+
}
|
|
2313
|
+
return filterDefined(current);
|
|
2314
|
+
}
|
|
2315
|
+
function filterDefined(obj) {
|
|
2316
|
+
if (Array.isArray(obj)) {
|
|
2317
|
+
return obj.filter((item) => typeof item !== "undefined");
|
|
2318
|
+
}
|
|
2319
|
+
return obj;
|
|
2320
|
+
}
|
|
2321
|
+
var setAtPath = (obj, selector, value, options = {}) => {
|
|
2322
|
+
if (selector === "[*]") {
|
|
2323
|
+
return applyDiff(obj, value, true);
|
|
2324
|
+
}
|
|
2325
|
+
const { createMissing = true } = options;
|
|
2326
|
+
const segments = parseSelector(selector);
|
|
2327
|
+
if (segments.length === 0) {
|
|
2328
|
+
return obj;
|
|
2329
|
+
}
|
|
2330
|
+
const lastSegment = segments[segments.length - 1];
|
|
2331
|
+
let current = obj;
|
|
2332
|
+
for (let i = 0; i < segments.length - 1; i++) {
|
|
2333
|
+
const segment = segments[i];
|
|
2334
|
+
if (current == null) {
|
|
2335
|
+
if (!createMissing) return obj;
|
|
2336
|
+
throw new Error(`Cannot navigate through null/undefined at segment ${i}`);
|
|
2337
|
+
}
|
|
2338
|
+
const nextSegment = segments[i + 1];
|
|
2339
|
+
const shouldBeArray = nextSegment.type === "index" || nextSegment.type === "slice" || nextSegment.type === "union" || nextSegment.type === "wildcard";
|
|
2340
|
+
if (segment.type === "property") {
|
|
2341
|
+
if (Array.isArray(current) && typeof typeof segment.value === "number") {
|
|
2342
|
+
const index = segment.value;
|
|
2343
|
+
if (index < 0) {
|
|
2344
|
+
throw new Error(`Negative index ${index} not allowed`);
|
|
2345
|
+
}
|
|
2346
|
+
if (index >= current.length && createMissing) {
|
|
2347
|
+
for (let j = current.length; j <= index; j++) {
|
|
2348
|
+
current.push(void 0);
|
|
2349
|
+
}
|
|
2350
|
+
}
|
|
2351
|
+
if ((current[index] === null || current[index] === void 0) && createMissing) {
|
|
2352
|
+
current[index] = shouldBeArray ? [] : {};
|
|
2353
|
+
}
|
|
2354
|
+
current = current[index];
|
|
2355
|
+
} else {
|
|
2356
|
+
if ((current[String(segment.value)] === null || current[String(segment.value)] === void 0) && createMissing) {
|
|
2357
|
+
current[String(segment.value)] = shouldBeArray ? [] : {};
|
|
2358
|
+
}
|
|
2359
|
+
if (typeof current === "object" && !Array.isArray(current)) {
|
|
2360
|
+
current = current[String(segment.value)];
|
|
2361
|
+
}
|
|
2362
|
+
}
|
|
2363
|
+
if (nextSegment) {
|
|
2364
|
+
continue;
|
|
2365
|
+
}
|
|
2366
|
+
break;
|
|
2367
|
+
} else if (segment.type === "index" || segment.type === "wildcard") {
|
|
2368
|
+
if (!Array.isArray(current)) {
|
|
2369
|
+
if (!createMissing) return obj;
|
|
2370
|
+
current = [];
|
|
2371
|
+
}
|
|
2372
|
+
if (segment.type === "index") {
|
|
2373
|
+
const index = segment.value;
|
|
2374
|
+
if (index < 0) {
|
|
2375
|
+
throw new Error(`Negative index ${index} not allowed`);
|
|
2376
|
+
}
|
|
2377
|
+
if (index >= current.length && createMissing) {
|
|
2378
|
+
for (let j = current.length; j <= index; j++) {
|
|
2379
|
+
current.push(void 0);
|
|
2380
|
+
}
|
|
2381
|
+
}
|
|
2382
|
+
if ((current[index] === null || current[index] === void 0) && createMissing) {
|
|
2383
|
+
current[index] = shouldBeArray ? [] : void 0;
|
|
2384
|
+
}
|
|
2385
|
+
current = current[index];
|
|
2386
|
+
}
|
|
2387
|
+
if (nextSegment) {
|
|
2388
|
+
continue;
|
|
2389
|
+
}
|
|
2390
|
+
break;
|
|
2391
|
+
} else {
|
|
2392
|
+
throw new Error(`Invalid segment type for set: ${segment.type}`);
|
|
2393
|
+
}
|
|
2394
|
+
}
|
|
2395
|
+
switch (lastSegment.type) {
|
|
2396
|
+
case "property":
|
|
2397
|
+
if (Array.isArray(current) && typeof lastSegment.value === "number") {
|
|
2398
|
+
const index2 = lastSegment.value;
|
|
2399
|
+
if (index2 < 0) {
|
|
2400
|
+
throw new Error(`Negative index ${index2} not allowed`);
|
|
2401
|
+
}
|
|
2402
|
+
if (index2 >= current.length) {
|
|
2403
|
+
for (let j = current.length; j <= index2; j++) {
|
|
2404
|
+
current.push(void 0);
|
|
2405
|
+
}
|
|
2406
|
+
}
|
|
2407
|
+
if (Array.isArray(value)) {
|
|
2408
|
+
current[index2] = applyDiff(
|
|
2409
|
+
structuredClone(current[index2]),
|
|
2410
|
+
value[index2],
|
|
2411
|
+
true
|
|
2412
|
+
);
|
|
2413
|
+
} else {
|
|
2414
|
+
current[index2] = value;
|
|
2415
|
+
}
|
|
2416
|
+
} else {
|
|
2417
|
+
if (!current) current = {};
|
|
2418
|
+
if (Array.isArray(current)) {
|
|
2419
|
+
const paths = selector.split(".");
|
|
2420
|
+
let ob;
|
|
2421
|
+
if (paths.length >= 2) {
|
|
2422
|
+
paths.shift();
|
|
2423
|
+
for (let i = 0; i < current.length; i++) {
|
|
2424
|
+
if (Array.isArray(value)) {
|
|
2425
|
+
ob = fromPath(paths.join("."), value[i]);
|
|
2426
|
+
current[i] = typeof current[i] === "object" ? applyDiff(current[i], ob, true) : ob;
|
|
2427
|
+
} else {
|
|
2428
|
+
ob = fromPath(paths.join("."), value);
|
|
2429
|
+
current[i] = typeof current[i] === "object" ? applyDiff(current[i], ob, true) : ob;
|
|
2430
|
+
}
|
|
2431
|
+
}
|
|
2432
|
+
} else {
|
|
2433
|
+
ob = {
|
|
2434
|
+
[String(lastSegment.value)]: value
|
|
2435
|
+
};
|
|
2436
|
+
}
|
|
2437
|
+
} else {
|
|
2438
|
+
if (Array.isArray(value) && value.length === 0) {
|
|
2439
|
+
if (typeof current[String(lastSegment.value)] === "undefined") {
|
|
2440
|
+
current[String(lastSegment.value)] = [];
|
|
2441
|
+
} else {
|
|
2442
|
+
current[String(lastSegment.value)] = [];
|
|
2443
|
+
}
|
|
2444
|
+
} else {
|
|
2445
|
+
current[String(lastSegment.value)] = value;
|
|
2446
|
+
}
|
|
2447
|
+
}
|
|
2448
|
+
}
|
|
2449
|
+
break;
|
|
2450
|
+
case "index":
|
|
2451
|
+
case "wildcard":
|
|
2452
|
+
if (!Array.isArray(current)) {
|
|
2453
|
+
throw new Error(`Cannot use index on non-array for final segment`);
|
|
2454
|
+
}
|
|
2455
|
+
const index = lastSegment.value;
|
|
2456
|
+
if (lastSegment.type === "index") {
|
|
2457
|
+
if (index < 0) {
|
|
2458
|
+
throw new Error(`Negative index ${index} not allowed`);
|
|
2459
|
+
}
|
|
2460
|
+
if (index >= current.length) {
|
|
2461
|
+
for (let j = current.length; j <= index; j++) {
|
|
2462
|
+
current.push(void 0);
|
|
2463
|
+
}
|
|
2464
|
+
}
|
|
2465
|
+
if (Array.isArray(value)) {
|
|
2466
|
+
if (typeof current[index] === typeof value[index]) {
|
|
2467
|
+
try {
|
|
2468
|
+
current[index] = applyDiff(
|
|
2469
|
+
deepClone(current[index]),
|
|
2470
|
+
value[index],
|
|
2471
|
+
true
|
|
2472
|
+
);
|
|
2473
|
+
} catch (e) {
|
|
2474
|
+
throw Error("expected on array");
|
|
2475
|
+
}
|
|
2476
|
+
} else {
|
|
2477
|
+
current[index] = value;
|
|
2478
|
+
}
|
|
2479
|
+
} else {
|
|
2480
|
+
current[index] = value;
|
|
2481
|
+
}
|
|
2482
|
+
} else if (lastSegment.type === "wildcard") {
|
|
2483
|
+
if (Array.isArray(value)) {
|
|
2484
|
+
for (let i = 0; i < current.length; i++) {
|
|
2485
|
+
if (typeof current[i] === "object") {
|
|
2486
|
+
current[i] = applyDiff(current[i], value[i], true);
|
|
2487
|
+
} else {
|
|
2488
|
+
current[i] = value[i];
|
|
2489
|
+
}
|
|
2490
|
+
}
|
|
2491
|
+
} else {
|
|
2492
|
+
for (let i = 0; i < current.length; i++) {
|
|
2493
|
+
if (typeof current[i] === "object") {
|
|
2494
|
+
current[i] = applyDiff(current[i], value, true);
|
|
2495
|
+
} else {
|
|
2496
|
+
current[i] = value;
|
|
2497
|
+
}
|
|
2498
|
+
}
|
|
2499
|
+
}
|
|
2500
|
+
}
|
|
2501
|
+
break;
|
|
2502
|
+
default:
|
|
2503
|
+
throw new Error(
|
|
2504
|
+
`Invalid final segment type for set: ${lastSegment.type}`
|
|
2505
|
+
);
|
|
2506
|
+
}
|
|
2507
|
+
return obj;
|
|
2508
|
+
};
|
|
2509
|
+
var fromPath = (path, value) => {
|
|
2510
|
+
const segments = parseSelector(path);
|
|
2511
|
+
if (segments.length === 0) {
|
|
2512
|
+
return value;
|
|
2513
|
+
}
|
|
2514
|
+
let result = value;
|
|
2515
|
+
for (let i = segments.length - 1; i >= 0; i--) {
|
|
2516
|
+
const segment = segments[i];
|
|
2517
|
+
if (segment.type === "property") {
|
|
2518
|
+
result = { [String(segment.value)]: result };
|
|
2519
|
+
} else if (segment.type === "index") {
|
|
2520
|
+
const index = parseInt(String(segment.value));
|
|
2521
|
+
const arr = [];
|
|
2522
|
+
arr[index] = result;
|
|
2523
|
+
result = arr;
|
|
2524
|
+
} else if (segment.type === "wildcard") {
|
|
2525
|
+
if (Array.isArray(result)) {
|
|
2526
|
+
result = result;
|
|
2527
|
+
} else {
|
|
2528
|
+
result = [result];
|
|
2529
|
+
}
|
|
2530
|
+
} else if (segment.type === "slice") {
|
|
2531
|
+
if (!Array.isArray(result)) {
|
|
2532
|
+
result = [result];
|
|
2533
|
+
}
|
|
2534
|
+
} else if (segment.type === "union") {
|
|
2535
|
+
if (!Array.isArray(result)) {
|
|
2536
|
+
result = [result];
|
|
2537
|
+
}
|
|
2538
|
+
} else if (segment.type === "filter") {
|
|
2539
|
+
if (!Array.isArray(result)) {
|
|
2540
|
+
result = [result];
|
|
2541
|
+
}
|
|
2542
|
+
} else if (segment.type === "descendant") {
|
|
2543
|
+
result = result;
|
|
2544
|
+
}
|
|
2545
|
+
}
|
|
2546
|
+
return result;
|
|
2547
|
+
};
|
|
2548
|
+
var select = (obj, selector, options = {}) => {
|
|
2549
|
+
return get(obj, selector, { ...options, preserveStructure: false });
|
|
2550
|
+
};
|
|
2551
|
+
var has = (obj, selector, options = {}) => {
|
|
2552
|
+
if (Array.isArray(selector)) {
|
|
2553
|
+
return selector.map((sel) => has(obj, sel, options));
|
|
2554
|
+
}
|
|
2555
|
+
const result = get(obj, selector, { ...options, preserveStructure: false });
|
|
2556
|
+
if (Array.isArray(result)) {
|
|
2557
|
+
if (result.length > 0) {
|
|
2558
|
+
return result.every((r) => r !== void 0);
|
|
2559
|
+
}
|
|
2560
|
+
return false;
|
|
2561
|
+
}
|
|
2562
|
+
return result !== void 0;
|
|
2563
|
+
};
|
|
2564
|
+
var deletePath = (obj, selector, options = {}) => {
|
|
2565
|
+
const { throwOnError = false } = options;
|
|
2566
|
+
if (Array.isArray(selector)) {
|
|
2567
|
+
return selector.map((sel) => deletePath(obj, sel, options));
|
|
2568
|
+
}
|
|
2569
|
+
const segments = parseSelector(selector, { throwOnError });
|
|
2570
|
+
if (segments.length === 0) {
|
|
2571
|
+
return false;
|
|
2572
|
+
}
|
|
2573
|
+
const lastSegment = segments[segments.length - 1];
|
|
2574
|
+
if (segments.some(
|
|
2575
|
+
(s) => s.type === "wildcard" || s.type === "slice" || s.type === "union"
|
|
2576
|
+
)) {
|
|
2577
|
+
const paths = getPaths(obj, selector, options);
|
|
2578
|
+
let deleted = false;
|
|
2579
|
+
for (const path of paths) {
|
|
2580
|
+
if (deleteSinglePath(obj, path)) {
|
|
2581
|
+
deleted = true;
|
|
2582
|
+
}
|
|
2583
|
+
}
|
|
2584
|
+
return deleted;
|
|
2585
|
+
}
|
|
2586
|
+
let current = obj;
|
|
2587
|
+
for (let i = 0; i < segments.length - 1; i++) {
|
|
2588
|
+
const segment = segments[i];
|
|
2589
|
+
if (current == null) {
|
|
2590
|
+
return false;
|
|
2591
|
+
}
|
|
2592
|
+
switch (segment.type) {
|
|
2593
|
+
case "property":
|
|
2594
|
+
current = current[String(segment.value)];
|
|
2595
|
+
break;
|
|
2596
|
+
case "index":
|
|
2597
|
+
if (!Array.isArray(current)) return false;
|
|
2598
|
+
const index = segment.value;
|
|
2599
|
+
if (index < 0 || index >= current.length) return false;
|
|
2600
|
+
current = current[index];
|
|
2601
|
+
break;
|
|
2602
|
+
default:
|
|
2603
|
+
return false;
|
|
2604
|
+
}
|
|
2605
|
+
}
|
|
2606
|
+
if (current == null) {
|
|
2607
|
+
return false;
|
|
2608
|
+
}
|
|
2609
|
+
switch (lastSegment.type) {
|
|
2610
|
+
case "property":
|
|
2611
|
+
if (String(lastSegment.value) in current) {
|
|
2612
|
+
delete current[String(lastSegment.value)];
|
|
2613
|
+
return true;
|
|
2614
|
+
}
|
|
2615
|
+
break;
|
|
2616
|
+
case "index":
|
|
2617
|
+
if (!Array.isArray(current)) return false;
|
|
2618
|
+
const index = lastSegment.value;
|
|
2619
|
+
if (index >= 0 && index < current.length) {
|
|
2620
|
+
current.splice(index, 1);
|
|
2621
|
+
return true;
|
|
2622
|
+
}
|
|
2623
|
+
break;
|
|
2624
|
+
}
|
|
2625
|
+
return false;
|
|
2626
|
+
};
|
|
2627
|
+
var deleteSinglePath = (obj, selector) => {
|
|
2628
|
+
const segments = parseSelector(selector);
|
|
2629
|
+
if (segments.length === 0) {
|
|
2630
|
+
return false;
|
|
2631
|
+
}
|
|
2632
|
+
const lastSegment = segments[segments.length - 1];
|
|
2633
|
+
let current = obj;
|
|
2634
|
+
for (let i = 0; i < segments.length - 1; i++) {
|
|
2635
|
+
const segment = segments[i];
|
|
2636
|
+
if (current == null) {
|
|
2637
|
+
return false;
|
|
2638
|
+
}
|
|
2639
|
+
switch (segment.type) {
|
|
2640
|
+
case "property":
|
|
2641
|
+
current = current[String(segment.value)];
|
|
2642
|
+
break;
|
|
2643
|
+
case "index":
|
|
2644
|
+
if (!Array.isArray(current)) return false;
|
|
2645
|
+
const index = segment.value;
|
|
2646
|
+
if (index < 0 || index >= current.length) return false;
|
|
2647
|
+
current = current[index];
|
|
2648
|
+
break;
|
|
2649
|
+
default:
|
|
2650
|
+
return false;
|
|
2651
|
+
}
|
|
2652
|
+
}
|
|
2653
|
+
if (current == null) {
|
|
2654
|
+
return false;
|
|
2655
|
+
}
|
|
2656
|
+
switch (lastSegment.type) {
|
|
2657
|
+
case "property":
|
|
2658
|
+
if (String(lastSegment.value) in current) {
|
|
2659
|
+
delete current[String(lastSegment.value)];
|
|
2660
|
+
return true;
|
|
2661
|
+
}
|
|
2662
|
+
break;
|
|
2663
|
+
case "index":
|
|
2664
|
+
if (!Array.isArray(current)) return false;
|
|
2665
|
+
const index = lastSegment.value;
|
|
2666
|
+
if (index >= 0 && index < current.length) {
|
|
2667
|
+
current.splice(index, 1);
|
|
2668
|
+
return true;
|
|
2669
|
+
}
|
|
2670
|
+
break;
|
|
2671
|
+
}
|
|
2672
|
+
return false;
|
|
2673
|
+
};
|
|
2674
|
+
var getPaths = (obj, selector, options = {}) => {
|
|
2675
|
+
const { format = "selector", maxDepth = 20 } = options;
|
|
2676
|
+
if (selector && Array.isArray(selector)) {
|
|
2677
|
+
const allPaths2 = [];
|
|
2678
|
+
for (const sel of selector) {
|
|
2679
|
+
const paths = getPaths(obj, sel, options);
|
|
2680
|
+
allPaths2.push(...paths);
|
|
2681
|
+
}
|
|
2682
|
+
return allPaths2;
|
|
2683
|
+
}
|
|
2684
|
+
if (selector) {
|
|
2685
|
+
const segments = parseSelector(selector, options);
|
|
2686
|
+
const results = traverseWithPaths(obj, segments, 0, maxDepth);
|
|
2687
|
+
return results.map(
|
|
2688
|
+
(r) => format === "selector" ? r.path : r.stringPath
|
|
2689
|
+
);
|
|
2690
|
+
}
|
|
2691
|
+
const allPaths = [];
|
|
2692
|
+
const traverseAll = (current, path = [], stringPath = [], depth = 0) => {
|
|
2693
|
+
if (depth > maxDepth) return;
|
|
2694
|
+
if (path.length > 0) {
|
|
2695
|
+
const pathStr = stringifySelector(path);
|
|
2696
|
+
allPaths.push({ path: pathStr, stringPath: stringPath.join(".") });
|
|
2697
|
+
}
|
|
2698
|
+
if (current && typeof current === "object") {
|
|
2699
|
+
if (Array.isArray(current)) {
|
|
2700
|
+
for (let i = 0; i < current.length; i++) {
|
|
2701
|
+
traverseAll(
|
|
2702
|
+
current[i],
|
|
2703
|
+
[...path, { type: "index", value: i, raw: `[${i}]` }],
|
|
2704
|
+
[...stringPath, `[${i}]`],
|
|
2705
|
+
depth + 1
|
|
2706
|
+
);
|
|
2707
|
+
}
|
|
2708
|
+
} else {
|
|
2709
|
+
for (const key in current) {
|
|
2710
|
+
if (current.hasOwnProperty(key)) {
|
|
2711
|
+
traverseAll(
|
|
2712
|
+
current[key],
|
|
2713
|
+
[...path, { type: "property", value: key, raw: key }],
|
|
2714
|
+
[...stringPath, key],
|
|
2715
|
+
depth + 1
|
|
2716
|
+
);
|
|
2717
|
+
}
|
|
2718
|
+
}
|
|
2719
|
+
}
|
|
2720
|
+
}
|
|
2721
|
+
};
|
|
2722
|
+
traverseAll(obj);
|
|
2723
|
+
return allPaths.map(
|
|
2724
|
+
(p) => format === "selector" ? p.path : p.stringPath
|
|
2725
|
+
);
|
|
2726
|
+
};
|
|
2727
|
+
var findPaths = (obj, predicate) => {
|
|
2728
|
+
const paths = getPaths(obj);
|
|
2729
|
+
return paths.filter((path) => {
|
|
2730
|
+
const value = get(obj, path);
|
|
2731
|
+
return predicate(path, value);
|
|
2732
|
+
});
|
|
2733
|
+
};
|
|
2734
|
+
var flattenWithArrays = (obj, separator = ".") => {
|
|
2735
|
+
const result = {};
|
|
2736
|
+
const traverse = (current, path = []) => {
|
|
2737
|
+
if (current == null) return;
|
|
2738
|
+
const trav = traverseStructure(current, path, 0, 20);
|
|
2739
|
+
if (trav !== void 0) {
|
|
2740
|
+
result[stringifySelector(path)] = trav;
|
|
2741
|
+
return;
|
|
2742
|
+
}
|
|
2743
|
+
if (Array.isArray(current)) {
|
|
2744
|
+
for (let i = 0; i < current.length; i++) {
|
|
2745
|
+
const newPath = [
|
|
2746
|
+
...path,
|
|
2747
|
+
{ type: "index", value: i, raw: `[${i}]` }
|
|
2748
|
+
];
|
|
2749
|
+
traverse(current[i], newPath);
|
|
2750
|
+
}
|
|
2751
|
+
} else if (isObject(current)) {
|
|
2752
|
+
for (const key in current) {
|
|
2753
|
+
if (current.hasOwnProperty(key)) {
|
|
2754
|
+
const newPath = [
|
|
2755
|
+
...path,
|
|
2756
|
+
{ type: "property", value: key, raw: key }
|
|
2757
|
+
];
|
|
2758
|
+
traverse(current[key], newPath);
|
|
2759
|
+
}
|
|
2760
|
+
}
|
|
2761
|
+
const symbols = Object.getOwnPropertySymbols(current);
|
|
2762
|
+
for (const symbol of symbols) {
|
|
2763
|
+
const newPath = [
|
|
2764
|
+
...path,
|
|
2765
|
+
{
|
|
2766
|
+
type: "property",
|
|
2767
|
+
value: symbol.toString(),
|
|
2768
|
+
raw: symbol.toString()
|
|
2769
|
+
}
|
|
2770
|
+
];
|
|
2771
|
+
traverse(current[symbol], newPath);
|
|
2772
|
+
}
|
|
2773
|
+
} else {
|
|
2774
|
+
const pathStr = stringifySelector(path);
|
|
2775
|
+
result[pathStr] = current;
|
|
2776
|
+
}
|
|
2777
|
+
};
|
|
2778
|
+
traverse(obj);
|
|
2779
|
+
return result;
|
|
2780
|
+
};
|
|
2781
|
+
var unflattenWithArrays = (obj) => {
|
|
2782
|
+
const result = {};
|
|
2783
|
+
for (const key in obj) {
|
|
2784
|
+
set(result, key, obj[key]);
|
|
2785
|
+
}
|
|
2786
|
+
return result;
|
|
2787
|
+
};
|
|
2788
|
+
var deepMergeWithArrays = (...objects) => {
|
|
2789
|
+
const result = {};
|
|
2790
|
+
for (const obj of objects) {
|
|
2791
|
+
if (!isObject(obj)) continue;
|
|
2792
|
+
const flat = flattenWithArrays(obj);
|
|
2793
|
+
for (const path in flat) {
|
|
2794
|
+
set(result, path, flat[path]);
|
|
2795
|
+
}
|
|
2796
|
+
}
|
|
2797
|
+
return result;
|
|
2798
|
+
};
|
|
2799
|
+
function traverseWithPaths(current, segments, depth, maxDepth, currentPath = [], currentStringPath = []) {
|
|
2800
|
+
if (depth > maxDepth) return [];
|
|
2801
|
+
if (segments.length === 0)
|
|
2802
|
+
return [
|
|
2803
|
+
{
|
|
2804
|
+
value: current,
|
|
2805
|
+
path: stringifySelector(currentPath),
|
|
2806
|
+
stringPath: currentStringPath.join(".")
|
|
2807
|
+
}
|
|
2808
|
+
];
|
|
2809
|
+
const segment = segments[0];
|
|
2810
|
+
const remaining = segments.slice(1);
|
|
2811
|
+
const results = [];
|
|
2812
|
+
if (segment.type === "descendant") {
|
|
2813
|
+
results.push(
|
|
2814
|
+
...traverseWithPaths(
|
|
2815
|
+
current,
|
|
2816
|
+
remaining,
|
|
2817
|
+
depth + 1,
|
|
2818
|
+
maxDepth,
|
|
2819
|
+
currentPath,
|
|
2820
|
+
currentStringPath
|
|
2821
|
+
)
|
|
2822
|
+
);
|
|
2823
|
+
if (current && typeof current === "object") {
|
|
2824
|
+
if (Array.isArray(current)) {
|
|
2825
|
+
for (let i = 0; i < current.length; i++) {
|
|
2826
|
+
results.push(
|
|
2827
|
+
...traverseWithPaths(
|
|
2828
|
+
current[i],
|
|
2829
|
+
segments,
|
|
2830
|
+
// Use same segments for recursion
|
|
2831
|
+
depth + 1,
|
|
2832
|
+
maxDepth,
|
|
2833
|
+
[...currentPath, { type: "index", value: i, raw: `[${i}]` }],
|
|
2834
|
+
[...currentStringPath, `[${i}]`]
|
|
2835
|
+
)
|
|
2836
|
+
);
|
|
2837
|
+
}
|
|
2838
|
+
} else {
|
|
2839
|
+
for (const key in current) {
|
|
2840
|
+
if (current.hasOwnProperty(key)) {
|
|
2841
|
+
results.push(
|
|
2842
|
+
...traverseWithPaths(
|
|
2843
|
+
current[key],
|
|
2844
|
+
segments,
|
|
2845
|
+
depth + 1,
|
|
2846
|
+
maxDepth,
|
|
2847
|
+
[...currentPath, { type: "property", value: key, raw: key }],
|
|
2848
|
+
[...currentStringPath, key]
|
|
2849
|
+
)
|
|
2850
|
+
);
|
|
2851
|
+
}
|
|
2852
|
+
}
|
|
2853
|
+
}
|
|
2854
|
+
}
|
|
2855
|
+
return results;
|
|
2856
|
+
}
|
|
2857
|
+
switch (segment.type) {
|
|
2858
|
+
case "property":
|
|
2859
|
+
if (current && typeof current === "object" && String(segment.value) in current) {
|
|
2860
|
+
const next = current[String(segment.value)];
|
|
2861
|
+
results.push(
|
|
2862
|
+
...traverseWithPaths(
|
|
2863
|
+
next,
|
|
2864
|
+
remaining,
|
|
2865
|
+
depth + 1,
|
|
2866
|
+
maxDepth,
|
|
2867
|
+
[...currentPath, segment],
|
|
2868
|
+
[...currentStringPath, String(segment.value)]
|
|
2869
|
+
)
|
|
2870
|
+
);
|
|
2871
|
+
}
|
|
2872
|
+
break;
|
|
2873
|
+
case "index":
|
|
2874
|
+
if (Array.isArray(current)) {
|
|
2875
|
+
const index = segment.value;
|
|
2876
|
+
if (index >= 0 && index < current.length) {
|
|
2877
|
+
results.push(
|
|
2878
|
+
...traverseWithPaths(
|
|
2879
|
+
current[index],
|
|
2880
|
+
remaining,
|
|
2881
|
+
depth + 1,
|
|
2882
|
+
maxDepth,
|
|
2883
|
+
[...currentPath, segment],
|
|
2884
|
+
[...currentStringPath, `[${index}]`]
|
|
2885
|
+
)
|
|
2886
|
+
);
|
|
2887
|
+
}
|
|
2888
|
+
} else if (current && typeof current === "object") {
|
|
2889
|
+
const key = String(segment.value);
|
|
2890
|
+
if (key in current) {
|
|
2891
|
+
results.push(
|
|
2892
|
+
...traverseWithPaths(
|
|
2893
|
+
current[key],
|
|
2894
|
+
remaining,
|
|
2895
|
+
depth + 1,
|
|
2896
|
+
maxDepth,
|
|
2897
|
+
[...currentPath, { ...segment, type: "property" }],
|
|
2898
|
+
[...currentStringPath, key]
|
|
2899
|
+
)
|
|
2900
|
+
);
|
|
2901
|
+
}
|
|
2902
|
+
}
|
|
2903
|
+
break;
|
|
2904
|
+
case "wildcard":
|
|
2905
|
+
if (current && typeof current === "object") {
|
|
2906
|
+
if (Array.isArray(current)) {
|
|
2907
|
+
for (let i = 0; i < current.length; i++) {
|
|
2908
|
+
results.push(
|
|
2909
|
+
...traverseWithPaths(
|
|
2910
|
+
current[i],
|
|
2911
|
+
remaining,
|
|
2912
|
+
depth + 1,
|
|
2913
|
+
maxDepth,
|
|
2914
|
+
[...currentPath, { type: "index", value: i, raw: `[${i}]` }],
|
|
2915
|
+
[...currentStringPath, `[${i}]`]
|
|
2916
|
+
)
|
|
2917
|
+
);
|
|
2918
|
+
}
|
|
2919
|
+
} else {
|
|
2920
|
+
for (const key in current) {
|
|
2921
|
+
if (current.hasOwnProperty(key)) {
|
|
2922
|
+
results.push(
|
|
2923
|
+
...traverseWithPaths(
|
|
2924
|
+
current[key],
|
|
2925
|
+
remaining,
|
|
2926
|
+
depth + 1,
|
|
2927
|
+
maxDepth,
|
|
2928
|
+
[...currentPath, { type: "property", value: key, raw: key }],
|
|
2929
|
+
[...currentStringPath, key]
|
|
2930
|
+
)
|
|
2931
|
+
);
|
|
2932
|
+
}
|
|
2933
|
+
}
|
|
2934
|
+
}
|
|
2935
|
+
}
|
|
2936
|
+
break;
|
|
2937
|
+
case "slice":
|
|
2938
|
+
if (Array.isArray(current)) {
|
|
2939
|
+
const [start, end] = segment.value;
|
|
2940
|
+
const actualStart = Math.max(0, start);
|
|
2941
|
+
const actualEnd = Math.min(
|
|
2942
|
+
current.length,
|
|
2943
|
+
end === Infinity ? current.length : end
|
|
2944
|
+
);
|
|
2945
|
+
for (let i = actualStart; i < actualEnd; i++) {
|
|
2946
|
+
results.push(
|
|
2947
|
+
...traverseWithPaths(
|
|
2948
|
+
current[i],
|
|
2949
|
+
remaining,
|
|
2950
|
+
depth + 1,
|
|
2951
|
+
maxDepth,
|
|
2952
|
+
[...currentPath, { type: "index", value: i, raw: `[${i}]` }],
|
|
2953
|
+
[...currentStringPath, `[${i}]`]
|
|
2954
|
+
)
|
|
2955
|
+
);
|
|
2956
|
+
}
|
|
2957
|
+
}
|
|
2958
|
+
break;
|
|
2959
|
+
case "union":
|
|
2960
|
+
if (Array.isArray(current)) {
|
|
2961
|
+
const indices = segment.value;
|
|
2962
|
+
for (const index of indices) {
|
|
2963
|
+
if (index >= 0 && index < current.length) {
|
|
2964
|
+
results.push(
|
|
2965
|
+
...traverseWithPaths(
|
|
2966
|
+
current[index],
|
|
2967
|
+
remaining,
|
|
2968
|
+
depth + 1,
|
|
2969
|
+
maxDepth,
|
|
2970
|
+
[
|
|
2971
|
+
...currentPath,
|
|
2972
|
+
{ type: "index", value: index, raw: `[${index}]` }
|
|
2973
|
+
],
|
|
2974
|
+
[...currentStringPath, `[${index}]`]
|
|
2975
|
+
)
|
|
2976
|
+
);
|
|
2977
|
+
}
|
|
2978
|
+
}
|
|
2979
|
+
}
|
|
2980
|
+
break;
|
|
2981
|
+
case "filter":
|
|
2982
|
+
if (Array.isArray(current)) {
|
|
2983
|
+
const filterFn = segment.value;
|
|
2984
|
+
for (let i = 0; i < current.length; i++) {
|
|
2985
|
+
if (filterFn(current[i])) {
|
|
2986
|
+
results.push(
|
|
2987
|
+
...traverseWithPaths(
|
|
2988
|
+
current[i],
|
|
2989
|
+
remaining,
|
|
2990
|
+
depth + 1,
|
|
2991
|
+
maxDepth,
|
|
2992
|
+
[...currentPath, { type: "index", value: i, raw: `[${i}]` }],
|
|
2993
|
+
[...currentStringPath, `[${i}]`]
|
|
2994
|
+
)
|
|
2995
|
+
);
|
|
2996
|
+
}
|
|
2997
|
+
}
|
|
2998
|
+
}
|
|
2999
|
+
break;
|
|
3000
|
+
}
|
|
3001
|
+
return results;
|
|
3002
|
+
}
|
|
3003
|
+
var update = (obj, path, updater) => {
|
|
3004
|
+
const currentValue = get(obj, path);
|
|
3005
|
+
const newValue = updater(currentValue);
|
|
3006
|
+
return setValueAtPath(obj, path, newValue);
|
|
3007
|
+
};
|
|
3008
|
+
var updateWithCallBack = (obj, updater) => {
|
|
3009
|
+
const newValue = updater(obj);
|
|
3010
|
+
const result = applyDiff(obj, newValue, true);
|
|
3011
|
+
return result;
|
|
3012
|
+
};
|
|
3013
|
+
var getTypeAtPath = (obj, path) => {
|
|
3014
|
+
const value = get(obj, path);
|
|
3015
|
+
if (value === void 0) return void 0;
|
|
3016
|
+
if (value === null) return "null";
|
|
3017
|
+
if (Array.isArray(value)) return "array";
|
|
3018
|
+
return typeof value;
|
|
3019
|
+
};
|
|
3020
|
+
|
|
3021
|
+
// src/lib/object/omit.ts
|
|
3022
|
+
function omit(obj, keys) {
|
|
3023
|
+
if (!isObject(obj)) {
|
|
3024
|
+
return {};
|
|
3025
|
+
}
|
|
3026
|
+
const result = { ...obj };
|
|
3027
|
+
for (const key of keys) {
|
|
3028
|
+
delete result[key];
|
|
3029
|
+
}
|
|
3030
|
+
return result;
|
|
3031
|
+
}
|
|
3032
|
+
function deepOmit(obj, keys) {
|
|
3033
|
+
if (obj === null || obj === void 0 || typeof obj !== "object") {
|
|
3034
|
+
return obj;
|
|
3035
|
+
}
|
|
3036
|
+
if (Array.isArray(obj)) {
|
|
3037
|
+
return obj.map((item) => deepOmit(item, keys));
|
|
3038
|
+
}
|
|
3039
|
+
const result = {};
|
|
3040
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
3041
|
+
if (!keys.includes(key)) {
|
|
3042
|
+
result[key] = deepOmit(value, keys);
|
|
3043
|
+
}
|
|
3044
|
+
}
|
|
3045
|
+
return result;
|
|
3046
|
+
}
|
|
3047
|
+
function createOmitter(keys) {
|
|
3048
|
+
return (obj) => {
|
|
3049
|
+
if (!isObject(obj)) {
|
|
3050
|
+
return {};
|
|
3051
|
+
}
|
|
3052
|
+
const result = { ...obj };
|
|
3053
|
+
for (const key of keys) {
|
|
3054
|
+
if (key in result) {
|
|
3055
|
+
delete result[key];
|
|
3056
|
+
}
|
|
3057
|
+
}
|
|
3058
|
+
return result;
|
|
3059
|
+
};
|
|
3060
|
+
}
|
|
3061
|
+
function omitBy(obj, predicate) {
|
|
3062
|
+
if (!isObject(obj)) {
|
|
3063
|
+
return {};
|
|
3064
|
+
}
|
|
3065
|
+
const result = {};
|
|
3066
|
+
for (const key in obj) {
|
|
3067
|
+
if (Object.prototype.hasOwnProperty.call(obj, key) && !predicate(obj[key], key, obj)) {
|
|
3068
|
+
result[key] = obj[key];
|
|
3069
|
+
}
|
|
3070
|
+
}
|
|
3071
|
+
return result;
|
|
3072
|
+
}
|
|
3073
|
+
function omitNil(obj) {
|
|
3074
|
+
return omitBy(obj, (value) => value === null || value === void 0);
|
|
3075
|
+
}
|
|
3076
|
+
function pick(obj, keys) {
|
|
3077
|
+
if (!isObject(obj)) {
|
|
3078
|
+
return {};
|
|
3079
|
+
}
|
|
3080
|
+
const result = {};
|
|
3081
|
+
for (const key of keys) {
|
|
3082
|
+
if (key in obj) {
|
|
3083
|
+
result[key] = obj[key];
|
|
3084
|
+
}
|
|
3085
|
+
}
|
|
3086
|
+
return result;
|
|
3087
|
+
}
|
|
3088
|
+
function pickBy(obj, predicate) {
|
|
3089
|
+
if (!isObject(obj)) {
|
|
3090
|
+
return {};
|
|
3091
|
+
}
|
|
3092
|
+
const result = {};
|
|
3093
|
+
for (const key in obj) {
|
|
3094
|
+
if (Object.prototype.hasOwnProperty.call(obj, key) && predicate(obj[key], key, obj)) {
|
|
3095
|
+
result[key] = obj[key];
|
|
3096
|
+
}
|
|
3097
|
+
}
|
|
3098
|
+
return result;
|
|
3099
|
+
}
|
|
3100
|
+
function omitPath(obj, paths, transform2) {
|
|
3101
|
+
if (!isObject(obj)) {
|
|
3102
|
+
return {};
|
|
3103
|
+
}
|
|
3104
|
+
const result = JSON.parse(JSON.stringify(obj));
|
|
3105
|
+
const pathArray = Array.isArray(paths) ? paths : [paths];
|
|
3106
|
+
for (let i = 0; i < pathArray.length; i++) {
|
|
3107
|
+
const path = pathArray[i];
|
|
3108
|
+
const nextPath = pathArray[i + 1];
|
|
3109
|
+
if (!path || path === "") continue;
|
|
3110
|
+
const segments = parseSelector(path);
|
|
3111
|
+
if (path.includes("*") || path.includes("[") || segments.length > 1) {
|
|
3112
|
+
let collectPaths2 = function(obj2, currentPath = "") {
|
|
3113
|
+
if (!isObject(obj2)) return;
|
|
3114
|
+
if (Array.isArray(obj2)) {
|
|
3115
|
+
obj2.forEach((item, index) => {
|
|
3116
|
+
collectPaths2(
|
|
3117
|
+
item,
|
|
3118
|
+
currentPath ? `${currentPath}[${index}]` : `[${index}]`
|
|
3119
|
+
);
|
|
3120
|
+
});
|
|
3121
|
+
return;
|
|
3122
|
+
}
|
|
3123
|
+
for (const key in obj2) {
|
|
3124
|
+
if (Object.prototype.hasOwnProperty.call(obj2, key)) {
|
|
3125
|
+
const fullPath = currentPath ? `${currentPath}.${key}` : key;
|
|
3126
|
+
pathsToProcess.push({
|
|
3127
|
+
fullPath,
|
|
3128
|
+
segments: parseSelector(fullPath)
|
|
3129
|
+
});
|
|
3130
|
+
collectPaths2(obj2[key], fullPath);
|
|
3131
|
+
}
|
|
3132
|
+
}
|
|
3133
|
+
};
|
|
3134
|
+
var collectPaths = collectPaths2;
|
|
3135
|
+
const value = get(result, path, { preserveStructure: true });
|
|
3136
|
+
if (value === void 0) {
|
|
3137
|
+
continue;
|
|
3138
|
+
}
|
|
3139
|
+
const pathsToProcess = [];
|
|
3140
|
+
collectPaths2(value);
|
|
3141
|
+
for (const { fullPath, segments: pathSegments } of pathsToProcess) {
|
|
3142
|
+
const pathSegmentsCopy = [...pathSegments];
|
|
3143
|
+
const lastSegment = pathSegmentsCopy.pop();
|
|
3144
|
+
if (!lastSegment) continue;
|
|
3145
|
+
const parentPath = pathSegmentsCopy.map((s) => {
|
|
3146
|
+
if (s.type === "index") return `[${s.value}]`;
|
|
3147
|
+
return s.value;
|
|
3148
|
+
}).join(".").replace(/\.\[/g, "[");
|
|
3149
|
+
const fg = get(result, parentPath.length > 0 ? parentPath : fullPath, {
|
|
3150
|
+
preserveStructure: true
|
|
3151
|
+
});
|
|
3152
|
+
let parent = parentPath ? fg : result;
|
|
3153
|
+
if (!parent) {
|
|
3154
|
+
continue;
|
|
3155
|
+
}
|
|
3156
|
+
const propertyKey = String(lastSegment.value);
|
|
3157
|
+
const currentValue = parent[propertyKey];
|
|
3158
|
+
if (transform2) {
|
|
3159
|
+
const transformResult = transform2(currentValue, fullPath, result);
|
|
3160
|
+
if (transformResult === true) {
|
|
3161
|
+
if (lastSegment.type === "index" && Array.isArray(parent)) {
|
|
3162
|
+
const index = lastSegment.value;
|
|
3163
|
+
if (index >= 0 && index < parent.length) {
|
|
3164
|
+
parent.splice(index, 1);
|
|
3165
|
+
}
|
|
3166
|
+
} else {
|
|
3167
|
+
deletePath(result, pathArray[i]);
|
|
3168
|
+
}
|
|
3169
|
+
} else if (transformResult !== false) {
|
|
3170
|
+
parent[propertyKey] = transformResult;
|
|
3171
|
+
}
|
|
3172
|
+
} else {
|
|
3173
|
+
if (lastSegment.type === "index" && Array.isArray(parent)) {
|
|
3174
|
+
const index = lastSegment.value;
|
|
3175
|
+
if (index >= 0 && index < parent.length) {
|
|
3176
|
+
parent.splice(index, 1);
|
|
3177
|
+
}
|
|
3178
|
+
} else {
|
|
3179
|
+
deletePath(result, pathArray[i]);
|
|
3180
|
+
}
|
|
3181
|
+
}
|
|
3182
|
+
}
|
|
3183
|
+
} else {
|
|
3184
|
+
const key = path;
|
|
3185
|
+
if (typeof key === "string" && key in result) {
|
|
3186
|
+
if (transform2) {
|
|
3187
|
+
const transformResult = transform2(result[key], path, result);
|
|
3188
|
+
if (transformResult === true) {
|
|
3189
|
+
delete result[key];
|
|
3190
|
+
} else if (transformResult !== false) {
|
|
3191
|
+
result[key] = transformResult;
|
|
3192
|
+
}
|
|
3193
|
+
} else {
|
|
3194
|
+
deletePath(result, pathArray[i]);
|
|
3195
|
+
}
|
|
3196
|
+
}
|
|
3197
|
+
}
|
|
3198
|
+
}
|
|
3199
|
+
return result;
|
|
3200
|
+
}
|
|
3201
|
+
function pickPath(obj, paths, transform2) {
|
|
3202
|
+
if (!isObject(obj)) {
|
|
3203
|
+
return {};
|
|
3204
|
+
}
|
|
3205
|
+
if (Array.isArray(paths)) {
|
|
3206
|
+
const result = {};
|
|
3207
|
+
for (const path of paths) {
|
|
3208
|
+
const pathValue = get(obj, path, { preserveStructure: true });
|
|
3209
|
+
if (pathValue === void 0) {
|
|
3210
|
+
continue;
|
|
3211
|
+
}
|
|
3212
|
+
if (transform2) {
|
|
3213
|
+
const transformValue = (value2, currentPath) => {
|
|
3214
|
+
if (isObject(value2)) {
|
|
3215
|
+
if (Array.isArray(value2)) {
|
|
3216
|
+
return value2.map(
|
|
3217
|
+
(item, index) => transformValue(item, `${currentPath}[${index}]`)
|
|
3218
|
+
);
|
|
3219
|
+
}
|
|
3220
|
+
const result2 = {};
|
|
3221
|
+
for (const key in value2) {
|
|
3222
|
+
if (Object.prototype.hasOwnProperty.call(value2, key)) {
|
|
3223
|
+
const nestedPath = currentPath ? `${currentPath}.${key}` : key;
|
|
3224
|
+
result2[key] = transformValue(value2[key], nestedPath);
|
|
3225
|
+
}
|
|
3226
|
+
}
|
|
3227
|
+
return result2;
|
|
3228
|
+
}
|
|
3229
|
+
return transform2(value2, currentPath, obj);
|
|
3230
|
+
};
|
|
3231
|
+
const transformedValue = transformValue(pathValue, path);
|
|
3232
|
+
deepMergeKeepReference(result, transformedValue);
|
|
3233
|
+
} else {
|
|
3234
|
+
Object.assign(result, deepMergeKeepReference(result, pathValue));
|
|
3235
|
+
}
|
|
3236
|
+
}
|
|
3237
|
+
return result;
|
|
3238
|
+
}
|
|
3239
|
+
const pathString = Array.isArray(paths) ? paths[0] : paths;
|
|
3240
|
+
const value = get(obj, pathString, { preserveStructure: true });
|
|
3241
|
+
if (value === void 0) {
|
|
3242
|
+
return {};
|
|
3243
|
+
}
|
|
3244
|
+
if (transform2) {
|
|
3245
|
+
const transformValue = (value2, currentPath) => {
|
|
3246
|
+
if (isObject(value2)) {
|
|
3247
|
+
if (Array.isArray(value2)) {
|
|
3248
|
+
return value2.map(
|
|
3249
|
+
(item, index) => transformValue(item, `${currentPath}[${index}]`)
|
|
3250
|
+
);
|
|
3251
|
+
}
|
|
3252
|
+
const result = {};
|
|
3253
|
+
for (const key in value2) {
|
|
3254
|
+
if (Object.prototype.hasOwnProperty.call(value2, key)) {
|
|
3255
|
+
const nestedPath = currentPath ? `${currentPath}.${key}` : key;
|
|
3256
|
+
result[key] = transformValue(value2[key], nestedPath);
|
|
3257
|
+
}
|
|
3258
|
+
}
|
|
3259
|
+
return result;
|
|
3260
|
+
}
|
|
3261
|
+
return transform2(value2, currentPath, obj);
|
|
3262
|
+
};
|
|
3263
|
+
return transformValue(value, pathString);
|
|
3264
|
+
}
|
|
3265
|
+
return value;
|
|
3266
|
+
}
|
|
3267
|
+
function deepOmitBy(obj, predicate) {
|
|
3268
|
+
if (!isObject(obj)) {
|
|
3269
|
+
return obj;
|
|
3270
|
+
}
|
|
3271
|
+
if (Array.isArray(obj)) {
|
|
3272
|
+
return obj.map((item) => deepOmitBy(item, predicate));
|
|
3273
|
+
}
|
|
3274
|
+
const result = {};
|
|
3275
|
+
for (const key in obj) {
|
|
3276
|
+
if (!Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
3277
|
+
continue;
|
|
3278
|
+
}
|
|
3279
|
+
const value = obj[key];
|
|
3280
|
+
if (predicate(value, key, obj)) {
|
|
3281
|
+
continue;
|
|
3282
|
+
}
|
|
3283
|
+
if (isObject(value)) {
|
|
3284
|
+
result[key] = deepOmitBy(value, predicate);
|
|
3285
|
+
} else if (Array.isArray(value)) {
|
|
3286
|
+
result[key] = value.map((item) => deepOmitBy(item, predicate));
|
|
3287
|
+
} else {
|
|
3288
|
+
result[key] = value;
|
|
3289
|
+
}
|
|
3290
|
+
}
|
|
3291
|
+
return result;
|
|
3292
|
+
}
|
|
3293
|
+
|
|
3294
|
+
// src/lib/object/transform.ts
|
|
3295
|
+
function mapValues(obj, fn) {
|
|
3296
|
+
if (!isObject(obj)) {
|
|
3297
|
+
return {};
|
|
3298
|
+
}
|
|
3299
|
+
const result = {};
|
|
3300
|
+
for (const key in obj) {
|
|
3301
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
3302
|
+
result[key] = fn(obj[key], key, obj);
|
|
3303
|
+
}
|
|
3304
|
+
}
|
|
3305
|
+
return result;
|
|
3306
|
+
}
|
|
3307
|
+
function mapKeys(obj, fn) {
|
|
3308
|
+
if (!isObject(obj)) {
|
|
3309
|
+
return {};
|
|
3310
|
+
}
|
|
3311
|
+
const result = {};
|
|
3312
|
+
for (const key in obj) {
|
|
3313
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
3314
|
+
const newKey = fn(key, obj[key], obj);
|
|
3315
|
+
result[newKey] = obj[key];
|
|
3316
|
+
}
|
|
3317
|
+
}
|
|
3318
|
+
return result;
|
|
3319
|
+
}
|
|
3320
|
+
function camelCaseKeys(obj, options = { deep: true }) {
|
|
3321
|
+
if (!isObject(obj)) {
|
|
3322
|
+
return obj;
|
|
3323
|
+
}
|
|
3324
|
+
if (Array.isArray(obj)) {
|
|
3325
|
+
return options.deep ? obj.map((item) => camelCaseKeys(item, options)) : obj;
|
|
3326
|
+
}
|
|
3327
|
+
const result = {};
|
|
3328
|
+
for (const key in obj) {
|
|
3329
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
3330
|
+
const value = obj[key];
|
|
3331
|
+
const camelKey = key.replace(/[-_]([a-z])/g, (_, c) => c.toUpperCase()).replace(/^([A-Z])/, (c) => c.toLowerCase());
|
|
3332
|
+
result[camelKey] = options.deep && isObject(value) ? camelCaseKeys(value, options) : value;
|
|
3333
|
+
}
|
|
3334
|
+
}
|
|
3335
|
+
return result;
|
|
3336
|
+
}
|
|
3337
|
+
function snakeCaseKeys(obj, options = { deep: true }) {
|
|
3338
|
+
if (!isObject(obj)) {
|
|
3339
|
+
return obj;
|
|
3340
|
+
}
|
|
3341
|
+
if (Array.isArray(obj)) {
|
|
3342
|
+
return options.deep ? obj.map((item) => snakeCaseKeys(item, options)) : obj;
|
|
3343
|
+
}
|
|
3344
|
+
const result = {};
|
|
3345
|
+
for (const key in obj) {
|
|
3346
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
3347
|
+
const value = obj[key];
|
|
3348
|
+
const snakeKey = key.replace(/[-]([a-z])/g, (_, c) => `_${c}`).replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
|
|
3349
|
+
result[snakeKey] = options.deep && isObject(value) ? snakeCaseKeys(value, options) : value;
|
|
3350
|
+
}
|
|
3351
|
+
}
|
|
3352
|
+
return result;
|
|
3353
|
+
}
|
|
3354
|
+
function kebabCaseKeys(obj, options = { deep: true }) {
|
|
3355
|
+
if (!isObject(obj)) {
|
|
3356
|
+
return obj;
|
|
3357
|
+
}
|
|
3358
|
+
if (Array.isArray(obj)) {
|
|
3359
|
+
return options.deep ? obj.map((item) => kebabCaseKeys(item, options)) : obj;
|
|
3360
|
+
}
|
|
3361
|
+
const result = {};
|
|
3362
|
+
for (const key in obj) {
|
|
3363
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
3364
|
+
const value = obj[key];
|
|
3365
|
+
const kebabKey = key.replace(/[_]([a-z])/g, (_, c) => `-${c}`).replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
|
|
3366
|
+
result[kebabKey] = options.deep && isObject(value) ? kebabCaseKeys(value, options) : value;
|
|
3367
|
+
}
|
|
3368
|
+
}
|
|
3369
|
+
return result;
|
|
3370
|
+
}
|
|
3371
|
+
var flatten = (obj, options = {}) => {
|
|
3372
|
+
const { delimiter = ".", includeArrays = false, maxDepth = 9 } = options;
|
|
3373
|
+
if (Array.isArray(obj)) {
|
|
3374
|
+
return obj.map((item) => flatten(item, options));
|
|
3375
|
+
}
|
|
3376
|
+
const result = {};
|
|
3377
|
+
const flattenRecursive = (current, path = "", depth = 0) => {
|
|
3378
|
+
if (depth > maxDepth) {
|
|
3379
|
+
result[path] = current;
|
|
3380
|
+
return;
|
|
3381
|
+
}
|
|
3382
|
+
if (!isObject(current) && !Array.isArray(current)) {
|
|
3383
|
+
if (path) {
|
|
3384
|
+
result[path] = current;
|
|
3385
|
+
}
|
|
3386
|
+
return;
|
|
3387
|
+
}
|
|
3388
|
+
if (Array.isArray(current)) {
|
|
3389
|
+
for (let i = 0; i < current.length; i++) {
|
|
3390
|
+
const newPath = path ? `${path}[${i}]` : `[${i}]`;
|
|
3391
|
+
flattenRecursive(current[i], newPath, depth + 1);
|
|
3392
|
+
}
|
|
3393
|
+
} else {
|
|
3394
|
+
for (const key in current) {
|
|
3395
|
+
const newPath = path ? `${path}${delimiter}${key}` : key;
|
|
3396
|
+
flattenRecursive(current[key], newPath, depth + 1);
|
|
3397
|
+
}
|
|
3398
|
+
const symbols = Object.getOwnPropertySymbols(current);
|
|
3399
|
+
for (const symbol of symbols) {
|
|
3400
|
+
const newPath = path ? `${path}${delimiter}${symbol.toString()}` : symbol.toString();
|
|
3401
|
+
flattenRecursive(current[symbol], newPath, depth + 1);
|
|
3402
|
+
}
|
|
3403
|
+
}
|
|
3404
|
+
};
|
|
3405
|
+
flattenRecursive(obj);
|
|
3406
|
+
return result;
|
|
3407
|
+
};
|
|
3408
|
+
function unflatten(obj, options = {}) {
|
|
3409
|
+
const { delimiter = ".", detectArrays = true } = options;
|
|
3410
|
+
const result = {};
|
|
3411
|
+
for (const key in obj) {
|
|
3412
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
3413
|
+
const value = obj[key];
|
|
3414
|
+
const parts = key.split(delimiter);
|
|
3415
|
+
let current = result;
|
|
3416
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
3417
|
+
const part = parts[i];
|
|
3418
|
+
const nextPart = parts[i + 1];
|
|
3419
|
+
const isNextPartArrayIndex = detectArrays && /^\d+$/.test(nextPart);
|
|
3420
|
+
if (!(part in current)) {
|
|
3421
|
+
current[part] = isNextPartArrayIndex ? [] : {};
|
|
3422
|
+
} else if (isNextPartArrayIndex && !Array.isArray(current[part])) {
|
|
3423
|
+
const temp = { ...current[part] };
|
|
3424
|
+
current[part] = [];
|
|
3425
|
+
Object.assign(current[part], temp);
|
|
3426
|
+
}
|
|
3427
|
+
current = current[part];
|
|
3428
|
+
}
|
|
3429
|
+
const lastPart = parts[parts.length - 1];
|
|
3430
|
+
current[lastPart] = value;
|
|
3431
|
+
}
|
|
3432
|
+
}
|
|
3433
|
+
if (detectArrays) {
|
|
3434
|
+
const convertToArrayIfNeeded = (obj2) => {
|
|
3435
|
+
if (!isObject(obj2)) return obj2;
|
|
3436
|
+
if (Array.isArray(obj2)) {
|
|
3437
|
+
return obj2.map(convertToArrayIfNeeded);
|
|
3438
|
+
}
|
|
3439
|
+
const keys = Object.keys(obj2);
|
|
3440
|
+
const isNumericSequence = keys.length > 0 && keys.every((key, index) => {
|
|
3441
|
+
return String(index) === key;
|
|
3442
|
+
});
|
|
3443
|
+
if (isNumericSequence) {
|
|
3444
|
+
return Object.values(obj2).map(convertToArrayIfNeeded);
|
|
3445
|
+
}
|
|
3446
|
+
const result2 = {};
|
|
3447
|
+
for (const key in obj2) {
|
|
3448
|
+
if (Object.prototype.hasOwnProperty.call(obj2, key)) {
|
|
3449
|
+
result2[key] = convertToArrayIfNeeded(obj2[key]);
|
|
3450
|
+
}
|
|
3451
|
+
}
|
|
3452
|
+
return result2;
|
|
3453
|
+
};
|
|
3454
|
+
return convertToArrayIfNeeded(result);
|
|
3455
|
+
}
|
|
3456
|
+
return result;
|
|
3457
|
+
}
|
|
3458
|
+
function transformValues(obj, ...transformers) {
|
|
3459
|
+
if (!isObject(obj) || transformers.length === 0) {
|
|
3460
|
+
return obj;
|
|
3461
|
+
}
|
|
3462
|
+
const result = { ...obj };
|
|
3463
|
+
for (const key in result) {
|
|
3464
|
+
if (Object.prototype.hasOwnProperty.call(result, key)) {
|
|
3465
|
+
let value = result[key];
|
|
3466
|
+
for (const transformer of transformers) {
|
|
3467
|
+
value = transformer(value);
|
|
3468
|
+
}
|
|
3469
|
+
result[key] = value;
|
|
3470
|
+
}
|
|
3471
|
+
}
|
|
3472
|
+
return result;
|
|
3473
|
+
}
|
|
3474
|
+
function renameKeys(obj, mapping, options = {}) {
|
|
3475
|
+
if (!isObject(obj)) {
|
|
3476
|
+
return obj;
|
|
3477
|
+
}
|
|
3478
|
+
const { removeUnmapped = false } = options;
|
|
3479
|
+
const result = {};
|
|
3480
|
+
for (const key in obj) {
|
|
3481
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
3482
|
+
if (key in mapping) {
|
|
3483
|
+
result[mapping[key]] = obj[key];
|
|
3484
|
+
} else if (!removeUnmapped) {
|
|
3485
|
+
result[key] = obj[key];
|
|
3486
|
+
}
|
|
3487
|
+
}
|
|
3488
|
+
}
|
|
3489
|
+
return result;
|
|
3490
|
+
}
|
|
3491
|
+
function deepTransform(data, options = {}) {
|
|
3492
|
+
const {
|
|
3493
|
+
transformObject = (x) => x,
|
|
3494
|
+
transformArray = (x) => x,
|
|
3495
|
+
transformValue = (x) => x,
|
|
3496
|
+
maxDepth = Infinity
|
|
3497
|
+
} = options;
|
|
3498
|
+
function transform2(value, path = [], depth = 0) {
|
|
3499
|
+
if (depth > maxDepth) {
|
|
3500
|
+
return value;
|
|
3501
|
+
}
|
|
3502
|
+
if (Array.isArray(value)) {
|
|
3503
|
+
const transformedArray = value.map(
|
|
3504
|
+
(item, index) => transform2(item, [...path, index.toString()], depth + 1)
|
|
3505
|
+
);
|
|
3506
|
+
return transformArray(transformedArray, path);
|
|
3507
|
+
}
|
|
3508
|
+
if (isObject(value)) {
|
|
3509
|
+
const transformedObj = {};
|
|
3510
|
+
for (const key2 in value) {
|
|
3511
|
+
if (Object.prototype.hasOwnProperty.call(value, key2)) {
|
|
3512
|
+
const newPath = [...path, key2];
|
|
3513
|
+
transformedObj[key2] = transform2(value[key2], newPath, depth + 1);
|
|
3514
|
+
if (!Array.isArray(value[key2]) && !isObject(value[key2])) {
|
|
3515
|
+
transformedObj[key2] = transformValue(
|
|
3516
|
+
transformedObj[key2],
|
|
3517
|
+
key2,
|
|
3518
|
+
newPath
|
|
3519
|
+
);
|
|
3520
|
+
}
|
|
3521
|
+
}
|
|
3522
|
+
}
|
|
3523
|
+
return transformObject(transformedObj, path);
|
|
3524
|
+
}
|
|
3525
|
+
const key = path.length > 0 ? path[path.length - 1] : "";
|
|
3526
|
+
return transformValue(value, key, path);
|
|
3527
|
+
}
|
|
3528
|
+
return transform2(data);
|
|
3529
|
+
}
|
|
3530
|
+
function structureByKeys(obj, options = {}) {
|
|
3531
|
+
return unflatten(obj, options);
|
|
3532
|
+
}
|
|
3533
|
+
function transformPath(obj, paths, transformer, options = {}) {
|
|
3534
|
+
if (!isObject(obj)) {
|
|
3535
|
+
return obj;
|
|
3536
|
+
}
|
|
3537
|
+
const result = JSON.parse(JSON.stringify(obj));
|
|
3538
|
+
const { transformAll = false } = options;
|
|
3539
|
+
const pathsArray = Array.isArray(paths) ? paths : [paths];
|
|
3540
|
+
for (const path of pathsArray) {
|
|
3541
|
+
if (path.includes("*") || path.includes("[")) {
|
|
3542
|
+
let applyTransform2 = function(obj2, basePath = "") {
|
|
3543
|
+
if (!isObject(obj2)) return;
|
|
3544
|
+
if (Array.isArray(obj2)) {
|
|
3545
|
+
obj2.forEach((item, index) => {
|
|
3546
|
+
const currentPath = basePath ? `${basePath}[${index}]` : `[${index}]`;
|
|
3547
|
+
applyTransform2(item, currentPath);
|
|
3548
|
+
});
|
|
3549
|
+
return;
|
|
3550
|
+
}
|
|
3551
|
+
for (const key in obj2) {
|
|
3552
|
+
if (Object.prototype.hasOwnProperty.call(obj2, key)) {
|
|
3553
|
+
const value = obj2[key];
|
|
3554
|
+
const currentPath = basePath ? `${basePath}.${key}` : key;
|
|
3555
|
+
if (value === null || typeof value !== "object") {
|
|
3556
|
+
const parent = basePath ? get(result, basePath) : result;
|
|
3557
|
+
const transformed = transformAll ? transformer(value, currentPath, parent) : transformer(value, key, parent);
|
|
3558
|
+
obj2[key] = transformed;
|
|
3559
|
+
} else {
|
|
3560
|
+
applyTransform2(value, currentPath);
|
|
3561
|
+
}
|
|
3562
|
+
}
|
|
3563
|
+
}
|
|
3564
|
+
};
|
|
3565
|
+
var applyTransform = applyTransform2;
|
|
3566
|
+
const matches = get(result, path, { preserveStructure: true });
|
|
3567
|
+
if (matches === void 0) {
|
|
3568
|
+
continue;
|
|
3569
|
+
}
|
|
3570
|
+
applyTransform2(matches);
|
|
3571
|
+
const segments = parseSelector(path);
|
|
3572
|
+
if (segments.length > 0) {
|
|
3573
|
+
const firstSegment = segments[0];
|
|
3574
|
+
if (firstSegment.type === "property") {
|
|
3575
|
+
result[String(firstSegment.value)] = matches[String(firstSegment.value)];
|
|
3576
|
+
}
|
|
3577
|
+
}
|
|
3578
|
+
} else {
|
|
3579
|
+
let setValueAtPath3 = function(obj2, segments2, index, fullPath) {
|
|
3580
|
+
if (index >= segments2.length) return obj2;
|
|
3581
|
+
const key = segments2[index];
|
|
3582
|
+
if (index === segments2.length - 1) {
|
|
3583
|
+
const parent = index > 0 ? setValueAtPath3(
|
|
3584
|
+
obj2,
|
|
3585
|
+
segments2,
|
|
3586
|
+
index - 1,
|
|
3587
|
+
segments2.slice(0, index).join(".")
|
|
3588
|
+
) : obj2;
|
|
3589
|
+
const transformed = transformAll ? transformer(parent[key], fullPath, parent) : transformer(parent[key], key, parent);
|
|
3590
|
+
parent[key] = transformed;
|
|
3591
|
+
return parent;
|
|
3592
|
+
}
|
|
3593
|
+
const nextObj = obj2[key] || {};
|
|
3594
|
+
obj2[key] = setValueAtPath3(nextObj, segments2, index + 1, fullPath);
|
|
3595
|
+
return obj2;
|
|
3596
|
+
};
|
|
3597
|
+
var setValueAtPath2 = setValueAtPath3;
|
|
3598
|
+
const segments = path.split(".");
|
|
3599
|
+
setValueAtPath3(result, segments, 0, path);
|
|
3600
|
+
}
|
|
3601
|
+
}
|
|
3602
|
+
return result;
|
|
3603
|
+
}
|
|
3604
|
+
function mapPath(obj, paths, mapper, options = {}) {
|
|
3605
|
+
if (!isObject(obj)) {
|
|
3606
|
+
return [];
|
|
3607
|
+
}
|
|
3608
|
+
const { flatten: flatten2 = true } = options;
|
|
3609
|
+
const pathsArray = Array.isArray(paths) ? paths : [paths];
|
|
3610
|
+
const results = pathsArray.map((path) => {
|
|
3611
|
+
const values = get(obj, path);
|
|
3612
|
+
if (values === void 0) {
|
|
3613
|
+
return [];
|
|
3614
|
+
}
|
|
3615
|
+
if (Array.isArray(values)) {
|
|
3616
|
+
return values.map((value, index) => {
|
|
3617
|
+
const fullPath = `${path}[${index}]`;
|
|
3618
|
+
return mapper(value, fullPath, obj);
|
|
3619
|
+
});
|
|
3620
|
+
}
|
|
3621
|
+
return [mapper(values, path, obj)];
|
|
3622
|
+
});
|
|
3623
|
+
return flatten2 ? results.flat() : results;
|
|
3624
|
+
}
|
|
3625
|
+
function transformKeysAtPath(obj, paths, transformer, options = {}) {
|
|
3626
|
+
if (!isObject(obj)) {
|
|
3627
|
+
return obj;
|
|
3628
|
+
}
|
|
3629
|
+
const result = JSON.parse(JSON.stringify(obj));
|
|
3630
|
+
const { recursive = false } = options;
|
|
3631
|
+
const pathsArray = Array.isArray(paths) ? paths : [paths];
|
|
3632
|
+
for (const path of pathsArray) {
|
|
3633
|
+
const target = path ? get(result, path) : result;
|
|
3634
|
+
if (!isObject(target)) {
|
|
3635
|
+
continue;
|
|
3636
|
+
}
|
|
3637
|
+
const transformObjectKeys = (obj2, currentPath = path) => {
|
|
3638
|
+
if (!isObject(obj2)) {
|
|
3639
|
+
return obj2;
|
|
3640
|
+
}
|
|
3641
|
+
if (Array.isArray(obj2)) {
|
|
3642
|
+
return obj2.map(
|
|
3643
|
+
(item, index) => isObject(item) && !Array.isArray(item) ? transformObjectKeys(item, `${currentPath}[${index}]`) : item
|
|
3644
|
+
);
|
|
3645
|
+
}
|
|
3646
|
+
const transformed = {};
|
|
3647
|
+
for (const key in obj2) {
|
|
3648
|
+
if (Object.prototype.hasOwnProperty.call(obj2, key)) {
|
|
3649
|
+
const newKey = transformer(key, obj2[key], currentPath);
|
|
3650
|
+
if (recursive && isObject(obj2[key])) {
|
|
3651
|
+
transformed[newKey] = transformObjectKeys(
|
|
3652
|
+
obj2[key],
|
|
3653
|
+
`${currentPath}.${newKey}`
|
|
3654
|
+
);
|
|
3655
|
+
} else {
|
|
3656
|
+
transformed[newKey] = obj2[key];
|
|
3657
|
+
}
|
|
3658
|
+
}
|
|
3659
|
+
}
|
|
3660
|
+
return transformed;
|
|
3661
|
+
};
|
|
3662
|
+
if (Array.isArray(target)) {
|
|
3663
|
+
for (let i = 0; i < target.length; i++) {
|
|
3664
|
+
if (isObject(target[i]) && !Array.isArray(target[i])) {
|
|
3665
|
+
target[i] = transformObjectKeys(target[i], `${path}[${i}]`);
|
|
3666
|
+
}
|
|
3667
|
+
}
|
|
3668
|
+
} else {
|
|
3669
|
+
const transformed = transformObjectKeys(target);
|
|
3670
|
+
if (path) {
|
|
3671
|
+
set(result, path, transformed);
|
|
3672
|
+
} else {
|
|
3673
|
+
Object.keys(result).forEach((key) => {
|
|
3674
|
+
delete result[key];
|
|
3675
|
+
});
|
|
3676
|
+
Object.assign(result, transformed);
|
|
3677
|
+
}
|
|
3678
|
+
}
|
|
3679
|
+
}
|
|
3680
|
+
return result;
|
|
3681
|
+
}
|
|
3682
|
+
|
|
3683
|
+
// src/lib/object/reconcile.ts
|
|
3684
|
+
function isPlain(v) {
|
|
3685
|
+
if (v == null || typeof v !== "object") return false;
|
|
3686
|
+
const p = Object.getPrototypeOf(v);
|
|
3687
|
+
return p === Object.prototype || p === null;
|
|
3688
|
+
}
|
|
3689
|
+
function reconcileValue(next, current, opts) {
|
|
3690
|
+
if (next === current) return current;
|
|
3691
|
+
if (Array.isArray(next)) return reconcileArr(next, Array.isArray(current) ? current : [], opts);
|
|
3692
|
+
if (isPlain(next)) return reconcileObj(next, isPlain(current) ? current : {}, opts);
|
|
3693
|
+
return next;
|
|
3694
|
+
}
|
|
3695
|
+
function reconcileArr(next, current, opts) {
|
|
3696
|
+
const { key, keepRef } = opts;
|
|
3697
|
+
const curLen = current.length;
|
|
3698
|
+
const nextLen = next.length;
|
|
3699
|
+
const curItems = new Array(curLen);
|
|
3700
|
+
for (let i = 0; i < curLen; i++) curItems[i] = current[i];
|
|
3701
|
+
if (key === false) {
|
|
3702
|
+
if (keepRef) {
|
|
3703
|
+
for (let i = 0; i < nextLen; i++) {
|
|
3704
|
+
const rv = reconcileValue(next[i], curItems[i], opts);
|
|
3705
|
+
if (rv !== curItems[i]) current[i] = rv;
|
|
3706
|
+
}
|
|
3707
|
+
if (curLen > nextLen) current.splice(nextLen);
|
|
3708
|
+
return current;
|
|
3709
|
+
}
|
|
3710
|
+
let changed2 = nextLen !== curLen;
|
|
3711
|
+
const out = new Array(nextLen);
|
|
3712
|
+
for (let i = 0; i < nextLen; i++) {
|
|
3713
|
+
out[i] = reconcileValue(next[i], curItems[i], opts);
|
|
3714
|
+
if (out[i] !== curItems[i]) changed2 = true;
|
|
3715
|
+
}
|
|
3716
|
+
return changed2 ? out : current;
|
|
3717
|
+
}
|
|
3718
|
+
const currentMap = /* @__PURE__ */ new Map();
|
|
3719
|
+
for (let i = 0; i < curLen; i++) {
|
|
3720
|
+
const item = curItems[i];
|
|
3721
|
+
if (item != null && typeof item === "object" && key in item) {
|
|
3722
|
+
currentMap.set(item[key], item);
|
|
3723
|
+
}
|
|
3724
|
+
}
|
|
3725
|
+
const resolved = new Array(nextLen);
|
|
3726
|
+
for (let i = 0; i < nextLen; i++) {
|
|
3727
|
+
const n = next[i];
|
|
3728
|
+
if (n != null && typeof n === "object" && key in n) {
|
|
3729
|
+
resolved[i] = reconcileValue(n, currentMap.get(n[key]), opts);
|
|
3730
|
+
} else {
|
|
3731
|
+
resolved[i] = reconcileValue(n, curItems[i], opts);
|
|
3732
|
+
}
|
|
3733
|
+
}
|
|
3734
|
+
if (keepRef) {
|
|
3735
|
+
for (let i = 0; i < nextLen; i++) {
|
|
3736
|
+
if (resolved[i] !== curItems[i]) current[i] = resolved[i];
|
|
3737
|
+
}
|
|
3738
|
+
if (curLen > nextLen) current.splice(nextLen);
|
|
3739
|
+
return current;
|
|
3740
|
+
}
|
|
3741
|
+
let changed = nextLen !== curLen;
|
|
3742
|
+
for (let i = 0; !changed && i < nextLen; i++) {
|
|
3743
|
+
if (resolved[i] !== curItems[i]) changed = true;
|
|
3744
|
+
}
|
|
3745
|
+
return changed ? resolved : current;
|
|
3746
|
+
}
|
|
3747
|
+
function reconcileObj(next, current, opts) {
|
|
3748
|
+
const { merge: merge2, keepRef } = opts;
|
|
3749
|
+
const nextKeys = Object.keys(next);
|
|
3750
|
+
const currentKeys = Object.keys(current);
|
|
3751
|
+
const curValues = /* @__PURE__ */ Object.create(null);
|
|
3752
|
+
for (let i = 0; i < currentKeys.length; i++) {
|
|
3753
|
+
curValues[currentKeys[i]] = current[currentKeys[i]];
|
|
3754
|
+
}
|
|
3755
|
+
let out = null;
|
|
3756
|
+
const getOut = () => {
|
|
3757
|
+
if (!out) out = keepRef ? current : { ...curValues };
|
|
3758
|
+
return out;
|
|
3759
|
+
};
|
|
3760
|
+
if (!merge2) {
|
|
3761
|
+
for (let i = 0; i < currentKeys.length; i++) {
|
|
3762
|
+
const k = currentKeys[i];
|
|
3763
|
+
if (!(k in next)) delete getOut()[k];
|
|
3764
|
+
}
|
|
3765
|
+
}
|
|
3766
|
+
for (let i = 0; i < nextKeys.length; i++) {
|
|
3767
|
+
const k = nextKeys[i];
|
|
3768
|
+
const cv = curValues[k];
|
|
3769
|
+
const rv = reconcileValue(next[k], cv, opts);
|
|
3770
|
+
if (rv !== cv || !(k in curValues)) getOut()[k] = rv;
|
|
3771
|
+
}
|
|
3772
|
+
return out ?? current;
|
|
3773
|
+
}
|
|
3774
|
+
function reconcile(next, options) {
|
|
3775
|
+
const opts = {
|
|
3776
|
+
key: options?.key !== void 0 ? options.key : "id",
|
|
3777
|
+
merge: options?.merge ?? false,
|
|
3778
|
+
keepRef: options?.keepRef ?? false
|
|
3779
|
+
};
|
|
3780
|
+
return (current) => {
|
|
3781
|
+
if (current === void 0 || current === null) return next;
|
|
3782
|
+
return reconcileValue(next, current, opts);
|
|
3783
|
+
};
|
|
3784
|
+
}
|
|
3785
|
+
|
|
3786
|
+
// src/lib/object/sort.ts
|
|
3787
|
+
function getAtPath(obj, path) {
|
|
3788
|
+
if (obj == null) return void 0;
|
|
3789
|
+
let cur = obj;
|
|
3790
|
+
for (const segment of path.split(".")) {
|
|
3791
|
+
if (cur == null) return void 0;
|
|
3792
|
+
cur = cur[segment];
|
|
3793
|
+
}
|
|
3794
|
+
return cur;
|
|
3795
|
+
}
|
|
3796
|
+
function compareValues(a, b, locale, localeOptions) {
|
|
3797
|
+
const aN = a == null;
|
|
3798
|
+
const bN = b == null;
|
|
3799
|
+
if (aN && bN) return 0;
|
|
3800
|
+
if (aN) return 1;
|
|
3801
|
+
if (bN) return -1;
|
|
3802
|
+
const aNaN = typeof a === "number" && Number.isNaN(a);
|
|
3803
|
+
const bNaN = typeof b === "number" && Number.isNaN(b);
|
|
3804
|
+
if (aNaN && bNaN) return 0;
|
|
3805
|
+
if (aNaN) return 1;
|
|
3806
|
+
if (bNaN) return -1;
|
|
3807
|
+
if (a instanceof Date && b instanceof Date) {
|
|
3808
|
+
return a.getTime() - b.getTime();
|
|
3809
|
+
}
|
|
3810
|
+
if (typeof a === "boolean" && typeof b === "boolean") {
|
|
3811
|
+
return a === b ? 0 : a ? 1 : -1;
|
|
3812
|
+
}
|
|
3813
|
+
if (typeof a === "number" && typeof b === "number") {
|
|
3814
|
+
return a - b;
|
|
3815
|
+
}
|
|
3816
|
+
if (typeof a === "string" && typeof b === "string") {
|
|
3817
|
+
return a.localeCompare(b, locale, localeOptions);
|
|
3818
|
+
}
|
|
3819
|
+
return String(a).localeCompare(String(b), locale, localeOptions);
|
|
3820
|
+
}
|
|
3821
|
+
function dirFor(order, i) {
|
|
3822
|
+
const o = Array.isArray(order) ? order[i] ?? "asc" : order ?? "asc";
|
|
3823
|
+
return o === "desc" ? -1 : 1;
|
|
3824
|
+
}
|
|
3825
|
+
function sortPrimitiveArray(arr, options) {
|
|
3826
|
+
const { order, locale, localeOptions, nullsLast = true } = options;
|
|
3827
|
+
const dir = dirFor(order, 0);
|
|
3828
|
+
return arr.slice().sort((a, b) => {
|
|
3829
|
+
const aN = a == null;
|
|
3830
|
+
const bN = b == null;
|
|
3831
|
+
if (aN && bN) return 0;
|
|
3832
|
+
if (aN) return nullsLast ? 1 : -1;
|
|
3833
|
+
if (bN) return nullsLast ? -1 : 1;
|
|
3834
|
+
return compareValues(a, b, locale, localeOptions) * dir;
|
|
3835
|
+
});
|
|
3836
|
+
}
|
|
3837
|
+
function sortObjectArray(arr, keys, options) {
|
|
3838
|
+
const { order, locale, localeOptions, nullsLast = true, comparator } = options;
|
|
3839
|
+
if (comparator) {
|
|
3840
|
+
return arr.slice().sort(comparator);
|
|
3841
|
+
}
|
|
3842
|
+
return arr.slice().sort((a, b) => {
|
|
3843
|
+
for (let i = 0; i < keys.length; i++) {
|
|
3844
|
+
const aVal = getAtPath(a, keys[i]);
|
|
3845
|
+
const bVal = getAtPath(b, keys[i]);
|
|
3846
|
+
const aN = aVal == null;
|
|
3847
|
+
const bN = bVal == null;
|
|
3848
|
+
if (aN && bN) continue;
|
|
3849
|
+
if (aN) return nullsLast ? 1 : -1;
|
|
3850
|
+
if (bN) return nullsLast ? -1 : 1;
|
|
3851
|
+
const cmp = compareValues(aVal, bVal, locale, localeOptions);
|
|
3852
|
+
if (cmp !== 0) return cmp * dirFor(order, i);
|
|
3853
|
+
}
|
|
3854
|
+
return 0;
|
|
3855
|
+
});
|
|
3856
|
+
}
|
|
3857
|
+
function sortRecord(record, options) {
|
|
3858
|
+
const { order, priorityKeys = [], locale, localeOptions, deep } = options;
|
|
3859
|
+
const dir = dirFor(order, 0);
|
|
3860
|
+
const prioritySet = new Set(priorityKeys);
|
|
3861
|
+
const rest = Object.keys(record).filter((k) => !prioritySet.has(k));
|
|
3862
|
+
rest.sort(
|
|
3863
|
+
(a, b) => a.localeCompare(b, locale, localeOptions) * dir
|
|
3864
|
+
);
|
|
3865
|
+
const ordered = [...priorityKeys.filter((k) => k in record), ...rest];
|
|
3866
|
+
const result = {};
|
|
3867
|
+
for (const key of ordered) {
|
|
3868
|
+
result[key] = deep ? sortDeep(record[key], options) : record[key];
|
|
3869
|
+
}
|
|
3870
|
+
return result;
|
|
3871
|
+
}
|
|
3872
|
+
function sortDeep(value, options) {
|
|
3873
|
+
if (Array.isArray(value)) {
|
|
3874
|
+
const deepSorted = value.map((item) => sortDeep(item, options));
|
|
3875
|
+
return sortArrayValue(deepSorted, options);
|
|
3876
|
+
}
|
|
3877
|
+
if (isPlainObject(value)) {
|
|
3878
|
+
return sortRecord(value, options);
|
|
3879
|
+
}
|
|
3880
|
+
return value;
|
|
3881
|
+
}
|
|
3882
|
+
function applyIndices(arr, indices) {
|
|
3883
|
+
const len = arr.length;
|
|
3884
|
+
const used = /* @__PURE__ */ new Set();
|
|
3885
|
+
const result = [];
|
|
3886
|
+
for (const idx of indices) {
|
|
3887
|
+
if (idx >= 0 && idx < len && !used.has(idx)) {
|
|
3888
|
+
result.push(arr[idx]);
|
|
3889
|
+
used.add(idx);
|
|
3890
|
+
}
|
|
3891
|
+
}
|
|
3892
|
+
for (let i = 0; i < len; i++) {
|
|
3893
|
+
if (!used.has(i)) result.push(arr[i]);
|
|
3894
|
+
}
|
|
3895
|
+
return result;
|
|
3896
|
+
}
|
|
3897
|
+
function sortArrayValue(arr, options) {
|
|
3898
|
+
const { by, comparator, indices, order } = options;
|
|
3899
|
+
if (indices) {
|
|
3900
|
+
const permuted = applyIndices(arr, indices);
|
|
3901
|
+
return dirFor(order, 0) === -1 ? permuted.reverse() : permuted;
|
|
3902
|
+
}
|
|
3903
|
+
if (comparator) {
|
|
3904
|
+
return arr.slice().sort(comparator);
|
|
3905
|
+
}
|
|
3906
|
+
if (by) {
|
|
3907
|
+
const keys = Array.isArray(by) ? by : [by];
|
|
3908
|
+
return sortObjectArray(arr, keys, options);
|
|
3909
|
+
}
|
|
3910
|
+
const firstReal = arr.find((x) => x != null);
|
|
3911
|
+
if (firstReal != null && isPlainObject(firstReal)) {
|
|
3912
|
+
return dirFor(order, 0) === -1 ? arr.slice().reverse() : arr.slice();
|
|
3913
|
+
}
|
|
3914
|
+
return sortPrimitiveArray(arr, options);
|
|
3915
|
+
}
|
|
3916
|
+
function sort(input, options = {}) {
|
|
3917
|
+
const { deep } = options;
|
|
3918
|
+
if (Array.isArray(input)) {
|
|
3919
|
+
const items = deep ? input.map((item) => sortDeep(item, options)) : input;
|
|
3920
|
+
return sortArrayValue(items, options);
|
|
3921
|
+
}
|
|
3922
|
+
if (isPlainObject(input)) {
|
|
3923
|
+
return sortRecord(input, options);
|
|
3924
|
+
}
|
|
3925
|
+
return input;
|
|
3926
|
+
}
|
|
3927
|
+
function sortBy(arr, by, order = "asc", options = {}) {
|
|
3928
|
+
return sort(arr, { ...options, by, order });
|
|
3929
|
+
}
|
|
3930
|
+
function sortKeys(record, options = {}) {
|
|
3931
|
+
return sort(record, options);
|
|
3932
|
+
}
|
|
3933
|
+
function move(arr, from, to) {
|
|
3934
|
+
const len = arr.length;
|
|
3935
|
+
if (len === 0) return arr.slice();
|
|
3936
|
+
const f = Math.max(0, Math.min(len - 1, from));
|
|
3937
|
+
const t = Math.max(0, Math.min(len - 1, to));
|
|
3938
|
+
if (f === t) return arr.slice();
|
|
3939
|
+
const result = arr.slice();
|
|
3940
|
+
const [item] = result.splice(f, 1);
|
|
3941
|
+
result.splice(t, 0, item);
|
|
3942
|
+
return result;
|
|
3943
|
+
}
|
|
3944
|
+
function swap(arr, i, j) {
|
|
3945
|
+
const len = arr.length;
|
|
3946
|
+
if (i < 0 || i >= len) throw new RangeError(`swap: index ${i} out of range`);
|
|
3947
|
+
if (j < 0 || j >= len) throw new RangeError(`swap: index ${j} out of range`);
|
|
3948
|
+
if (i === j) return arr.slice();
|
|
3949
|
+
const result = arr.slice();
|
|
3950
|
+
result[i] = arr[j];
|
|
3951
|
+
result[j] = arr[i];
|
|
3952
|
+
return result;
|
|
3953
|
+
}
|
|
3954
|
+
function reorder(arr, indices, order = "asc") {
|
|
3955
|
+
return sort(arr, { indices, order });
|
|
3956
|
+
}
|