@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 +1,2351 @@
|
|
|
1
|
-
"use strict";var N=Object.defineProperty;var fe=Object.getOwnPropertyDescriptor;var ye=Object.getOwnPropertyNames;var ue=Object.prototype.hasOwnProperty;var ce=(e,r)=>{for(var t in r)N(e,t,{get:r[t],enumerable:!0})},de=(e,r,t,i)=>{if(r&&typeof r=="object"||typeof r=="function")for(let s of ye(r))!ue.call(e,s)&&s!==t&&N(e,s,{get:()=>r[s],enumerable:!(i=fe(r,s))||i.enumerable});return e};var pe=e=>de(N({},"__esModule",{value:!0}),e);var ke={};ce(ke,{ValueOfKey:()=>P,castProps:()=>Ke,deepMergeWithArrays:()=>Fe,deletePath:()=>ae,findPaths:()=>we,flattenWithArrays:()=>oe,fromPath:()=>q,get:()=>V,getPaths:()=>W,getTypeAtPath:()=>Pe,has:()=>ie,isRangeObject:()=>ne,mergeUpdateAtPath:()=>se,select:()=>be,set:()=>L,setArrayValueWithFilter:()=>re,setAtPath:()=>I,setValueAtIndex:()=>E,setValueAtPath:()=>k,setValueBetweenRange:()=>M,setValueBetweenRangeWithSubPath:()=>te,setValueWithArraySelectors:()=>B,setValueWithFilter:()=>D,setValueWithFilterWithSubPath:()=>_,traverseWithPaths:()=>O,unflattenWithArrays:()=>Oe,update:()=>Ie,updateWithCallBack:()=>le});module.exports=pe(ke);var z=require("@fluixi/utils/primitive"),Q=require("@fluixi/utils/object"),ge=require("@fluixi/utils/array"),Ae=require("@fluixi/utils/functions");function Z(e){let r=typeof e;return e===null||r==="string"||r==="number"||r==="boolean"||r==="symbol"||r==="bigint"||r==="undefined"||(0,z.isDate)(e)}var m=e=>e==null||Array.isArray(e)||typeof e=="function"||e instanceof Date||e instanceof RegExp||e instanceof Map||e instanceof Set||e instanceof Error?!1:typeof e=="object";var w=(e,r,t,i)=>{if(t>i)return;if(r.length===0)return e;let s=r[0],l=r.slice(1);if(s.type==="descendant"){let a=w(e,l,t+1,i);if(a!==void 0)return a;if(e&&typeof e=="object")if(Array.isArray(e)){let n=[];for(let f=0;f<e.length;f++){let o=w(e[f],r,t+1,i);o!==void 0&&(n[f]=o)}return n.length>0?n:void 0}else{let n={};for(let f in e)if(e.hasOwnProperty(f)){let o=w(e[f],r,t+1,i);o!==void 0&&(n[f]=o)}return Object.keys(n).length>0?n:void 0}return}switch(s.type){case"property":if(m(e)&&s.value&&String(s.value)in e){let a=e[String(s.value)],n=w(a,l,t+1,i);if(n!==void 0)return{[String(s.value)]:n}}break;case"index":if(Array.isArray(e)){let a=s.value;if(a>=0&&a<e.length){let n=w(e[a],l,t+1,i);if(n!==void 0){let f=new Array(e.length).filter(o=>o!==void 0);return f[a]=n,f}}}else if(e&&typeof e=="object"){let a=String(s.value);if(a in e){let n=w(e[a],l,t+1,i);if(n!==void 0)return{[a]:n}}}break;case"wildcard":if(e&&typeof e=="object")if(Array.isArray(e)){let a=[],n=!1;for(let f=0;f<e.length;f++){let o=w(e[f],l,t+1,i);o!==void 0&&(a[f]=o,n=!0)}return n?a:void 0}else{let a={},n=!1;for(let f in e)if(e.hasOwnProperty(f)){let o=w(e[f],l,t+1,i);o!==void 0&&(a[f]=o,n=!0)}return n?a:void 0}break;case"slice":if(Array.isArray(e)){let[a,n]=s.value,f=Math.max(0,a),o=Math.min(e.length,n===1/0?e.length:n),u=[],g=!1;for(let p=f;p<o;p++){let A=w(e[p],l,t+1,i);A!==void 0&&(u[p]=A,g=!0)}return g?u:void 0}break;case"union":if(Array.isArray(e)){let a=s.value,n=[],f=!1;for(let o of a)if(o>=0&&o<e.length){let u=w(e[o],l,t+1,i);u!==void 0&&(n[o]=u,f=!0)}return f?n:void 0}break;case"filter":if(Array.isArray(e)){let a=s.value,n=[],f=!1;for(let o=0;o<e.length;o++)if(a(e[o])){let u=w(e[o],l,t+1,i);u!==void 0&&(n[o]=u,f=!0)}return f?n:void 0}break}};var $=(e,r)=>{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 t=new Map;return e.forEach((i,s)=>{(!r||!r.some(l=>l.toString()===s.toString()))&&t.set(s,$(i))}),t}if(e instanceof Set){let t=new Set;return e.forEach(i=>{t.add($(i))}),t}if(Array.isArray(e))return Array.from(e).map(t=>$(t));if(m(e)){let t={};for(let s in e)Object.prototype.hasOwnProperty.call(e,s)&&(!r||!r.some(l=>l.toString()===s.toString()))&&(t[s]=$(e[s]));let i=Object.getOwnPropertySymbols(e);for(let s of i)(!r||!r.some(l=>l.toString()===s.toString()))&&(t[s]=$(e[s]));return t}return e};var G=(e,r)=>e===r,j=(e,r)=>{if(e.length!==r.length)return!1;for(let t=0;t<e.length;t++)if(Array.isArray(e[t])&&Array.isArray(r[t])){if(!j(e[t],r[t]))return!1}else if(m(e[t])&&m(r[t])){if(!H(e[t],r[t]))return!1}else if(e[t]!==r[t])return!1;return!0},H=(e,r)=>{let t=Object.keys(e),i=Object.keys(r);if(t.length!==i.length)return!1;for(let s of t){if(!(s in r))return!1;let l=e[s],a=r[s];if(Array.isArray(l)&&Array.isArray(a)){if(!j(l,a))return!1}else if(m(l)&&m(a)){if(!H(l,a))return!1}else if(l!==a)return!1}return!0},X=(e,r,t={})=>{let{includeDeleted:i=!1,includeAdded:s=!0,deepArrayComparison:l=!0,equalityFn:a=G,preserveStructure:n=!1}=t;if(e==null&&r==null)return{};if(e==null)return s?{...r}:{};if(r==null){if(i){let u={};for(let g in e)u[g]=void 0;return u}return{}}if(!m(e)||!m(r))return a(e,r)?{}:r;let f={},o=new Set;for(let u in r){o.add(u);let g=e[u],p=r[u];if(!(u in e)){s&&(f[u]=p);continue}if(Array.isArray(g)&&Array.isArray(p)){l?j(g,p)?n&&(f[u]=void 0):f[u]=p:g!==p?f[u]=p:n&&(f[u]=void 0);continue}if(m(g)&&m(p)){let A=X(g,p,t);Object.keys(A).length>0?f[u]=A:n&&(f[u]=void 0);continue}a(g,p)?n&&(f[u]=void 0):f[u]=p}if(i)for(let u in e)o.has(u)||(f[u]=void 0);return f},U=(e,r,t={})=>{let i=[],s=[],l=[],a={},{includeDeleted:n=!1,includeAdded:f=!0,deepArrayComparison:o=!0,equalityFn:u=G}=t;if(Array.isArray(e)&&Array.isArray(r)){let A=Math.max(e.length,r.length),d=[],c=!1;for(let y=0;y<A;y++){let T=`[${y}]`;if(y>=e.length)i.push(T),d[y]=r[y],c=!0;else if(y>=r.length)s.push(T),c=!0;else{let x=e[y],h=r[y];m(x)&&m(h)||Array.isArray(x)&&Array.isArray(h)?U(x,h,t).hasChanges?(l.push(T),d[y]=h,c=!0):d[y]=x:u(x,h)?d[y]=x:(l.push(T),d[y]=h,c=!0)}}return{changes:c?d:{},added:i,deleted:s,modified:l,hasChanges:c||s.length>0}}let g=(A,d,c="")=>{let y={added:[],deleted:[],modified:[]};if(Array.isArray(A)&&Array.isArray(d)){let T=Math.max(A.length,d.length);for(let x=0;x<T;x++){let h=c?`${c}[${x}]`:`[${x}]`;if(x>=A.length)y.added.push(h);else if(x>=d.length)y.deleted.push(h);else{let S=A[x],K=d[x];if(m(S)&&m(K)){let v=g(S,K,h);y.added.push(...v.added),y.deleted.push(...v.deleted),y.modified.push(...v.modified)}else if(Array.isArray(S)&&Array.isArray(K)){let v=g(S,K,h);y.added.push(...v.added),y.deleted.push(...v.deleted),y.modified.push(...v.modified)}else S!==K&&y.modified.push(h)}}return y}if(m(A)&&m(d)){let T=new Set([...Object.keys(A||{}),...Object.keys(d||{})]);for(let x of T){let h=A?.[x],S=d?.[x],K=c?`${c}.${x}`:x;if(!(x in(A||{})))y.added.push(K);else if(!(x in(d||{})))y.deleted.push(K);else if(Array.isArray(h)&&Array.isArray(S)){if(!j(h,S)){let v=g(h,S,K);y.added.push(...v.added),y.deleted.push(...v.deleted),y.modified.push(...v.modified)}}else if(m(h)&&m(S)){let v=g(h,S,K);y.added.push(...v.added),y.deleted.push(...v.deleted),y.modified.push(...v.modified)}else h!==S&&y.modified.push(K)}}return y};if(e==null&&r==null)return{changes:{},added:i,deleted:s,modified:l,hasChanges:!1};if(e==null){if(f&&r)for(let A in r)i.push(A),a[A]=r[A];return{changes:a,added:i,deleted:s,modified:l,hasChanges:i.length>0}}if(r==null){if(n)for(let A in e)s.push(A),a[A]=void 0;return{changes:a,added:i,deleted:s,modified:l,hasChanges:s.length>0}}let p=new Set;for(let A in r){p.add(A);let d=e[A],c=r[A];if(!(A in e)){f&&(i.push(A),a[A]=c);continue}let y=!1;if(Array.isArray(d)&&Array.isArray(c)){if(y=o?!j(d,c):d!==c,y){let T=g(d,c,A);i.push(...T.added),s.push(...T.deleted),l.push(...T.modified),a[A]=c}}else if(m(d)&&m(c)){let T=X(d,c,t);if(y=Object.keys(T).length>0,y){let x=g(d,c,A);i.push(...x.added),s.push(...x.deleted),l.push(...x.modified),a[A]=T}}else y=!u(d,c),y&&(l.push(A),a[A]=c)}for(let A in e)p.has(A)||(s.push(A),a[A]=void 0);return{changes:a,added:i,deleted:s,modified:l,hasChanges:i.length>0||s.length>0||l.length>0}};function Te(e){return!!(e&&(e.diff||e.changes)&&(e.modified||e.deleted||e.hasChanges))}var b=(e,r,t)=>{if(!Te(r)){let y=r,T=t||!1;if(Array.isArray(e)&&Array.isArray(y)){let h=T?e:[...e];y.length<h.length&&(h.length=y.length);for(let S=0;S<y.length;S++){let K=y[S];K!==void 0&&(m(h[S])&&m(K)||Array.isArray(h[S])&&Array.isArray(K)?h[S]=b(h[S],K,T):h[S]=K)}return h}if(!m(e))return y;if(!m(y))return e;let x=T?e:{...e};for(let h in y){let S=y[h];S===void 0?delete x[h]:Array.isArray(x[h])&&Array.isArray(S)||m(x[h])&&m(S)?x[h]=b(x[h],S,T):x[h]=S}return x}let{diff:i,added:s=[],deleted:l=[],modified:a=[],changes:n,keepRef:f}=r,o=t!==void 0?t:f,u=n||i;if(!u)return e;let g=new Set(s),p=new Set(l),A=new Set(a),d=y=>{if(s.length===0&&l.length===0&&a.length===0)return!0;let T=y.split(/[.\[\]]+/).filter(Boolean);for(let x=0;x<=T.length;x++){let h=T.slice(0,x).join("."),S=T.slice(0,x).map((K,v)=>/^\d+$/.test(K)?`[${K}]`:v>0?`.${K}`:K).join("");if(g.has(h)||g.has(S)||p.has(h)||p.has(S)||A.has(h)||A.has(S))return!0}return!1};if(Array.isArray(e)&&Array.isArray(u)){let y=o?e:[...e];y.length=u.length;for(let T=0;T<u.length;T++){let x=`[${T}]`,h=u[T];h!==void 0&&d(x)&&(m(y[T])&&m(h)||Array.isArray(y[T])&&Array.isArray(h)?y[T]=b(y[T],h,o):y[T]=h)}return y}if(!m(e))return u;if(!m(u))return e;let c=o?e:{...e};for(let y of l)y.includes(".")||y.includes("[")||d(y)&&delete c[y];for(let y in u){let T=u[y];d(y)&&(T===void 0?delete c[y]:Array.isArray(c[y])&&Array.isArray(T)||m(c[y])&&m(T)?c[y]=b(c[y],T,o):c[y]=T)}return c};var F=(e,r={})=>{let{type:t="auto",throwOnError:i=!1}=r;if(!e||typeof e!="string")return[];let s=[],l="",a=!1,n=!1,f="",o="",u=0,p=t==="auto"?(d=>d.includes("..")?"descendant":d.includes("[?(")?"filter":d.includes("[:")||d.includes("[0:")?"slice":d.includes("[,")||d.includes("[0,1]")?"union":d.includes("[*]")||d.includes(".*.")?"wildcard":d.includes("[]")?"array-wildcard":d.includes("[")&&d.includes("]")?"bracket":"dot")(e):t,A=d=>{let c=d.trim();if(c==="*"||c==="")return{type:"wildcard",value:c===""?"[]":"*",raw:c===""?"[]":d};if(c==="[]")return{type:"wildcard",value:"[]",raw:d};if(/^-?\d+$/.test(c))return{type:"index",value:parseInt(c,10),raw:d};if(c.includes(":")){let y=c.split(":").map(h=>h.trim()),T=y[0]===""?0:parseInt(y[0],10),x=y[1]===""?1/0:parseInt(y[1],10);if(isNaN(T)||isNaN(x)){if(i)throw new Error(`Invalid slice notation: ${d}`);return{type:"wildcard",value:"*",raw:d}}return{type:"slice",value:[T,x],raw:d}}if(c.includes(",")){let y=c.split(",").map(T=>{let x=parseInt(T.trim(),10);return isNaN(x)?-1:x}).filter(T=>T>=0);if(y.length===0){if(i)throw new Error(`Invalid union notation: ${d}`);return{type:"wildcard",value:"*",raw:d}}return{type:"union",value:y,raw:d}}if(c.startsWith("?(")&&c.endsWith(")")){let y=c.slice(2,-1);try{return{type:"filter",value:xe(y),raw:d}}catch(T){if(i)throw T;return{type:"wildcard",value:"*",raw:d}}}return c.startsWith('"')&&c.endsWith('"')||c.startsWith("'")&&c.endsWith("'")?{type:"property",value:c.slice(1,-1),raw:d}:{type:"property",value:c,raw:d}};for(let d=0;d<e.length;d++){let c=e[d],y=e[d+1];if((c==='"'||c==="'")&&!n){n=!0,f=c;continue}else if(c===f&&n){n=!1,f="",c==='"'||c==="'"||(a?o+=c:l+=c);continue}if(n){a?o+=c:l+=c;continue}if(c==="["&&!a){l&&(s.push({type:"property",value:l,raw:l}),l=""),a=!0,o="";continue}if(c==="]"&&a&&!n){a=!1;let x=A(o);s.push(x);continue}let T=s.length>0?s[s.length-1]:null;if(c==="."&&!a&&!n){if(y==="."){let x=e.split("..")[0];s.push({type:"property",value:x,raw:x}),s.push({type:"descendant",value:"..",raw:".."}),d++}else l&&(l==="*"?s.push({type:"wildcard",value:l,raw:l}):s.push({type:"property",value:l,raw:l}),l="");continue}if(a)o+=c;else if(T&&T.type!=="descendant")l+=c;else{let x=e.split(T?.raw)[1];x!==void 0?l=x:l+=c}}return l&&s.push({type:"property",value:l,raw:l}),s.map(d=>{if(d.type==="property"&&typeof d.value=="string"&&/^-?\d+$/.test(d.value)){let c=parseInt(d.value,10);if(!isNaN(c))return{...d,type:"index",value:c}}return d})},xe=e=>{let r=e.replace(/@\.([a-zA-Z_$][\w$]*)/g,(t,i)=>`item.${i}`).replace(/@/g,"item");try{return new Function("item",`return ${r}`)}catch{throw new Error(`Invalid filter expression: ${e}`)}},C=e=>{let r=[];for(let t=0;t<e.length;t++){let i=e[t],s=t>0?e[t-1]:null;switch(i.type){case"property":/[\.\[\]\s]/.test(String(i.value))?r.push(`["${i.value}"]`):(s&&s.type!=="descendant"&&r.push("."),r.push(String(i.value)));break;case"index":r.push(`[${i.value}]`);break;case"wildcard":i.value==="[]"?r.push("[]"):r.push("[*]");break;case"slice":let[a,n]=i.value;r.push(`[${a}:${n===1/0?"":n}]`);break;case"union":let f=i.value;r.push(`[${f.join(",")}]`);break;case"filter":r.push(`[?(${i.raw?.slice(2,-1)||""})]`);break;case"descendant":r.push("..");break}}return r.join("").replace(/\.\[/g,"[")};var Y=(e,r,t)=>{let i=F(r),s=e;for(let l=0;l<i.length;l++){let a=i[l],n=l===i.length-1;switch(a.type){case"property":if(n)s[String(a.value)]=t;else{if(!s[String(a.value)]){let o=i[l+1];s[String(a.value)]=o.type==="index"||o.type==="slice"||o.type==="union"||o.type==="wildcard"?[]:{}}s=s[String(a.value)]}break;case"index":Array.isArray(s)||(s=[]);let f=a.value;if(n){if(f>=s.length)for(let o=s.length;o<=f;o++)s.push(void 0);s[f]=t}else{if(!s[f]){let o=i[l+1];s[f]=o.type==="index"||o.type==="slice"||o.type==="union"||o.type==="wildcard"?[]:{}}s=s[f]}break;case"wildcard":if(Array.isArray(s))if(n)for(let o=0;o<s.length;o++)s[o]=t;else{let o=i[l+1];for(let u=0;u<s.length;u++)s[u]||(s[u]=o.type==="index"||o.type==="slice"||o.type==="union"||o.type==="wildcard"?[]:{});s.length>0&&(s=s[0])}break}}};function V(e,r,t={}){let{preserveStructure:i,multiple:s,throwOnError:l=!1,defaultValue:a,maxDepth:n=20}=t;if(!e||typeof e!="object")return a;if(!r)return e;if(Array.isArray(r))if(i){let u={};for(let g of r){let p=V(e,g,{...t,preserveStructure:!1});p!==void 0&&Y(u,g,p)}return u}else return r.map(u=>V(e,u,{...t,preserveStructure:!1}));let f=F(r,{...t,throwOnError:l});if(f.length===0)return e;if(typeof i>"u"||i===!1){let u=P(e,r);return s===!1&&Array.isArray(u)?u[0]:u===void 0&&a!==void 0?a:u}let o=w(e,f,0,n);return o===void 0&&a!==void 0?a:o}function L(e,r,t,i,s){let l=$(e),a=[];if(typeof r<"u"&&a.push(r),typeof t<"u"&&a.push(t),typeof i<"u"&&a.push(i),typeof s<"u"&&a.push(s),Z(l))return typeof r=="function"?r(l):r;if(a.length===1){if(typeof a[0]=="function")return le(l,a[0]);if(Array.isArray(a[0])){if(Array.isArray(l))if(r.length===0)for(let n=0;n<l.length;n++)l.pop();else return U([...l],[...a[0]]).hasChanges&&b(l,[...a[0]],!0),l}else typeof a[0]=="object"&&!Array.isArray(a[0])?typeof l=="object"&&!Array.isArray(l)&&b(l,a[0],!0):l=a[0];return l}if(Array.isArray(l)){if(typeof r=="string"&&(he(t)||typeof t=="string")&&a.length===3){if(Array.isArray(t)){for(let n=0;n<t.length;n++)k(l,r,i,t[n]);return l}return k(l,r,i,t)}if(typeof r=="string"&&typeof t=="function"&&a.length===2)return k(l,r,t)}if((typeof r=="string"||typeof r=="number")&&(a.length===2||a.length===3))return ne(t)?M(l,r,t,i):typeof r=="number"?typeof i<"u"?k(l,`[${r}].${t}`,i):k(l,`[${r}]`,t,i):typeof t=="number"&&typeof i<"u"?E(l,r,t,i):Array.isArray(t)&&t.length>0&&t.every(n=>typeof n=="number")?R(l,r,t,i):typeof i<"u"&&typeof t=="function"?D(l,r,t,i):k(l,r,t,i);if(typeof r=="string"&&typeof t=="number"){if(a.length===3)return E(l,r,t,i);if(a.length===4)return me(l,r,t,i,s)}if(typeof r=="string"&&Array.isArray(t)){if(a.length===3)return R(l,r,t,i);if(a.length===4)return Se(l,r,t,i,s)}if(typeof r=="string"&&typeof t=="object"&&!Array.isArray(t)&&t!==null){let n=t;if("from"in n||"to"in n){if(a.length===3)return M(l,r,n,i);if(a.length===4)return te(l,r,n,i,s)}}if(typeof r=="object"&&!Array.isArray(r)&&a.length===1)return b(l,r,!0);if(typeof r=="string"&&typeof t=="object"&&!Array.isArray(t)&&a.length===2)return se(l,r,t);if(typeof r=="string"&&typeof t=="function"){if(a.length===3)return D(l,r,t,i);if(a.length===4)return _(l,r,t,i,s)}if(typeof r=="function"&&typeof t=="string"){if(a.length===3)return re(l,r,t,i);if(a.length===4)return _(l,t,r,i,s)}if(Array.isArray(r)){if(a.length===2&&typeof t=="object"&&!Array.isArray(t))return B(l,r,t);if(a.length===2)return B(l,r,t)}throw new Error(`Invalid set parameters: ${JSON.stringify(a)}`)}function J(e,r,t,i=!1){let s=F(e);if(s.length===0)return t;let l=s[s.length-1].type==="wildcard",a=s.some(f=>f.type==="wildcard")&&Array.isArray(r),n;if((l||a&&i||a&&s.length===2)&&Array.isArray(r))for(let f=0;f<r.length;f++){let o=typeof t=="function"?t(r[f]):typeof r[f]=="object"?b(r[f],t,!0):t;n||(n=[]),n.push(o)}return!l&&!n&&(n=typeof t=="function"?t(r):t),n}function ne(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 he(e){return Array.isArray(e)&&e.every(r=>typeof r=="string")}function k(e,r,t,i){let s=F(r),l=P(e,r,{paths:s,createMissing:!0}),a;i&&typeof t=="function"&&(a=V(l,i));let f=J(r,i?a:l,t,Array.isArray(e));return i?I(e,`${r}.${i}`,f,{createMissing:!0}):I(e,r,f,{createMissing:!0})}function E(e,r,t,i){let s=P(e,r,{createMissing:!0});if(!Array.isArray(s))throw new Error(`Target at ${r} is not an array ${t}`);let l=structuredClone(s),a=J(r,l[t],i);if(t<0){let n=s.length+t;if(n<0)throw new Error(`Index ${t} is out of bounds`);return I(e,`${r}[${n}]`,a,{createMissing:!0})}return I(e,`${r}[${t}]`,a,{createMissing:!0})}var me=(e,r,t,i,s)=>typeof i=="function"?k(e,`${r}[${t}]`,i):typeof i=="string"?k(e,`${r}[${t}]`,s,i):null,R=(e,r,t,i)=>{let s=P(e,r);if(!Array.isArray(s))throw new Error(`Target at ${r} is not an array`);for(let l of t)typeof l=="number"?E(e,r,l,i):typeof l=="string"&&k(e,r,i,l);return e},Se=(e,r,t,i,s)=>R(e,r,t,a=>{let n=typeof s=="function"?s(a?P(a,i):void 0):s,f=a?{...a}:{};return I(f,i,n),f});function M(e,r,t,i){let s=P(e,r);if(!Array.isArray(s))throw new Error(`Target at ${r} is not an array`);let l=t.from??0,a=t.to??s.length-1,n=t.by??1,f=[];for(let o=l;o<=a;o+=n)o>=0&&o<s.length&&f.push(o);return R(e,r,f,i)}var te=(e,r,t,i,s)=>M(e,r,t,a=>{let n=typeof s=="function"?s(a?P(a,i):void 0):s,f=a?{...a}:{};return I(f,i,n),f});function Ke(e,r){return Object.assign(e,r)}function D(e,r,t,i){let s=P(e,r);if(!Array.isArray(s))throw new Error(`Target at ${r} is not an array`);let l=[],a=[...s];return s.forEach((n,f)=>{t(n,f)&&l.push(f)}),R(e,r,l,i)}function re(e,r,t,i){let s=e;if(!Array.isArray(s))throw new Error(`Target at ${t} is not an array`);let l=[],a=[...s];return s.forEach((n,f)=>{n=I(n,t,i,{createMissing:!0})}),s}function _(e,r,t,i,s){return D(e,r,t,a=>{let n=typeof s=="function"?s(a?P(a,i):void 0):s,f=a?{...a}:{};return I(f,i,n),f})}function se(e,r,t){let i=P(e,r,{createMissing:!0}),s=b(i||{},t,!0);return I(e,r,s,{createMissing:!0})}function B(e,r,t){if(typeof t=="object"&&!Array.isArray(t)&&t!==null)for(let[i,s]of Object.entries(t))r.includes(i)&&I(e,i,s,{createMissing:!0});else for(let i of r){let s=J(i,P(e,i),t);I(e,i,s,{createMissing:!0})}return e}function P(e,r,t={}){let{createMissing:i=!1,add:s=!1,debug:l=!1}=t,a=F(r),n=e,f=!1,o="",u=r;if(typeof n=="object"&&!Array.isArray(n)&&u.length>0&&n[u])return n=n[u],n;for(let g=0;g<a.length;g++){let p=a[g],A=a.length>1?g===a.length-1:!1;if(n===null&&!i)return;let d=a[g+1];switch(p.type){case"property":if(n&&typeof n=="object")if(Array.isArray(n)){if(typeof p.value=="number"){let c=p.value;if(i&&c>=n.length)for(let y=n.length;y<=c;y++)n.push(void 0);if(i&&(n[c]===null||n[c]===void 0)&&(!A||f)&&(d?n[c]=d.type==="index"||d.type==="slice"||d.type==="union"||d.type==="wildcard"?[]:{}:n[c]=null,f=!1),n=n[c],d){f=!0;continue}}else s?n.push({[String(p.value)]:null}):n=n.map(c=>c[String(p.value)]);if(d)continue;return ee(n)}else{if(n[o]&&o.split(".").length>=2){n=n[o];continue}if(i&&(n[String(p.value)]===null||n[String(p.value)]===void 0)&&(!A||f)&&(d?n[String(p.value)]=d.type==="index"||d.type==="slice"||d.type==="union"||d.type==="wildcard"?[]:{}:n[String(p.value)]=null,f=!1,n=n[String(p.value)],d)){f=!0;continue}if(n?(!n[String(p.value)]&&i&&(n[String(p.value)]=null),n=n[String(p.value)]):n={[String(p.value)]:null},d)continue}else if(i)n={},n[String(p.value)]=d.type==="index"||d.type==="slice"||d.type==="union"||d.type==="wildcard"||t.type==="array"?[]:{},n=n[String(p.value)];else return;break;case"index":case"wildcard":if(Array.isArray(n)){if(p.type==="index"){let c=p.value;if(c<0){n=n[n.length-Math.abs(c)];continue}if(i&&(c>=n.length||n[c]==null||n[c]==null)&&!A){for(let y=n.length;y<=c;y++)n.push(void 0);d&&(n[c]=d.type==="index"||d.type==="slice"||d.type==="union"||d.type==="wildcard"?[]:{})}n=n[c]}else if(!A)continue}else if(i){let c=a[g+1];n=[];let y=p.value;for(let T=0;T<=y;T++)n.push(void 0);n[y]=c.type==="index"||c.type==="slice"||c.type==="union"||c.type==="wildcard"||t.type==="array"?[]:{},n=n[y]}else{if(p.type==="index"&&typeof n=="object"&&n&&n[String(p.value)]!==void 0){n=n[String(p.value)];continue}if(typeof n=="object"){n=Object.values(n);continue}return}break;default:return}}return ee(n)}function ee(e){return Array.isArray(e)?e.filter(r=>typeof r<"u"):e}var I=(e,r,t,i={})=>{if(r==="[*]")return b(e,t,!0);let{createMissing:s=!0}=i,l=F(r);if(l.length===0)return e;let a=l[l.length-1],n=e;for(let f=0;f<l.length-1;f++){let o=l[f];if(n==null){if(!s)return e;throw new Error(`Cannot navigate through null/undefined at segment ${f}`)}let u=l[f+1],g=u.type==="index"||u.type==="slice"||u.type==="union"||u.type==="wildcard";if(o.type==="property"){if(Array.isArray(n)&&typeof typeof o.value=="number"){let p=o.value;if(p<0)throw new Error(`Negative index ${p} not allowed`);if(p>=n.length&&s)for(let A=n.length;A<=p;A++)n.push(void 0);(n[p]===null||n[p]===void 0)&&s&&(n[p]=g?[]:{}),n=n[p]}else(n[String(o.value)]===null||n[String(o.value)]===void 0)&&s&&(n[String(o.value)]=g?[]:{}),typeof n=="object"&&!Array.isArray(n)&&(n=n[String(o.value)]);if(u)continue;break}else if(o.type==="index"||o.type==="wildcard"){if(!Array.isArray(n)){if(!s)return e;n=[]}if(o.type==="index"){let p=o.value;if(p<0)throw new Error(`Negative index ${p} not allowed`);if(p>=n.length&&s)for(let A=n.length;A<=p;A++)n.push(void 0);(n[p]===null||n[p]===void 0)&&s&&(n[p]=g?[]:void 0),n=n[p]}if(u)continue;break}else throw new Error(`Invalid segment type for set: ${o.type}`)}switch(a.type){case"property":if(Array.isArray(n)&&typeof a.value=="number"){let o=a.value;if(o<0)throw new Error(`Negative index ${o} not allowed`);if(o>=n.length)for(let u=n.length;u<=o;u++)n.push(void 0);Array.isArray(t)?n[o]=b(structuredClone(n[o]),t[o],!0):n[o]=t}else if(n||(n={}),Array.isArray(n)){let o=r.split("."),u;if(o.length>=2){o.shift();for(let g=0;g<n.length;g++)Array.isArray(t)?(u=q(o.join("."),t[g]),n[g]=typeof n[g]=="object"?b(n[g],u,!0):u):(u=q(o.join("."),t),n[g]=typeof n[g]=="object"?b(n[g],u,!0):u)}else u={[String(a.value)]:t}}else Array.isArray(t)&&t.length===0?typeof n[String(a.value)]>"u"?n[String(a.value)]=[]:n[String(a.value)]=[]:n[String(a.value)]=t;break;case"index":case"wildcard":if(!Array.isArray(n))throw new Error("Cannot use index on non-array for final segment");let f=a.value;if(a.type==="index"){if(f<0)throw new Error(`Negative index ${f} not allowed`);if(f>=n.length)for(let o=n.length;o<=f;o++)n.push(void 0);if(Array.isArray(t))if(typeof n[f]==typeof t[f])try{n[f]=b($(n[f]),t[f],!0)}catch{throw Error("expected on array")}else n[f]=t;else n[f]=t}else if(a.type==="wildcard")if(Array.isArray(t))for(let o=0;o<n.length;o++)typeof n[o]=="object"?n[o]=b(n[o],t[o],!0):n[o]=t[o];else for(let o=0;o<n.length;o++)typeof n[o]=="object"?n[o]=b(n[o],t,!0):n[o]=t;break;default:throw new Error(`Invalid final segment type for set: ${a.type}`)}return e};var q=(e,r)=>{let t=F(e);if(t.length===0)return r;let i=r;for(let s=t.length-1;s>=0;s--){let l=t[s];if(l.type==="property")i={[String(l.value)]:i};else if(l.type==="index"){let a=parseInt(String(l.value)),n=[];n[a]=i,i=n}else l.type==="wildcard"?Array.isArray(i)?i=i:i=[i]:l.type==="slice"?Array.isArray(i)||(i=[i]):l.type==="union"?Array.isArray(i)||(i=[i]):l.type==="filter"?Array.isArray(i)||(i=[i]):l.type==="descendant"&&(i=i)}return i},be=(e,r,t={})=>V(e,r,{...t,preserveStructure:!1}),ie=(e,r,t={})=>{if(Array.isArray(r))return r.map(s=>ie(e,s,t));let i=V(e,r,{...t,preserveStructure:!1});return Array.isArray(i)?i.length>0?i.every(s=>s!==void 0):!1:i!==void 0},ae=(e,r,t={})=>{let{throwOnError:i=!1}=t;if(Array.isArray(r))return r.map(n=>ae(e,n,t));let s=F(r,{throwOnError:i});if(s.length===0)return!1;let l=s[s.length-1];if(s.some(n=>n.type==="wildcard"||n.type==="slice"||n.type==="union")){let n=W(e,r,t),f=!1;for(let o of n)ve(e,o)&&(f=!0);return f}let a=e;for(let n=0;n<s.length-1;n++){let f=s[n];if(a==null)return!1;switch(f.type){case"property":a=a[String(f.value)];break;case"index":if(!Array.isArray(a))return!1;let o=f.value;if(o<0||o>=a.length)return!1;a=a[o];break;default:return!1}}if(a==null)return!1;switch(l.type){case"property":if(String(l.value)in a)return delete a[String(l.value)],!0;break;case"index":if(!Array.isArray(a))return!1;let n=l.value;if(n>=0&&n<a.length)return a.splice(n,1),!0;break}return!1},ve=(e,r)=>{let t=F(r);if(t.length===0)return!1;let i=t[t.length-1],s=e;for(let l=0;l<t.length-1;l++){let a=t[l];if(s==null)return!1;switch(a.type){case"property":s=s[String(a.value)];break;case"index":if(!Array.isArray(s))return!1;let n=a.value;if(n<0||n>=s.length)return!1;s=s[n];break;default:return!1}}if(s==null)return!1;switch(i.type){case"property":if(String(i.value)in s)return delete s[String(i.value)],!0;break;case"index":if(!Array.isArray(s))return!1;let l=i.value;if(l>=0&&l<s.length)return s.splice(l,1),!0;break}return!1},W=(e,r,t={})=>{let{format:i="selector",maxDepth:s=20}=t;if(r&&Array.isArray(r)){let n=[];for(let f of r){let o=W(e,f,t);n.push(...o)}return n}if(r){let n=F(r,t);return O(e,n,0,s).map(o=>i==="selector"?o.path:o.stringPath)}let l=[],a=(n,f=[],o=[],u=0)=>{if(!(u>s)){if(f.length>0){let g=C(f);l.push({path:g,stringPath:o.join(".")})}if(n&&typeof n=="object")if(Array.isArray(n))for(let g=0;g<n.length;g++)a(n[g],[...f,{type:"index",value:g,raw:`[${g}]`}],[...o,`[${g}]`],u+1);else for(let g in n)n.hasOwnProperty(g)&&a(n[g],[...f,{type:"property",value:g,raw:g}],[...o,g],u+1)}};return a(e),l.map(n=>i==="selector"?n.path:n.stringPath)},we=(e,r)=>W(e).filter(i=>{let s=V(e,i);return r(i,s)}),oe=(e,r=".")=>{let t={},i=(s,l=[])=>{if(s==null)return;let a=w(s,l,0,20);if(a!==void 0){t[C(l)]=a;return}if(Array.isArray(s))for(let n=0;n<s.length;n++){let f=[...l,{type:"index",value:n,raw:`[${n}]`}];i(s[n],f)}else if(m(s)){for(let f in s)if(s.hasOwnProperty(f)){let o=[...l,{type:"property",value:f,raw:f}];i(s[f],o)}let n=Object.getOwnPropertySymbols(s);for(let f of n){let o=[...l,{type:"property",value:f.toString(),raw:f.toString()}];i(s[f],o)}}else{let n=C(l);t[n]=s}};return i(e),t},Oe=e=>{let r={};for(let t in e)L(r,t,e[t]);return r},Fe=(...e)=>{let r={};for(let t of e){if(!m(t))continue;let i=oe(t);for(let s in i)L(r,s,i[s])}return r};function O(e,r,t,i,s=[],l=[]){if(t>i)return[];if(r.length===0)return[{value:e,path:C(s),stringPath:l.join(".")}];let a=r[0],n=r.slice(1),f=[];if(a.type==="descendant"){if(f.push(...O(e,n,t+1,i,s,l)),e&&typeof e=="object")if(Array.isArray(e))for(let o=0;o<e.length;o++)f.push(...O(e[o],r,t+1,i,[...s,{type:"index",value:o,raw:`[${o}]`}],[...l,`[${o}]`]));else for(let o in e)e.hasOwnProperty(o)&&f.push(...O(e[o],r,t+1,i,[...s,{type:"property",value:o,raw:o}],[...l,o]));return f}switch(a.type){case"property":if(e&&typeof e=="object"&&String(a.value)in e){let o=e[String(a.value)];f.push(...O(o,n,t+1,i,[...s,a],[...l,String(a.value)]))}break;case"index":if(Array.isArray(e)){let o=a.value;o>=0&&o<e.length&&f.push(...O(e[o],n,t+1,i,[...s,a],[...l,`[${o}]`]))}else if(e&&typeof e=="object"){let o=String(a.value);o in e&&f.push(...O(e[o],n,t+1,i,[...s,{...a,type:"property"}],[...l,o]))}break;case"wildcard":if(e&&typeof e=="object")if(Array.isArray(e))for(let o=0;o<e.length;o++)f.push(...O(e[o],n,t+1,i,[...s,{type:"index",value:o,raw:`[${o}]`}],[...l,`[${o}]`]));else for(let o in e)e.hasOwnProperty(o)&&f.push(...O(e[o],n,t+1,i,[...s,{type:"property",value:o,raw:o}],[...l,o]));break;case"slice":if(Array.isArray(e)){let[o,u]=a.value,g=Math.max(0,o),p=Math.min(e.length,u===1/0?e.length:u);for(let A=g;A<p;A++)f.push(...O(e[A],n,t+1,i,[...s,{type:"index",value:A,raw:`[${A}]`}],[...l,`[${A}]`]))}break;case"union":if(Array.isArray(e)){let o=a.value;for(let u of o)u>=0&&u<e.length&&f.push(...O(e[u],n,t+1,i,[...s,{type:"index",value:u,raw:`[${u}]`}],[...l,`[${u}]`]))}break;case"filter":if(Array.isArray(e)){let o=a.value;for(let u=0;u<e.length;u++)o(e[u])&&f.push(...O(e[u],n,t+1,i,[...s,{type:"index",value:u,raw:`[${u}]`}],[...l,`[${u}]`]))}break}return f}var Ie=(e,r,t)=>{let i=V(e,r),s=t(i);return k(e,r,s)},le=(e,r)=>{let t=r(e);return b(e,t,!0)},Pe=(e,r)=>{let t=V(e,r);if(t!==void 0)return t===null?"null":Array.isArray(t)?"array":typeof t};
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/lib/object/getter-setter.ts
|
|
21
|
+
var getter_setter_exports = {};
|
|
22
|
+
__export(getter_setter_exports, {
|
|
23
|
+
ValueOfKey: () => ValueOfKey,
|
|
24
|
+
castProps: () => castProps,
|
|
25
|
+
deepMergeWithArrays: () => deepMergeWithArrays,
|
|
26
|
+
deletePath: () => deletePath,
|
|
27
|
+
findPaths: () => findPaths,
|
|
28
|
+
flattenWithArrays: () => flattenWithArrays,
|
|
29
|
+
fromPath: () => fromPath,
|
|
30
|
+
get: () => get,
|
|
31
|
+
getPaths: () => getPaths,
|
|
32
|
+
getTypeAtPath: () => getTypeAtPath,
|
|
33
|
+
has: () => has,
|
|
34
|
+
isRangeObject: () => isRangeObject,
|
|
35
|
+
mergeUpdateAtPath: () => mergeUpdateAtPath,
|
|
36
|
+
select: () => select,
|
|
37
|
+
set: () => set,
|
|
38
|
+
setArrayValueWithFilter: () => setArrayValueWithFilter,
|
|
39
|
+
setAtPath: () => setAtPath,
|
|
40
|
+
setValueAtIndex: () => setValueAtIndex,
|
|
41
|
+
setValueAtPath: () => setValueAtPath,
|
|
42
|
+
setValueBetweenRange: () => setValueBetweenRange,
|
|
43
|
+
setValueBetweenRangeWithSubPath: () => setValueBetweenRangeWithSubPath,
|
|
44
|
+
setValueWithArraySelectors: () => setValueWithArraySelectors,
|
|
45
|
+
setValueWithFilter: () => setValueWithFilter,
|
|
46
|
+
setValueWithFilterWithSubPath: () => setValueWithFilterWithSubPath,
|
|
47
|
+
traverseWithPaths: () => traverseWithPaths,
|
|
48
|
+
unflattenWithArrays: () => unflattenWithArrays,
|
|
49
|
+
update: () => update,
|
|
50
|
+
updateWithCallBack: () => updateWithCallBack
|
|
51
|
+
});
|
|
52
|
+
module.exports = __toCommonJS(getter_setter_exports);
|
|
53
|
+
|
|
54
|
+
// src/lib/helpers.ts
|
|
55
|
+
var import_primitive = require("@fluixi/utils/primitive");
|
|
56
|
+
var import_object = require("@fluixi/utils/object");
|
|
57
|
+
var import_array = require("@fluixi/utils/array");
|
|
58
|
+
var import_functions = require("@fluixi/utils/functions");
|
|
59
|
+
function isPrimitive(v) {
|
|
60
|
+
const type = typeof v;
|
|
61
|
+
return v === null || type === "string" || type === "number" || type === "boolean" || type === "symbol" || type === "bigint" || type === "undefined" || (0, import_primitive.isDate)(v);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// src/lib/object/utils.ts
|
|
65
|
+
var isObject2 = (value) => {
|
|
66
|
+
if (value == null) return false;
|
|
67
|
+
if (Array.isArray(value)) return false;
|
|
68
|
+
if (typeof value === "function") return false;
|
|
69
|
+
if (value instanceof Date) return false;
|
|
70
|
+
if (value instanceof RegExp) return false;
|
|
71
|
+
if (value instanceof Map) return false;
|
|
72
|
+
if (value instanceof Set) return false;
|
|
73
|
+
if (value instanceof Error) return false;
|
|
74
|
+
return typeof value === "object";
|
|
75
|
+
};
|
|
76
|
+
var traverseStructure = (current, segments, depth, maxDepth) => {
|
|
77
|
+
if (depth > maxDepth) return void 0;
|
|
78
|
+
if (segments.length === 0) return current;
|
|
79
|
+
const segment = segments[0];
|
|
80
|
+
const remaining = segments.slice(1);
|
|
81
|
+
if (segment.type === "descendant") {
|
|
82
|
+
const result = traverseStructure(current, remaining, depth + 1, maxDepth);
|
|
83
|
+
if (result !== void 0) {
|
|
84
|
+
return result;
|
|
85
|
+
}
|
|
86
|
+
if (current && typeof current === "object") {
|
|
87
|
+
if (Array.isArray(current)) {
|
|
88
|
+
const arrResult = [];
|
|
89
|
+
for (let i = 0; i < current.length; i++) {
|
|
90
|
+
const itemResult = traverseStructure(
|
|
91
|
+
current[i],
|
|
92
|
+
segments,
|
|
93
|
+
depth + 1,
|
|
94
|
+
maxDepth
|
|
95
|
+
);
|
|
96
|
+
if (itemResult !== void 0) {
|
|
97
|
+
arrResult[i] = itemResult;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return arrResult.length > 0 ? arrResult : void 0;
|
|
101
|
+
} else {
|
|
102
|
+
const objResult = {};
|
|
103
|
+
for (const key in current) {
|
|
104
|
+
if (current.hasOwnProperty(key)) {
|
|
105
|
+
const itemResult = traverseStructure(
|
|
106
|
+
current[key],
|
|
107
|
+
segments,
|
|
108
|
+
depth + 1,
|
|
109
|
+
maxDepth
|
|
110
|
+
);
|
|
111
|
+
if (itemResult !== void 0) {
|
|
112
|
+
objResult[key] = itemResult;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return Object.keys(objResult).length > 0 ? objResult : void 0;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return void 0;
|
|
120
|
+
}
|
|
121
|
+
switch (segment.type) {
|
|
122
|
+
case "property":
|
|
123
|
+
if (isObject2(current) && segment.value && String(segment.value) in current) {
|
|
124
|
+
const next = current[String(segment.value)];
|
|
125
|
+
const result = traverseStructure(next, remaining, depth + 1, maxDepth);
|
|
126
|
+
if (result !== void 0) {
|
|
127
|
+
return { [String(segment.value)]: result };
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
break;
|
|
131
|
+
case "index":
|
|
132
|
+
if (Array.isArray(current)) {
|
|
133
|
+
const index = segment.value;
|
|
134
|
+
if (index >= 0 && index < current.length) {
|
|
135
|
+
const result = traverseStructure(
|
|
136
|
+
current[index],
|
|
137
|
+
remaining,
|
|
138
|
+
depth + 1,
|
|
139
|
+
maxDepth
|
|
140
|
+
);
|
|
141
|
+
if (result !== void 0) {
|
|
142
|
+
const arr = new Array(current.length).filter(
|
|
143
|
+
(a) => a !== void 0
|
|
144
|
+
);
|
|
145
|
+
arr[index] = result;
|
|
146
|
+
return arr;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
} else if (current && typeof current === "object") {
|
|
150
|
+
const key = String(segment.value);
|
|
151
|
+
if (key in current) {
|
|
152
|
+
const result = traverseStructure(
|
|
153
|
+
current[key],
|
|
154
|
+
remaining,
|
|
155
|
+
depth + 1,
|
|
156
|
+
maxDepth
|
|
157
|
+
);
|
|
158
|
+
if (result !== void 0) {
|
|
159
|
+
return { [key]: result };
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
break;
|
|
164
|
+
case "wildcard":
|
|
165
|
+
if (current && typeof current === "object") {
|
|
166
|
+
if (Array.isArray(current)) {
|
|
167
|
+
const arrResult = [];
|
|
168
|
+
let hasResults = false;
|
|
169
|
+
for (let i = 0; i < current.length; i++) {
|
|
170
|
+
const result = traverseStructure(
|
|
171
|
+
current[i],
|
|
172
|
+
remaining,
|
|
173
|
+
depth + 1,
|
|
174
|
+
maxDepth
|
|
175
|
+
);
|
|
176
|
+
if (result !== void 0) {
|
|
177
|
+
arrResult[i] = result;
|
|
178
|
+
hasResults = true;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return hasResults ? arrResult : void 0;
|
|
182
|
+
} else {
|
|
183
|
+
const objResult = {};
|
|
184
|
+
let hasResults = false;
|
|
185
|
+
for (const key in current) {
|
|
186
|
+
if (current.hasOwnProperty(key)) {
|
|
187
|
+
const result = traverseStructure(
|
|
188
|
+
current[key],
|
|
189
|
+
remaining,
|
|
190
|
+
depth + 1,
|
|
191
|
+
maxDepth
|
|
192
|
+
);
|
|
193
|
+
if (result !== void 0) {
|
|
194
|
+
objResult[key] = result;
|
|
195
|
+
hasResults = true;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return hasResults ? objResult : void 0;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
break;
|
|
203
|
+
case "slice":
|
|
204
|
+
if (Array.isArray(current)) {
|
|
205
|
+
const [start, end] = segment.value;
|
|
206
|
+
const actualStart = Math.max(0, start);
|
|
207
|
+
const actualEnd = Math.min(
|
|
208
|
+
current.length,
|
|
209
|
+
end === Infinity ? current.length : end
|
|
210
|
+
);
|
|
211
|
+
const arrResult = [];
|
|
212
|
+
let hasResults = false;
|
|
213
|
+
for (let i = actualStart; i < actualEnd; i++) {
|
|
214
|
+
const result = traverseStructure(
|
|
215
|
+
current[i],
|
|
216
|
+
remaining,
|
|
217
|
+
depth + 1,
|
|
218
|
+
maxDepth
|
|
219
|
+
);
|
|
220
|
+
if (result !== void 0) {
|
|
221
|
+
arrResult[i] = result;
|
|
222
|
+
hasResults = true;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return hasResults ? arrResult : void 0;
|
|
226
|
+
}
|
|
227
|
+
break;
|
|
228
|
+
case "union":
|
|
229
|
+
if (Array.isArray(current)) {
|
|
230
|
+
const indices = segment.value;
|
|
231
|
+
const arrResult = [];
|
|
232
|
+
let hasResults = false;
|
|
233
|
+
for (const index of indices) {
|
|
234
|
+
if (index >= 0 && index < current.length) {
|
|
235
|
+
const result = traverseStructure(
|
|
236
|
+
current[index],
|
|
237
|
+
remaining,
|
|
238
|
+
depth + 1,
|
|
239
|
+
maxDepth
|
|
240
|
+
);
|
|
241
|
+
if (result !== void 0) {
|
|
242
|
+
arrResult[index] = result;
|
|
243
|
+
hasResults = true;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return hasResults ? arrResult : void 0;
|
|
248
|
+
}
|
|
249
|
+
break;
|
|
250
|
+
case "filter":
|
|
251
|
+
if (Array.isArray(current)) {
|
|
252
|
+
const filterFn = segment.value;
|
|
253
|
+
const arrResult = [];
|
|
254
|
+
let hasResults = false;
|
|
255
|
+
for (let i = 0; i < current.length; i++) {
|
|
256
|
+
if (filterFn(current[i])) {
|
|
257
|
+
const result = traverseStructure(
|
|
258
|
+
current[i],
|
|
259
|
+
remaining,
|
|
260
|
+
depth + 1,
|
|
261
|
+
maxDepth
|
|
262
|
+
);
|
|
263
|
+
if (result !== void 0) {
|
|
264
|
+
arrResult[i] = result;
|
|
265
|
+
hasResults = true;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return hasResults ? arrResult : void 0;
|
|
270
|
+
}
|
|
271
|
+
break;
|
|
272
|
+
}
|
|
273
|
+
return void 0;
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
// src/lib/object/clone.ts
|
|
277
|
+
var deepClone = (obj, skipKeys) => {
|
|
278
|
+
if (obj == null || typeof obj !== "object") {
|
|
279
|
+
return obj;
|
|
280
|
+
}
|
|
281
|
+
if (obj instanceof Date) {
|
|
282
|
+
return new Date(obj.getTime());
|
|
283
|
+
}
|
|
284
|
+
if (obj instanceof RegExp) {
|
|
285
|
+
return new RegExp(obj.source, obj.flags);
|
|
286
|
+
}
|
|
287
|
+
if (obj instanceof Map) {
|
|
288
|
+
const clone = /* @__PURE__ */ new Map();
|
|
289
|
+
obj.forEach((value, key) => {
|
|
290
|
+
if (!skipKeys || !skipKeys.some((k) => k.toString() === key.toString())) {
|
|
291
|
+
clone.set(key, deepClone(value));
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
return clone;
|
|
295
|
+
}
|
|
296
|
+
if (obj instanceof Set) {
|
|
297
|
+
const clone = /* @__PURE__ */ new Set();
|
|
298
|
+
obj.forEach((value) => {
|
|
299
|
+
clone.add(deepClone(value));
|
|
300
|
+
});
|
|
301
|
+
return clone;
|
|
302
|
+
}
|
|
303
|
+
if (Array.isArray(obj)) {
|
|
304
|
+
return Array.from(obj).map((item) => deepClone(item));
|
|
305
|
+
}
|
|
306
|
+
if (isObject2(obj)) {
|
|
307
|
+
const clone = {};
|
|
308
|
+
for (const key in obj) {
|
|
309
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
310
|
+
if (!skipKeys || !skipKeys.some((k) => k.toString() === key.toString())) {
|
|
311
|
+
clone[key] = deepClone(obj[key]);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
const symbols = Object.getOwnPropertySymbols(obj);
|
|
316
|
+
for (const symbol of symbols) {
|
|
317
|
+
if (!skipKeys || !skipKeys.some((s) => s.toString() === symbol.toString())) {
|
|
318
|
+
clone[symbol] = deepClone(obj[symbol]);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
return clone;
|
|
322
|
+
}
|
|
323
|
+
return obj;
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
// src/lib/object/diff.ts
|
|
327
|
+
var defaultEqualityFn = (a, b) => a === b;
|
|
328
|
+
var deepArrayEqual = (arr1, arr2) => {
|
|
329
|
+
if (arr1.length !== arr2.length) return false;
|
|
330
|
+
for (let i = 0; i < arr1.length; i++) {
|
|
331
|
+
if (Array.isArray(arr1[i]) && Array.isArray(arr2[i])) {
|
|
332
|
+
if (!deepArrayEqual(arr1[i], arr2[i])) return false;
|
|
333
|
+
} else if (isObject2(arr1[i]) && isObject2(arr2[i])) {
|
|
334
|
+
if (!deepObjectEqual(arr1[i], arr2[i])) return false;
|
|
335
|
+
} else if (arr1[i] !== arr2[i]) {
|
|
336
|
+
return false;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return true;
|
|
340
|
+
};
|
|
341
|
+
var deepObjectEqual = (obj1, obj2) => {
|
|
342
|
+
const keys1 = Object.keys(obj1);
|
|
343
|
+
const keys2 = Object.keys(obj2);
|
|
344
|
+
if (keys1.length !== keys2.length) return false;
|
|
345
|
+
for (const key of keys1) {
|
|
346
|
+
if (!(key in obj2)) return false;
|
|
347
|
+
const val1 = obj1[key];
|
|
348
|
+
const val2 = obj2[key];
|
|
349
|
+
if (Array.isArray(val1) && Array.isArray(val2)) {
|
|
350
|
+
if (!deepArrayEqual(val1, val2)) return false;
|
|
351
|
+
} else if (isObject2(val1) && isObject2(val2)) {
|
|
352
|
+
if (!deepObjectEqual(val1, val2)) return false;
|
|
353
|
+
} else if (val1 !== val2) {
|
|
354
|
+
return false;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
return true;
|
|
358
|
+
};
|
|
359
|
+
var diffObject = (oldObj, newObj, options = {}) => {
|
|
360
|
+
const {
|
|
361
|
+
includeDeleted = false,
|
|
362
|
+
includeAdded = true,
|
|
363
|
+
deepArrayComparison = true,
|
|
364
|
+
equalityFn = defaultEqualityFn,
|
|
365
|
+
preserveStructure = false
|
|
366
|
+
} = options;
|
|
367
|
+
if (oldObj == null && newObj == null) {
|
|
368
|
+
return {};
|
|
369
|
+
}
|
|
370
|
+
if (oldObj == null) {
|
|
371
|
+
return includeAdded ? { ...newObj } : {};
|
|
372
|
+
}
|
|
373
|
+
if (newObj == null) {
|
|
374
|
+
if (includeDeleted) {
|
|
375
|
+
const result2 = {};
|
|
376
|
+
for (const key in oldObj) {
|
|
377
|
+
result2[key] = void 0;
|
|
378
|
+
}
|
|
379
|
+
return result2;
|
|
380
|
+
}
|
|
381
|
+
return {};
|
|
382
|
+
}
|
|
383
|
+
if (!isObject2(oldObj) || !isObject2(newObj)) {
|
|
384
|
+
return equalityFn(oldObj, newObj) ? {} : newObj;
|
|
385
|
+
}
|
|
386
|
+
const result = {};
|
|
387
|
+
const processedKeys = /* @__PURE__ */ new Set();
|
|
388
|
+
for (const key in newObj) {
|
|
389
|
+
processedKeys.add(key);
|
|
390
|
+
const oldValue = oldObj[key];
|
|
391
|
+
const newValue = newObj[key];
|
|
392
|
+
if (!(key in oldObj)) {
|
|
393
|
+
if (includeAdded) {
|
|
394
|
+
result[key] = newValue;
|
|
395
|
+
}
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
if (Array.isArray(oldValue) && Array.isArray(newValue)) {
|
|
399
|
+
if (deepArrayComparison) {
|
|
400
|
+
if (!deepArrayEqual(oldValue, newValue)) {
|
|
401
|
+
result[key] = newValue;
|
|
402
|
+
} else if (preserveStructure) {
|
|
403
|
+
result[key] = void 0;
|
|
404
|
+
}
|
|
405
|
+
} else {
|
|
406
|
+
if (oldValue !== newValue) {
|
|
407
|
+
result[key] = newValue;
|
|
408
|
+
} else if (preserveStructure) {
|
|
409
|
+
result[key] = void 0;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
continue;
|
|
413
|
+
}
|
|
414
|
+
if (isObject2(oldValue) && isObject2(newValue)) {
|
|
415
|
+
const nestedDiff = diffObject(oldValue, newValue, options);
|
|
416
|
+
if (Object.keys(nestedDiff).length > 0) {
|
|
417
|
+
result[key] = nestedDiff;
|
|
418
|
+
} else if (preserveStructure) {
|
|
419
|
+
result[key] = void 0;
|
|
420
|
+
}
|
|
421
|
+
continue;
|
|
422
|
+
}
|
|
423
|
+
if (!equalityFn(oldValue, newValue)) {
|
|
424
|
+
result[key] = newValue;
|
|
425
|
+
} else if (preserveStructure) {
|
|
426
|
+
result[key] = void 0;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
if (includeDeleted) {
|
|
430
|
+
for (const key in oldObj) {
|
|
431
|
+
if (!processedKeys.has(key)) {
|
|
432
|
+
result[key] = void 0;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
return result;
|
|
437
|
+
};
|
|
438
|
+
var diffObjectDetailed = (oldObj, newObj, options = {}) => {
|
|
439
|
+
const added = [];
|
|
440
|
+
const deleted = [];
|
|
441
|
+
const modified = [];
|
|
442
|
+
const changes = {};
|
|
443
|
+
const {
|
|
444
|
+
includeDeleted = false,
|
|
445
|
+
includeAdded = true,
|
|
446
|
+
deepArrayComparison = true,
|
|
447
|
+
equalityFn = defaultEqualityFn
|
|
448
|
+
} = options;
|
|
449
|
+
if (Array.isArray(oldObj) && Array.isArray(newObj)) {
|
|
450
|
+
const maxLength = Math.max(oldObj.length, newObj.length);
|
|
451
|
+
const arrayChanges = [];
|
|
452
|
+
let hasArrayChanges = false;
|
|
453
|
+
for (let i = 0; i < maxLength; i++) {
|
|
454
|
+
const indexPath = `[${i}]`;
|
|
455
|
+
if (i >= oldObj.length) {
|
|
456
|
+
added.push(indexPath);
|
|
457
|
+
arrayChanges[i] = newObj[i];
|
|
458
|
+
hasArrayChanges = true;
|
|
459
|
+
} else if (i >= newObj.length) {
|
|
460
|
+
deleted.push(indexPath);
|
|
461
|
+
hasArrayChanges = true;
|
|
462
|
+
} else {
|
|
463
|
+
const oldElement = oldObj[i];
|
|
464
|
+
const newElement = newObj[i];
|
|
465
|
+
if (isObject2(oldElement) && isObject2(newElement)) {
|
|
466
|
+
const nestedDiff = diffObjectDetailed(
|
|
467
|
+
oldElement,
|
|
468
|
+
newElement,
|
|
469
|
+
options
|
|
470
|
+
);
|
|
471
|
+
if (nestedDiff.hasChanges) {
|
|
472
|
+
modified.push(indexPath);
|
|
473
|
+
arrayChanges[i] = newElement;
|
|
474
|
+
hasArrayChanges = true;
|
|
475
|
+
} else {
|
|
476
|
+
arrayChanges[i] = oldElement;
|
|
477
|
+
}
|
|
478
|
+
} else if (Array.isArray(oldElement) && Array.isArray(newElement)) {
|
|
479
|
+
const nestedDiff = diffObjectDetailed(
|
|
480
|
+
oldElement,
|
|
481
|
+
newElement,
|
|
482
|
+
options
|
|
483
|
+
);
|
|
484
|
+
if (nestedDiff.hasChanges) {
|
|
485
|
+
modified.push(indexPath);
|
|
486
|
+
arrayChanges[i] = newElement;
|
|
487
|
+
hasArrayChanges = true;
|
|
488
|
+
} else {
|
|
489
|
+
arrayChanges[i] = oldElement;
|
|
490
|
+
}
|
|
491
|
+
} else if (!equalityFn(oldElement, newElement)) {
|
|
492
|
+
modified.push(indexPath);
|
|
493
|
+
arrayChanges[i] = newElement;
|
|
494
|
+
hasArrayChanges = true;
|
|
495
|
+
} else {
|
|
496
|
+
arrayChanges[i] = oldElement;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
return {
|
|
501
|
+
changes: hasArrayChanges ? arrayChanges : {},
|
|
502
|
+
added,
|
|
503
|
+
deleted,
|
|
504
|
+
modified,
|
|
505
|
+
hasChanges: hasArrayChanges || deleted.length > 0
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
const collectDetailedPaths = (old, newVal, currentPath = "") => {
|
|
509
|
+
const result = {
|
|
510
|
+
added: [],
|
|
511
|
+
deleted: [],
|
|
512
|
+
modified: []
|
|
513
|
+
};
|
|
514
|
+
if (Array.isArray(old) && Array.isArray(newVal)) {
|
|
515
|
+
const maxLength = Math.max(old.length, newVal.length);
|
|
516
|
+
for (let i = 0; i < maxLength; i++) {
|
|
517
|
+
const indexPath = currentPath ? `${currentPath}[${i}]` : `[${i}]`;
|
|
518
|
+
if (i >= old.length) {
|
|
519
|
+
result.added.push(indexPath);
|
|
520
|
+
} else if (i >= newVal.length) {
|
|
521
|
+
result.deleted.push(indexPath);
|
|
522
|
+
} else {
|
|
523
|
+
const oldElement = old[i];
|
|
524
|
+
const newElement = newVal[i];
|
|
525
|
+
if (isObject2(oldElement) && isObject2(newElement)) {
|
|
526
|
+
const nested = collectDetailedPaths(
|
|
527
|
+
oldElement,
|
|
528
|
+
newElement,
|
|
529
|
+
indexPath
|
|
530
|
+
);
|
|
531
|
+
result.added.push(...nested.added);
|
|
532
|
+
result.deleted.push(...nested.deleted);
|
|
533
|
+
result.modified.push(...nested.modified);
|
|
534
|
+
} else if (Array.isArray(oldElement) && Array.isArray(newElement)) {
|
|
535
|
+
const nested = collectDetailedPaths(
|
|
536
|
+
oldElement,
|
|
537
|
+
newElement,
|
|
538
|
+
indexPath
|
|
539
|
+
);
|
|
540
|
+
result.added.push(...nested.added);
|
|
541
|
+
result.deleted.push(...nested.deleted);
|
|
542
|
+
result.modified.push(...nested.modified);
|
|
543
|
+
} else if (oldElement !== newElement) {
|
|
544
|
+
result.modified.push(indexPath);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
return result;
|
|
549
|
+
}
|
|
550
|
+
if (isObject2(old) && isObject2(newVal)) {
|
|
551
|
+
const allKeys = /* @__PURE__ */ new Set([
|
|
552
|
+
...Object.keys(old || {}),
|
|
553
|
+
...Object.keys(newVal || {})
|
|
554
|
+
]);
|
|
555
|
+
for (const key of allKeys) {
|
|
556
|
+
const oldValue = old?.[key];
|
|
557
|
+
const newValue = newVal?.[key];
|
|
558
|
+
const path = currentPath ? `${currentPath}.${key}` : key;
|
|
559
|
+
if (!(key in (old || {}))) {
|
|
560
|
+
result.added.push(path);
|
|
561
|
+
} else if (!(key in (newVal || {}))) {
|
|
562
|
+
result.deleted.push(path);
|
|
563
|
+
} else if (Array.isArray(oldValue) && Array.isArray(newValue)) {
|
|
564
|
+
if (!deepArrayEqual(oldValue, newValue)) {
|
|
565
|
+
const nested = collectDetailedPaths(oldValue, newValue, path);
|
|
566
|
+
result.added.push(...nested.added);
|
|
567
|
+
result.deleted.push(...nested.deleted);
|
|
568
|
+
result.modified.push(...nested.modified);
|
|
569
|
+
}
|
|
570
|
+
} else if (isObject2(oldValue) && isObject2(newValue)) {
|
|
571
|
+
const nested = collectDetailedPaths(oldValue, newValue, path);
|
|
572
|
+
result.added.push(...nested.added);
|
|
573
|
+
result.deleted.push(...nested.deleted);
|
|
574
|
+
result.modified.push(...nested.modified);
|
|
575
|
+
} else if (oldValue !== newValue) {
|
|
576
|
+
result.modified.push(path);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
return result;
|
|
581
|
+
};
|
|
582
|
+
if (oldObj == null && newObj == null) {
|
|
583
|
+
return { changes: {}, added, deleted, modified, hasChanges: false };
|
|
584
|
+
}
|
|
585
|
+
if (oldObj == null) {
|
|
586
|
+
if (includeAdded && newObj) {
|
|
587
|
+
for (const key in newObj) {
|
|
588
|
+
added.push(key);
|
|
589
|
+
changes[key] = newObj[key];
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
return {
|
|
593
|
+
changes,
|
|
594
|
+
added,
|
|
595
|
+
deleted,
|
|
596
|
+
modified,
|
|
597
|
+
hasChanges: added.length > 0
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
if (newObj == null) {
|
|
601
|
+
if (includeDeleted) {
|
|
602
|
+
for (const key in oldObj) {
|
|
603
|
+
deleted.push(key);
|
|
604
|
+
changes[key] = void 0;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
return {
|
|
608
|
+
changes,
|
|
609
|
+
added,
|
|
610
|
+
deleted,
|
|
611
|
+
modified,
|
|
612
|
+
hasChanges: deleted.length > 0
|
|
613
|
+
};
|
|
614
|
+
}
|
|
615
|
+
const processedKeys = /* @__PURE__ */ new Set();
|
|
616
|
+
for (const key in newObj) {
|
|
617
|
+
processedKeys.add(key);
|
|
618
|
+
const oldValue = oldObj[key];
|
|
619
|
+
const newValue = newObj[key];
|
|
620
|
+
if (!(key in oldObj)) {
|
|
621
|
+
if (includeAdded) {
|
|
622
|
+
added.push(key);
|
|
623
|
+
changes[key] = newValue;
|
|
624
|
+
}
|
|
625
|
+
continue;
|
|
626
|
+
}
|
|
627
|
+
let isDifferent = false;
|
|
628
|
+
if (Array.isArray(oldValue) && Array.isArray(newValue)) {
|
|
629
|
+
isDifferent = deepArrayComparison ? !deepArrayEqual(oldValue, newValue) : oldValue !== newValue;
|
|
630
|
+
if (isDifferent) {
|
|
631
|
+
const paths = collectDetailedPaths(oldValue, newValue, key);
|
|
632
|
+
added.push(...paths.added);
|
|
633
|
+
deleted.push(...paths.deleted);
|
|
634
|
+
modified.push(...paths.modified);
|
|
635
|
+
changes[key] = newValue;
|
|
636
|
+
}
|
|
637
|
+
} else if (isObject2(oldValue) && isObject2(newValue)) {
|
|
638
|
+
const nestedDiff = diffObject(oldValue, newValue, options);
|
|
639
|
+
isDifferent = Object.keys(nestedDiff).length > 0;
|
|
640
|
+
if (isDifferent) {
|
|
641
|
+
const paths = collectDetailedPaths(oldValue, newValue, key);
|
|
642
|
+
added.push(...paths.added);
|
|
643
|
+
deleted.push(...paths.deleted);
|
|
644
|
+
modified.push(...paths.modified);
|
|
645
|
+
changes[key] = nestedDiff;
|
|
646
|
+
}
|
|
647
|
+
} else {
|
|
648
|
+
isDifferent = !equalityFn(oldValue, newValue);
|
|
649
|
+
if (isDifferent) {
|
|
650
|
+
modified.push(key);
|
|
651
|
+
changes[key] = newValue;
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
for (const key in oldObj) {
|
|
656
|
+
if (!processedKeys.has(key)) {
|
|
657
|
+
deleted.push(key);
|
|
658
|
+
changes[key] = void 0;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
return {
|
|
662
|
+
changes,
|
|
663
|
+
added,
|
|
664
|
+
deleted,
|
|
665
|
+
modified,
|
|
666
|
+
hasChanges: added.length > 0 || deleted.length > 0 || modified.length > 0
|
|
667
|
+
};
|
|
668
|
+
};
|
|
669
|
+
function isApplyDiff(value) {
|
|
670
|
+
if (value && (value.diff || value.changes) && (value.modified || value.deleted || value.hasChanges)) {
|
|
671
|
+
return true;
|
|
672
|
+
}
|
|
673
|
+
return false;
|
|
674
|
+
}
|
|
675
|
+
var applyDiff = (baseObj, option, keepRefParam) => {
|
|
676
|
+
if (!isApplyDiff(option)) {
|
|
677
|
+
const diff2 = option;
|
|
678
|
+
const shouldKeepRef2 = keepRefParam || false;
|
|
679
|
+
if (Array.isArray(baseObj) && Array.isArray(diff2)) {
|
|
680
|
+
const result3 = shouldKeepRef2 ? baseObj : [...baseObj];
|
|
681
|
+
if (diff2.length < result3.length) {
|
|
682
|
+
result3.length = diff2.length;
|
|
683
|
+
}
|
|
684
|
+
for (let i = 0; i < diff2.length; i++) {
|
|
685
|
+
const diffValue = diff2[i];
|
|
686
|
+
if (diffValue === void 0) {
|
|
687
|
+
continue;
|
|
688
|
+
}
|
|
689
|
+
if (isObject2(result3[i]) && isObject2(diffValue)) {
|
|
690
|
+
result3[i] = applyDiff(result3[i], diffValue, shouldKeepRef2);
|
|
691
|
+
} else if (Array.isArray(result3[i]) && Array.isArray(diffValue)) {
|
|
692
|
+
result3[i] = applyDiff(
|
|
693
|
+
result3[i],
|
|
694
|
+
diffValue,
|
|
695
|
+
shouldKeepRef2
|
|
696
|
+
);
|
|
697
|
+
} else {
|
|
698
|
+
result3[i] = diffValue;
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
return result3;
|
|
702
|
+
}
|
|
703
|
+
if (!isObject2(baseObj)) return diff2;
|
|
704
|
+
if (!isObject2(diff2)) return baseObj;
|
|
705
|
+
const result2 = shouldKeepRef2 ? baseObj : { ...baseObj };
|
|
706
|
+
for (const key in diff2) {
|
|
707
|
+
const diffValue = diff2[key];
|
|
708
|
+
if (diffValue === void 0) {
|
|
709
|
+
delete result2[key];
|
|
710
|
+
} else if (Array.isArray(result2[key]) && Array.isArray(diffValue)) {
|
|
711
|
+
result2[key] = applyDiff(result2[key], diffValue, shouldKeepRef2);
|
|
712
|
+
} else if (isObject2(result2[key]) && isObject2(diffValue)) {
|
|
713
|
+
result2[key] = applyDiff(result2[key], diffValue, shouldKeepRef2);
|
|
714
|
+
} else {
|
|
715
|
+
result2[key] = diffValue;
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
return result2;
|
|
719
|
+
}
|
|
720
|
+
const {
|
|
721
|
+
diff,
|
|
722
|
+
added = [],
|
|
723
|
+
deleted = [],
|
|
724
|
+
modified = [],
|
|
725
|
+
changes,
|
|
726
|
+
keepRef: keepRefOption
|
|
727
|
+
} = option;
|
|
728
|
+
const shouldKeepRef = keepRefParam !== void 0 ? keepRefParam : keepRefOption;
|
|
729
|
+
const diffToApply = changes || diff;
|
|
730
|
+
if (!diffToApply) return baseObj;
|
|
731
|
+
const addedSet = new Set(added);
|
|
732
|
+
const deletedSet = new Set(deleted);
|
|
733
|
+
const modifiedSet = new Set(modified);
|
|
734
|
+
const shouldApplyPath = (path) => {
|
|
735
|
+
if (added.length === 0 && deleted.length === 0 && modified.length === 0) {
|
|
736
|
+
return true;
|
|
737
|
+
}
|
|
738
|
+
const pathParts = path.split(/[.\[\]]+/).filter(Boolean);
|
|
739
|
+
for (let i = 0; i <= pathParts.length; i++) {
|
|
740
|
+
const partialPath = pathParts.slice(0, i).join(".");
|
|
741
|
+
const bracketPath = pathParts.slice(0, i).map((p, idx) => /^\d+$/.test(p) ? `[${p}]` : idx > 0 ? `.${p}` : p).join("");
|
|
742
|
+
if (addedSet.has(partialPath) || addedSet.has(bracketPath) || deletedSet.has(partialPath) || deletedSet.has(bracketPath) || modifiedSet.has(partialPath) || modifiedSet.has(bracketPath)) {
|
|
743
|
+
return true;
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
return false;
|
|
747
|
+
};
|
|
748
|
+
if (Array.isArray(baseObj) && Array.isArray(diffToApply)) {
|
|
749
|
+
const result2 = shouldKeepRef ? baseObj : [...baseObj];
|
|
750
|
+
result2.length = diffToApply.length;
|
|
751
|
+
for (let i = 0; i < diffToApply.length; i++) {
|
|
752
|
+
const indexPath = `[${i}]`;
|
|
753
|
+
const diffValue = diffToApply[i];
|
|
754
|
+
if (diffValue === void 0) {
|
|
755
|
+
continue;
|
|
756
|
+
}
|
|
757
|
+
if (!shouldApplyPath(indexPath)) {
|
|
758
|
+
continue;
|
|
759
|
+
}
|
|
760
|
+
if (isObject2(result2[i]) && isObject2(diffValue)) {
|
|
761
|
+
result2[i] = applyDiff(result2[i], diffValue, shouldKeepRef);
|
|
762
|
+
} else if (Array.isArray(result2[i]) && Array.isArray(diffValue)) {
|
|
763
|
+
result2[i] = applyDiff(
|
|
764
|
+
result2[i],
|
|
765
|
+
diffValue,
|
|
766
|
+
shouldKeepRef
|
|
767
|
+
);
|
|
768
|
+
} else {
|
|
769
|
+
result2[i] = diffValue;
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
return result2;
|
|
773
|
+
}
|
|
774
|
+
if (!isObject2(baseObj)) return diffToApply;
|
|
775
|
+
if (!isObject2(diffToApply)) return baseObj;
|
|
776
|
+
const result = shouldKeepRef ? baseObj : { ...baseObj };
|
|
777
|
+
for (const path of deleted) {
|
|
778
|
+
if (path.includes(".") || path.includes("[")) continue;
|
|
779
|
+
if (shouldApplyPath(path)) {
|
|
780
|
+
delete result[path];
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
for (const key in diffToApply) {
|
|
784
|
+
const diffValue = diffToApply[key];
|
|
785
|
+
if (!shouldApplyPath(key)) {
|
|
786
|
+
continue;
|
|
787
|
+
}
|
|
788
|
+
if (diffValue === void 0) {
|
|
789
|
+
delete result[key];
|
|
790
|
+
} else if (Array.isArray(result[key]) && Array.isArray(diffValue)) {
|
|
791
|
+
result[key] = applyDiff(result[key], diffValue, shouldKeepRef);
|
|
792
|
+
} else if (isObject2(result[key]) && isObject2(diffValue)) {
|
|
793
|
+
result[key] = applyDiff(result[key], diffValue, shouldKeepRef);
|
|
794
|
+
} else {
|
|
795
|
+
result[key] = diffValue;
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
return result;
|
|
799
|
+
};
|
|
800
|
+
|
|
801
|
+
// src/lib/object/selector.ts
|
|
802
|
+
var parseSelector = (selector, options = {}) => {
|
|
803
|
+
const { type = "auto", throwOnError = false } = options;
|
|
804
|
+
if (!selector || typeof selector !== "string") {
|
|
805
|
+
return [];
|
|
806
|
+
}
|
|
807
|
+
const segments = [];
|
|
808
|
+
let current = "";
|
|
809
|
+
let inBrackets = false;
|
|
810
|
+
let inQuotes = false;
|
|
811
|
+
let quoteChar = "";
|
|
812
|
+
let bracketContent = "";
|
|
813
|
+
let bracketDepth = 0;
|
|
814
|
+
const detectType = (str) => {
|
|
815
|
+
if (str.includes("..")) return "descendant";
|
|
816
|
+
if (str.includes("[?(")) return "filter";
|
|
817
|
+
if (str.includes("[:") || str.includes("[0:")) return "slice";
|
|
818
|
+
if (str.includes("[,") || str.includes("[0,1]")) return "union";
|
|
819
|
+
if (str.includes("[*]") || str.includes(".*.")) return "wildcard";
|
|
820
|
+
if (str.includes("[]")) return "array-wildcard";
|
|
821
|
+
if (str.includes("[") && str.includes("]")) return "bracket";
|
|
822
|
+
return "dot";
|
|
823
|
+
};
|
|
824
|
+
const actualType = type === "auto" ? detectType(selector) : type;
|
|
825
|
+
const parseBracketContent = (content) => {
|
|
826
|
+
const trimmed = content.trim();
|
|
827
|
+
if (trimmed === "*" || trimmed === "") {
|
|
828
|
+
return {
|
|
829
|
+
type: "wildcard",
|
|
830
|
+
value: trimmed === "" ? "[]" : "*",
|
|
831
|
+
raw: trimmed === "" ? "[]" : content
|
|
832
|
+
};
|
|
833
|
+
}
|
|
834
|
+
if (trimmed === "[]") {
|
|
835
|
+
return { type: "wildcard", value: "[]", raw: content };
|
|
836
|
+
}
|
|
837
|
+
if (/^-?\d+$/.test(trimmed)) {
|
|
838
|
+
return { type: "index", value: parseInt(trimmed, 10), raw: content };
|
|
839
|
+
}
|
|
840
|
+
if (trimmed.includes(":")) {
|
|
841
|
+
const parts = trimmed.split(":").map((p) => p.trim());
|
|
842
|
+
const start = parts[0] === "" ? 0 : parseInt(parts[0], 10);
|
|
843
|
+
const end = parts[1] === "" ? Infinity : parseInt(parts[1], 10);
|
|
844
|
+
if (isNaN(start) || isNaN(end)) {
|
|
845
|
+
if (throwOnError) throw new Error(`Invalid slice notation: ${content}`);
|
|
846
|
+
return { type: "wildcard", value: "*", raw: content };
|
|
847
|
+
}
|
|
848
|
+
return { type: "slice", value: [start, end], raw: content };
|
|
849
|
+
}
|
|
850
|
+
if (trimmed.includes(",")) {
|
|
851
|
+
const indices = trimmed.split(",").map((p) => {
|
|
852
|
+
const num = parseInt(p.trim(), 10);
|
|
853
|
+
return isNaN(num) ? -1 : num;
|
|
854
|
+
}).filter((n) => n >= 0);
|
|
855
|
+
if (indices.length === 0) {
|
|
856
|
+
if (throwOnError) throw new Error(`Invalid union notation: ${content}`);
|
|
857
|
+
return { type: "wildcard", value: "*", raw: content };
|
|
858
|
+
}
|
|
859
|
+
return { type: "union", value: indices, raw: content };
|
|
860
|
+
}
|
|
861
|
+
if (trimmed.startsWith("?(") && trimmed.endsWith(")")) {
|
|
862
|
+
const expr = trimmed.slice(2, -1);
|
|
863
|
+
try {
|
|
864
|
+
const filterFn = createFilterFunction(expr);
|
|
865
|
+
return { type: "filter", value: filterFn, raw: content };
|
|
866
|
+
} catch (error) {
|
|
867
|
+
if (throwOnError) throw error;
|
|
868
|
+
return { type: "wildcard", value: "*", raw: content };
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
if (trimmed.startsWith('"') && trimmed.endsWith('"') || trimmed.startsWith("'") && trimmed.endsWith("'")) {
|
|
872
|
+
return { type: "property", value: trimmed.slice(1, -1), raw: content };
|
|
873
|
+
}
|
|
874
|
+
return { type: "property", value: trimmed, raw: content };
|
|
875
|
+
};
|
|
876
|
+
for (let i = 0; i < selector.length; i++) {
|
|
877
|
+
const char = selector[i];
|
|
878
|
+
const nextChar = selector[i + 1];
|
|
879
|
+
if ((char === '"' || char === "'") && !inQuotes) {
|
|
880
|
+
inQuotes = true;
|
|
881
|
+
quoteChar = char;
|
|
882
|
+
continue;
|
|
883
|
+
} else if (char === quoteChar && inQuotes) {
|
|
884
|
+
inQuotes = false;
|
|
885
|
+
quoteChar = "";
|
|
886
|
+
if (!(char === '"' || char === "'")) {
|
|
887
|
+
if (inBrackets) {
|
|
888
|
+
bracketContent += char;
|
|
889
|
+
} else {
|
|
890
|
+
current += char;
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
continue;
|
|
894
|
+
}
|
|
895
|
+
if (inQuotes) {
|
|
896
|
+
if (inBrackets) {
|
|
897
|
+
bracketContent += char;
|
|
898
|
+
} else {
|
|
899
|
+
current += char;
|
|
900
|
+
}
|
|
901
|
+
continue;
|
|
902
|
+
}
|
|
903
|
+
if (char === "[" && !inBrackets) {
|
|
904
|
+
if (current) {
|
|
905
|
+
segments.push({ type: "property", value: current, raw: current });
|
|
906
|
+
current = "";
|
|
907
|
+
}
|
|
908
|
+
inBrackets = true;
|
|
909
|
+
bracketContent = "";
|
|
910
|
+
continue;
|
|
911
|
+
}
|
|
912
|
+
if (char === "]" && inBrackets && !inQuotes) {
|
|
913
|
+
inBrackets = false;
|
|
914
|
+
const segment = parseBracketContent(bracketContent);
|
|
915
|
+
segments.push(segment);
|
|
916
|
+
continue;
|
|
917
|
+
}
|
|
918
|
+
let lastDot = segments.length > 0 ? segments[segments.length - 1] : null;
|
|
919
|
+
if (char === "." && !inBrackets && !inQuotes) {
|
|
920
|
+
if (nextChar === ".") {
|
|
921
|
+
const l = selector.split("..")[0];
|
|
922
|
+
segments.push({ type: "property", value: l, raw: l });
|
|
923
|
+
segments.push({ type: "descendant", value: "..", raw: ".." });
|
|
924
|
+
i++;
|
|
925
|
+
} else if (current) {
|
|
926
|
+
if (current === "*") {
|
|
927
|
+
segments.push({ type: "wildcard", value: current, raw: current });
|
|
928
|
+
} else {
|
|
929
|
+
segments.push({ type: "property", value: current, raw: current });
|
|
930
|
+
}
|
|
931
|
+
current = "";
|
|
932
|
+
}
|
|
933
|
+
continue;
|
|
934
|
+
}
|
|
935
|
+
if (inBrackets) {
|
|
936
|
+
bracketContent += char;
|
|
937
|
+
} else {
|
|
938
|
+
if (lastDot && lastDot.type !== "descendant") {
|
|
939
|
+
current += char;
|
|
940
|
+
} else {
|
|
941
|
+
const l = selector.split(lastDot?.raw)[1];
|
|
942
|
+
if (l !== void 0) {
|
|
943
|
+
current = l;
|
|
944
|
+
} else {
|
|
945
|
+
current += char;
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
if (current) {
|
|
951
|
+
segments.push({ type: "property", value: current, raw: current });
|
|
952
|
+
}
|
|
953
|
+
return segments.map((segment) => {
|
|
954
|
+
if (segment.type === "property" && typeof segment.value === "string") {
|
|
955
|
+
if (/^-?\d+$/.test(segment.value)) {
|
|
956
|
+
const num = parseInt(segment.value, 10);
|
|
957
|
+
if (!isNaN(num)) {
|
|
958
|
+
return { ...segment, type: "index", value: num };
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
return segment;
|
|
963
|
+
});
|
|
964
|
+
};
|
|
965
|
+
var createFilterFunction = (expr) => {
|
|
966
|
+
const safeExpr = expr.replace(/@\.([a-zA-Z_$][\w$]*)/g, (_, prop) => `item.${prop}`).replace(/@/g, "item");
|
|
967
|
+
try {
|
|
968
|
+
return new Function("item", `return ${safeExpr}`);
|
|
969
|
+
} catch (error) {
|
|
970
|
+
throw new Error(`Invalid filter expression: ${expr}`);
|
|
971
|
+
}
|
|
972
|
+
};
|
|
973
|
+
var stringifySelector = (segments) => {
|
|
974
|
+
const parts = [];
|
|
975
|
+
for (let i = 0; i < segments.length; i++) {
|
|
976
|
+
const segment = segments[i];
|
|
977
|
+
const prevSegment = i > 0 ? segments[i - 1] : null;
|
|
978
|
+
switch (segment.type) {
|
|
979
|
+
case "property":
|
|
980
|
+
const needsQuotes = /[\.\[\]\s]/.test(String(segment.value));
|
|
981
|
+
if (needsQuotes) {
|
|
982
|
+
parts.push(`["${segment.value}"]`);
|
|
983
|
+
} else {
|
|
984
|
+
if (prevSegment && prevSegment.type !== "descendant") {
|
|
985
|
+
parts.push(".");
|
|
986
|
+
}
|
|
987
|
+
parts.push(String(segment.value));
|
|
988
|
+
}
|
|
989
|
+
break;
|
|
990
|
+
case "index":
|
|
991
|
+
parts.push(`[${segment.value}]`);
|
|
992
|
+
break;
|
|
993
|
+
case "wildcard":
|
|
994
|
+
if (segment.value === "[]") {
|
|
995
|
+
parts.push("[]");
|
|
996
|
+
} else {
|
|
997
|
+
parts.push("[*]");
|
|
998
|
+
}
|
|
999
|
+
break;
|
|
1000
|
+
case "slice":
|
|
1001
|
+
const [start, end] = segment.value;
|
|
1002
|
+
parts.push(`[${start}:${end === Infinity ? "" : end}]`);
|
|
1003
|
+
break;
|
|
1004
|
+
case "union":
|
|
1005
|
+
const indices = segment.value;
|
|
1006
|
+
parts.push(`[${indices.join(",")}]`);
|
|
1007
|
+
break;
|
|
1008
|
+
case "filter":
|
|
1009
|
+
parts.push(`[?(${segment.raw?.slice(2, -1) || ""})]`);
|
|
1010
|
+
break;
|
|
1011
|
+
case "descendant":
|
|
1012
|
+
parts.push("..");
|
|
1013
|
+
break;
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
return parts.join("").replace(/\.\[/g, "[");
|
|
1017
|
+
};
|
|
1018
|
+
|
|
1019
|
+
// src/lib/object/merge.ts
|
|
1020
|
+
var mergeIntoStructure = (target, selector, value) => {
|
|
1021
|
+
const segments = parseSelector(selector);
|
|
1022
|
+
let current = target;
|
|
1023
|
+
for (let i = 0; i < segments.length; i++) {
|
|
1024
|
+
const segment = segments[i];
|
|
1025
|
+
const isLast = i === segments.length - 1;
|
|
1026
|
+
switch (segment.type) {
|
|
1027
|
+
case "property":
|
|
1028
|
+
if (isLast) {
|
|
1029
|
+
current[String(segment.value)] = value;
|
|
1030
|
+
} else {
|
|
1031
|
+
if (!current[String(segment.value)]) {
|
|
1032
|
+
const nextSegment = segments[i + 1];
|
|
1033
|
+
current[String(segment.value)] = nextSegment.type === "index" || nextSegment.type === "slice" || nextSegment.type === "union" || nextSegment.type === "wildcard" ? [] : {};
|
|
1034
|
+
}
|
|
1035
|
+
current = current[String(segment.value)];
|
|
1036
|
+
}
|
|
1037
|
+
break;
|
|
1038
|
+
case "index":
|
|
1039
|
+
if (!Array.isArray(current)) {
|
|
1040
|
+
current = [];
|
|
1041
|
+
}
|
|
1042
|
+
const index = segment.value;
|
|
1043
|
+
if (isLast) {
|
|
1044
|
+
if (index >= current.length) {
|
|
1045
|
+
for (let j = current.length; j <= index; j++) {
|
|
1046
|
+
current.push(void 0);
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
current[index] = value;
|
|
1050
|
+
} else {
|
|
1051
|
+
if (!current[index]) {
|
|
1052
|
+
const nextSegment = segments[i + 1];
|
|
1053
|
+
current[index] = nextSegment.type === "index" || nextSegment.type === "slice" || nextSegment.type === "union" || nextSegment.type === "wildcard" ? [] : {};
|
|
1054
|
+
}
|
|
1055
|
+
current = current[index];
|
|
1056
|
+
}
|
|
1057
|
+
break;
|
|
1058
|
+
case "wildcard":
|
|
1059
|
+
if (Array.isArray(current)) {
|
|
1060
|
+
if (isLast) {
|
|
1061
|
+
for (let j = 0; j < current.length; j++) {
|
|
1062
|
+
current[j] = value;
|
|
1063
|
+
}
|
|
1064
|
+
} else {
|
|
1065
|
+
const nextSegment = segments[i + 1];
|
|
1066
|
+
for (let j = 0; j < current.length; j++) {
|
|
1067
|
+
if (!current[j]) {
|
|
1068
|
+
current[j] = nextSegment.type === "index" || nextSegment.type === "slice" || nextSegment.type === "union" || nextSegment.type === "wildcard" ? [] : {};
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
if (current.length > 0) {
|
|
1072
|
+
current = current[0];
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
break;
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
};
|
|
1080
|
+
|
|
1081
|
+
// src/lib/object/getter-setter.ts
|
|
1082
|
+
function get(obj, selector, options = {}) {
|
|
1083
|
+
const {
|
|
1084
|
+
preserveStructure,
|
|
1085
|
+
multiple,
|
|
1086
|
+
throwOnError = false,
|
|
1087
|
+
defaultValue,
|
|
1088
|
+
maxDepth = 20
|
|
1089
|
+
} = options;
|
|
1090
|
+
if (!obj || typeof obj !== "object") {
|
|
1091
|
+
return defaultValue;
|
|
1092
|
+
}
|
|
1093
|
+
if (!selector) {
|
|
1094
|
+
return obj;
|
|
1095
|
+
}
|
|
1096
|
+
if (Array.isArray(selector)) {
|
|
1097
|
+
if (preserveStructure) {
|
|
1098
|
+
const result2 = {};
|
|
1099
|
+
for (const sel of selector) {
|
|
1100
|
+
const value = get(obj, sel, { ...options, preserveStructure: false });
|
|
1101
|
+
if (value !== void 0) {
|
|
1102
|
+
mergeIntoStructure(result2, sel, value);
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
return result2;
|
|
1106
|
+
} else {
|
|
1107
|
+
return selector.map(
|
|
1108
|
+
(sel) => get(obj, sel, { ...options, preserveStructure: false })
|
|
1109
|
+
);
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
const segments = parseSelector(selector, { ...options, throwOnError });
|
|
1113
|
+
if (segments.length === 0) {
|
|
1114
|
+
return obj;
|
|
1115
|
+
}
|
|
1116
|
+
if (typeof preserveStructure === "undefined" || preserveStructure === false) {
|
|
1117
|
+
const v = ValueOfKey(obj, selector);
|
|
1118
|
+
if (multiple === false && Array.isArray(v)) {
|
|
1119
|
+
return v[0];
|
|
1120
|
+
}
|
|
1121
|
+
if (v === void 0 && defaultValue !== void 0) {
|
|
1122
|
+
return defaultValue;
|
|
1123
|
+
}
|
|
1124
|
+
return v;
|
|
1125
|
+
}
|
|
1126
|
+
const result = traverseStructure(obj, segments, 0, maxDepth);
|
|
1127
|
+
if (result === void 0 && defaultValue !== void 0) {
|
|
1128
|
+
return defaultValue;
|
|
1129
|
+
}
|
|
1130
|
+
return result;
|
|
1131
|
+
}
|
|
1132
|
+
function set(_obj, first, second, third, fourth) {
|
|
1133
|
+
let obj = deepClone(_obj);
|
|
1134
|
+
const params = [];
|
|
1135
|
+
if (typeof first !== "undefined") params.push(first);
|
|
1136
|
+
if (typeof second !== "undefined") params.push(second);
|
|
1137
|
+
if (typeof third !== "undefined") params.push(third);
|
|
1138
|
+
if (typeof fourth !== "undefined") params.push(fourth);
|
|
1139
|
+
if (isPrimitive(obj)) {
|
|
1140
|
+
if (typeof first === "function") {
|
|
1141
|
+
return first(obj);
|
|
1142
|
+
}
|
|
1143
|
+
return first;
|
|
1144
|
+
}
|
|
1145
|
+
if (params.length === 1) {
|
|
1146
|
+
if (typeof params[0] === "function") {
|
|
1147
|
+
return updateWithCallBack(obj, params[0]);
|
|
1148
|
+
}
|
|
1149
|
+
if (Array.isArray(params[0])) {
|
|
1150
|
+
if (Array.isArray(obj)) {
|
|
1151
|
+
if (first.length === 0) {
|
|
1152
|
+
for (let i = 0; i < obj.length; i++) {
|
|
1153
|
+
obj.pop();
|
|
1154
|
+
}
|
|
1155
|
+
} else {
|
|
1156
|
+
const diff = diffObjectDetailed([...obj], [...params[0]]);
|
|
1157
|
+
if (diff.hasChanges) {
|
|
1158
|
+
applyDiff(obj, [...params[0]], true);
|
|
1159
|
+
}
|
|
1160
|
+
return obj;
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
} else if (typeof params[0] === "object" && !Array.isArray(params[0])) {
|
|
1164
|
+
if (typeof obj === "object" && !Array.isArray(obj)) {
|
|
1165
|
+
applyDiff(obj, params[0], true);
|
|
1166
|
+
}
|
|
1167
|
+
} else {
|
|
1168
|
+
obj = params[0];
|
|
1169
|
+
}
|
|
1170
|
+
return obj;
|
|
1171
|
+
}
|
|
1172
|
+
if (Array.isArray(obj)) {
|
|
1173
|
+
if (typeof first === "string" && (isArrayOfString(second) || typeof second === "string") && params.length === 3) {
|
|
1174
|
+
if (Array.isArray(second)) {
|
|
1175
|
+
for (let i = 0; i < second.length; i++) {
|
|
1176
|
+
setValueAtPath(obj, first, third, second[i]);
|
|
1177
|
+
}
|
|
1178
|
+
return obj;
|
|
1179
|
+
}
|
|
1180
|
+
return setValueAtPath(obj, first, third, second);
|
|
1181
|
+
}
|
|
1182
|
+
if (typeof first === "string" && typeof second === "function" && params.length === 2) {
|
|
1183
|
+
return setValueAtPath(obj, first, second);
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
1186
|
+
if ((typeof first === "string" || typeof first === "number") && (params.length === 2 || params.length === 3)) {
|
|
1187
|
+
if (isRangeObject(second)) {
|
|
1188
|
+
return setValueBetweenRange(
|
|
1189
|
+
obj,
|
|
1190
|
+
first,
|
|
1191
|
+
second,
|
|
1192
|
+
third
|
|
1193
|
+
);
|
|
1194
|
+
}
|
|
1195
|
+
if (typeof first === "number") {
|
|
1196
|
+
if (typeof third !== "undefined") {
|
|
1197
|
+
return setValueAtPath(obj, `[${first}].${second}`, third);
|
|
1198
|
+
} else {
|
|
1199
|
+
return setValueAtPath(obj, `[${first}]`, second, third);
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
if (typeof second === "number" && typeof third !== "undefined") {
|
|
1203
|
+
return setValueAtIndex(obj, first, second, third);
|
|
1204
|
+
}
|
|
1205
|
+
if (Array.isArray(second) && second.length > 0 && second.every((s) => typeof s === "number")) {
|
|
1206
|
+
return setValueWithIndices(obj, first, second, third);
|
|
1207
|
+
}
|
|
1208
|
+
if (typeof third !== "undefined" && typeof second === "function") {
|
|
1209
|
+
return setValueWithFilter(obj, first, second, third);
|
|
1210
|
+
}
|
|
1211
|
+
return setValueAtPath(obj, first, second, third);
|
|
1212
|
+
}
|
|
1213
|
+
if (typeof first === "string" && typeof second === "number") {
|
|
1214
|
+
if (params.length === 3) {
|
|
1215
|
+
return setValueAtIndex(obj, first, second, third);
|
|
1216
|
+
} else if (params.length === 4) {
|
|
1217
|
+
return setIndexWithSubPath(
|
|
1218
|
+
obj,
|
|
1219
|
+
first,
|
|
1220
|
+
second,
|
|
1221
|
+
third,
|
|
1222
|
+
fourth
|
|
1223
|
+
);
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
if (typeof first === "string" && Array.isArray(second)) {
|
|
1227
|
+
if (params.length === 3) {
|
|
1228
|
+
return setValueWithIndices(obj, first, second, third);
|
|
1229
|
+
} else if (params.length === 4) {
|
|
1230
|
+
return setValueWithIndicesWithSubPath(
|
|
1231
|
+
obj,
|
|
1232
|
+
first,
|
|
1233
|
+
second,
|
|
1234
|
+
third,
|
|
1235
|
+
fourth
|
|
1236
|
+
);
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
if (typeof first === "string" && typeof second === "object" && !Array.isArray(second) && second !== null) {
|
|
1240
|
+
const range = second;
|
|
1241
|
+
if ("from" in range || "to" in range) {
|
|
1242
|
+
if (params.length === 3) {
|
|
1243
|
+
return setValueBetweenRange(obj, first, range, third);
|
|
1244
|
+
} else if (params.length === 4) {
|
|
1245
|
+
return setValueBetweenRangeWithSubPath(
|
|
1246
|
+
obj,
|
|
1247
|
+
first,
|
|
1248
|
+
range,
|
|
1249
|
+
third,
|
|
1250
|
+
fourth
|
|
1251
|
+
);
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
if (typeof first === "object" && !Array.isArray(first) && params.length === 1) {
|
|
1256
|
+
return applyDiff(obj, first, true);
|
|
1257
|
+
}
|
|
1258
|
+
if (typeof first === "string" && typeof second === "object" && !Array.isArray(second) && params.length === 2) {
|
|
1259
|
+
return mergeUpdateAtPath(obj, first, second);
|
|
1260
|
+
}
|
|
1261
|
+
if (typeof first === "string" && typeof second === "function") {
|
|
1262
|
+
if (params.length === 3) {
|
|
1263
|
+
return setValueWithFilter(
|
|
1264
|
+
obj,
|
|
1265
|
+
first,
|
|
1266
|
+
second,
|
|
1267
|
+
third
|
|
1268
|
+
);
|
|
1269
|
+
} else if (params.length === 4) {
|
|
1270
|
+
return setValueWithFilterWithSubPath(
|
|
1271
|
+
obj,
|
|
1272
|
+
first,
|
|
1273
|
+
second,
|
|
1274
|
+
third,
|
|
1275
|
+
fourth
|
|
1276
|
+
);
|
|
1277
|
+
}
|
|
1278
|
+
}
|
|
1279
|
+
if (typeof first === "function" && typeof second === "string") {
|
|
1280
|
+
if (params.length === 3) {
|
|
1281
|
+
return setArrayValueWithFilter(
|
|
1282
|
+
obj,
|
|
1283
|
+
first,
|
|
1284
|
+
second,
|
|
1285
|
+
third
|
|
1286
|
+
);
|
|
1287
|
+
} else if (params.length === 4) {
|
|
1288
|
+
return setValueWithFilterWithSubPath(
|
|
1289
|
+
obj,
|
|
1290
|
+
second,
|
|
1291
|
+
first,
|
|
1292
|
+
third,
|
|
1293
|
+
fourth
|
|
1294
|
+
);
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
if (Array.isArray(first)) {
|
|
1298
|
+
if (params.length === 2 && typeof second === "object" && !Array.isArray(second)) {
|
|
1299
|
+
return setValueWithArraySelectors(obj, first, second);
|
|
1300
|
+
} else if (params.length === 2) {
|
|
1301
|
+
return setValueWithArraySelectors(obj, first, second);
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1304
|
+
throw new Error(`Invalid set parameters: ${JSON.stringify(params)}`);
|
|
1305
|
+
}
|
|
1306
|
+
function handleFunctionParams(selector, data, value, isArray2 = false) {
|
|
1307
|
+
const paths = parseSelector(selector);
|
|
1308
|
+
if (paths.length === 0) return value;
|
|
1309
|
+
const isWildcard = paths[paths.length - 1].type === "wildcard";
|
|
1310
|
+
const containWildcard = paths.some((path) => path.type === "wildcard") && Array.isArray(data);
|
|
1311
|
+
let finalValue;
|
|
1312
|
+
if (isWildcard || containWildcard && isArray2 || containWildcard && paths.length === 2) {
|
|
1313
|
+
if (Array.isArray(data)) {
|
|
1314
|
+
for (let i = 0; i < data.length; i++) {
|
|
1315
|
+
const v = typeof value === "function" ? value(data[i]) : typeof data[i] === "object" ? applyDiff(data[i], value, true) : value;
|
|
1316
|
+
if (!finalValue) finalValue = [];
|
|
1317
|
+
finalValue.push(v);
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
if (!isWildcard && !finalValue) {
|
|
1322
|
+
finalValue = typeof value === "function" ? value(data) : value;
|
|
1323
|
+
}
|
|
1324
|
+
return finalValue;
|
|
1325
|
+
}
|
|
1326
|
+
function isRangeObject(obj) {
|
|
1327
|
+
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;
|
|
1328
|
+
}
|
|
1329
|
+
function isArrayOfString(value) {
|
|
1330
|
+
return Array.isArray(value) && value.every((item) => typeof item === "string");
|
|
1331
|
+
}
|
|
1332
|
+
function setValueAtPath(obj, selector, value, subPath) {
|
|
1333
|
+
const paths = parseSelector(selector);
|
|
1334
|
+
let target = ValueOfKey(obj, selector, {
|
|
1335
|
+
paths,
|
|
1336
|
+
createMissing: true
|
|
1337
|
+
});
|
|
1338
|
+
let get_value;
|
|
1339
|
+
if (subPath) {
|
|
1340
|
+
if (typeof value === "function") {
|
|
1341
|
+
get_value = get(target, subPath);
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
1344
|
+
const data = subPath ? get_value : target;
|
|
1345
|
+
const finalValue = handleFunctionParams(
|
|
1346
|
+
selector,
|
|
1347
|
+
data,
|
|
1348
|
+
value,
|
|
1349
|
+
Array.isArray(obj)
|
|
1350
|
+
);
|
|
1351
|
+
if (subPath) {
|
|
1352
|
+
const result = setAtPath(obj, `${selector}.${subPath}`, finalValue, {
|
|
1353
|
+
createMissing: true
|
|
1354
|
+
});
|
|
1355
|
+
return result;
|
|
1356
|
+
} else {
|
|
1357
|
+
const r = setAtPath(obj, selector, finalValue, { createMissing: true });
|
|
1358
|
+
return r;
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1361
|
+
function setValueAtIndex(obj, selector, index, value) {
|
|
1362
|
+
const target = ValueOfKey(obj, selector, { createMissing: true });
|
|
1363
|
+
if (!Array.isArray(target)) {
|
|
1364
|
+
throw new Error(`Target at ${selector} is not an array ${index}`);
|
|
1365
|
+
}
|
|
1366
|
+
const target_copy = structuredClone(target);
|
|
1367
|
+
const finalValue = handleFunctionParams(selector, target_copy[index], value);
|
|
1368
|
+
if (index < 0) {
|
|
1369
|
+
const actualIndex = target.length + index;
|
|
1370
|
+
if (actualIndex < 0) {
|
|
1371
|
+
throw new Error(`Index ${index} is out of bounds`);
|
|
1372
|
+
}
|
|
1373
|
+
return setAtPath(obj, `${selector}[${actualIndex}]`, finalValue, {
|
|
1374
|
+
createMissing: true
|
|
1375
|
+
});
|
|
1376
|
+
}
|
|
1377
|
+
return setAtPath(obj, `${selector}[${index}]`, finalValue, {
|
|
1378
|
+
createMissing: true
|
|
1379
|
+
});
|
|
1380
|
+
}
|
|
1381
|
+
var setIndexWithSubPath = (obj, selector, index, subPath, value) => {
|
|
1382
|
+
if (typeof subPath === "function") {
|
|
1383
|
+
return setValueAtPath(obj, `${selector}[${index}]`, subPath);
|
|
1384
|
+
}
|
|
1385
|
+
if (typeof subPath === "string") {
|
|
1386
|
+
return setValueAtPath(obj, `${selector}[${index}]`, value, subPath);
|
|
1387
|
+
}
|
|
1388
|
+
return null;
|
|
1389
|
+
};
|
|
1390
|
+
var setValueWithIndices = (obj, selector, indices, value) => {
|
|
1391
|
+
const target = ValueOfKey(obj, selector);
|
|
1392
|
+
if (!Array.isArray(target)) {
|
|
1393
|
+
throw new Error(`Target at ${selector} is not an array`);
|
|
1394
|
+
}
|
|
1395
|
+
for (const index of indices) {
|
|
1396
|
+
if (typeof index === "number") {
|
|
1397
|
+
setValueAtIndex(obj, selector, index, value);
|
|
1398
|
+
} else if (typeof index === "string") {
|
|
1399
|
+
setValueAtPath(obj, selector, value, index);
|
|
1400
|
+
}
|
|
1401
|
+
}
|
|
1402
|
+
return obj;
|
|
1403
|
+
};
|
|
1404
|
+
var setValueWithIndicesWithSubPath = (obj, selector, indices, subPath, value) => {
|
|
1405
|
+
const updater = (prev) => {
|
|
1406
|
+
const newValue = typeof value === "function" ? value(prev ? ValueOfKey(prev, subPath) : void 0) : value;
|
|
1407
|
+
const result = prev ? { ...prev } : {};
|
|
1408
|
+
setAtPath(result, subPath, newValue);
|
|
1409
|
+
return result;
|
|
1410
|
+
};
|
|
1411
|
+
return setValueWithIndices(obj, selector, indices, updater);
|
|
1412
|
+
};
|
|
1413
|
+
function setValueBetweenRange(obj, selector, range, value) {
|
|
1414
|
+
const target = ValueOfKey(obj, selector);
|
|
1415
|
+
if (!Array.isArray(target)) {
|
|
1416
|
+
throw new Error(`Target at ${selector} is not an array`);
|
|
1417
|
+
}
|
|
1418
|
+
const from = range.from ?? 0;
|
|
1419
|
+
const to = range.to ?? target.length - 1;
|
|
1420
|
+
const by = range.by ?? 1;
|
|
1421
|
+
const indices = [];
|
|
1422
|
+
for (let i = from; i <= to; i += by) {
|
|
1423
|
+
if (i >= 0 && i < target.length) {
|
|
1424
|
+
indices.push(i);
|
|
1425
|
+
}
|
|
1426
|
+
}
|
|
1427
|
+
return setValueWithIndices(obj, selector, indices, value);
|
|
1428
|
+
}
|
|
1429
|
+
var setValueBetweenRangeWithSubPath = (obj, selector, range, subPath, value) => {
|
|
1430
|
+
const updater = (prev) => {
|
|
1431
|
+
const newValue = typeof value === "function" ? value(prev ? ValueOfKey(prev, subPath) : void 0) : value;
|
|
1432
|
+
const result = prev ? { ...prev } : {};
|
|
1433
|
+
setAtPath(result, subPath, newValue);
|
|
1434
|
+
return result;
|
|
1435
|
+
};
|
|
1436
|
+
return setValueBetweenRange(obj, selector, range, updater);
|
|
1437
|
+
};
|
|
1438
|
+
function castProps(arr, props) {
|
|
1439
|
+
return Object.assign(arr, props);
|
|
1440
|
+
}
|
|
1441
|
+
function setValueWithFilter(obj, selector, filterFn, value) {
|
|
1442
|
+
const target = ValueOfKey(obj, selector);
|
|
1443
|
+
if (!Array.isArray(target)) {
|
|
1444
|
+
throw new Error(`Target at ${selector} is not an array`);
|
|
1445
|
+
}
|
|
1446
|
+
const indices = [];
|
|
1447
|
+
const target_copy = [...target];
|
|
1448
|
+
target.forEach((item, index) => {
|
|
1449
|
+
if (filterFn(item, index)) {
|
|
1450
|
+
indices.push(index);
|
|
1451
|
+
}
|
|
1452
|
+
});
|
|
1453
|
+
return setValueWithIndices(obj, selector, indices, value);
|
|
1454
|
+
}
|
|
1455
|
+
function setArrayValueWithFilter(obj, filterFn, selector, value) {
|
|
1456
|
+
let target = obj;
|
|
1457
|
+
if (!Array.isArray(target)) {
|
|
1458
|
+
throw new Error(`Target at ${selector} is not an array`);
|
|
1459
|
+
}
|
|
1460
|
+
const indices = [];
|
|
1461
|
+
const target_copy = [...target];
|
|
1462
|
+
target.forEach((item, index) => {
|
|
1463
|
+
const n = setAtPath(item, selector, value, { createMissing: true });
|
|
1464
|
+
item = n;
|
|
1465
|
+
});
|
|
1466
|
+
return target;
|
|
1467
|
+
}
|
|
1468
|
+
function setValueWithFilterWithSubPath(obj, selector, filterFn, subPath, value) {
|
|
1469
|
+
const updater = (prev) => {
|
|
1470
|
+
const newValue = typeof value === "function" ? value(prev ? ValueOfKey(prev, subPath) : void 0) : value;
|
|
1471
|
+
const result = prev ? { ...prev } : {};
|
|
1472
|
+
setAtPath(result, subPath, newValue);
|
|
1473
|
+
return result;
|
|
1474
|
+
};
|
|
1475
|
+
return setValueWithFilter(obj, selector, filterFn, updater);
|
|
1476
|
+
}
|
|
1477
|
+
function mergeUpdateAtPath(obj, selector, updates) {
|
|
1478
|
+
const current = ValueOfKey(obj, selector, { createMissing: true });
|
|
1479
|
+
const merged = applyDiff(current || {}, updates, true);
|
|
1480
|
+
return setAtPath(obj, selector, merged, { createMissing: true });
|
|
1481
|
+
}
|
|
1482
|
+
function setValueWithArraySelectors(obj, selectors, value) {
|
|
1483
|
+
if (typeof value === "object" && !Array.isArray(value) && value !== null) {
|
|
1484
|
+
for (const [path, val] of Object.entries(value)) {
|
|
1485
|
+
if (selectors.includes(path)) {
|
|
1486
|
+
setAtPath(obj, path, val, { createMissing: true });
|
|
1487
|
+
}
|
|
1488
|
+
}
|
|
1489
|
+
} else {
|
|
1490
|
+
for (const selector of selectors) {
|
|
1491
|
+
const finalValue = handleFunctionParams(
|
|
1492
|
+
selector,
|
|
1493
|
+
ValueOfKey(obj, selector),
|
|
1494
|
+
value
|
|
1495
|
+
);
|
|
1496
|
+
setAtPath(obj, selector, finalValue, { createMissing: true });
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
return obj;
|
|
1500
|
+
}
|
|
1501
|
+
function ValueOfKey(obj, selector, options = {}) {
|
|
1502
|
+
const { createMissing = false, add = false, debug = false } = options;
|
|
1503
|
+
const segments = parseSelector(selector);
|
|
1504
|
+
let current = obj;
|
|
1505
|
+
let updateEvenIfNotlast = false;
|
|
1506
|
+
let chained_property = "";
|
|
1507
|
+
const stringify = selector;
|
|
1508
|
+
if (typeof current === "object" && !Array.isArray(current) && stringify.length > 0) {
|
|
1509
|
+
if (current[stringify]) {
|
|
1510
|
+
current = current[stringify];
|
|
1511
|
+
return current;
|
|
1512
|
+
}
|
|
1513
|
+
}
|
|
1514
|
+
for (let i = 0; i < segments.length; i++) {
|
|
1515
|
+
const segment = segments[i];
|
|
1516
|
+
const isLast = segments.length > 1 ? i === segments.length - 1 : false;
|
|
1517
|
+
if (current === null && !createMissing) {
|
|
1518
|
+
return void 0;
|
|
1519
|
+
}
|
|
1520
|
+
const nextSegment = segments[i + 1];
|
|
1521
|
+
switch (segment.type) {
|
|
1522
|
+
case "property":
|
|
1523
|
+
if (current && typeof current === "object") {
|
|
1524
|
+
if (Array.isArray(current)) {
|
|
1525
|
+
if (typeof segment.value === "number") {
|
|
1526
|
+
const index = segment.value;
|
|
1527
|
+
if (createMissing && index >= current.length) {
|
|
1528
|
+
for (let j = current.length; j <= index; j++) {
|
|
1529
|
+
current.push(void 0);
|
|
1530
|
+
}
|
|
1531
|
+
}
|
|
1532
|
+
if (createMissing && (current[index] === null || current[index] === void 0) && (!isLast || updateEvenIfNotlast)) {
|
|
1533
|
+
if (nextSegment) {
|
|
1534
|
+
current[index] = nextSegment.type === "index" || nextSegment.type === "slice" || nextSegment.type === "union" || nextSegment.type === "wildcard" ? [] : {};
|
|
1535
|
+
} else {
|
|
1536
|
+
current[index] = null;
|
|
1537
|
+
}
|
|
1538
|
+
updateEvenIfNotlast = false;
|
|
1539
|
+
}
|
|
1540
|
+
current = current[index];
|
|
1541
|
+
if (nextSegment) {
|
|
1542
|
+
updateEvenIfNotlast = true;
|
|
1543
|
+
continue;
|
|
1544
|
+
}
|
|
1545
|
+
} else if (add) {
|
|
1546
|
+
current.push({
|
|
1547
|
+
[String(segment.value)]: null
|
|
1548
|
+
});
|
|
1549
|
+
} else {
|
|
1550
|
+
current = current.map((item) => item[String(segment.value)]);
|
|
1551
|
+
}
|
|
1552
|
+
if (nextSegment) {
|
|
1553
|
+
continue;
|
|
1554
|
+
}
|
|
1555
|
+
return filterDefined(current);
|
|
1556
|
+
} else {
|
|
1557
|
+
if (current[chained_property] && chained_property.split(".").length >= 2) {
|
|
1558
|
+
current = current[chained_property];
|
|
1559
|
+
continue;
|
|
1560
|
+
}
|
|
1561
|
+
if (createMissing && (current[String(segment.value)] === null || current[String(segment.value)] === void 0) && (!isLast || updateEvenIfNotlast)) {
|
|
1562
|
+
if (nextSegment) {
|
|
1563
|
+
current[String(segment.value)] = nextSegment.type === "index" || nextSegment.type === "slice" || nextSegment.type === "union" || nextSegment.type === "wildcard" ? [] : {};
|
|
1564
|
+
} else {
|
|
1565
|
+
current[String(segment.value)] = null;
|
|
1566
|
+
}
|
|
1567
|
+
updateEvenIfNotlast = false;
|
|
1568
|
+
current = current[String(segment.value)];
|
|
1569
|
+
if (nextSegment) {
|
|
1570
|
+
updateEvenIfNotlast = true;
|
|
1571
|
+
continue;
|
|
1572
|
+
}
|
|
1573
|
+
}
|
|
1574
|
+
if (!current) {
|
|
1575
|
+
current = { [String(segment.value)]: null };
|
|
1576
|
+
} else {
|
|
1577
|
+
if (!current[String(segment.value)] && createMissing) {
|
|
1578
|
+
current[String(segment.value)] = null;
|
|
1579
|
+
}
|
|
1580
|
+
current = current[String(segment.value)];
|
|
1581
|
+
}
|
|
1582
|
+
if (nextSegment) {
|
|
1583
|
+
continue;
|
|
1584
|
+
}
|
|
1585
|
+
}
|
|
1586
|
+
} else if (createMissing) {
|
|
1587
|
+
current = {};
|
|
1588
|
+
current[String(segment.value)] = nextSegment.type === "index" || nextSegment.type === "slice" || nextSegment.type === "union" || nextSegment.type === "wildcard" || options.type === "array" ? [] : {};
|
|
1589
|
+
current = current[String(segment.value)];
|
|
1590
|
+
} else {
|
|
1591
|
+
return void 0;
|
|
1592
|
+
}
|
|
1593
|
+
break;
|
|
1594
|
+
case "index":
|
|
1595
|
+
case "wildcard":
|
|
1596
|
+
if (Array.isArray(current)) {
|
|
1597
|
+
if (segment.type === "index") {
|
|
1598
|
+
const index = segment.value;
|
|
1599
|
+
if (index < 0) {
|
|
1600
|
+
current = current[current.length - Math.abs(index)];
|
|
1601
|
+
continue;
|
|
1602
|
+
}
|
|
1603
|
+
if (createMissing && (index >= current.length || current[index] == null || current[index] == void 0) && !isLast) {
|
|
1604
|
+
for (let j = current.length; j <= index; j++) {
|
|
1605
|
+
current.push(void 0);
|
|
1606
|
+
}
|
|
1607
|
+
if (nextSegment) {
|
|
1608
|
+
current[index] = nextSegment.type === "index" || nextSegment.type === "slice" || nextSegment.type === "union" || nextSegment.type === "wildcard" ? [] : {};
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
current = current[index];
|
|
1612
|
+
} else {
|
|
1613
|
+
if (!isLast) {
|
|
1614
|
+
continue;
|
|
1615
|
+
}
|
|
1616
|
+
}
|
|
1617
|
+
} else if (createMissing) {
|
|
1618
|
+
const nextSegment2 = segments[i + 1];
|
|
1619
|
+
current = [];
|
|
1620
|
+
const index = segment.value;
|
|
1621
|
+
for (let j = 0; j <= index; j++) {
|
|
1622
|
+
current.push(void 0);
|
|
1623
|
+
}
|
|
1624
|
+
current[index] = nextSegment2.type === "index" || nextSegment2.type === "slice" || nextSegment2.type === "union" || nextSegment2.type === "wildcard" || options.type === "array" ? [] : {};
|
|
1625
|
+
current = current[index];
|
|
1626
|
+
} else {
|
|
1627
|
+
if (segment.type === "index" && typeof current === "object") {
|
|
1628
|
+
if (current && current[String(segment.value)] !== void 0) {
|
|
1629
|
+
current = current[String(segment.value)];
|
|
1630
|
+
continue;
|
|
1631
|
+
}
|
|
1632
|
+
}
|
|
1633
|
+
if (typeof current === "object") {
|
|
1634
|
+
const values = Object.values(current);
|
|
1635
|
+
current = values;
|
|
1636
|
+
continue;
|
|
1637
|
+
}
|
|
1638
|
+
return void 0;
|
|
1639
|
+
}
|
|
1640
|
+
break;
|
|
1641
|
+
default:
|
|
1642
|
+
return void 0;
|
|
1643
|
+
}
|
|
1644
|
+
}
|
|
1645
|
+
return filterDefined(current);
|
|
1646
|
+
}
|
|
1647
|
+
function filterDefined(obj) {
|
|
1648
|
+
if (Array.isArray(obj)) {
|
|
1649
|
+
return obj.filter((item) => typeof item !== "undefined");
|
|
1650
|
+
}
|
|
1651
|
+
return obj;
|
|
1652
|
+
}
|
|
1653
|
+
var setAtPath = (obj, selector, value, options = {}) => {
|
|
1654
|
+
if (selector === "[*]") {
|
|
1655
|
+
return applyDiff(obj, value, true);
|
|
1656
|
+
}
|
|
1657
|
+
const { createMissing = true } = options;
|
|
1658
|
+
const segments = parseSelector(selector);
|
|
1659
|
+
if (segments.length === 0) {
|
|
1660
|
+
return obj;
|
|
1661
|
+
}
|
|
1662
|
+
const lastSegment = segments[segments.length - 1];
|
|
1663
|
+
let current = obj;
|
|
1664
|
+
for (let i = 0; i < segments.length - 1; i++) {
|
|
1665
|
+
const segment = segments[i];
|
|
1666
|
+
if (current == null) {
|
|
1667
|
+
if (!createMissing) return obj;
|
|
1668
|
+
throw new Error(`Cannot navigate through null/undefined at segment ${i}`);
|
|
1669
|
+
}
|
|
1670
|
+
const nextSegment = segments[i + 1];
|
|
1671
|
+
const shouldBeArray = nextSegment.type === "index" || nextSegment.type === "slice" || nextSegment.type === "union" || nextSegment.type === "wildcard";
|
|
1672
|
+
if (segment.type === "property") {
|
|
1673
|
+
if (Array.isArray(current) && typeof typeof segment.value === "number") {
|
|
1674
|
+
const index = segment.value;
|
|
1675
|
+
if (index < 0) {
|
|
1676
|
+
throw new Error(`Negative index ${index} not allowed`);
|
|
1677
|
+
}
|
|
1678
|
+
if (index >= current.length && createMissing) {
|
|
1679
|
+
for (let j = current.length; j <= index; j++) {
|
|
1680
|
+
current.push(void 0);
|
|
1681
|
+
}
|
|
1682
|
+
}
|
|
1683
|
+
if ((current[index] === null || current[index] === void 0) && createMissing) {
|
|
1684
|
+
current[index] = shouldBeArray ? [] : {};
|
|
1685
|
+
}
|
|
1686
|
+
current = current[index];
|
|
1687
|
+
} else {
|
|
1688
|
+
if ((current[String(segment.value)] === null || current[String(segment.value)] === void 0) && createMissing) {
|
|
1689
|
+
current[String(segment.value)] = shouldBeArray ? [] : {};
|
|
1690
|
+
}
|
|
1691
|
+
if (typeof current === "object" && !Array.isArray(current)) {
|
|
1692
|
+
current = current[String(segment.value)];
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1695
|
+
if (nextSegment) {
|
|
1696
|
+
continue;
|
|
1697
|
+
}
|
|
1698
|
+
break;
|
|
1699
|
+
} else if (segment.type === "index" || segment.type === "wildcard") {
|
|
1700
|
+
if (!Array.isArray(current)) {
|
|
1701
|
+
if (!createMissing) return obj;
|
|
1702
|
+
current = [];
|
|
1703
|
+
}
|
|
1704
|
+
if (segment.type === "index") {
|
|
1705
|
+
const index = segment.value;
|
|
1706
|
+
if (index < 0) {
|
|
1707
|
+
throw new Error(`Negative index ${index} not allowed`);
|
|
1708
|
+
}
|
|
1709
|
+
if (index >= current.length && createMissing) {
|
|
1710
|
+
for (let j = current.length; j <= index; j++) {
|
|
1711
|
+
current.push(void 0);
|
|
1712
|
+
}
|
|
1713
|
+
}
|
|
1714
|
+
if ((current[index] === null || current[index] === void 0) && createMissing) {
|
|
1715
|
+
current[index] = shouldBeArray ? [] : void 0;
|
|
1716
|
+
}
|
|
1717
|
+
current = current[index];
|
|
1718
|
+
}
|
|
1719
|
+
if (nextSegment) {
|
|
1720
|
+
continue;
|
|
1721
|
+
}
|
|
1722
|
+
break;
|
|
1723
|
+
} else {
|
|
1724
|
+
throw new Error(`Invalid segment type for set: ${segment.type}`);
|
|
1725
|
+
}
|
|
1726
|
+
}
|
|
1727
|
+
switch (lastSegment.type) {
|
|
1728
|
+
case "property":
|
|
1729
|
+
if (Array.isArray(current) && typeof lastSegment.value === "number") {
|
|
1730
|
+
const index2 = lastSegment.value;
|
|
1731
|
+
if (index2 < 0) {
|
|
1732
|
+
throw new Error(`Negative index ${index2} not allowed`);
|
|
1733
|
+
}
|
|
1734
|
+
if (index2 >= current.length) {
|
|
1735
|
+
for (let j = current.length; j <= index2; j++) {
|
|
1736
|
+
current.push(void 0);
|
|
1737
|
+
}
|
|
1738
|
+
}
|
|
1739
|
+
if (Array.isArray(value)) {
|
|
1740
|
+
current[index2] = applyDiff(
|
|
1741
|
+
structuredClone(current[index2]),
|
|
1742
|
+
value[index2],
|
|
1743
|
+
true
|
|
1744
|
+
);
|
|
1745
|
+
} else {
|
|
1746
|
+
current[index2] = value;
|
|
1747
|
+
}
|
|
1748
|
+
} else {
|
|
1749
|
+
if (!current) current = {};
|
|
1750
|
+
if (Array.isArray(current)) {
|
|
1751
|
+
const paths = selector.split(".");
|
|
1752
|
+
let ob;
|
|
1753
|
+
if (paths.length >= 2) {
|
|
1754
|
+
paths.shift();
|
|
1755
|
+
for (let i = 0; i < current.length; i++) {
|
|
1756
|
+
if (Array.isArray(value)) {
|
|
1757
|
+
ob = fromPath(paths.join("."), value[i]);
|
|
1758
|
+
current[i] = typeof current[i] === "object" ? applyDiff(current[i], ob, true) : ob;
|
|
1759
|
+
} else {
|
|
1760
|
+
ob = fromPath(paths.join("."), value);
|
|
1761
|
+
current[i] = typeof current[i] === "object" ? applyDiff(current[i], ob, true) : ob;
|
|
1762
|
+
}
|
|
1763
|
+
}
|
|
1764
|
+
} else {
|
|
1765
|
+
ob = {
|
|
1766
|
+
[String(lastSegment.value)]: value
|
|
1767
|
+
};
|
|
1768
|
+
}
|
|
1769
|
+
} else {
|
|
1770
|
+
if (Array.isArray(value) && value.length === 0) {
|
|
1771
|
+
if (typeof current[String(lastSegment.value)] === "undefined") {
|
|
1772
|
+
current[String(lastSegment.value)] = [];
|
|
1773
|
+
} else {
|
|
1774
|
+
current[String(lastSegment.value)] = [];
|
|
1775
|
+
}
|
|
1776
|
+
} else {
|
|
1777
|
+
current[String(lastSegment.value)] = value;
|
|
1778
|
+
}
|
|
1779
|
+
}
|
|
1780
|
+
}
|
|
1781
|
+
break;
|
|
1782
|
+
case "index":
|
|
1783
|
+
case "wildcard":
|
|
1784
|
+
if (!Array.isArray(current)) {
|
|
1785
|
+
throw new Error(`Cannot use index on non-array for final segment`);
|
|
1786
|
+
}
|
|
1787
|
+
const index = lastSegment.value;
|
|
1788
|
+
if (lastSegment.type === "index") {
|
|
1789
|
+
if (index < 0) {
|
|
1790
|
+
throw new Error(`Negative index ${index} not allowed`);
|
|
1791
|
+
}
|
|
1792
|
+
if (index >= current.length) {
|
|
1793
|
+
for (let j = current.length; j <= index; j++) {
|
|
1794
|
+
current.push(void 0);
|
|
1795
|
+
}
|
|
1796
|
+
}
|
|
1797
|
+
if (Array.isArray(value)) {
|
|
1798
|
+
if (typeof current[index] === typeof value[index]) {
|
|
1799
|
+
try {
|
|
1800
|
+
current[index] = applyDiff(
|
|
1801
|
+
deepClone(current[index]),
|
|
1802
|
+
value[index],
|
|
1803
|
+
true
|
|
1804
|
+
);
|
|
1805
|
+
} catch (e) {
|
|
1806
|
+
throw Error("expected on array");
|
|
1807
|
+
}
|
|
1808
|
+
} else {
|
|
1809
|
+
current[index] = value;
|
|
1810
|
+
}
|
|
1811
|
+
} else {
|
|
1812
|
+
current[index] = value;
|
|
1813
|
+
}
|
|
1814
|
+
} else if (lastSegment.type === "wildcard") {
|
|
1815
|
+
if (Array.isArray(value)) {
|
|
1816
|
+
for (let i = 0; i < current.length; i++) {
|
|
1817
|
+
if (typeof current[i] === "object") {
|
|
1818
|
+
current[i] = applyDiff(current[i], value[i], true);
|
|
1819
|
+
} else {
|
|
1820
|
+
current[i] = value[i];
|
|
1821
|
+
}
|
|
1822
|
+
}
|
|
1823
|
+
} else {
|
|
1824
|
+
for (let i = 0; i < current.length; i++) {
|
|
1825
|
+
if (typeof current[i] === "object") {
|
|
1826
|
+
current[i] = applyDiff(current[i], value, true);
|
|
1827
|
+
} else {
|
|
1828
|
+
current[i] = value;
|
|
1829
|
+
}
|
|
1830
|
+
}
|
|
1831
|
+
}
|
|
1832
|
+
}
|
|
1833
|
+
break;
|
|
1834
|
+
default:
|
|
1835
|
+
throw new Error(
|
|
1836
|
+
`Invalid final segment type for set: ${lastSegment.type}`
|
|
1837
|
+
);
|
|
1838
|
+
}
|
|
1839
|
+
return obj;
|
|
1840
|
+
};
|
|
1841
|
+
var fromPath = (path, value) => {
|
|
1842
|
+
const segments = parseSelector(path);
|
|
1843
|
+
if (segments.length === 0) {
|
|
1844
|
+
return value;
|
|
1845
|
+
}
|
|
1846
|
+
let result = value;
|
|
1847
|
+
for (let i = segments.length - 1; i >= 0; i--) {
|
|
1848
|
+
const segment = segments[i];
|
|
1849
|
+
if (segment.type === "property") {
|
|
1850
|
+
result = { [String(segment.value)]: result };
|
|
1851
|
+
} else if (segment.type === "index") {
|
|
1852
|
+
const index = parseInt(String(segment.value));
|
|
1853
|
+
const arr = [];
|
|
1854
|
+
arr[index] = result;
|
|
1855
|
+
result = arr;
|
|
1856
|
+
} else if (segment.type === "wildcard") {
|
|
1857
|
+
if (Array.isArray(result)) {
|
|
1858
|
+
result = result;
|
|
1859
|
+
} else {
|
|
1860
|
+
result = [result];
|
|
1861
|
+
}
|
|
1862
|
+
} else if (segment.type === "slice") {
|
|
1863
|
+
if (!Array.isArray(result)) {
|
|
1864
|
+
result = [result];
|
|
1865
|
+
}
|
|
1866
|
+
} else if (segment.type === "union") {
|
|
1867
|
+
if (!Array.isArray(result)) {
|
|
1868
|
+
result = [result];
|
|
1869
|
+
}
|
|
1870
|
+
} else if (segment.type === "filter") {
|
|
1871
|
+
if (!Array.isArray(result)) {
|
|
1872
|
+
result = [result];
|
|
1873
|
+
}
|
|
1874
|
+
} else if (segment.type === "descendant") {
|
|
1875
|
+
result = result;
|
|
1876
|
+
}
|
|
1877
|
+
}
|
|
1878
|
+
return result;
|
|
1879
|
+
};
|
|
1880
|
+
var select = (obj, selector, options = {}) => {
|
|
1881
|
+
return get(obj, selector, { ...options, preserveStructure: false });
|
|
1882
|
+
};
|
|
1883
|
+
var has = (obj, selector, options = {}) => {
|
|
1884
|
+
if (Array.isArray(selector)) {
|
|
1885
|
+
return selector.map((sel) => has(obj, sel, options));
|
|
1886
|
+
}
|
|
1887
|
+
const result = get(obj, selector, { ...options, preserveStructure: false });
|
|
1888
|
+
if (Array.isArray(result)) {
|
|
1889
|
+
if (result.length > 0) {
|
|
1890
|
+
return result.every((r) => r !== void 0);
|
|
1891
|
+
}
|
|
1892
|
+
return false;
|
|
1893
|
+
}
|
|
1894
|
+
return result !== void 0;
|
|
1895
|
+
};
|
|
1896
|
+
var deletePath = (obj, selector, options = {}) => {
|
|
1897
|
+
const { throwOnError = false } = options;
|
|
1898
|
+
if (Array.isArray(selector)) {
|
|
1899
|
+
return selector.map((sel) => deletePath(obj, sel, options));
|
|
1900
|
+
}
|
|
1901
|
+
const segments = parseSelector(selector, { throwOnError });
|
|
1902
|
+
if (segments.length === 0) {
|
|
1903
|
+
return false;
|
|
1904
|
+
}
|
|
1905
|
+
const lastSegment = segments[segments.length - 1];
|
|
1906
|
+
if (segments.some(
|
|
1907
|
+
(s) => s.type === "wildcard" || s.type === "slice" || s.type === "union"
|
|
1908
|
+
)) {
|
|
1909
|
+
const paths = getPaths(obj, selector, options);
|
|
1910
|
+
let deleted = false;
|
|
1911
|
+
for (const path of paths) {
|
|
1912
|
+
if (deleteSinglePath(obj, path)) {
|
|
1913
|
+
deleted = true;
|
|
1914
|
+
}
|
|
1915
|
+
}
|
|
1916
|
+
return deleted;
|
|
1917
|
+
}
|
|
1918
|
+
let current = obj;
|
|
1919
|
+
for (let i = 0; i < segments.length - 1; i++) {
|
|
1920
|
+
const segment = segments[i];
|
|
1921
|
+
if (current == null) {
|
|
1922
|
+
return false;
|
|
1923
|
+
}
|
|
1924
|
+
switch (segment.type) {
|
|
1925
|
+
case "property":
|
|
1926
|
+
current = current[String(segment.value)];
|
|
1927
|
+
break;
|
|
1928
|
+
case "index":
|
|
1929
|
+
if (!Array.isArray(current)) return false;
|
|
1930
|
+
const index = segment.value;
|
|
1931
|
+
if (index < 0 || index >= current.length) return false;
|
|
1932
|
+
current = current[index];
|
|
1933
|
+
break;
|
|
1934
|
+
default:
|
|
1935
|
+
return false;
|
|
1936
|
+
}
|
|
1937
|
+
}
|
|
1938
|
+
if (current == null) {
|
|
1939
|
+
return false;
|
|
1940
|
+
}
|
|
1941
|
+
switch (lastSegment.type) {
|
|
1942
|
+
case "property":
|
|
1943
|
+
if (String(lastSegment.value) in current) {
|
|
1944
|
+
delete current[String(lastSegment.value)];
|
|
1945
|
+
return true;
|
|
1946
|
+
}
|
|
1947
|
+
break;
|
|
1948
|
+
case "index":
|
|
1949
|
+
if (!Array.isArray(current)) return false;
|
|
1950
|
+
const index = lastSegment.value;
|
|
1951
|
+
if (index >= 0 && index < current.length) {
|
|
1952
|
+
current.splice(index, 1);
|
|
1953
|
+
return true;
|
|
1954
|
+
}
|
|
1955
|
+
break;
|
|
1956
|
+
}
|
|
1957
|
+
return false;
|
|
1958
|
+
};
|
|
1959
|
+
var deleteSinglePath = (obj, selector) => {
|
|
1960
|
+
const segments = parseSelector(selector);
|
|
1961
|
+
if (segments.length === 0) {
|
|
1962
|
+
return false;
|
|
1963
|
+
}
|
|
1964
|
+
const lastSegment = segments[segments.length - 1];
|
|
1965
|
+
let current = obj;
|
|
1966
|
+
for (let i = 0; i < segments.length - 1; i++) {
|
|
1967
|
+
const segment = segments[i];
|
|
1968
|
+
if (current == null) {
|
|
1969
|
+
return false;
|
|
1970
|
+
}
|
|
1971
|
+
switch (segment.type) {
|
|
1972
|
+
case "property":
|
|
1973
|
+
current = current[String(segment.value)];
|
|
1974
|
+
break;
|
|
1975
|
+
case "index":
|
|
1976
|
+
if (!Array.isArray(current)) return false;
|
|
1977
|
+
const index = segment.value;
|
|
1978
|
+
if (index < 0 || index >= current.length) return false;
|
|
1979
|
+
current = current[index];
|
|
1980
|
+
break;
|
|
1981
|
+
default:
|
|
1982
|
+
return false;
|
|
1983
|
+
}
|
|
1984
|
+
}
|
|
1985
|
+
if (current == null) {
|
|
1986
|
+
return false;
|
|
1987
|
+
}
|
|
1988
|
+
switch (lastSegment.type) {
|
|
1989
|
+
case "property":
|
|
1990
|
+
if (String(lastSegment.value) in current) {
|
|
1991
|
+
delete current[String(lastSegment.value)];
|
|
1992
|
+
return true;
|
|
1993
|
+
}
|
|
1994
|
+
break;
|
|
1995
|
+
case "index":
|
|
1996
|
+
if (!Array.isArray(current)) return false;
|
|
1997
|
+
const index = lastSegment.value;
|
|
1998
|
+
if (index >= 0 && index < current.length) {
|
|
1999
|
+
current.splice(index, 1);
|
|
2000
|
+
return true;
|
|
2001
|
+
}
|
|
2002
|
+
break;
|
|
2003
|
+
}
|
|
2004
|
+
return false;
|
|
2005
|
+
};
|
|
2006
|
+
var getPaths = (obj, selector, options = {}) => {
|
|
2007
|
+
const { format = "selector", maxDepth = 20 } = options;
|
|
2008
|
+
if (selector && Array.isArray(selector)) {
|
|
2009
|
+
const allPaths2 = [];
|
|
2010
|
+
for (const sel of selector) {
|
|
2011
|
+
const paths = getPaths(obj, sel, options);
|
|
2012
|
+
allPaths2.push(...paths);
|
|
2013
|
+
}
|
|
2014
|
+
return allPaths2;
|
|
2015
|
+
}
|
|
2016
|
+
if (selector) {
|
|
2017
|
+
const segments = parseSelector(selector, options);
|
|
2018
|
+
const results = traverseWithPaths(obj, segments, 0, maxDepth);
|
|
2019
|
+
return results.map(
|
|
2020
|
+
(r) => format === "selector" ? r.path : r.stringPath
|
|
2021
|
+
);
|
|
2022
|
+
}
|
|
2023
|
+
const allPaths = [];
|
|
2024
|
+
const traverseAll = (current, path = [], stringPath = [], depth = 0) => {
|
|
2025
|
+
if (depth > maxDepth) return;
|
|
2026
|
+
if (path.length > 0) {
|
|
2027
|
+
const pathStr = stringifySelector(path);
|
|
2028
|
+
allPaths.push({ path: pathStr, stringPath: stringPath.join(".") });
|
|
2029
|
+
}
|
|
2030
|
+
if (current && typeof current === "object") {
|
|
2031
|
+
if (Array.isArray(current)) {
|
|
2032
|
+
for (let i = 0; i < current.length; i++) {
|
|
2033
|
+
traverseAll(
|
|
2034
|
+
current[i],
|
|
2035
|
+
[...path, { type: "index", value: i, raw: `[${i}]` }],
|
|
2036
|
+
[...stringPath, `[${i}]`],
|
|
2037
|
+
depth + 1
|
|
2038
|
+
);
|
|
2039
|
+
}
|
|
2040
|
+
} else {
|
|
2041
|
+
for (const key in current) {
|
|
2042
|
+
if (current.hasOwnProperty(key)) {
|
|
2043
|
+
traverseAll(
|
|
2044
|
+
current[key],
|
|
2045
|
+
[...path, { type: "property", value: key, raw: key }],
|
|
2046
|
+
[...stringPath, key],
|
|
2047
|
+
depth + 1
|
|
2048
|
+
);
|
|
2049
|
+
}
|
|
2050
|
+
}
|
|
2051
|
+
}
|
|
2052
|
+
}
|
|
2053
|
+
};
|
|
2054
|
+
traverseAll(obj);
|
|
2055
|
+
return allPaths.map(
|
|
2056
|
+
(p) => format === "selector" ? p.path : p.stringPath
|
|
2057
|
+
);
|
|
2058
|
+
};
|
|
2059
|
+
var findPaths = (obj, predicate) => {
|
|
2060
|
+
const paths = getPaths(obj);
|
|
2061
|
+
return paths.filter((path) => {
|
|
2062
|
+
const value = get(obj, path);
|
|
2063
|
+
return predicate(path, value);
|
|
2064
|
+
});
|
|
2065
|
+
};
|
|
2066
|
+
var flattenWithArrays = (obj, separator = ".") => {
|
|
2067
|
+
const result = {};
|
|
2068
|
+
const traverse = (current, path = []) => {
|
|
2069
|
+
if (current == null) return;
|
|
2070
|
+
const trav = traverseStructure(current, path, 0, 20);
|
|
2071
|
+
if (trav !== void 0) {
|
|
2072
|
+
result[stringifySelector(path)] = trav;
|
|
2073
|
+
return;
|
|
2074
|
+
}
|
|
2075
|
+
if (Array.isArray(current)) {
|
|
2076
|
+
for (let i = 0; i < current.length; i++) {
|
|
2077
|
+
const newPath = [
|
|
2078
|
+
...path,
|
|
2079
|
+
{ type: "index", value: i, raw: `[${i}]` }
|
|
2080
|
+
];
|
|
2081
|
+
traverse(current[i], newPath);
|
|
2082
|
+
}
|
|
2083
|
+
} else if (isObject2(current)) {
|
|
2084
|
+
for (const key in current) {
|
|
2085
|
+
if (current.hasOwnProperty(key)) {
|
|
2086
|
+
const newPath = [
|
|
2087
|
+
...path,
|
|
2088
|
+
{ type: "property", value: key, raw: key }
|
|
2089
|
+
];
|
|
2090
|
+
traverse(current[key], newPath);
|
|
2091
|
+
}
|
|
2092
|
+
}
|
|
2093
|
+
const symbols = Object.getOwnPropertySymbols(current);
|
|
2094
|
+
for (const symbol of symbols) {
|
|
2095
|
+
const newPath = [
|
|
2096
|
+
...path,
|
|
2097
|
+
{
|
|
2098
|
+
type: "property",
|
|
2099
|
+
value: symbol.toString(),
|
|
2100
|
+
raw: symbol.toString()
|
|
2101
|
+
}
|
|
2102
|
+
];
|
|
2103
|
+
traverse(current[symbol], newPath);
|
|
2104
|
+
}
|
|
2105
|
+
} else {
|
|
2106
|
+
const pathStr = stringifySelector(path);
|
|
2107
|
+
result[pathStr] = current;
|
|
2108
|
+
}
|
|
2109
|
+
};
|
|
2110
|
+
traverse(obj);
|
|
2111
|
+
return result;
|
|
2112
|
+
};
|
|
2113
|
+
var unflattenWithArrays = (obj) => {
|
|
2114
|
+
const result = {};
|
|
2115
|
+
for (const key in obj) {
|
|
2116
|
+
set(result, key, obj[key]);
|
|
2117
|
+
}
|
|
2118
|
+
return result;
|
|
2119
|
+
};
|
|
2120
|
+
var deepMergeWithArrays = (...objects) => {
|
|
2121
|
+
const result = {};
|
|
2122
|
+
for (const obj of objects) {
|
|
2123
|
+
if (!isObject2(obj)) continue;
|
|
2124
|
+
const flat = flattenWithArrays(obj);
|
|
2125
|
+
for (const path in flat) {
|
|
2126
|
+
set(result, path, flat[path]);
|
|
2127
|
+
}
|
|
2128
|
+
}
|
|
2129
|
+
return result;
|
|
2130
|
+
};
|
|
2131
|
+
function traverseWithPaths(current, segments, depth, maxDepth, currentPath = [], currentStringPath = []) {
|
|
2132
|
+
if (depth > maxDepth) return [];
|
|
2133
|
+
if (segments.length === 0)
|
|
2134
|
+
return [
|
|
2135
|
+
{
|
|
2136
|
+
value: current,
|
|
2137
|
+
path: stringifySelector(currentPath),
|
|
2138
|
+
stringPath: currentStringPath.join(".")
|
|
2139
|
+
}
|
|
2140
|
+
];
|
|
2141
|
+
const segment = segments[0];
|
|
2142
|
+
const remaining = segments.slice(1);
|
|
2143
|
+
const results = [];
|
|
2144
|
+
if (segment.type === "descendant") {
|
|
2145
|
+
results.push(
|
|
2146
|
+
...traverseWithPaths(
|
|
2147
|
+
current,
|
|
2148
|
+
remaining,
|
|
2149
|
+
depth + 1,
|
|
2150
|
+
maxDepth,
|
|
2151
|
+
currentPath,
|
|
2152
|
+
currentStringPath
|
|
2153
|
+
)
|
|
2154
|
+
);
|
|
2155
|
+
if (current && typeof current === "object") {
|
|
2156
|
+
if (Array.isArray(current)) {
|
|
2157
|
+
for (let i = 0; i < current.length; i++) {
|
|
2158
|
+
results.push(
|
|
2159
|
+
...traverseWithPaths(
|
|
2160
|
+
current[i],
|
|
2161
|
+
segments,
|
|
2162
|
+
// Use same segments for recursion
|
|
2163
|
+
depth + 1,
|
|
2164
|
+
maxDepth,
|
|
2165
|
+
[...currentPath, { type: "index", value: i, raw: `[${i}]` }],
|
|
2166
|
+
[...currentStringPath, `[${i}]`]
|
|
2167
|
+
)
|
|
2168
|
+
);
|
|
2169
|
+
}
|
|
2170
|
+
} else {
|
|
2171
|
+
for (const key in current) {
|
|
2172
|
+
if (current.hasOwnProperty(key)) {
|
|
2173
|
+
results.push(
|
|
2174
|
+
...traverseWithPaths(
|
|
2175
|
+
current[key],
|
|
2176
|
+
segments,
|
|
2177
|
+
depth + 1,
|
|
2178
|
+
maxDepth,
|
|
2179
|
+
[...currentPath, { type: "property", value: key, raw: key }],
|
|
2180
|
+
[...currentStringPath, key]
|
|
2181
|
+
)
|
|
2182
|
+
);
|
|
2183
|
+
}
|
|
2184
|
+
}
|
|
2185
|
+
}
|
|
2186
|
+
}
|
|
2187
|
+
return results;
|
|
2188
|
+
}
|
|
2189
|
+
switch (segment.type) {
|
|
2190
|
+
case "property":
|
|
2191
|
+
if (current && typeof current === "object" && String(segment.value) in current) {
|
|
2192
|
+
const next = current[String(segment.value)];
|
|
2193
|
+
results.push(
|
|
2194
|
+
...traverseWithPaths(
|
|
2195
|
+
next,
|
|
2196
|
+
remaining,
|
|
2197
|
+
depth + 1,
|
|
2198
|
+
maxDepth,
|
|
2199
|
+
[...currentPath, segment],
|
|
2200
|
+
[...currentStringPath, String(segment.value)]
|
|
2201
|
+
)
|
|
2202
|
+
);
|
|
2203
|
+
}
|
|
2204
|
+
break;
|
|
2205
|
+
case "index":
|
|
2206
|
+
if (Array.isArray(current)) {
|
|
2207
|
+
const index = segment.value;
|
|
2208
|
+
if (index >= 0 && index < current.length) {
|
|
2209
|
+
results.push(
|
|
2210
|
+
...traverseWithPaths(
|
|
2211
|
+
current[index],
|
|
2212
|
+
remaining,
|
|
2213
|
+
depth + 1,
|
|
2214
|
+
maxDepth,
|
|
2215
|
+
[...currentPath, segment],
|
|
2216
|
+
[...currentStringPath, `[${index}]`]
|
|
2217
|
+
)
|
|
2218
|
+
);
|
|
2219
|
+
}
|
|
2220
|
+
} else if (current && typeof current === "object") {
|
|
2221
|
+
const key = String(segment.value);
|
|
2222
|
+
if (key in current) {
|
|
2223
|
+
results.push(
|
|
2224
|
+
...traverseWithPaths(
|
|
2225
|
+
current[key],
|
|
2226
|
+
remaining,
|
|
2227
|
+
depth + 1,
|
|
2228
|
+
maxDepth,
|
|
2229
|
+
[...currentPath, { ...segment, type: "property" }],
|
|
2230
|
+
[...currentStringPath, key]
|
|
2231
|
+
)
|
|
2232
|
+
);
|
|
2233
|
+
}
|
|
2234
|
+
}
|
|
2235
|
+
break;
|
|
2236
|
+
case "wildcard":
|
|
2237
|
+
if (current && typeof current === "object") {
|
|
2238
|
+
if (Array.isArray(current)) {
|
|
2239
|
+
for (let i = 0; i < current.length; i++) {
|
|
2240
|
+
results.push(
|
|
2241
|
+
...traverseWithPaths(
|
|
2242
|
+
current[i],
|
|
2243
|
+
remaining,
|
|
2244
|
+
depth + 1,
|
|
2245
|
+
maxDepth,
|
|
2246
|
+
[...currentPath, { type: "index", value: i, raw: `[${i}]` }],
|
|
2247
|
+
[...currentStringPath, `[${i}]`]
|
|
2248
|
+
)
|
|
2249
|
+
);
|
|
2250
|
+
}
|
|
2251
|
+
} else {
|
|
2252
|
+
for (const key in current) {
|
|
2253
|
+
if (current.hasOwnProperty(key)) {
|
|
2254
|
+
results.push(
|
|
2255
|
+
...traverseWithPaths(
|
|
2256
|
+
current[key],
|
|
2257
|
+
remaining,
|
|
2258
|
+
depth + 1,
|
|
2259
|
+
maxDepth,
|
|
2260
|
+
[...currentPath, { type: "property", value: key, raw: key }],
|
|
2261
|
+
[...currentStringPath, key]
|
|
2262
|
+
)
|
|
2263
|
+
);
|
|
2264
|
+
}
|
|
2265
|
+
}
|
|
2266
|
+
}
|
|
2267
|
+
}
|
|
2268
|
+
break;
|
|
2269
|
+
case "slice":
|
|
2270
|
+
if (Array.isArray(current)) {
|
|
2271
|
+
const [start, end] = segment.value;
|
|
2272
|
+
const actualStart = Math.max(0, start);
|
|
2273
|
+
const actualEnd = Math.min(
|
|
2274
|
+
current.length,
|
|
2275
|
+
end === Infinity ? current.length : end
|
|
2276
|
+
);
|
|
2277
|
+
for (let i = actualStart; i < actualEnd; i++) {
|
|
2278
|
+
results.push(
|
|
2279
|
+
...traverseWithPaths(
|
|
2280
|
+
current[i],
|
|
2281
|
+
remaining,
|
|
2282
|
+
depth + 1,
|
|
2283
|
+
maxDepth,
|
|
2284
|
+
[...currentPath, { type: "index", value: i, raw: `[${i}]` }],
|
|
2285
|
+
[...currentStringPath, `[${i}]`]
|
|
2286
|
+
)
|
|
2287
|
+
);
|
|
2288
|
+
}
|
|
2289
|
+
}
|
|
2290
|
+
break;
|
|
2291
|
+
case "union":
|
|
2292
|
+
if (Array.isArray(current)) {
|
|
2293
|
+
const indices = segment.value;
|
|
2294
|
+
for (const index of indices) {
|
|
2295
|
+
if (index >= 0 && index < current.length) {
|
|
2296
|
+
results.push(
|
|
2297
|
+
...traverseWithPaths(
|
|
2298
|
+
current[index],
|
|
2299
|
+
remaining,
|
|
2300
|
+
depth + 1,
|
|
2301
|
+
maxDepth,
|
|
2302
|
+
[
|
|
2303
|
+
...currentPath,
|
|
2304
|
+
{ type: "index", value: index, raw: `[${index}]` }
|
|
2305
|
+
],
|
|
2306
|
+
[...currentStringPath, `[${index}]`]
|
|
2307
|
+
)
|
|
2308
|
+
);
|
|
2309
|
+
}
|
|
2310
|
+
}
|
|
2311
|
+
}
|
|
2312
|
+
break;
|
|
2313
|
+
case "filter":
|
|
2314
|
+
if (Array.isArray(current)) {
|
|
2315
|
+
const filterFn = segment.value;
|
|
2316
|
+
for (let i = 0; i < current.length; i++) {
|
|
2317
|
+
if (filterFn(current[i])) {
|
|
2318
|
+
results.push(
|
|
2319
|
+
...traverseWithPaths(
|
|
2320
|
+
current[i],
|
|
2321
|
+
remaining,
|
|
2322
|
+
depth + 1,
|
|
2323
|
+
maxDepth,
|
|
2324
|
+
[...currentPath, { type: "index", value: i, raw: `[${i}]` }],
|
|
2325
|
+
[...currentStringPath, `[${i}]`]
|
|
2326
|
+
)
|
|
2327
|
+
);
|
|
2328
|
+
}
|
|
2329
|
+
}
|
|
2330
|
+
}
|
|
2331
|
+
break;
|
|
2332
|
+
}
|
|
2333
|
+
return results;
|
|
2334
|
+
}
|
|
2335
|
+
var update = (obj, path, updater) => {
|
|
2336
|
+
const currentValue = get(obj, path);
|
|
2337
|
+
const newValue = updater(currentValue);
|
|
2338
|
+
return setValueAtPath(obj, path, newValue);
|
|
2339
|
+
};
|
|
2340
|
+
var updateWithCallBack = (obj, updater) => {
|
|
2341
|
+
const newValue = updater(obj);
|
|
2342
|
+
const result = applyDiff(obj, newValue, true);
|
|
2343
|
+
return result;
|
|
2344
|
+
};
|
|
2345
|
+
var getTypeAtPath = (obj, path) => {
|
|
2346
|
+
const value = get(obj, path);
|
|
2347
|
+
if (value === void 0) return void 0;
|
|
2348
|
+
if (value === null) return "null";
|
|
2349
|
+
if (Array.isArray(value)) return "array";
|
|
2350
|
+
return typeof value;
|
|
2351
|
+
};
|