@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,2227 @@
|
|
|
1
|
-
"use strict";var I=Object.defineProperty;var N=Object.getOwnPropertyDescriptor;var L=Object.getOwnPropertyNames;var _=Object.prototype.hasOwnProperty;var z=(e,t)=>{for(var n in t)I(e,n,{get:t[n],enumerable:!0})},J=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of L(t))!_.call(e,i)&&i!==n&&I(e,i,{get:()=>t[i],enumerable:!(r=N(t,i))||r.enumerable});return e};var D=e=>J(I({},"__esModule",{value:!0}),e);var _n={};z(_n,{applyByIndex:()=>mn,avgBy:()=>Ne,batch:()=>En,batchProcess:()=>_e,batchProcessParallel:()=>ze,bifurcate:()=>He,binarySearch:()=>Nn,cartesian:()=>Ve,chunk:()=>v,chunkBy:()=>Ce,compact:()=>le,countBy:()=>X,cumulativeSum:()=>yn,dedupeConsecutive:()=>on,deepCloneArray:()=>we,diff:()=>pn,difference:()=>ne,differenceWith:()=>rn,drop:()=>ae,dropWhile:()=>fe,ensureArray:()=>$,enumerate:()=>gn,everyAsync:()=>kn,fill:()=>Ke,filterAsync:()=>Pn,filterCompact:()=>ke,filterConsecutive:()=>Mn,findAllIndices:()=>Re,findAsync:()=>Fn,findIndex:()=>Te,findLastIndex:()=>Ae,flatMap:()=>he,flatten:()=>G,flattenDeep:()=>V,frequencies:()=>K,generate:()=>Xe,groupBy:()=>H,groupConsecutive:()=>sn,hasDuplicates:()=>Kn,hasSameElements:()=>Qe,indexBy:()=>Bn,insert:()=>Ie,insertSorted:()=>Ln,interleave:()=>Ge,intersection:()=>te,isArrayEqual:()=>De,isEqual:()=>$e,isSorted:()=>Sn,isSubset:()=>U,isSuperset:()=>Ze,keyBy:()=>Y,leastCommon:()=>Le,mapAsync:()=>vn,mapAsyncParallel:()=>On,mapWithContext:()=>Un,maxBy:()=>Be,median:()=>en,mergeArraysByKey:()=>qn,mergeByKey:()=>ve,minBy:()=>qe,mode:()=>nn,mostCommon:()=>M,move:()=>Oe,normalize:()=>ln,omit:()=>xe,pad:()=>dn,pairwise:()=>An,partition:()=>ie,percentile:()=>P,pick:()=>me,pickMany:()=>wn,pluck:()=>ge,quartiles:()=>an,range:()=>Se,rank:()=>un,reduceAsync:()=>In,reduceRight:()=>be,remove:()=>Fe,repeat:()=>Ye,replace:()=>je,reverseSegment:()=>bn,rotate:()=>Pe,rotateLeft:()=>W,rotateRight:()=>xn,sample:()=>pe,sampleSize:()=>de,scan:()=>E,segment:()=>$n,selectByCount:()=>Cn,shuffle:()=>R,slidingWindow:()=>We,someAsync:()=>jn,sortBy:()=>ee,splitDuplicates:()=>Ue,standardDeviation:()=>F,standardize:()=>cn,sumBy:()=>C,swap:()=>hn,take:()=>se,takeLast:()=>oe,takeWhile:()=>ue,toObject:()=>q,transpose:()=>Je,trim:()=>Tn,union:()=>re,unique:()=>O,unzip:()=>ye,updateBy:()=>Me,variance:()=>fn,weightedAverage:()=>Vn,weightedSample:()=>Rn,without:()=>tn,wrap:()=>Ee,zip:()=>ce,zipWith:()=>Wn});module.exports=D(_n);var A=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 h=(e,t,n,r)=>{if(n>r)return;if(t.length===0)return e;let i=t[0],s=t.slice(1);if(i.type==="descendant"){let u=h(e,s,n+1,r);if(u!==void 0)return u;if(e&&typeof e=="object")if(Array.isArray(e)){let o=[];for(let a=0;a<e.length;a++){let f=h(e[a],t,n+1,r);f!==void 0&&(o[a]=f)}return o.length>0?o:void 0}else{let o={};for(let a in e)if(e.hasOwnProperty(a)){let f=h(e[a],t,n+1,r);f!==void 0&&(o[a]=f)}return Object.keys(o).length>0?o:void 0}return}switch(i.type){case"property":if(A(e)&&i.value&&String(i.value)in e){let u=e[String(i.value)],o=h(u,s,n+1,r);if(o!==void 0)return{[String(i.value)]:o}}break;case"index":if(Array.isArray(e)){let u=i.value;if(u>=0&&u<e.length){let o=h(e[u],s,n+1,r);if(o!==void 0){let a=new Array(e.length).filter(f=>f!==void 0);return a[u]=o,a}}}else if(e&&typeof e=="object"){let u=String(i.value);if(u in e){let o=h(e[u],s,n+1,r);if(o!==void 0)return{[u]:o}}}break;case"wildcard":if(e&&typeof e=="object")if(Array.isArray(e)){let u=[],o=!1;for(let a=0;a<e.length;a++){let f=h(e[a],s,n+1,r);f!==void 0&&(u[a]=f,o=!0)}return o?u:void 0}else{let u={},o=!1;for(let a in e)if(e.hasOwnProperty(a)){let f=h(e[a],s,n+1,r);f!==void 0&&(u[a]=f,o=!0)}return o?u:void 0}break;case"slice":if(Array.isArray(e)){let[u,o]=i.value,a=Math.max(0,u),f=Math.min(e.length,o===1/0?e.length:o),y=[],g=!1;for(let p=a;p<f;p++){let b=h(e[p],s,n+1,r);b!==void 0&&(y[p]=b,g=!0)}return g?y:void 0}break;case"union":if(Array.isArray(e)){let u=i.value,o=[],a=!1;for(let f of u)if(f>=0&&f<e.length){let y=h(e[f],s,n+1,r);y!==void 0&&(o[f]=y,a=!0)}return a?o:void 0}break;case"filter":if(Array.isArray(e)){let u=i.value,o=[],a=!1;for(let f=0;f<e.length;f++)if(u(e[f])){let y=h(e[f],s,n+1,r);y!==void 0&&(o[f]=y,a=!0)}return a?o:void 0}break}};var S=(e,t)=>{if(e==null||typeof e!="object")return e;if(e instanceof Date)return new Date(e.getTime());if(e instanceof RegExp)return new RegExp(e.source,e.flags);if(e instanceof Map){let n=new Map;return e.forEach((r,i)=>{(!t||!t.some(s=>s.toString()===i.toString()))&&n.set(i,S(r))}),n}if(e instanceof Set){let n=new Set;return e.forEach(r=>{n.add(S(r))}),n}if(Array.isArray(e))return Array.from(e).map(n=>S(n));if(A(e)){let n={};for(let i in e)Object.prototype.hasOwnProperty.call(e,i)&&(!t||!t.some(s=>s.toString()===i.toString()))&&(n[i]=S(e[i]));let r=Object.getOwnPropertySymbols(e);for(let i of r)(!t||!t.some(s=>s.toString()===i.toString()))&&(n[i]=S(e[i]));return n}return e};var w=(e,t={})=>{let{type:n="auto",throwOnError:r=!1}=t;if(!e||typeof e!="string")return[];let i=[],s="",u=!1,o=!1,a="",f="",y=0,p=n==="auto"?(c=>c.includes("..")?"descendant":c.includes("[?(")?"filter":c.includes("[:")||c.includes("[0:")?"slice":c.includes("[,")||c.includes("[0,1]")?"union":c.includes("[*]")||c.includes(".*.")?"wildcard":c.includes("[]")?"array-wildcard":c.includes("[")&&c.includes("]")?"bracket":"dot")(e):n,b=c=>{let l=c.trim();if(l==="*"||l==="")return{type:"wildcard",value:l===""?"[]":"*",raw:l===""?"[]":c};if(l==="[]")return{type:"wildcard",value:"[]",raw:c};if(/^-?\d+$/.test(l))return{type:"index",value:parseInt(l,10),raw:c};if(l.includes(":")){let T=l.split(":").map(B=>B.trim()),m=T[0]===""?0:parseInt(T[0],10),x=T[1]===""?1/0:parseInt(T[1],10);if(isNaN(m)||isNaN(x)){if(r)throw new Error(`Invalid slice notation: ${c}`);return{type:"wildcard",value:"*",raw:c}}return{type:"slice",value:[m,x],raw:c}}if(l.includes(",")){let T=l.split(",").map(m=>{let x=parseInt(m.trim(),10);return isNaN(x)?-1:x}).filter(m=>m>=0);if(T.length===0){if(r)throw new Error(`Invalid union notation: ${c}`);return{type:"wildcard",value:"*",raw:c}}return{type:"union",value:T,raw:c}}if(l.startsWith("?(")&&l.endsWith(")")){let T=l.slice(2,-1);try{return{type:"filter",value:Q(T),raw:c}}catch(m){if(r)throw m;return{type:"wildcard",value:"*",raw:c}}}return l.startsWith('"')&&l.endsWith('"')||l.startsWith("'")&&l.endsWith("'")?{type:"property",value:l.slice(1,-1),raw:c}:{type:"property",value:l,raw:c}};for(let c=0;c<e.length;c++){let l=e[c],T=e[c+1];if((l==='"'||l==="'")&&!o){o=!0,a=l;continue}else if(l===a&&o){o=!1,a="",l==='"'||l==="'"||(u?f+=l:s+=l);continue}if(o){u?f+=l:s+=l;continue}if(l==="["&&!u){s&&(i.push({type:"property",value:s,raw:s}),s=""),u=!0,f="";continue}if(l==="]"&&u&&!o){u=!1;let x=b(f);i.push(x);continue}let m=i.length>0?i[i.length-1]:null;if(l==="."&&!u&&!o){if(T==="."){let x=e.split("..")[0];i.push({type:"property",value:x,raw:x}),i.push({type:"descendant",value:"..",raw:".."}),c++}else s&&(s==="*"?i.push({type:"wildcard",value:s,raw:s}):i.push({type:"property",value:s,raw:s}),s="");continue}if(u)f+=l;else if(m&&m.type!=="descendant")s+=l;else{let x=e.split(m?.raw)[1];x!==void 0?s=x:s+=l}}return s&&i.push({type:"property",value:s,raw:s}),i.map(c=>{if(c.type==="property"&&typeof c.value=="string"&&/^-?\d+$/.test(c.value)){let l=parseInt(c.value,10);if(!isNaN(l))return{...c,type:"index",value:l}}return c})},Q=e=>{let t=e.replace(/@\.([a-zA-Z_$][\w$]*)/g,(n,r)=>`item.${r}`).replace(/@/g,"item");try{return new Function("item",`return ${t}`)}catch{throw new Error(`Invalid filter expression: ${e}`)}};var j=(e,t,n)=>{let r=w(t),i=e;for(let s=0;s<r.length;s++){let u=r[s],o=s===r.length-1;switch(u.type){case"property":if(o)i[String(u.value)]=n;else{if(!i[String(u.value)]){let f=r[s+1];i[String(u.value)]=f.type==="index"||f.type==="slice"||f.type==="union"||f.type==="wildcard"?[]:{}}i=i[String(u.value)]}break;case"index":Array.isArray(i)||(i=[]);let a=u.value;if(o){if(a>=i.length)for(let f=i.length;f<=a;f++)i.push(void 0);i[a]=n}else{if(!i[a]){let f=r[s+1];i[a]=f.type==="index"||f.type==="slice"||f.type==="union"||f.type==="wildcard"?[]:{}}i=i[a]}break;case"wildcard":if(Array.isArray(i))if(o)for(let f=0;f<i.length;f++)i[f]=n;else{let f=r[s+1];for(let y=0;y<i.length;y++)i[y]||(i[y]=f.type==="index"||f.type==="slice"||f.type==="union"||f.type==="wildcard"?[]:{});i.length>0&&(i=i[0])}break}}};function d(e,t,n={}){let{preserveStructure:r,multiple:i,throwOnError:s=!1,defaultValue:u,maxDepth:o=20}=n;if(!e||typeof e!="object")return u;if(!t)return e;if(Array.isArray(t))if(r){let y={};for(let g of t){let p=d(e,g,{...n,preserveStructure:!1});p!==void 0&&j(y,g,p)}return y}else return t.map(y=>d(e,y,{...n,preserveStructure:!1}));let a=w(t,{...n,throwOnError:s});if(a.length===0)return e;if(typeof r>"u"||r===!1){let y=Z(e,t);return i===!1&&Array.isArray(y)?y[0]:y===void 0&&u!==void 0?u:y}let f=h(e,a,0,o);return f===void 0&&u!==void 0?u:f}function Z(e,t,n={}){let{createMissing:r=!1,add:i=!1,debug:s=!1}=n,u=w(t),o=e,a=!1,f="",y=t;if(typeof o=="object"&&!Array.isArray(o)&&y.length>0&&o[y])return o=o[y],o;for(let g=0;g<u.length;g++){let p=u[g],b=u.length>1?g===u.length-1:!1;if(o===null&&!r)return;let c=u[g+1];switch(p.type){case"property":if(o&&typeof o=="object")if(Array.isArray(o)){if(typeof p.value=="number"){let l=p.value;if(r&&l>=o.length)for(let T=o.length;T<=l;T++)o.push(void 0);if(r&&(o[l]===null||o[l]===void 0)&&(!b||a)&&(c?o[l]=c.type==="index"||c.type==="slice"||c.type==="union"||c.type==="wildcard"?[]:{}:o[l]=null,a=!1),o=o[l],c){a=!0;continue}}else i?o.push({[String(p.value)]:null}):o=o.map(l=>l[String(p.value)]);if(c)continue;return k(o)}else{if(o[f]&&f.split(".").length>=2){o=o[f];continue}if(r&&(o[String(p.value)]===null||o[String(p.value)]===void 0)&&(!b||a)&&(c?o[String(p.value)]=c.type==="index"||c.type==="slice"||c.type==="union"||c.type==="wildcard"?[]:{}:o[String(p.value)]=null,a=!1,o=o[String(p.value)],c)){a=!0;continue}if(o?(!o[String(p.value)]&&r&&(o[String(p.value)]=null),o=o[String(p.value)]):o={[String(p.value)]:null},c)continue}else if(r)o={},o[String(p.value)]=c.type==="index"||c.type==="slice"||c.type==="union"||c.type==="wildcard"||n.type==="array"?[]:{},o=o[String(p.value)];else return;break;case"index":case"wildcard":if(Array.isArray(o)){if(p.type==="index"){let l=p.value;if(l<0){o=o[o.length-Math.abs(l)];continue}if(r&&(l>=o.length||o[l]==null||o[l]==null)&&!b){for(let T=o.length;T<=l;T++)o.push(void 0);c&&(o[l]=c.type==="index"||c.type==="slice"||c.type==="union"||c.type==="wildcard"?[]:{})}o=o[l]}else if(!b)continue}else if(r){let l=u[g+1];o=[];let T=p.value;for(let m=0;m<=T;m++)o.push(void 0);o[T]=l.type==="index"||l.type==="slice"||l.type==="union"||l.type==="wildcard"||n.type==="array"?[]:{},o=o[T]}else{if(p.type==="index"&&typeof o=="object"&&o&&o[String(p.value)]!==void 0){o=o[String(p.value)];continue}if(typeof o=="object"){o=Object.values(o);continue}return}break;default:return}}return k(o)}function k(e){return Array.isArray(e)?e.filter(t=>typeof t<"u"):e}function v(e,t){if(!Array.isArray(e)||t<1)return[];let n=[];for(let r=0;r<e.length;r+=t)n.push(e.slice(r,r+t));return n}function G(e){return e.reduce((t,n)=>t.concat(n),[])}function V(e,t=1/0){return t<1?e.slice():e.reduce((n,r)=>(Array.isArray(r)?n.push(...V(r,t-1)):n.push(r),n),[])}function O(e,t){if(!Array.isArray(e))return[];if(!t)return[...new Set(e)];let n=new Set;return e.filter(r=>{let i=typeof t=="function"?t(r):A(r)?d(r,t):r;return n.has(i)?!1:(n.add(i),!0)})}function H(e,t){return Array.isArray(e)?e.reduce((n,r)=>{let i;return typeof t=="function"?i=String(t(r)):i=String(A(r)?d(r,t):r),n[i]||(n[i]=[]),n[i].push(r),n},{}):{}}function X(e,t){return Array.isArray(e)?e.reduce((n,r)=>{let i;return typeof t=="function"?i=String(t(r)):t?i=String(A(r)?d(r,t):r):i=String(r),n[i]=(n[i]||0)+1,n},{}):{}}function Y(e,t,n){return Array.isArray(e)?e.reduce((r,i)=>{let s;typeof t=="function"?s=String(t(i)):s=String(A(i)?d(i,t):i);let u;return n?typeof n=="function"?u=n(i):u=d(i,n):u=i,r[s]=u,r},{}):{}}function ee(e,t){if(!Array.isArray(e)||e.length===0)return[];let n=[...e],i=(Array.isArray(t)?t:[t]).map(s=>typeof s=="object"&&"key"in s?{key:s.key,dir:s.dir||"asc"}:{key:s,dir:"asc"});return n.sort((s,u)=>{for(let{key:o,dir:a}of i){let f=A(s)?d(s,o):s,y=A(u)?d(u,o):u;if(f===y)continue;let g=f<y?-1:1;return a==="asc"?g:-g}return 0}),n}function ne(e,t,n){if(!Array.isArray(e))return[];if(!Array.isArray(t))return e;if(!n){let i=new Set(t);return e.filter(s=>!i.has(s))}let r=new Set(t.map(i=>A(i)?d(i,n):i));return e.filter(i=>{let s=A(i)?d(i,n):i;return!r.has(s)})}function te(e,t,n){if(!Array.isArray(e)||!Array.isArray(t))return[];if(!n){let i=new Set(t);return e.filter(s=>i.has(s))}let r=new Set(t.map(i=>A(i)?d(i,n):i));return e.filter(i=>{let s=A(i)?d(i,n):i;return r.has(s)})}function re(e,t,n){return!Array.isArray(e)&&!Array.isArray(t)?[]:Array.isArray(e)?Array.isArray(t)?O([...e,...t],n):O(e,n):O(t,n)}function ie(e,t){return Array.isArray(e)?e.reduce((n,r,i)=>(n[t(r,i)?0:1].push(r),n),[[],[]]):[[],[]]}function se(e,t){return!Array.isArray(e)||t<=0?[]:e.slice(0,t)}function oe(e,t){return!Array.isArray(e)||t<=0?[]:e.slice(-t)}function ue(e,t){if(!Array.isArray(e))return[];let n=[];for(let r=0;r<e.length&&t(e[r],r);r++)n.push(e[r]);return n}function ae(e,t){return!Array.isArray(e)||t<=0?e?[...e]:[]:e.slice(t)}function fe(e,t){if(!Array.isArray(e))return[];let n=0;for(;n<e.length&&t(e[n],n);)n++;return e.slice(n)}function le(e){return Array.isArray(e)?e.filter(Boolean):[]}function ce(...e){if(e.length===0)return[];let t=Math.max(...e.map(r=>Array.isArray(r)?r.length:0)),n=[];for(let r=0;r<t;r++){let i=[];for(let s of e)i.push(Array.isArray(s)?s[r]:void 0);n.push(i)}return n}function ye(e){if(!Array.isArray(e)||e.length===0)return[];let t=Math.max(...e.map(r=>Array.isArray(r)?r.length:0)),n=[];for(let r=0;r<t;r++){let i=[];for(let s of e)Array.isArray(s)&&i.push(s[r]);n.push(i)}return n}function R(e){if(!Array.isArray(e))return[];let t=[...e];for(let n=t.length-1;n>0;n--){let r=Math.floor(Math.random()*(n+1));[t[n],t[r]]=[t[r],t[n]]}return t}function pe(e){if(!(!Array.isArray(e)||e.length===0))return e[Math.floor(Math.random()*e.length)]}function de(e,t){return!Array.isArray(e)||t<=0?[]:R(e).slice(0,Math.min(t,e.length))}function Te(e,t,n=0){if(!Array.isArray(e))return-1;for(let r=n;r<e.length;r++)if(t(e[r],r))return r;return-1}function Ae(e,t,n){if(!Array.isArray(e))return-1;let r=n!==void 0?n:e.length-1;for(let i=r;i>=0;i--)if(t(e[i],i))return i;return-1}function ge(e,t){return Array.isArray(e)?e.map(n=>d(n,t)):[]}function me(e,t){return!Array.isArray(e)||!Array.isArray(t)?[]:e.map(n=>{let r={};for(let i of t)i in n&&(r[i]=n[i]);return r})}function xe(e,t){return!Array.isArray(e)||!Array.isArray(t)?[]:e.map(n=>{let r={...n};for(let i of t)delete r[i];return r})}function he(e,t){return Array.isArray(e)?e.reduce((n,r,i)=>{let s=t(r,i);return n.concat(s)},[]):[]}function be(e,t,n){if(!Array.isArray(e))return n;let r=n;for(let i=e.length-1;i>=0;i--)r=t(r,e[i],i);return r}function Se(e,t,n=1){if(n===0)return[];let r=[];if(n>0)for(let i=e;i<t;i+=n)r.push(i);else for(let i=e;i>t;i+=n)r.push(i);return r}function Ke(e,t,n=0,r){if(!Array.isArray(e))return[];let i=[...e],s=r===void 0?i.length:r;for(let u=n;u<s&&u<i.length;u++)i[u]=t;return i}function we(e){return Array.isArray(e)?S(e):[]}function ve(e,t,...n){let r,i;if(typeof t=="string")r=t,i=[e];else{let u=n[n.length-1];typeof u=="string"?(r=u,i=[e,t,...n.slice(0,-1).filter(Array.isArray)]):(r="id",i=[e,t,...n.filter(Array.isArray)])}if(i.length===0)return[];let s=new Map;for(let u of i)if(Array.isArray(u))for(let o of u){let a=A(o)?d(o,r):void 0;if(a!==void 0){let f=s.get(a);s.set(a,f?{...f,...o}:o)}}return Array.from(s.values())}function Oe(e,t,n){if(!Array.isArray(e)||t===n)return e?[...e]:[];let r=[...e],[i]=r.splice(t,1);return r.splice(n,0,i),r}function Pe(e,t){if(!Array.isArray(e)||e.length===0||t===0)return e?[...e]:[];let n=e.length,r=(t%n+n)%n;return[...e.slice(-r),...e.slice(0,-r)]}function Ie(e,t,...n){if(!Array.isArray(e))return n;let r=[...e];return r.splice(t,0,...n),r}function Fe(e,t,n=1){if(!Array.isArray(e))return[];let r=[...e];return r.splice(t,n),r}function je(e,t,n){if(!Array.isArray(e))return[];let r=[...e];return r[t]=n,r}function ke(e,t){if(!Array.isArray(e))return[];let n=e.filter(r=>r!=null);return t?n.filter(t):n}function Ve(...e){return e.length===0?[[]]:e.length===1?e[0].map(t=>[t]):e.reduce((t,n)=>t.flatMap(r=>n.map(i=>[...r,i])),[[]])}function Re(e,t){if(!Array.isArray(e))return[];let n=[];for(let r=0;r<e.length;r++)t(e[r],r)&&n.push(r);return n}function $e(e,t,n=!0){if(!Array.isArray(e)||!Array.isArray(t)||e.length!==t.length)return!1;for(let r=0;r<e.length;r++)if(n&&A(e[r])&&A(t[r])){if(JSON.stringify(e[r])!==JSON.stringify(t[r]))return!1}else if(e[r]!==t[r])return!1;return!0}function Ce(e,t){if(!Array.isArray(e)||e.length===0)return[];let n=[],r=[e[0]],i=t(e[0],0);for(let s=1;s<e.length;s++){let u=t(e[s],s);u===i?r.push(e[s]):(n.push(r),r=[e[s]],i=u)}return n.push(r),n}function Me(e,t,n,r){if(!Array.isArray(e))return[];let i=typeof t=="function",s=i?n:r;return e.map(u=>{let o=!1;return i?o=t(u):o=(A(u)?d(u,t):u)===n,o?{...u,...s}:u})}function Ue(e,t){if(!Array.isArray(e))return{unique:[],duplicates:[]};let n=new Set,r=[],i=[];for(let s of e){let u=t&&A(s)?d(s,t):s;n.has(u)?i.push(s):(n.add(u),r.push(s))}return{unique:r,duplicates:i}}function $(e){return Array.isArray(e)?e:e==null?[]:[e]}function Ee(e){return $(e)}function We(e,t,n=1){if(!Array.isArray(e)||t<1||n<1)return[];let r=[];for(let i=0;i<=e.length-t;i+=n)r.push(e.slice(i,i+t));return r}function qe(e,t){if(!Array.isArray(e)||e.length===0)return;let n=e[0],r=typeof t=="function"?t(n):d(n,t);for(let i=1;i<e.length;i++){let s=e[i],u=typeof t=="function"?t(s):d(s,t);u<r&&(r=u,n=s)}return n}function Be(e,t){if(!Array.isArray(e)||e.length===0)return;let n=e[0],r=typeof t=="function"?t(n):d(n,t);for(let i=1;i<e.length;i++){let s=e[i],u=typeof t=="function"?t(s):d(s,t);u>r&&(r=u,n=s)}return n}function C(e,t){if(!Array.isArray(e)||e.length===0)return 0;let n=0;for(let r of e){let i=typeof t=="function"?t(r):d(r,t);typeof i=="number"&&!isNaN(i)&&(n+=i)}return n}function Ne(e,t){return!Array.isArray(e)||e.length===0?0:C(e,t)/e.length}function K(e){let t=new Map;if(!Array.isArray(e))return t;for(let n of e)t.set(n,(t.get(n)||0)+1);return t}function M(e,t=1){if(!Array.isArray(e)||e.length===0||t<1)return[];let n=K(e);return Array.from(n.entries()).sort((i,s)=>s[1]-i[1]).slice(0,t).map(([i])=>i)}function Le(e,t=1){if(!Array.isArray(e)||e.length===0||t<1)return[];let n=K(e);return Array.from(n.entries()).sort((i,s)=>i[1]-s[1]).slice(0,t).map(([i])=>i)}async function _e(e,t,n){if(!Array.isArray(e)||t<1)return;let r=v(e,t);for(let i=0;i<r.length;i++)await n(r[i],i)}async function ze(e,t,n,r){if(!Array.isArray(e)||t<1||n<1)return;let i=v(e,t),s=v(i.map((u,o)=>({batch:u,index:o})),n);for(let u of s)await Promise.all(u.map(({batch:o,index:a})=>r(o,a)))}function Je(e){if(!Array.isArray(e)||e.length===0)return[];let t=Math.max(...e.map(r=>r.length)),n=[];for(let r=0;r<t;r++){let i=[];for(let s=0;s<e.length;s++)r<e[s].length&&i.push(e[s][r]);n.push(i)}return n}function De(e,t){if(!Array.isArray(e)||!Array.isArray(t)||e.length!==t.length)return!1;for(let n=0;n<e.length;n++)if(e[n]!==t[n])return!1;return!0}function Qe(e,t){if(!Array.isArray(e)||!Array.isArray(t)||e.length!==t.length)return!1;let n=K(e),r=K(t);if(n.size!==r.size)return!1;for(let[i,s]of n)if(r.get(i)!==s)return!1;return!0}function U(e,t){if(!Array.isArray(e)||!Array.isArray(t))return!1;let n=new Set(t);return e.every(r=>n.has(r))}function Ze(e,t){return U(t,e)}function Ge(...e){if(e.length===0)return[];let t=Math.max(...e.map(r=>r.length)),n=[];for(let r=0;r<t;r++)for(let i of e)r<i.length&&n.push(i[r]);return n}function He(e,t){let n=[],r=[];if(!Array.isArray(e))return{pass:n,fail:r};for(let i=0;i<e.length;i++)t(e[i],i)?n.push(e[i]):r.push(e[i]);return{pass:n,fail:r}}function Xe(e,t){if(e<0)return[];let n=[];for(let r=0;r<e;r++)n.push(t(r));return n}function Ye(e,t){if(!Array.isArray(e)||t<0)return[];let n=[];for(let r=0;r<t;r++)n.push(...e);return n}function en(e){if(!Array.isArray(e)||e.length===0)return;let t=[...e].sort((r,i)=>r-i),n=Math.floor(t.length/2);return t.length%2===0?(t[n-1]+t[n])/2:t[n]}function nn(e){let t=M(e,1);return t.length>0?t[0]:void 0}function tn(e,...t){if(!Array.isArray(e))return[];let n=new Set(t);return e.filter(r=>!n.has(r))}function rn(e,...t){if(!Array.isArray(e))return[];let n=new Set(t.flat());return e.filter(r=>!n.has(r))}function sn(e){if(!Array.isArray(e)||e.length===0)return[];let t=[],n=[e[0]];for(let r=1;r<e.length;r++)e[r]===e[r-1]?n.push(e[r]):(t.push(n),n=[e[r]]);return t.push(n),t}function on(e){if(!Array.isArray(e)||e.length===0)return[];let t=[e[0]];for(let n=1;n<e.length;n++)e[n]!==e[n-1]&&t.push(e[n]);return t}function un(e,t="standard"){if(!Array.isArray(e)||e.length===0)return[];let n=e.map((i,s)=>({value:i,index:s}));n.sort((i,s)=>i.value-s.value);let r=new Array(e.length);if(t==="ordinal")for(let i=0;i<n.length;i++)r[n[i].index]=i+1;else if(t==="dense"){let i=1;r[n[0].index]=i;for(let s=1;s<n.length;s++)n[s].value!==n[s-1].value&&i++,r[n[s].index]=i}else{let i=0;for(;i<n.length;){let s=i;for(;s<n.length&&n[s].value===n[i].value;)s++;let u=i+1;for(let o=i;o<s;o++)r[n[o].index]=u;i=s}}return r}function P(e,t){if(!Array.isArray(e)||e.length===0||t<0||t>100)return;let n=[...e].sort((o,a)=>o-a),r=t/100*(n.length-1),i=Math.floor(r),s=Math.ceil(r),u=r-i;return i===s?n[i]:n[i]*(1-u)+n[s]*u}function an(e){if(!Array.isArray(e)||e.length===0)return;let t=P(e,25),n=P(e,50),r=P(e,75);return{q1:t,q2:n,q3:r,iqr:r-t}}function F(e,t=!1){if(!Array.isArray(e)||e.length===0||t&&e.length===1)return;let n=e.reduce((s,u)=>s+u,0)/e.length,i=e.map(s=>Math.pow(s-n,2)).reduce((s,u)=>s+u,0)/(t?e.length-1:e.length);return Math.sqrt(i)}function fn(e,t=!1){let n=F(e,t);return n!==void 0?Math.pow(n,2):void 0}function ln(e){if(!Array.isArray(e)||e.length===0)return[];let t=Math.min(...e),r=Math.max(...e)-t;return r===0?e.map(()=>0):e.map(i=>(i-t)/r)}function cn(e){if(!Array.isArray(e)||e.length===0)return[];let t=e.reduce((r,i)=>r+i,0)/e.length,n=F(e);return n===void 0||n===0?e.map(()=>0):e.map(r=>(r-t)/n)}function E(e,t,n){if(!Array.isArray(e))return[];let r=[],i=n;for(let s=0;s<e.length;s++)i=t(i,e[s],s),r.push(i);return r}function yn(e){return E(e,(t,n)=>t+n,0)}function pn(e){if(!Array.isArray(e)||e.length<2)return[];let t=[];for(let n=1;n<e.length;n++)t.push(e[n]-e[n-1]);return t}function dn(e,t,n,r="end"){if(!Array.isArray(e)||t<=e.length)return[...e];let i=new Array(t-e.length).fill(n);return r==="start"?[...i,...e]:[...e,...i]}function Tn(e,t){if(!Array.isArray(e)||e.length===0)return[];let n=0,r=e.length-1;for(;n<=r&&t(e[n]);)n++;for(;r>=n&&t(e[r]);)r--;return e.slice(n,r+1)}function An(e){if(!Array.isArray(e)||e.length<2)return[];let t=[];for(let n=0;n<e.length-1;n++)t.push([e[n],e[n+1]]);return t}function gn(e){return Array.isArray(e)?e.map((t,n)=>[n,t]):[]}function mn(e,t){return Array.isArray(e)?e.map((n,r)=>{let i=t[r%t.length];return i(n)}):[]}function W(e,t=1){if(!Array.isArray(e)||e.length===0)return[];let n=e.length,r=(t%n+n)%n;return[...e.slice(r),...e.slice(0,r)]}function xn(e,t=1){return W(e,-t)}function hn(e,t,n){if(!Array.isArray(e)||t<0||n<0||t>=e.length||n>=e.length)return[...e];let r=[...e];return[r[t],r[n]]=[r[n],r[t]],r}function bn(e,t,n){if(!Array.isArray(e)||t<0||n>=e.length||t>=n)return[...e];let r=[...e],i=r.slice(t,n+1).reverse();return r.splice(t,n-t+1,...i),r}function Sn(e,t){if(!Array.isArray(e)||e.length<=1)return!0;let n=t||((r,i)=>r<i?-1:r>i?1:0);for(let r=1;r<e.length;r++)if(n(e[r-1],e[r])>0)return!1;return!0}function Kn(e){return Array.isArray(e)?new Set(e).size!==e.length:!1}function wn(e,t){return Array.isArray(e)?e.map(n=>{let r={};for(let i of t)i in n&&(r[i]=n[i]);return r}):[]}function q(e,t,n){if(!Array.isArray(e))return{};let r={};for(let i=0;i<e.length;i++){let s=t(e[i],i),u=n?n(e[i],i):e[i];r[s]=u}return r}async function vn(e,t){if(!Array.isArray(e))return[];let n=[];for(let r=0;r<e.length;r++)n.push(await t(e[r],r));return n}async function On(e,t){return Array.isArray(e)?Promise.all(e.map((n,r)=>t(n,r))):[]}async function Pn(e,t){if(!Array.isArray(e))return[];let n=[];for(let r=0;r<e.length;r++)await t(e[r],r)&&n.push(e[r]);return n}async function In(e,t,n){if(!Array.isArray(e))return n;let r=n;for(let i=0;i<e.length;i++)r=await t(r,e[i],i);return r}async function Fn(e,t){if(Array.isArray(e)){for(let n=0;n<e.length;n++)if(await t(e[n],n))return e[n]}}async function jn(e,t){if(!Array.isArray(e))return!1;for(let n=0;n<e.length;n++)if(await t(e[n],n))return!0;return!1}async function kn(e,t){if(!Array.isArray(e))return!0;for(let n=0;n<e.length;n++)if(!await t(e[n],n))return!1;return!0}function Vn(e,t,n){if(!Array.isArray(e)||e.length===0)return 0;let r=0,i=0;for(let s of e){let u=typeof t=="function"?t(s):d(s,t),o=typeof n=="function"?n(s):d(s,n);typeof u=="number"&&typeof o=="number"&&(i+=u*o,r+=o)}return r===0?0:i/r}function Rn(e,t,n=1){if(!Array.isArray(e)||e.length===0||n<1)return[];let r=[],i=0;for(let u of e){let o=typeof t=="function"?t(u):d(u,t);i+=typeof o=="number"&&o>0?o:0,r.push(i)}if(i===0)return[];let s=[];for(let u=0;u<n;u++){let o=Math.random()*i,a=r.findIndex(f=>o<f);a!==-1&&s.push(e[a])}return s}function $n(e,t){if(!Array.isArray(e)||!Array.isArray(t))return[];let n=[],r=0;for(let i of t){if(r>=e.length)break;n.push(e.slice(r,r+i)),r+=i}return n}function Cn(e,t){if(!Array.isArray(e)||t<1)return[];let n=K(e),r=[];for(let i of e)n.get(i)===t&&r.push(i);return r}function Mn(e,t,n){if(!Array.isArray(e)||t<1)return[];let r=[],i=0,s=[];for(let u of e)n(u)?(i++,s.push(u),i>=t&&r.push(...s.splice(0,s.length-t+1))):(i=0,s.length=0);return i>=t&&r.push(...s),r}function Un(e,t,n){if(!Array.isArray(e))return[];let r=[],i=n;for(let s=0;s<e.length;s++){let{value:u,newContext:o}=t(e[s],i,s);r.push(u),i=o}return r}function En(e,t){return v(e,t)}function Wn(e,t,n){if(!Array.isArray(e)||!Array.isArray(t))return[];let r=Math.min(e.length,t.length),i=[];for(let s=0;s<r;s++)i.push(n(e[s],t[s]));return i}function qn(e,t,n="last"){if(!Array.isArray(e)||e.length===0)return[];let r=new Map;for(let s of e)if(Array.isArray(s))for(let u of s){let o=d(u,t);if(n==="first")r.has(o)||r.set(o,u);else if(n==="last")r.set(o,u);else if(n==="concat"){let a=r.get(o);Array.isArray(a)?a.push(u):a?r.set(o,[a,u]):r.set(o,u)}else if(typeof n=="function"){let a=r.get(o);a&&!Array.isArray(a)?r.set(o,n(a,u)):r.set(o,u)}}let i=[];for(let s of r.values())Array.isArray(s)&&n==="concat"?i.push(...s):Array.isArray(s)||i.push(s);return i}function Bn(e,t){return q(e,n=>String(d(n,t)))}function Nn(e,t,n){if(!Array.isArray(e)||e.length===0)return-1;let r=n||((u,o)=>u<o?-1:u>o?1:0),i=0,s=e.length-1;for(;i<=s;){let u=Math.floor((i+s)/2),o=r(e[u],t);if(o===0)return u;o<0?i=u+1:s=u-1}return-1}function Ln(e,t,n){if(!Array.isArray(e))return[t];let r=n||((u,o)=>u<o?-1:u>o?1:0),i=[...e],s=0;for(;s<i.length&&r(i[s],t)<0;)s++;return i.splice(s,0,t),i}
|
|
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/array/advanced.ts
|
|
21
|
+
var advanced_exports = {};
|
|
22
|
+
__export(advanced_exports, {
|
|
23
|
+
applyByIndex: () => applyByIndex,
|
|
24
|
+
avgBy: () => avgBy,
|
|
25
|
+
batch: () => batch,
|
|
26
|
+
batchProcess: () => batchProcess,
|
|
27
|
+
batchProcessParallel: () => batchProcessParallel,
|
|
28
|
+
bifurcate: () => bifurcate,
|
|
29
|
+
binarySearch: () => binarySearch,
|
|
30
|
+
cartesian: () => cartesian,
|
|
31
|
+
chunk: () => chunk,
|
|
32
|
+
chunkBy: () => chunkBy,
|
|
33
|
+
compact: () => compact,
|
|
34
|
+
countBy: () => countBy,
|
|
35
|
+
cumulativeSum: () => cumulativeSum,
|
|
36
|
+
dedupeConsecutive: () => dedupeConsecutive,
|
|
37
|
+
deepCloneArray: () => deepCloneArray,
|
|
38
|
+
diff: () => diff,
|
|
39
|
+
difference: () => difference,
|
|
40
|
+
differenceWith: () => differenceWith,
|
|
41
|
+
drop: () => drop,
|
|
42
|
+
dropWhile: () => dropWhile,
|
|
43
|
+
ensureArray: () => ensureArray,
|
|
44
|
+
enumerate: () => enumerate,
|
|
45
|
+
everyAsync: () => everyAsync,
|
|
46
|
+
fill: () => fill,
|
|
47
|
+
filterAsync: () => filterAsync,
|
|
48
|
+
filterCompact: () => filterCompact,
|
|
49
|
+
filterConsecutive: () => filterConsecutive,
|
|
50
|
+
findAllIndices: () => findAllIndices,
|
|
51
|
+
findAsync: () => findAsync,
|
|
52
|
+
findIndex: () => findIndex,
|
|
53
|
+
findLastIndex: () => findLastIndex,
|
|
54
|
+
flatMap: () => flatMap,
|
|
55
|
+
flatten: () => flatten,
|
|
56
|
+
flattenDeep: () => flattenDeep,
|
|
57
|
+
frequencies: () => frequencies,
|
|
58
|
+
generate: () => generate,
|
|
59
|
+
groupBy: () => groupBy,
|
|
60
|
+
groupConsecutive: () => groupConsecutive,
|
|
61
|
+
hasDuplicates: () => hasDuplicates,
|
|
62
|
+
hasSameElements: () => hasSameElements,
|
|
63
|
+
indexBy: () => indexBy,
|
|
64
|
+
insert: () => insert,
|
|
65
|
+
insertSorted: () => insertSorted,
|
|
66
|
+
interleave: () => interleave,
|
|
67
|
+
intersection: () => intersection,
|
|
68
|
+
isArrayEqual: () => isArrayEqual,
|
|
69
|
+
isEqual: () => isEqual,
|
|
70
|
+
isSorted: () => isSorted,
|
|
71
|
+
isSubset: () => isSubset,
|
|
72
|
+
isSuperset: () => isSuperset,
|
|
73
|
+
keyBy: () => keyBy,
|
|
74
|
+
leastCommon: () => leastCommon,
|
|
75
|
+
mapAsync: () => mapAsync,
|
|
76
|
+
mapAsyncParallel: () => mapAsyncParallel,
|
|
77
|
+
mapWithContext: () => mapWithContext,
|
|
78
|
+
maxBy: () => maxBy,
|
|
79
|
+
median: () => median,
|
|
80
|
+
mergeArraysByKey: () => mergeArraysByKey,
|
|
81
|
+
mergeByKey: () => mergeByKey,
|
|
82
|
+
minBy: () => minBy,
|
|
83
|
+
mode: () => mode,
|
|
84
|
+
mostCommon: () => mostCommon,
|
|
85
|
+
move: () => move,
|
|
86
|
+
normalize: () => normalize,
|
|
87
|
+
omit: () => omit,
|
|
88
|
+
pad: () => pad,
|
|
89
|
+
pairwise: () => pairwise,
|
|
90
|
+
partition: () => partition,
|
|
91
|
+
percentile: () => percentile,
|
|
92
|
+
pick: () => pick,
|
|
93
|
+
pickMany: () => pickMany,
|
|
94
|
+
pluck: () => pluck,
|
|
95
|
+
quartiles: () => quartiles,
|
|
96
|
+
range: () => range,
|
|
97
|
+
rank: () => rank,
|
|
98
|
+
reduceAsync: () => reduceAsync,
|
|
99
|
+
reduceRight: () => reduceRight,
|
|
100
|
+
remove: () => remove,
|
|
101
|
+
repeat: () => repeat,
|
|
102
|
+
replace: () => replace,
|
|
103
|
+
reverseSegment: () => reverseSegment,
|
|
104
|
+
rotate: () => rotate,
|
|
105
|
+
rotateLeft: () => rotateLeft,
|
|
106
|
+
rotateRight: () => rotateRight,
|
|
107
|
+
sample: () => sample,
|
|
108
|
+
sampleSize: () => sampleSize,
|
|
109
|
+
scan: () => scan,
|
|
110
|
+
segment: () => segment,
|
|
111
|
+
selectByCount: () => selectByCount,
|
|
112
|
+
shuffle: () => shuffle,
|
|
113
|
+
slidingWindow: () => slidingWindow,
|
|
114
|
+
someAsync: () => someAsync,
|
|
115
|
+
sortBy: () => sortBy,
|
|
116
|
+
splitDuplicates: () => splitDuplicates,
|
|
117
|
+
standardDeviation: () => standardDeviation,
|
|
118
|
+
standardize: () => standardize,
|
|
119
|
+
sumBy: () => sumBy,
|
|
120
|
+
swap: () => swap,
|
|
121
|
+
take: () => take,
|
|
122
|
+
takeLast: () => takeLast,
|
|
123
|
+
takeWhile: () => takeWhile,
|
|
124
|
+
toObject: () => toObject,
|
|
125
|
+
transpose: () => transpose,
|
|
126
|
+
trim: () => trim,
|
|
127
|
+
union: () => union,
|
|
128
|
+
unique: () => unique,
|
|
129
|
+
unzip: () => unzip,
|
|
130
|
+
updateBy: () => updateBy,
|
|
131
|
+
variance: () => variance,
|
|
132
|
+
weightedAverage: () => weightedAverage,
|
|
133
|
+
weightedSample: () => weightedSample,
|
|
134
|
+
without: () => without,
|
|
135
|
+
wrap: () => wrap,
|
|
136
|
+
zip: () => zip,
|
|
137
|
+
zipWith: () => zipWith
|
|
138
|
+
});
|
|
139
|
+
module.exports = __toCommonJS(advanced_exports);
|
|
140
|
+
|
|
141
|
+
// src/lib/object/utils.ts
|
|
142
|
+
var isObject = (value) => {
|
|
143
|
+
if (value == null) return false;
|
|
144
|
+
if (Array.isArray(value)) return false;
|
|
145
|
+
if (typeof value === "function") return false;
|
|
146
|
+
if (value instanceof Date) return false;
|
|
147
|
+
if (value instanceof RegExp) return false;
|
|
148
|
+
if (value instanceof Map) return false;
|
|
149
|
+
if (value instanceof Set) return false;
|
|
150
|
+
if (value instanceof Error) return false;
|
|
151
|
+
return typeof value === "object";
|
|
152
|
+
};
|
|
153
|
+
var traverseStructure = (current, segments, depth, maxDepth) => {
|
|
154
|
+
if (depth > maxDepth) return void 0;
|
|
155
|
+
if (segments.length === 0) return current;
|
|
156
|
+
const segment2 = segments[0];
|
|
157
|
+
const remaining = segments.slice(1);
|
|
158
|
+
if (segment2.type === "descendant") {
|
|
159
|
+
const result = traverseStructure(current, remaining, depth + 1, maxDepth);
|
|
160
|
+
if (result !== void 0) {
|
|
161
|
+
return result;
|
|
162
|
+
}
|
|
163
|
+
if (current && typeof current === "object") {
|
|
164
|
+
if (Array.isArray(current)) {
|
|
165
|
+
const arrResult = [];
|
|
166
|
+
for (let i = 0; i < current.length; i++) {
|
|
167
|
+
const itemResult = traverseStructure(
|
|
168
|
+
current[i],
|
|
169
|
+
segments,
|
|
170
|
+
depth + 1,
|
|
171
|
+
maxDepth
|
|
172
|
+
);
|
|
173
|
+
if (itemResult !== void 0) {
|
|
174
|
+
arrResult[i] = itemResult;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return arrResult.length > 0 ? arrResult : void 0;
|
|
178
|
+
} else {
|
|
179
|
+
const objResult = {};
|
|
180
|
+
for (const key in current) {
|
|
181
|
+
if (current.hasOwnProperty(key)) {
|
|
182
|
+
const itemResult = traverseStructure(
|
|
183
|
+
current[key],
|
|
184
|
+
segments,
|
|
185
|
+
depth + 1,
|
|
186
|
+
maxDepth
|
|
187
|
+
);
|
|
188
|
+
if (itemResult !== void 0) {
|
|
189
|
+
objResult[key] = itemResult;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return Object.keys(objResult).length > 0 ? objResult : void 0;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return void 0;
|
|
197
|
+
}
|
|
198
|
+
switch (segment2.type) {
|
|
199
|
+
case "property":
|
|
200
|
+
if (isObject(current) && segment2.value && String(segment2.value) in current) {
|
|
201
|
+
const next = current[String(segment2.value)];
|
|
202
|
+
const result = traverseStructure(next, remaining, depth + 1, maxDepth);
|
|
203
|
+
if (result !== void 0) {
|
|
204
|
+
return { [String(segment2.value)]: result };
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
break;
|
|
208
|
+
case "index":
|
|
209
|
+
if (Array.isArray(current)) {
|
|
210
|
+
const index = segment2.value;
|
|
211
|
+
if (index >= 0 && index < current.length) {
|
|
212
|
+
const result = traverseStructure(
|
|
213
|
+
current[index],
|
|
214
|
+
remaining,
|
|
215
|
+
depth + 1,
|
|
216
|
+
maxDepth
|
|
217
|
+
);
|
|
218
|
+
if (result !== void 0) {
|
|
219
|
+
const arr = new Array(current.length).filter(
|
|
220
|
+
(a) => a !== void 0
|
|
221
|
+
);
|
|
222
|
+
arr[index] = result;
|
|
223
|
+
return arr;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
} else if (current && typeof current === "object") {
|
|
227
|
+
const key = String(segment2.value);
|
|
228
|
+
if (key in current) {
|
|
229
|
+
const result = traverseStructure(
|
|
230
|
+
current[key],
|
|
231
|
+
remaining,
|
|
232
|
+
depth + 1,
|
|
233
|
+
maxDepth
|
|
234
|
+
);
|
|
235
|
+
if (result !== void 0) {
|
|
236
|
+
return { [key]: result };
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
break;
|
|
241
|
+
case "wildcard":
|
|
242
|
+
if (current && typeof current === "object") {
|
|
243
|
+
if (Array.isArray(current)) {
|
|
244
|
+
const arrResult = [];
|
|
245
|
+
let hasResults = false;
|
|
246
|
+
for (let i = 0; i < current.length; i++) {
|
|
247
|
+
const result = traverseStructure(
|
|
248
|
+
current[i],
|
|
249
|
+
remaining,
|
|
250
|
+
depth + 1,
|
|
251
|
+
maxDepth
|
|
252
|
+
);
|
|
253
|
+
if (result !== void 0) {
|
|
254
|
+
arrResult[i] = result;
|
|
255
|
+
hasResults = true;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return hasResults ? arrResult : void 0;
|
|
259
|
+
} else {
|
|
260
|
+
const objResult = {};
|
|
261
|
+
let hasResults = false;
|
|
262
|
+
for (const key in current) {
|
|
263
|
+
if (current.hasOwnProperty(key)) {
|
|
264
|
+
const result = traverseStructure(
|
|
265
|
+
current[key],
|
|
266
|
+
remaining,
|
|
267
|
+
depth + 1,
|
|
268
|
+
maxDepth
|
|
269
|
+
);
|
|
270
|
+
if (result !== void 0) {
|
|
271
|
+
objResult[key] = result;
|
|
272
|
+
hasResults = true;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
return hasResults ? objResult : void 0;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
break;
|
|
280
|
+
case "slice":
|
|
281
|
+
if (Array.isArray(current)) {
|
|
282
|
+
const [start, end] = segment2.value;
|
|
283
|
+
const actualStart = Math.max(0, start);
|
|
284
|
+
const actualEnd = Math.min(
|
|
285
|
+
current.length,
|
|
286
|
+
end === Infinity ? current.length : end
|
|
287
|
+
);
|
|
288
|
+
const arrResult = [];
|
|
289
|
+
let hasResults = false;
|
|
290
|
+
for (let i = actualStart; i < actualEnd; i++) {
|
|
291
|
+
const result = traverseStructure(
|
|
292
|
+
current[i],
|
|
293
|
+
remaining,
|
|
294
|
+
depth + 1,
|
|
295
|
+
maxDepth
|
|
296
|
+
);
|
|
297
|
+
if (result !== void 0) {
|
|
298
|
+
arrResult[i] = result;
|
|
299
|
+
hasResults = true;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
return hasResults ? arrResult : void 0;
|
|
303
|
+
}
|
|
304
|
+
break;
|
|
305
|
+
case "union":
|
|
306
|
+
if (Array.isArray(current)) {
|
|
307
|
+
const indices = segment2.value;
|
|
308
|
+
const arrResult = [];
|
|
309
|
+
let hasResults = false;
|
|
310
|
+
for (const index of indices) {
|
|
311
|
+
if (index >= 0 && index < current.length) {
|
|
312
|
+
const result = traverseStructure(
|
|
313
|
+
current[index],
|
|
314
|
+
remaining,
|
|
315
|
+
depth + 1,
|
|
316
|
+
maxDepth
|
|
317
|
+
);
|
|
318
|
+
if (result !== void 0) {
|
|
319
|
+
arrResult[index] = result;
|
|
320
|
+
hasResults = true;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
return hasResults ? arrResult : void 0;
|
|
325
|
+
}
|
|
326
|
+
break;
|
|
327
|
+
case "filter":
|
|
328
|
+
if (Array.isArray(current)) {
|
|
329
|
+
const filterFn = segment2.value;
|
|
330
|
+
const arrResult = [];
|
|
331
|
+
let hasResults = false;
|
|
332
|
+
for (let i = 0; i < current.length; i++) {
|
|
333
|
+
if (filterFn(current[i])) {
|
|
334
|
+
const result = traverseStructure(
|
|
335
|
+
current[i],
|
|
336
|
+
remaining,
|
|
337
|
+
depth + 1,
|
|
338
|
+
maxDepth
|
|
339
|
+
);
|
|
340
|
+
if (result !== void 0) {
|
|
341
|
+
arrResult[i] = result;
|
|
342
|
+
hasResults = true;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return hasResults ? arrResult : void 0;
|
|
347
|
+
}
|
|
348
|
+
break;
|
|
349
|
+
}
|
|
350
|
+
return void 0;
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
// src/lib/object/clone.ts
|
|
354
|
+
var deepClone = (obj, skipKeys) => {
|
|
355
|
+
if (obj == null || typeof obj !== "object") {
|
|
356
|
+
return obj;
|
|
357
|
+
}
|
|
358
|
+
if (obj instanceof Date) {
|
|
359
|
+
return new Date(obj.getTime());
|
|
360
|
+
}
|
|
361
|
+
if (obj instanceof RegExp) {
|
|
362
|
+
return new RegExp(obj.source, obj.flags);
|
|
363
|
+
}
|
|
364
|
+
if (obj instanceof Map) {
|
|
365
|
+
const clone = /* @__PURE__ */ new Map();
|
|
366
|
+
obj.forEach((value, key) => {
|
|
367
|
+
if (!skipKeys || !skipKeys.some((k) => k.toString() === key.toString())) {
|
|
368
|
+
clone.set(key, deepClone(value));
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
return clone;
|
|
372
|
+
}
|
|
373
|
+
if (obj instanceof Set) {
|
|
374
|
+
const clone = /* @__PURE__ */ new Set();
|
|
375
|
+
obj.forEach((value) => {
|
|
376
|
+
clone.add(deepClone(value));
|
|
377
|
+
});
|
|
378
|
+
return clone;
|
|
379
|
+
}
|
|
380
|
+
if (Array.isArray(obj)) {
|
|
381
|
+
return Array.from(obj).map((item) => deepClone(item));
|
|
382
|
+
}
|
|
383
|
+
if (isObject(obj)) {
|
|
384
|
+
const clone = {};
|
|
385
|
+
for (const key in obj) {
|
|
386
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
387
|
+
if (!skipKeys || !skipKeys.some((k) => k.toString() === key.toString())) {
|
|
388
|
+
clone[key] = deepClone(obj[key]);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
const symbols = Object.getOwnPropertySymbols(obj);
|
|
393
|
+
for (const symbol of symbols) {
|
|
394
|
+
if (!skipKeys || !skipKeys.some((s) => s.toString() === symbol.toString())) {
|
|
395
|
+
clone[symbol] = deepClone(obj[symbol]);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
return clone;
|
|
399
|
+
}
|
|
400
|
+
return obj;
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
// src/lib/object/selector.ts
|
|
404
|
+
var parseSelector = (selector, options = {}) => {
|
|
405
|
+
const { type = "auto", throwOnError = false } = options;
|
|
406
|
+
if (!selector || typeof selector !== "string") {
|
|
407
|
+
return [];
|
|
408
|
+
}
|
|
409
|
+
const segments = [];
|
|
410
|
+
let current = "";
|
|
411
|
+
let inBrackets = false;
|
|
412
|
+
let inQuotes = false;
|
|
413
|
+
let quoteChar = "";
|
|
414
|
+
let bracketContent = "";
|
|
415
|
+
let bracketDepth = 0;
|
|
416
|
+
const detectType = (str) => {
|
|
417
|
+
if (str.includes("..")) return "descendant";
|
|
418
|
+
if (str.includes("[?(")) return "filter";
|
|
419
|
+
if (str.includes("[:") || str.includes("[0:")) return "slice";
|
|
420
|
+
if (str.includes("[,") || str.includes("[0,1]")) return "union";
|
|
421
|
+
if (str.includes("[*]") || str.includes(".*.")) return "wildcard";
|
|
422
|
+
if (str.includes("[]")) return "array-wildcard";
|
|
423
|
+
if (str.includes("[") && str.includes("]")) return "bracket";
|
|
424
|
+
return "dot";
|
|
425
|
+
};
|
|
426
|
+
const actualType = type === "auto" ? detectType(selector) : type;
|
|
427
|
+
const parseBracketContent = (content) => {
|
|
428
|
+
const trimmed = content.trim();
|
|
429
|
+
if (trimmed === "*" || trimmed === "") {
|
|
430
|
+
return {
|
|
431
|
+
type: "wildcard",
|
|
432
|
+
value: trimmed === "" ? "[]" : "*",
|
|
433
|
+
raw: trimmed === "" ? "[]" : content
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
if (trimmed === "[]") {
|
|
437
|
+
return { type: "wildcard", value: "[]", raw: content };
|
|
438
|
+
}
|
|
439
|
+
if (/^-?\d+$/.test(trimmed)) {
|
|
440
|
+
return { type: "index", value: parseInt(trimmed, 10), raw: content };
|
|
441
|
+
}
|
|
442
|
+
if (trimmed.includes(":")) {
|
|
443
|
+
const parts = trimmed.split(":").map((p) => p.trim());
|
|
444
|
+
const start = parts[0] === "" ? 0 : parseInt(parts[0], 10);
|
|
445
|
+
const end = parts[1] === "" ? Infinity : parseInt(parts[1], 10);
|
|
446
|
+
if (isNaN(start) || isNaN(end)) {
|
|
447
|
+
if (throwOnError) throw new Error(`Invalid slice notation: ${content}`);
|
|
448
|
+
return { type: "wildcard", value: "*", raw: content };
|
|
449
|
+
}
|
|
450
|
+
return { type: "slice", value: [start, end], raw: content };
|
|
451
|
+
}
|
|
452
|
+
if (trimmed.includes(",")) {
|
|
453
|
+
const indices = trimmed.split(",").map((p) => {
|
|
454
|
+
const num = parseInt(p.trim(), 10);
|
|
455
|
+
return isNaN(num) ? -1 : num;
|
|
456
|
+
}).filter((n) => n >= 0);
|
|
457
|
+
if (indices.length === 0) {
|
|
458
|
+
if (throwOnError) throw new Error(`Invalid union notation: ${content}`);
|
|
459
|
+
return { type: "wildcard", value: "*", raw: content };
|
|
460
|
+
}
|
|
461
|
+
return { type: "union", value: indices, raw: content };
|
|
462
|
+
}
|
|
463
|
+
if (trimmed.startsWith("?(") && trimmed.endsWith(")")) {
|
|
464
|
+
const expr = trimmed.slice(2, -1);
|
|
465
|
+
try {
|
|
466
|
+
const filterFn = createFilterFunction(expr);
|
|
467
|
+
return { type: "filter", value: filterFn, raw: content };
|
|
468
|
+
} catch (error) {
|
|
469
|
+
if (throwOnError) throw error;
|
|
470
|
+
return { type: "wildcard", value: "*", raw: content };
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
if (trimmed.startsWith('"') && trimmed.endsWith('"') || trimmed.startsWith("'") && trimmed.endsWith("'")) {
|
|
474
|
+
return { type: "property", value: trimmed.slice(1, -1), raw: content };
|
|
475
|
+
}
|
|
476
|
+
return { type: "property", value: trimmed, raw: content };
|
|
477
|
+
};
|
|
478
|
+
for (let i = 0; i < selector.length; i++) {
|
|
479
|
+
const char = selector[i];
|
|
480
|
+
const nextChar = selector[i + 1];
|
|
481
|
+
if ((char === '"' || char === "'") && !inQuotes) {
|
|
482
|
+
inQuotes = true;
|
|
483
|
+
quoteChar = char;
|
|
484
|
+
continue;
|
|
485
|
+
} else if (char === quoteChar && inQuotes) {
|
|
486
|
+
inQuotes = false;
|
|
487
|
+
quoteChar = "";
|
|
488
|
+
if (!(char === '"' || char === "'")) {
|
|
489
|
+
if (inBrackets) {
|
|
490
|
+
bracketContent += char;
|
|
491
|
+
} else {
|
|
492
|
+
current += char;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
continue;
|
|
496
|
+
}
|
|
497
|
+
if (inQuotes) {
|
|
498
|
+
if (inBrackets) {
|
|
499
|
+
bracketContent += char;
|
|
500
|
+
} else {
|
|
501
|
+
current += char;
|
|
502
|
+
}
|
|
503
|
+
continue;
|
|
504
|
+
}
|
|
505
|
+
if (char === "[" && !inBrackets) {
|
|
506
|
+
if (current) {
|
|
507
|
+
segments.push({ type: "property", value: current, raw: current });
|
|
508
|
+
current = "";
|
|
509
|
+
}
|
|
510
|
+
inBrackets = true;
|
|
511
|
+
bracketContent = "";
|
|
512
|
+
continue;
|
|
513
|
+
}
|
|
514
|
+
if (char === "]" && inBrackets && !inQuotes) {
|
|
515
|
+
inBrackets = false;
|
|
516
|
+
const segment2 = parseBracketContent(bracketContent);
|
|
517
|
+
segments.push(segment2);
|
|
518
|
+
continue;
|
|
519
|
+
}
|
|
520
|
+
let lastDot = segments.length > 0 ? segments[segments.length - 1] : null;
|
|
521
|
+
if (char === "." && !inBrackets && !inQuotes) {
|
|
522
|
+
if (nextChar === ".") {
|
|
523
|
+
const l = selector.split("..")[0];
|
|
524
|
+
segments.push({ type: "property", value: l, raw: l });
|
|
525
|
+
segments.push({ type: "descendant", value: "..", raw: ".." });
|
|
526
|
+
i++;
|
|
527
|
+
} else if (current) {
|
|
528
|
+
if (current === "*") {
|
|
529
|
+
segments.push({ type: "wildcard", value: current, raw: current });
|
|
530
|
+
} else {
|
|
531
|
+
segments.push({ type: "property", value: current, raw: current });
|
|
532
|
+
}
|
|
533
|
+
current = "";
|
|
534
|
+
}
|
|
535
|
+
continue;
|
|
536
|
+
}
|
|
537
|
+
if (inBrackets) {
|
|
538
|
+
bracketContent += char;
|
|
539
|
+
} else {
|
|
540
|
+
if (lastDot && lastDot.type !== "descendant") {
|
|
541
|
+
current += char;
|
|
542
|
+
} else {
|
|
543
|
+
const l = selector.split(lastDot?.raw)[1];
|
|
544
|
+
if (l !== void 0) {
|
|
545
|
+
current = l;
|
|
546
|
+
} else {
|
|
547
|
+
current += char;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
if (current) {
|
|
553
|
+
segments.push({ type: "property", value: current, raw: current });
|
|
554
|
+
}
|
|
555
|
+
return segments.map((segment2) => {
|
|
556
|
+
if (segment2.type === "property" && typeof segment2.value === "string") {
|
|
557
|
+
if (/^-?\d+$/.test(segment2.value)) {
|
|
558
|
+
const num = parseInt(segment2.value, 10);
|
|
559
|
+
if (!isNaN(num)) {
|
|
560
|
+
return { ...segment2, type: "index", value: num };
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
return segment2;
|
|
565
|
+
});
|
|
566
|
+
};
|
|
567
|
+
var createFilterFunction = (expr) => {
|
|
568
|
+
const safeExpr = expr.replace(/@\.([a-zA-Z_$][\w$]*)/g, (_, prop) => `item.${prop}`).replace(/@/g, "item");
|
|
569
|
+
try {
|
|
570
|
+
return new Function("item", `return ${safeExpr}`);
|
|
571
|
+
} catch (error) {
|
|
572
|
+
throw new Error(`Invalid filter expression: ${expr}`);
|
|
573
|
+
}
|
|
574
|
+
};
|
|
575
|
+
|
|
576
|
+
// src/lib/object/merge.ts
|
|
577
|
+
var mergeIntoStructure = (target, selector, value) => {
|
|
578
|
+
const segments = parseSelector(selector);
|
|
579
|
+
let current = target;
|
|
580
|
+
for (let i = 0; i < segments.length; i++) {
|
|
581
|
+
const segment2 = segments[i];
|
|
582
|
+
const isLast = i === segments.length - 1;
|
|
583
|
+
switch (segment2.type) {
|
|
584
|
+
case "property":
|
|
585
|
+
if (isLast) {
|
|
586
|
+
current[String(segment2.value)] = value;
|
|
587
|
+
} else {
|
|
588
|
+
if (!current[String(segment2.value)]) {
|
|
589
|
+
const nextSegment = segments[i + 1];
|
|
590
|
+
current[String(segment2.value)] = nextSegment.type === "index" || nextSegment.type === "slice" || nextSegment.type === "union" || nextSegment.type === "wildcard" ? [] : {};
|
|
591
|
+
}
|
|
592
|
+
current = current[String(segment2.value)];
|
|
593
|
+
}
|
|
594
|
+
break;
|
|
595
|
+
case "index":
|
|
596
|
+
if (!Array.isArray(current)) {
|
|
597
|
+
current = [];
|
|
598
|
+
}
|
|
599
|
+
const index = segment2.value;
|
|
600
|
+
if (isLast) {
|
|
601
|
+
if (index >= current.length) {
|
|
602
|
+
for (let j = current.length; j <= index; j++) {
|
|
603
|
+
current.push(void 0);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
current[index] = value;
|
|
607
|
+
} else {
|
|
608
|
+
if (!current[index]) {
|
|
609
|
+
const nextSegment = segments[i + 1];
|
|
610
|
+
current[index] = nextSegment.type === "index" || nextSegment.type === "slice" || nextSegment.type === "union" || nextSegment.type === "wildcard" ? [] : {};
|
|
611
|
+
}
|
|
612
|
+
current = current[index];
|
|
613
|
+
}
|
|
614
|
+
break;
|
|
615
|
+
case "wildcard":
|
|
616
|
+
if (Array.isArray(current)) {
|
|
617
|
+
if (isLast) {
|
|
618
|
+
for (let j = 0; j < current.length; j++) {
|
|
619
|
+
current[j] = value;
|
|
620
|
+
}
|
|
621
|
+
} else {
|
|
622
|
+
const nextSegment = segments[i + 1];
|
|
623
|
+
for (let j = 0; j < current.length; j++) {
|
|
624
|
+
if (!current[j]) {
|
|
625
|
+
current[j] = nextSegment.type === "index" || nextSegment.type === "slice" || nextSegment.type === "union" || nextSegment.type === "wildcard" ? [] : {};
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
if (current.length > 0) {
|
|
629
|
+
current = current[0];
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
break;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
};
|
|
637
|
+
|
|
638
|
+
// src/lib/object/getter-setter.ts
|
|
639
|
+
function get(obj, selector, options = {}) {
|
|
640
|
+
const {
|
|
641
|
+
preserveStructure,
|
|
642
|
+
multiple,
|
|
643
|
+
throwOnError = false,
|
|
644
|
+
defaultValue,
|
|
645
|
+
maxDepth = 20
|
|
646
|
+
} = options;
|
|
647
|
+
if (!obj || typeof obj !== "object") {
|
|
648
|
+
return defaultValue;
|
|
649
|
+
}
|
|
650
|
+
if (!selector) {
|
|
651
|
+
return obj;
|
|
652
|
+
}
|
|
653
|
+
if (Array.isArray(selector)) {
|
|
654
|
+
if (preserveStructure) {
|
|
655
|
+
const result2 = {};
|
|
656
|
+
for (const sel of selector) {
|
|
657
|
+
const value = get(obj, sel, { ...options, preserveStructure: false });
|
|
658
|
+
if (value !== void 0) {
|
|
659
|
+
mergeIntoStructure(result2, sel, value);
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
return result2;
|
|
663
|
+
} else {
|
|
664
|
+
return selector.map(
|
|
665
|
+
(sel) => get(obj, sel, { ...options, preserveStructure: false })
|
|
666
|
+
);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
const segments = parseSelector(selector, { ...options, throwOnError });
|
|
670
|
+
if (segments.length === 0) {
|
|
671
|
+
return obj;
|
|
672
|
+
}
|
|
673
|
+
if (typeof preserveStructure === "undefined" || preserveStructure === false) {
|
|
674
|
+
const v = ValueOfKey(obj, selector);
|
|
675
|
+
if (multiple === false && Array.isArray(v)) {
|
|
676
|
+
return v[0];
|
|
677
|
+
}
|
|
678
|
+
if (v === void 0 && defaultValue !== void 0) {
|
|
679
|
+
return defaultValue;
|
|
680
|
+
}
|
|
681
|
+
return v;
|
|
682
|
+
}
|
|
683
|
+
const result = traverseStructure(obj, segments, 0, maxDepth);
|
|
684
|
+
if (result === void 0 && defaultValue !== void 0) {
|
|
685
|
+
return defaultValue;
|
|
686
|
+
}
|
|
687
|
+
return result;
|
|
688
|
+
}
|
|
689
|
+
function ValueOfKey(obj, selector, options = {}) {
|
|
690
|
+
const { createMissing = false, add = false, debug = false } = options;
|
|
691
|
+
const segments = parseSelector(selector);
|
|
692
|
+
let current = obj;
|
|
693
|
+
let updateEvenIfNotlast = false;
|
|
694
|
+
let chained_property = "";
|
|
695
|
+
const stringify = selector;
|
|
696
|
+
if (typeof current === "object" && !Array.isArray(current) && stringify.length > 0) {
|
|
697
|
+
if (current[stringify]) {
|
|
698
|
+
current = current[stringify];
|
|
699
|
+
return current;
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
for (let i = 0; i < segments.length; i++) {
|
|
703
|
+
const segment2 = segments[i];
|
|
704
|
+
const isLast = segments.length > 1 ? i === segments.length - 1 : false;
|
|
705
|
+
if (current === null && !createMissing) {
|
|
706
|
+
return void 0;
|
|
707
|
+
}
|
|
708
|
+
const nextSegment = segments[i + 1];
|
|
709
|
+
switch (segment2.type) {
|
|
710
|
+
case "property":
|
|
711
|
+
if (current && typeof current === "object") {
|
|
712
|
+
if (Array.isArray(current)) {
|
|
713
|
+
if (typeof segment2.value === "number") {
|
|
714
|
+
const index = segment2.value;
|
|
715
|
+
if (createMissing && index >= current.length) {
|
|
716
|
+
for (let j = current.length; j <= index; j++) {
|
|
717
|
+
current.push(void 0);
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
if (createMissing && (current[index] === null || current[index] === void 0) && (!isLast || updateEvenIfNotlast)) {
|
|
721
|
+
if (nextSegment) {
|
|
722
|
+
current[index] = nextSegment.type === "index" || nextSegment.type === "slice" || nextSegment.type === "union" || nextSegment.type === "wildcard" ? [] : {};
|
|
723
|
+
} else {
|
|
724
|
+
current[index] = null;
|
|
725
|
+
}
|
|
726
|
+
updateEvenIfNotlast = false;
|
|
727
|
+
}
|
|
728
|
+
current = current[index];
|
|
729
|
+
if (nextSegment) {
|
|
730
|
+
updateEvenIfNotlast = true;
|
|
731
|
+
continue;
|
|
732
|
+
}
|
|
733
|
+
} else if (add) {
|
|
734
|
+
current.push({
|
|
735
|
+
[String(segment2.value)]: null
|
|
736
|
+
});
|
|
737
|
+
} else {
|
|
738
|
+
current = current.map((item) => item[String(segment2.value)]);
|
|
739
|
+
}
|
|
740
|
+
if (nextSegment) {
|
|
741
|
+
continue;
|
|
742
|
+
}
|
|
743
|
+
return filterDefined(current);
|
|
744
|
+
} else {
|
|
745
|
+
if (current[chained_property] && chained_property.split(".").length >= 2) {
|
|
746
|
+
current = current[chained_property];
|
|
747
|
+
continue;
|
|
748
|
+
}
|
|
749
|
+
if (createMissing && (current[String(segment2.value)] === null || current[String(segment2.value)] === void 0) && (!isLast || updateEvenIfNotlast)) {
|
|
750
|
+
if (nextSegment) {
|
|
751
|
+
current[String(segment2.value)] = nextSegment.type === "index" || nextSegment.type === "slice" || nextSegment.type === "union" || nextSegment.type === "wildcard" ? [] : {};
|
|
752
|
+
} else {
|
|
753
|
+
current[String(segment2.value)] = null;
|
|
754
|
+
}
|
|
755
|
+
updateEvenIfNotlast = false;
|
|
756
|
+
current = current[String(segment2.value)];
|
|
757
|
+
if (nextSegment) {
|
|
758
|
+
updateEvenIfNotlast = true;
|
|
759
|
+
continue;
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
if (!current) {
|
|
763
|
+
current = { [String(segment2.value)]: null };
|
|
764
|
+
} else {
|
|
765
|
+
if (!current[String(segment2.value)] && createMissing) {
|
|
766
|
+
current[String(segment2.value)] = null;
|
|
767
|
+
}
|
|
768
|
+
current = current[String(segment2.value)];
|
|
769
|
+
}
|
|
770
|
+
if (nextSegment) {
|
|
771
|
+
continue;
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
} else if (createMissing) {
|
|
775
|
+
current = {};
|
|
776
|
+
current[String(segment2.value)] = nextSegment.type === "index" || nextSegment.type === "slice" || nextSegment.type === "union" || nextSegment.type === "wildcard" || options.type === "array" ? [] : {};
|
|
777
|
+
current = current[String(segment2.value)];
|
|
778
|
+
} else {
|
|
779
|
+
return void 0;
|
|
780
|
+
}
|
|
781
|
+
break;
|
|
782
|
+
case "index":
|
|
783
|
+
case "wildcard":
|
|
784
|
+
if (Array.isArray(current)) {
|
|
785
|
+
if (segment2.type === "index") {
|
|
786
|
+
const index = segment2.value;
|
|
787
|
+
if (index < 0) {
|
|
788
|
+
current = current[current.length - Math.abs(index)];
|
|
789
|
+
continue;
|
|
790
|
+
}
|
|
791
|
+
if (createMissing && (index >= current.length || current[index] == null || current[index] == void 0) && !isLast) {
|
|
792
|
+
for (let j = current.length; j <= index; j++) {
|
|
793
|
+
current.push(void 0);
|
|
794
|
+
}
|
|
795
|
+
if (nextSegment) {
|
|
796
|
+
current[index] = nextSegment.type === "index" || nextSegment.type === "slice" || nextSegment.type === "union" || nextSegment.type === "wildcard" ? [] : {};
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
current = current[index];
|
|
800
|
+
} else {
|
|
801
|
+
if (!isLast) {
|
|
802
|
+
continue;
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
} else if (createMissing) {
|
|
806
|
+
const nextSegment2 = segments[i + 1];
|
|
807
|
+
current = [];
|
|
808
|
+
const index = segment2.value;
|
|
809
|
+
for (let j = 0; j <= index; j++) {
|
|
810
|
+
current.push(void 0);
|
|
811
|
+
}
|
|
812
|
+
current[index] = nextSegment2.type === "index" || nextSegment2.type === "slice" || nextSegment2.type === "union" || nextSegment2.type === "wildcard" || options.type === "array" ? [] : {};
|
|
813
|
+
current = current[index];
|
|
814
|
+
} else {
|
|
815
|
+
if (segment2.type === "index" && typeof current === "object") {
|
|
816
|
+
if (current && current[String(segment2.value)] !== void 0) {
|
|
817
|
+
current = current[String(segment2.value)];
|
|
818
|
+
continue;
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
if (typeof current === "object") {
|
|
822
|
+
const values = Object.values(current);
|
|
823
|
+
current = values;
|
|
824
|
+
continue;
|
|
825
|
+
}
|
|
826
|
+
return void 0;
|
|
827
|
+
}
|
|
828
|
+
break;
|
|
829
|
+
default:
|
|
830
|
+
return void 0;
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
return filterDefined(current);
|
|
834
|
+
}
|
|
835
|
+
function filterDefined(obj) {
|
|
836
|
+
if (Array.isArray(obj)) {
|
|
837
|
+
return obj.filter((item) => typeof item !== "undefined");
|
|
838
|
+
}
|
|
839
|
+
return obj;
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
// src/lib/array/advanced.ts
|
|
843
|
+
function chunk(array, size) {
|
|
844
|
+
if (!Array.isArray(array) || size < 1) {
|
|
845
|
+
return [];
|
|
846
|
+
}
|
|
847
|
+
const chunks = [];
|
|
848
|
+
for (let i = 0; i < array.length; i += size) {
|
|
849
|
+
chunks.push(array.slice(i, i + size));
|
|
850
|
+
}
|
|
851
|
+
return chunks;
|
|
852
|
+
}
|
|
853
|
+
function flatten(array) {
|
|
854
|
+
return array.reduce((acc, val) => {
|
|
855
|
+
return acc.concat(val);
|
|
856
|
+
}, []);
|
|
857
|
+
}
|
|
858
|
+
function flattenDeep(array, depth = Infinity) {
|
|
859
|
+
if (depth < 1) {
|
|
860
|
+
return array.slice();
|
|
861
|
+
}
|
|
862
|
+
return array.reduce((acc, val) => {
|
|
863
|
+
if (Array.isArray(val)) {
|
|
864
|
+
acc.push(...flattenDeep(val, depth - 1));
|
|
865
|
+
} else {
|
|
866
|
+
acc.push(val);
|
|
867
|
+
}
|
|
868
|
+
return acc;
|
|
869
|
+
}, []);
|
|
870
|
+
}
|
|
871
|
+
function unique(array, selector) {
|
|
872
|
+
if (!Array.isArray(array)) {
|
|
873
|
+
return [];
|
|
874
|
+
}
|
|
875
|
+
if (!selector) {
|
|
876
|
+
return [...new Set(array)];
|
|
877
|
+
}
|
|
878
|
+
const seen = /* @__PURE__ */ new Set();
|
|
879
|
+
return array.filter((item) => {
|
|
880
|
+
const value = typeof selector === "function" ? selector(item) : isObject(item) ? get(item, selector) : item;
|
|
881
|
+
if (seen.has(value)) {
|
|
882
|
+
return false;
|
|
883
|
+
}
|
|
884
|
+
seen.add(value);
|
|
885
|
+
return true;
|
|
886
|
+
});
|
|
887
|
+
}
|
|
888
|
+
function groupBy(array, keyOrFn) {
|
|
889
|
+
if (!Array.isArray(array)) {
|
|
890
|
+
return {};
|
|
891
|
+
}
|
|
892
|
+
return array.reduce((acc, item) => {
|
|
893
|
+
let key;
|
|
894
|
+
if (typeof keyOrFn === "function") {
|
|
895
|
+
key = String(keyOrFn(item));
|
|
896
|
+
} else {
|
|
897
|
+
key = String(isObject(item) ? get(item, keyOrFn) : item);
|
|
898
|
+
}
|
|
899
|
+
if (!acc[key]) {
|
|
900
|
+
acc[key] = [];
|
|
901
|
+
}
|
|
902
|
+
acc[key].push(item);
|
|
903
|
+
return acc;
|
|
904
|
+
}, {});
|
|
905
|
+
}
|
|
906
|
+
function countBy(array, key) {
|
|
907
|
+
if (!Array.isArray(array)) {
|
|
908
|
+
return {};
|
|
909
|
+
}
|
|
910
|
+
return array.reduce((acc, item) => {
|
|
911
|
+
let countKey;
|
|
912
|
+
if (typeof key === "function") {
|
|
913
|
+
countKey = String(key(item));
|
|
914
|
+
} else if (key) {
|
|
915
|
+
countKey = String(
|
|
916
|
+
isObject(item) ? get(item, key) : item
|
|
917
|
+
);
|
|
918
|
+
} else {
|
|
919
|
+
countKey = String(item);
|
|
920
|
+
}
|
|
921
|
+
acc[countKey] = (acc[countKey] || 0) + 1;
|
|
922
|
+
return acc;
|
|
923
|
+
}, {});
|
|
924
|
+
}
|
|
925
|
+
function keyBy(array, keyFn, valueFn) {
|
|
926
|
+
if (!Array.isArray(array)) {
|
|
927
|
+
return {};
|
|
928
|
+
}
|
|
929
|
+
return array.reduce((acc, item) => {
|
|
930
|
+
let key;
|
|
931
|
+
if (typeof keyFn === "function") {
|
|
932
|
+
key = String(keyFn(item));
|
|
933
|
+
} else {
|
|
934
|
+
key = String(isObject(item) ? get(item, keyFn) : item);
|
|
935
|
+
}
|
|
936
|
+
let value;
|
|
937
|
+
if (valueFn) {
|
|
938
|
+
if (typeof valueFn === "function") {
|
|
939
|
+
value = valueFn(item);
|
|
940
|
+
} else {
|
|
941
|
+
value = get(item, valueFn);
|
|
942
|
+
}
|
|
943
|
+
} else {
|
|
944
|
+
value = item;
|
|
945
|
+
}
|
|
946
|
+
acc[key] = value;
|
|
947
|
+
return acc;
|
|
948
|
+
}, {});
|
|
949
|
+
}
|
|
950
|
+
function sortBy(array, keys) {
|
|
951
|
+
if (!Array.isArray(array) || array.length === 0) {
|
|
952
|
+
return [];
|
|
953
|
+
}
|
|
954
|
+
const sorted = [...array];
|
|
955
|
+
const keysArray = Array.isArray(keys) ? keys : [keys];
|
|
956
|
+
const normalizedKeys = keysArray.map((k) => {
|
|
957
|
+
if (typeof k === "object" && "key" in k) {
|
|
958
|
+
return { key: k.key, dir: k.dir || "asc" };
|
|
959
|
+
}
|
|
960
|
+
return { key: k, dir: "asc" };
|
|
961
|
+
});
|
|
962
|
+
sorted.sort((a, b) => {
|
|
963
|
+
for (const { key, dir } of normalizedKeys) {
|
|
964
|
+
const aVal = isObject(a) ? get(a, key) : a;
|
|
965
|
+
const bVal = isObject(b) ? get(b, key) : b;
|
|
966
|
+
if (aVal === bVal) continue;
|
|
967
|
+
const comparison = aVal < bVal ? -1 : 1;
|
|
968
|
+
return dir === "asc" ? comparison : -comparison;
|
|
969
|
+
}
|
|
970
|
+
return 0;
|
|
971
|
+
});
|
|
972
|
+
return sorted;
|
|
973
|
+
}
|
|
974
|
+
function difference(array1, array2, key) {
|
|
975
|
+
if (!Array.isArray(array1)) return [];
|
|
976
|
+
if (!Array.isArray(array2)) return array1;
|
|
977
|
+
if (!key) {
|
|
978
|
+
const set2 = new Set(array2);
|
|
979
|
+
return array1.filter((item) => !set2.has(item));
|
|
980
|
+
}
|
|
981
|
+
const values2 = new Set(
|
|
982
|
+
array2.map(
|
|
983
|
+
(item) => isObject(item) ? get(item, key) : item
|
|
984
|
+
)
|
|
985
|
+
);
|
|
986
|
+
return array1.filter((item) => {
|
|
987
|
+
const value = isObject(item) ? get(item, key) : item;
|
|
988
|
+
return !values2.has(value);
|
|
989
|
+
});
|
|
990
|
+
}
|
|
991
|
+
function intersection(array1, array2, key) {
|
|
992
|
+
if (!Array.isArray(array1) || !Array.isArray(array2)) {
|
|
993
|
+
return [];
|
|
994
|
+
}
|
|
995
|
+
if (!key) {
|
|
996
|
+
const set2 = new Set(array2);
|
|
997
|
+
return array1.filter((item) => set2.has(item));
|
|
998
|
+
}
|
|
999
|
+
const values2 = new Set(
|
|
1000
|
+
array2.map(
|
|
1001
|
+
(item) => isObject(item) ? get(item, key) : item
|
|
1002
|
+
)
|
|
1003
|
+
);
|
|
1004
|
+
return array1.filter((item) => {
|
|
1005
|
+
const value = isObject(item) ? get(item, key) : item;
|
|
1006
|
+
return values2.has(value);
|
|
1007
|
+
});
|
|
1008
|
+
}
|
|
1009
|
+
function union(array1, array2, key) {
|
|
1010
|
+
if (!Array.isArray(array1) && !Array.isArray(array2)) {
|
|
1011
|
+
return [];
|
|
1012
|
+
}
|
|
1013
|
+
if (!Array.isArray(array1)) return unique(array2, key);
|
|
1014
|
+
if (!Array.isArray(array2)) return unique(array1, key);
|
|
1015
|
+
return unique([...array1, ...array2], key);
|
|
1016
|
+
}
|
|
1017
|
+
function partition(array, predicate) {
|
|
1018
|
+
if (!Array.isArray(array)) {
|
|
1019
|
+
return [[], []];
|
|
1020
|
+
}
|
|
1021
|
+
return array.reduce(
|
|
1022
|
+
(acc, item, index) => {
|
|
1023
|
+
acc[predicate(item, index) ? 0 : 1].push(item);
|
|
1024
|
+
return acc;
|
|
1025
|
+
},
|
|
1026
|
+
[[], []]
|
|
1027
|
+
);
|
|
1028
|
+
}
|
|
1029
|
+
function take(array, n) {
|
|
1030
|
+
if (!Array.isArray(array) || n <= 0) {
|
|
1031
|
+
return [];
|
|
1032
|
+
}
|
|
1033
|
+
return array.slice(0, n);
|
|
1034
|
+
}
|
|
1035
|
+
function takeLast(array, n) {
|
|
1036
|
+
if (!Array.isArray(array) || n <= 0) {
|
|
1037
|
+
return [];
|
|
1038
|
+
}
|
|
1039
|
+
return array.slice(-n);
|
|
1040
|
+
}
|
|
1041
|
+
function takeWhile(array, predicate) {
|
|
1042
|
+
if (!Array.isArray(array)) {
|
|
1043
|
+
return [];
|
|
1044
|
+
}
|
|
1045
|
+
const result = [];
|
|
1046
|
+
for (let i = 0; i < array.length; i++) {
|
|
1047
|
+
if (!predicate(array[i], i)) break;
|
|
1048
|
+
result.push(array[i]);
|
|
1049
|
+
}
|
|
1050
|
+
return result;
|
|
1051
|
+
}
|
|
1052
|
+
function drop(array, n) {
|
|
1053
|
+
if (!Array.isArray(array) || n <= 0) {
|
|
1054
|
+
return array ? [...array] : [];
|
|
1055
|
+
}
|
|
1056
|
+
return array.slice(n);
|
|
1057
|
+
}
|
|
1058
|
+
function dropWhile(array, predicate) {
|
|
1059
|
+
if (!Array.isArray(array)) {
|
|
1060
|
+
return [];
|
|
1061
|
+
}
|
|
1062
|
+
let index = 0;
|
|
1063
|
+
while (index < array.length && predicate(array[index], index)) {
|
|
1064
|
+
index++;
|
|
1065
|
+
}
|
|
1066
|
+
return array.slice(index);
|
|
1067
|
+
}
|
|
1068
|
+
function compact(array) {
|
|
1069
|
+
if (!Array.isArray(array)) {
|
|
1070
|
+
return [];
|
|
1071
|
+
}
|
|
1072
|
+
return array.filter(Boolean);
|
|
1073
|
+
}
|
|
1074
|
+
function zip(...arrays) {
|
|
1075
|
+
if (arrays.length === 0) return [];
|
|
1076
|
+
const maxLength = Math.max(
|
|
1077
|
+
...arrays.map((arr) => Array.isArray(arr) ? arr.length : 0)
|
|
1078
|
+
);
|
|
1079
|
+
const result = [];
|
|
1080
|
+
for (let i = 0; i < maxLength; i++) {
|
|
1081
|
+
const tuple = [];
|
|
1082
|
+
for (const array of arrays) {
|
|
1083
|
+
tuple.push(Array.isArray(array) ? array[i] : void 0);
|
|
1084
|
+
}
|
|
1085
|
+
result.push(tuple);
|
|
1086
|
+
}
|
|
1087
|
+
return result;
|
|
1088
|
+
}
|
|
1089
|
+
function unzip(array) {
|
|
1090
|
+
if (!Array.isArray(array) || array.length === 0) {
|
|
1091
|
+
return [];
|
|
1092
|
+
}
|
|
1093
|
+
const maxLength = Math.max(
|
|
1094
|
+
...array.map((arr) => Array.isArray(arr) ? arr.length : 0)
|
|
1095
|
+
);
|
|
1096
|
+
const result = [];
|
|
1097
|
+
for (let i = 0; i < maxLength; i++) {
|
|
1098
|
+
const tuple = [];
|
|
1099
|
+
for (const arr of array) {
|
|
1100
|
+
if (Array.isArray(arr)) {
|
|
1101
|
+
tuple.push(arr[i]);
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
result.push(tuple);
|
|
1105
|
+
}
|
|
1106
|
+
return result;
|
|
1107
|
+
}
|
|
1108
|
+
function shuffle(array) {
|
|
1109
|
+
if (!Array.isArray(array)) {
|
|
1110
|
+
return [];
|
|
1111
|
+
}
|
|
1112
|
+
const result = [...array];
|
|
1113
|
+
for (let i = result.length - 1; i > 0; i--) {
|
|
1114
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
1115
|
+
[result[i], result[j]] = [result[j], result[i]];
|
|
1116
|
+
}
|
|
1117
|
+
return result;
|
|
1118
|
+
}
|
|
1119
|
+
function sample(array) {
|
|
1120
|
+
if (!Array.isArray(array) || array.length === 0) {
|
|
1121
|
+
return void 0;
|
|
1122
|
+
}
|
|
1123
|
+
return array[Math.floor(Math.random() * array.length)];
|
|
1124
|
+
}
|
|
1125
|
+
function sampleSize(array, n) {
|
|
1126
|
+
if (!Array.isArray(array) || n <= 0) {
|
|
1127
|
+
return [];
|
|
1128
|
+
}
|
|
1129
|
+
const shuffled = shuffle(array);
|
|
1130
|
+
return shuffled.slice(0, Math.min(n, array.length));
|
|
1131
|
+
}
|
|
1132
|
+
function findIndex(array, predicate, fromIndex = 0) {
|
|
1133
|
+
if (!Array.isArray(array)) {
|
|
1134
|
+
return -1;
|
|
1135
|
+
}
|
|
1136
|
+
for (let i = fromIndex; i < array.length; i++) {
|
|
1137
|
+
if (predicate(array[i], i)) {
|
|
1138
|
+
return i;
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
return -1;
|
|
1142
|
+
}
|
|
1143
|
+
function findLastIndex(array, predicate, fromIndex) {
|
|
1144
|
+
if (!Array.isArray(array)) {
|
|
1145
|
+
return -1;
|
|
1146
|
+
}
|
|
1147
|
+
const startIndex = fromIndex !== void 0 ? fromIndex : array.length - 1;
|
|
1148
|
+
for (let i = startIndex; i >= 0; i--) {
|
|
1149
|
+
if (predicate(array[i], i)) {
|
|
1150
|
+
return i;
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
return -1;
|
|
1154
|
+
}
|
|
1155
|
+
function pluck(array, path) {
|
|
1156
|
+
if (!Array.isArray(array)) {
|
|
1157
|
+
return [];
|
|
1158
|
+
}
|
|
1159
|
+
return array.map((item) => get(item, path));
|
|
1160
|
+
}
|
|
1161
|
+
function pick(array, keys) {
|
|
1162
|
+
if (!Array.isArray(array) || !Array.isArray(keys)) {
|
|
1163
|
+
return [];
|
|
1164
|
+
}
|
|
1165
|
+
return array.map((item) => {
|
|
1166
|
+
const result = {};
|
|
1167
|
+
for (const key of keys) {
|
|
1168
|
+
if (key in item) {
|
|
1169
|
+
result[key] = item[key];
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
return result;
|
|
1173
|
+
});
|
|
1174
|
+
}
|
|
1175
|
+
function omit(array, keys) {
|
|
1176
|
+
if (!Array.isArray(array) || !Array.isArray(keys)) {
|
|
1177
|
+
return [];
|
|
1178
|
+
}
|
|
1179
|
+
return array.map((item) => {
|
|
1180
|
+
const result = { ...item };
|
|
1181
|
+
for (const key of keys) {
|
|
1182
|
+
delete result[key];
|
|
1183
|
+
}
|
|
1184
|
+
return result;
|
|
1185
|
+
});
|
|
1186
|
+
}
|
|
1187
|
+
function flatMap(array, callback) {
|
|
1188
|
+
if (!Array.isArray(array)) {
|
|
1189
|
+
return [];
|
|
1190
|
+
}
|
|
1191
|
+
return array.reduce((acc, item, index) => {
|
|
1192
|
+
const result = callback(item, index);
|
|
1193
|
+
return acc.concat(result);
|
|
1194
|
+
}, []);
|
|
1195
|
+
}
|
|
1196
|
+
function reduceRight(array, callback, initialValue) {
|
|
1197
|
+
if (!Array.isArray(array)) {
|
|
1198
|
+
return initialValue;
|
|
1199
|
+
}
|
|
1200
|
+
let accumulator = initialValue;
|
|
1201
|
+
for (let i = array.length - 1; i >= 0; i--) {
|
|
1202
|
+
accumulator = callback(accumulator, array[i], i);
|
|
1203
|
+
}
|
|
1204
|
+
return accumulator;
|
|
1205
|
+
}
|
|
1206
|
+
function range(start, end, step = 1) {
|
|
1207
|
+
if (step === 0) {
|
|
1208
|
+
return [];
|
|
1209
|
+
}
|
|
1210
|
+
const result = [];
|
|
1211
|
+
if (step > 0) {
|
|
1212
|
+
for (let i = start; i < end; i += step) {
|
|
1213
|
+
result.push(i);
|
|
1214
|
+
}
|
|
1215
|
+
} else {
|
|
1216
|
+
for (let i = start; i > end; i += step) {
|
|
1217
|
+
result.push(i);
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
return result;
|
|
1221
|
+
}
|
|
1222
|
+
function fill(array, value, start = 0, end) {
|
|
1223
|
+
if (!Array.isArray(array)) {
|
|
1224
|
+
return [];
|
|
1225
|
+
}
|
|
1226
|
+
const result = [...array];
|
|
1227
|
+
const actualEnd = end === void 0 ? result.length : end;
|
|
1228
|
+
for (let i = start; i < actualEnd && i < result.length; i++) {
|
|
1229
|
+
result[i] = value;
|
|
1230
|
+
}
|
|
1231
|
+
return result;
|
|
1232
|
+
}
|
|
1233
|
+
function deepCloneArray(array) {
|
|
1234
|
+
if (!Array.isArray(array)) {
|
|
1235
|
+
return [];
|
|
1236
|
+
}
|
|
1237
|
+
return deepClone(array);
|
|
1238
|
+
}
|
|
1239
|
+
function mergeByKey(arr1, arr2, ...keyOrMore) {
|
|
1240
|
+
let key;
|
|
1241
|
+
let arrays;
|
|
1242
|
+
if (typeof arr2 === "string") {
|
|
1243
|
+
key = arr2;
|
|
1244
|
+
arrays = [arr1];
|
|
1245
|
+
} else {
|
|
1246
|
+
const lastArg = keyOrMore[keyOrMore.length - 1];
|
|
1247
|
+
if (typeof lastArg === "string") {
|
|
1248
|
+
key = lastArg;
|
|
1249
|
+
arrays = [
|
|
1250
|
+
arr1,
|
|
1251
|
+
arr2,
|
|
1252
|
+
...keyOrMore.slice(0, -1).filter(Array.isArray)
|
|
1253
|
+
];
|
|
1254
|
+
} else {
|
|
1255
|
+
key = "id";
|
|
1256
|
+
arrays = [arr1, arr2, ...keyOrMore.filter(Array.isArray)];
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
if (arrays.length === 0) {
|
|
1260
|
+
return [];
|
|
1261
|
+
}
|
|
1262
|
+
const map = /* @__PURE__ */ new Map();
|
|
1263
|
+
for (const array of arrays) {
|
|
1264
|
+
if (Array.isArray(array)) {
|
|
1265
|
+
for (const item of array) {
|
|
1266
|
+
const keyValue = isObject(item) ? get(item, key) : void 0;
|
|
1267
|
+
if (keyValue !== void 0) {
|
|
1268
|
+
const existing = map.get(keyValue);
|
|
1269
|
+
map.set(keyValue, existing ? { ...existing, ...item } : item);
|
|
1270
|
+
}
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1273
|
+
}
|
|
1274
|
+
return Array.from(map.values());
|
|
1275
|
+
}
|
|
1276
|
+
function move(array, fromIndex, toIndex) {
|
|
1277
|
+
if (!Array.isArray(array) || fromIndex === toIndex) {
|
|
1278
|
+
return array ? [...array] : [];
|
|
1279
|
+
}
|
|
1280
|
+
const result = [...array];
|
|
1281
|
+
const [removed] = result.splice(fromIndex, 1);
|
|
1282
|
+
result.splice(toIndex, 0, removed);
|
|
1283
|
+
return result;
|
|
1284
|
+
}
|
|
1285
|
+
function rotate(array, n) {
|
|
1286
|
+
if (!Array.isArray(array) || array.length === 0 || n === 0) {
|
|
1287
|
+
return array ? [...array] : [];
|
|
1288
|
+
}
|
|
1289
|
+
const len = array.length;
|
|
1290
|
+
const rotations = (n % len + len) % len;
|
|
1291
|
+
return [...array.slice(-rotations), ...array.slice(0, -rotations)];
|
|
1292
|
+
}
|
|
1293
|
+
function insert(array, index, ...items) {
|
|
1294
|
+
if (!Array.isArray(array)) {
|
|
1295
|
+
return items;
|
|
1296
|
+
}
|
|
1297
|
+
const result = [...array];
|
|
1298
|
+
result.splice(index, 0, ...items);
|
|
1299
|
+
return result;
|
|
1300
|
+
}
|
|
1301
|
+
function remove(array, index, count = 1) {
|
|
1302
|
+
if (!Array.isArray(array)) {
|
|
1303
|
+
return [];
|
|
1304
|
+
}
|
|
1305
|
+
const result = [...array];
|
|
1306
|
+
result.splice(index, count);
|
|
1307
|
+
return result;
|
|
1308
|
+
}
|
|
1309
|
+
function replace(array, index, item) {
|
|
1310
|
+
if (!Array.isArray(array)) {
|
|
1311
|
+
return [];
|
|
1312
|
+
}
|
|
1313
|
+
const result = [...array];
|
|
1314
|
+
result[index] = item;
|
|
1315
|
+
return result;
|
|
1316
|
+
}
|
|
1317
|
+
function filterCompact(array, predicate) {
|
|
1318
|
+
if (!Array.isArray(array)) {
|
|
1319
|
+
return [];
|
|
1320
|
+
}
|
|
1321
|
+
const filtered = array.filter((item) => item != null);
|
|
1322
|
+
return predicate ? filtered.filter(predicate) : filtered;
|
|
1323
|
+
}
|
|
1324
|
+
function cartesian(...arrays) {
|
|
1325
|
+
if (arrays.length === 0) return [[]];
|
|
1326
|
+
if (arrays.length === 1) return arrays[0].map((item) => [item]);
|
|
1327
|
+
return arrays.reduce(
|
|
1328
|
+
(acc, array) => {
|
|
1329
|
+
return acc.flatMap((x) => array.map((y) => [...x, y]));
|
|
1330
|
+
},
|
|
1331
|
+
[[]]
|
|
1332
|
+
);
|
|
1333
|
+
}
|
|
1334
|
+
function findAllIndices(array, predicate) {
|
|
1335
|
+
if (!Array.isArray(array)) {
|
|
1336
|
+
return [];
|
|
1337
|
+
}
|
|
1338
|
+
const indices = [];
|
|
1339
|
+
for (let i = 0; i < array.length; i++) {
|
|
1340
|
+
if (predicate(array[i], i)) {
|
|
1341
|
+
indices.push(i);
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
1344
|
+
return indices;
|
|
1345
|
+
}
|
|
1346
|
+
function isEqual(array1, array2, deep = true) {
|
|
1347
|
+
if (!Array.isArray(array1) || !Array.isArray(array2)) {
|
|
1348
|
+
return false;
|
|
1349
|
+
}
|
|
1350
|
+
if (array1.length !== array2.length) {
|
|
1351
|
+
return false;
|
|
1352
|
+
}
|
|
1353
|
+
for (let i = 0; i < array1.length; i++) {
|
|
1354
|
+
if (deep && isObject(array1[i]) && isObject(array2[i])) {
|
|
1355
|
+
if (JSON.stringify(array1[i]) !== JSON.stringify(array2[i])) {
|
|
1356
|
+
return false;
|
|
1357
|
+
}
|
|
1358
|
+
} else if (array1[i] !== array2[i]) {
|
|
1359
|
+
return false;
|
|
1360
|
+
}
|
|
1361
|
+
}
|
|
1362
|
+
return true;
|
|
1363
|
+
}
|
|
1364
|
+
function chunkBy(array, predicate) {
|
|
1365
|
+
if (!Array.isArray(array) || array.length === 0) {
|
|
1366
|
+
return [];
|
|
1367
|
+
}
|
|
1368
|
+
const result = [];
|
|
1369
|
+
let currentChunk = [array[0]];
|
|
1370
|
+
let currentKey = predicate(array[0], 0);
|
|
1371
|
+
for (let i = 1; i < array.length; i++) {
|
|
1372
|
+
const key = predicate(array[i], i);
|
|
1373
|
+
if (key === currentKey) {
|
|
1374
|
+
currentChunk.push(array[i]);
|
|
1375
|
+
} else {
|
|
1376
|
+
result.push(currentChunk);
|
|
1377
|
+
currentChunk = [array[i]];
|
|
1378
|
+
currentKey = key;
|
|
1379
|
+
}
|
|
1380
|
+
}
|
|
1381
|
+
result.push(currentChunk);
|
|
1382
|
+
return result;
|
|
1383
|
+
}
|
|
1384
|
+
function updateBy(array, predicate, updates, updatesOrUndefined) {
|
|
1385
|
+
if (!Array.isArray(array)) {
|
|
1386
|
+
return [];
|
|
1387
|
+
}
|
|
1388
|
+
const isPredicate = typeof predicate === "function";
|
|
1389
|
+
const actualUpdates = isPredicate ? updates : updatesOrUndefined;
|
|
1390
|
+
return array.map((item) => {
|
|
1391
|
+
let shouldUpdate = false;
|
|
1392
|
+
if (isPredicate) {
|
|
1393
|
+
shouldUpdate = predicate(item);
|
|
1394
|
+
} else {
|
|
1395
|
+
const itemValue = isObject(item) ? get(item, predicate) : item;
|
|
1396
|
+
shouldUpdate = itemValue === updates;
|
|
1397
|
+
}
|
|
1398
|
+
if (shouldUpdate) {
|
|
1399
|
+
return { ...item, ...actualUpdates };
|
|
1400
|
+
}
|
|
1401
|
+
return item;
|
|
1402
|
+
});
|
|
1403
|
+
}
|
|
1404
|
+
function splitDuplicates(array, key) {
|
|
1405
|
+
if (!Array.isArray(array)) {
|
|
1406
|
+
return { unique: [], duplicates: [] };
|
|
1407
|
+
}
|
|
1408
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1409
|
+
const unique2 = [];
|
|
1410
|
+
const duplicates = [];
|
|
1411
|
+
for (const item of array) {
|
|
1412
|
+
const value = key && isObject(item) ? get(item, key) : item;
|
|
1413
|
+
if (seen.has(value)) {
|
|
1414
|
+
duplicates.push(item);
|
|
1415
|
+
} else {
|
|
1416
|
+
seen.add(value);
|
|
1417
|
+
unique2.push(item);
|
|
1418
|
+
}
|
|
1419
|
+
}
|
|
1420
|
+
return { unique: unique2, duplicates };
|
|
1421
|
+
}
|
|
1422
|
+
function ensureArray(value) {
|
|
1423
|
+
if (Array.isArray(value)) {
|
|
1424
|
+
return value;
|
|
1425
|
+
}
|
|
1426
|
+
return value === void 0 || value === null ? [] : [value];
|
|
1427
|
+
}
|
|
1428
|
+
function wrap(value) {
|
|
1429
|
+
return ensureArray(value);
|
|
1430
|
+
}
|
|
1431
|
+
function slidingWindow(array, size, step = 1) {
|
|
1432
|
+
if (!Array.isArray(array) || size < 1 || step < 1) {
|
|
1433
|
+
return [];
|
|
1434
|
+
}
|
|
1435
|
+
const windows = [];
|
|
1436
|
+
for (let i = 0; i <= array.length - size; i += step) {
|
|
1437
|
+
windows.push(array.slice(i, i + size));
|
|
1438
|
+
}
|
|
1439
|
+
return windows;
|
|
1440
|
+
}
|
|
1441
|
+
function minBy(array, selector) {
|
|
1442
|
+
if (!Array.isArray(array) || array.length === 0) {
|
|
1443
|
+
return void 0;
|
|
1444
|
+
}
|
|
1445
|
+
let minItem = array[0];
|
|
1446
|
+
let minValue = typeof selector === "function" ? selector(minItem) : get(minItem, selector);
|
|
1447
|
+
for (let i = 1; i < array.length; i++) {
|
|
1448
|
+
const item = array[i];
|
|
1449
|
+
const value = typeof selector === "function" ? selector(item) : get(item, selector);
|
|
1450
|
+
if (value < minValue) {
|
|
1451
|
+
minValue = value;
|
|
1452
|
+
minItem = item;
|
|
1453
|
+
}
|
|
1454
|
+
}
|
|
1455
|
+
return minItem;
|
|
1456
|
+
}
|
|
1457
|
+
function maxBy(array, selector) {
|
|
1458
|
+
if (!Array.isArray(array) || array.length === 0) {
|
|
1459
|
+
return void 0;
|
|
1460
|
+
}
|
|
1461
|
+
let maxItem = array[0];
|
|
1462
|
+
let maxValue = typeof selector === "function" ? selector(maxItem) : get(maxItem, selector);
|
|
1463
|
+
for (let i = 1; i < array.length; i++) {
|
|
1464
|
+
const item = array[i];
|
|
1465
|
+
const value = typeof selector === "function" ? selector(item) : get(item, selector);
|
|
1466
|
+
if (value > maxValue) {
|
|
1467
|
+
maxValue = value;
|
|
1468
|
+
maxItem = item;
|
|
1469
|
+
}
|
|
1470
|
+
}
|
|
1471
|
+
return maxItem;
|
|
1472
|
+
}
|
|
1473
|
+
function sumBy(array, selector) {
|
|
1474
|
+
if (!Array.isArray(array) || array.length === 0) {
|
|
1475
|
+
return 0;
|
|
1476
|
+
}
|
|
1477
|
+
let sum = 0;
|
|
1478
|
+
for (const item of array) {
|
|
1479
|
+
const value = typeof selector === "function" ? selector(item) : get(item, selector);
|
|
1480
|
+
if (typeof value === "number" && !isNaN(value)) {
|
|
1481
|
+
sum += value;
|
|
1482
|
+
}
|
|
1483
|
+
}
|
|
1484
|
+
return sum;
|
|
1485
|
+
}
|
|
1486
|
+
function avgBy(array, selector) {
|
|
1487
|
+
if (!Array.isArray(array) || array.length === 0) {
|
|
1488
|
+
return 0;
|
|
1489
|
+
}
|
|
1490
|
+
const sum = sumBy(array, selector);
|
|
1491
|
+
return sum / array.length;
|
|
1492
|
+
}
|
|
1493
|
+
function frequencies(array) {
|
|
1494
|
+
const freqMap = /* @__PURE__ */ new Map();
|
|
1495
|
+
if (!Array.isArray(array)) {
|
|
1496
|
+
return freqMap;
|
|
1497
|
+
}
|
|
1498
|
+
for (const item of array) {
|
|
1499
|
+
freqMap.set(item, (freqMap.get(item) || 0) + 1);
|
|
1500
|
+
}
|
|
1501
|
+
return freqMap;
|
|
1502
|
+
}
|
|
1503
|
+
function mostCommon(array, limit = 1) {
|
|
1504
|
+
if (!Array.isArray(array) || array.length === 0 || limit < 1) {
|
|
1505
|
+
return [];
|
|
1506
|
+
}
|
|
1507
|
+
const freqMap = frequencies(array);
|
|
1508
|
+
const sorted = Array.from(freqMap.entries()).sort((a, b) => b[1] - a[1]);
|
|
1509
|
+
return sorted.slice(0, limit).map(([value]) => value);
|
|
1510
|
+
}
|
|
1511
|
+
function leastCommon(array, limit = 1) {
|
|
1512
|
+
if (!Array.isArray(array) || array.length === 0 || limit < 1) {
|
|
1513
|
+
return [];
|
|
1514
|
+
}
|
|
1515
|
+
const freqMap = frequencies(array);
|
|
1516
|
+
const sorted = Array.from(freqMap.entries()).sort((a, b) => a[1] - b[1]);
|
|
1517
|
+
return sorted.slice(0, limit).map(([value]) => value);
|
|
1518
|
+
}
|
|
1519
|
+
async function batchProcess(array, batchSize, callback) {
|
|
1520
|
+
if (!Array.isArray(array) || batchSize < 1) {
|
|
1521
|
+
return;
|
|
1522
|
+
}
|
|
1523
|
+
const batches = chunk(array, batchSize);
|
|
1524
|
+
for (let i = 0; i < batches.length; i++) {
|
|
1525
|
+
await callback(batches[i], i);
|
|
1526
|
+
}
|
|
1527
|
+
}
|
|
1528
|
+
async function batchProcessParallel(array, batchSize, concurrency, callback) {
|
|
1529
|
+
if (!Array.isArray(array) || batchSize < 1 || concurrency < 1) {
|
|
1530
|
+
return;
|
|
1531
|
+
}
|
|
1532
|
+
const batches = chunk(array, batchSize);
|
|
1533
|
+
const batchChunks = chunk(
|
|
1534
|
+
batches.map((batch2, index) => ({ batch: batch2, index })),
|
|
1535
|
+
concurrency
|
|
1536
|
+
);
|
|
1537
|
+
for (const batchChunk of batchChunks) {
|
|
1538
|
+
await Promise.all(
|
|
1539
|
+
batchChunk.map(({ batch: batch2, index }) => callback(batch2, index))
|
|
1540
|
+
);
|
|
1541
|
+
}
|
|
1542
|
+
}
|
|
1543
|
+
function transpose(matrix) {
|
|
1544
|
+
if (!Array.isArray(matrix) || matrix.length === 0) {
|
|
1545
|
+
return [];
|
|
1546
|
+
}
|
|
1547
|
+
const maxLength = Math.max(...matrix.map((row) => row.length));
|
|
1548
|
+
const result = [];
|
|
1549
|
+
for (let col = 0; col < maxLength; col++) {
|
|
1550
|
+
const column = [];
|
|
1551
|
+
for (let row = 0; row < matrix.length; row++) {
|
|
1552
|
+
if (col < matrix[row].length) {
|
|
1553
|
+
column.push(matrix[row][col]);
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
result.push(column);
|
|
1557
|
+
}
|
|
1558
|
+
return result;
|
|
1559
|
+
}
|
|
1560
|
+
function isArrayEqual(array1, array2) {
|
|
1561
|
+
if (!Array.isArray(array1) || !Array.isArray(array2)) {
|
|
1562
|
+
return false;
|
|
1563
|
+
}
|
|
1564
|
+
if (array1.length !== array2.length) {
|
|
1565
|
+
return false;
|
|
1566
|
+
}
|
|
1567
|
+
for (let i = 0; i < array1.length; i++) {
|
|
1568
|
+
if (array1[i] !== array2[i]) {
|
|
1569
|
+
return false;
|
|
1570
|
+
}
|
|
1571
|
+
}
|
|
1572
|
+
return true;
|
|
1573
|
+
}
|
|
1574
|
+
function hasSameElements(array1, array2) {
|
|
1575
|
+
if (!Array.isArray(array1) || !Array.isArray(array2)) {
|
|
1576
|
+
return false;
|
|
1577
|
+
}
|
|
1578
|
+
if (array1.length !== array2.length) {
|
|
1579
|
+
return false;
|
|
1580
|
+
}
|
|
1581
|
+
const freq1 = frequencies(array1);
|
|
1582
|
+
const freq2 = frequencies(array2);
|
|
1583
|
+
if (freq1.size !== freq2.size) {
|
|
1584
|
+
return false;
|
|
1585
|
+
}
|
|
1586
|
+
for (const [key, count] of freq1) {
|
|
1587
|
+
if (freq2.get(key) !== count) {
|
|
1588
|
+
return false;
|
|
1589
|
+
}
|
|
1590
|
+
}
|
|
1591
|
+
return true;
|
|
1592
|
+
}
|
|
1593
|
+
function isSubset(array1, array2) {
|
|
1594
|
+
if (!Array.isArray(array1) || !Array.isArray(array2)) {
|
|
1595
|
+
return false;
|
|
1596
|
+
}
|
|
1597
|
+
const set2 = new Set(array2);
|
|
1598
|
+
return array1.every((item) => set2.has(item));
|
|
1599
|
+
}
|
|
1600
|
+
function isSuperset(array1, array2) {
|
|
1601
|
+
return isSubset(array2, array1);
|
|
1602
|
+
}
|
|
1603
|
+
function interleave(...arrays) {
|
|
1604
|
+
if (arrays.length === 0) {
|
|
1605
|
+
return [];
|
|
1606
|
+
}
|
|
1607
|
+
const maxLength = Math.max(...arrays.map((arr) => arr.length));
|
|
1608
|
+
const result = [];
|
|
1609
|
+
for (let i = 0; i < maxLength; i++) {
|
|
1610
|
+
for (const arr of arrays) {
|
|
1611
|
+
if (i < arr.length) {
|
|
1612
|
+
result.push(arr[i]);
|
|
1613
|
+
}
|
|
1614
|
+
}
|
|
1615
|
+
}
|
|
1616
|
+
return result;
|
|
1617
|
+
}
|
|
1618
|
+
function bifurcate(array, predicate) {
|
|
1619
|
+
const pass = [];
|
|
1620
|
+
const fail = [];
|
|
1621
|
+
if (!Array.isArray(array)) {
|
|
1622
|
+
return { pass, fail };
|
|
1623
|
+
}
|
|
1624
|
+
for (let i = 0; i < array.length; i++) {
|
|
1625
|
+
if (predicate(array[i], i)) {
|
|
1626
|
+
pass.push(array[i]);
|
|
1627
|
+
} else {
|
|
1628
|
+
fail.push(array[i]);
|
|
1629
|
+
}
|
|
1630
|
+
}
|
|
1631
|
+
return { pass, fail };
|
|
1632
|
+
}
|
|
1633
|
+
function generate(length, generator) {
|
|
1634
|
+
if (length < 0) {
|
|
1635
|
+
return [];
|
|
1636
|
+
}
|
|
1637
|
+
const result = [];
|
|
1638
|
+
for (let i = 0; i < length; i++) {
|
|
1639
|
+
result.push(generator(i));
|
|
1640
|
+
}
|
|
1641
|
+
return result;
|
|
1642
|
+
}
|
|
1643
|
+
function repeat(array, times) {
|
|
1644
|
+
if (!Array.isArray(array) || times < 0) {
|
|
1645
|
+
return [];
|
|
1646
|
+
}
|
|
1647
|
+
const result = [];
|
|
1648
|
+
for (let i = 0; i < times; i++) {
|
|
1649
|
+
result.push(...array);
|
|
1650
|
+
}
|
|
1651
|
+
return result;
|
|
1652
|
+
}
|
|
1653
|
+
function median(array) {
|
|
1654
|
+
if (!Array.isArray(array) || array.length === 0) {
|
|
1655
|
+
return void 0;
|
|
1656
|
+
}
|
|
1657
|
+
const sorted = [...array].sort((a, b) => a - b);
|
|
1658
|
+
const mid = Math.floor(sorted.length / 2);
|
|
1659
|
+
if (sorted.length % 2 === 0) {
|
|
1660
|
+
return (sorted[mid - 1] + sorted[mid]) / 2;
|
|
1661
|
+
}
|
|
1662
|
+
return sorted[mid];
|
|
1663
|
+
}
|
|
1664
|
+
function mode(array) {
|
|
1665
|
+
const common = mostCommon(array, 1);
|
|
1666
|
+
return common.length > 0 ? common[0] : void 0;
|
|
1667
|
+
}
|
|
1668
|
+
function without(array, ...values) {
|
|
1669
|
+
if (!Array.isArray(array)) {
|
|
1670
|
+
return [];
|
|
1671
|
+
}
|
|
1672
|
+
const excludeSet = new Set(values);
|
|
1673
|
+
return array.filter((item) => !excludeSet.has(item));
|
|
1674
|
+
}
|
|
1675
|
+
function differenceWith(array, ...others) {
|
|
1676
|
+
if (!Array.isArray(array)) {
|
|
1677
|
+
return [];
|
|
1678
|
+
}
|
|
1679
|
+
const excludeSet = new Set(others.flat());
|
|
1680
|
+
return array.filter((item) => !excludeSet.has(item));
|
|
1681
|
+
}
|
|
1682
|
+
function groupConsecutive(array) {
|
|
1683
|
+
if (!Array.isArray(array) || array.length === 0) {
|
|
1684
|
+
return [];
|
|
1685
|
+
}
|
|
1686
|
+
const result = [];
|
|
1687
|
+
let currentGroup = [array[0]];
|
|
1688
|
+
for (let i = 1; i < array.length; i++) {
|
|
1689
|
+
if (array[i] === array[i - 1]) {
|
|
1690
|
+
currentGroup.push(array[i]);
|
|
1691
|
+
} else {
|
|
1692
|
+
result.push(currentGroup);
|
|
1693
|
+
currentGroup = [array[i]];
|
|
1694
|
+
}
|
|
1695
|
+
}
|
|
1696
|
+
result.push(currentGroup);
|
|
1697
|
+
return result;
|
|
1698
|
+
}
|
|
1699
|
+
function dedupeConsecutive(array) {
|
|
1700
|
+
if (!Array.isArray(array) || array.length === 0) {
|
|
1701
|
+
return [];
|
|
1702
|
+
}
|
|
1703
|
+
const result = [array[0]];
|
|
1704
|
+
for (let i = 1; i < array.length; i++) {
|
|
1705
|
+
if (array[i] !== array[i - 1]) {
|
|
1706
|
+
result.push(array[i]);
|
|
1707
|
+
}
|
|
1708
|
+
}
|
|
1709
|
+
return result;
|
|
1710
|
+
}
|
|
1711
|
+
function rank(array, method = "standard") {
|
|
1712
|
+
if (!Array.isArray(array) || array.length === 0) {
|
|
1713
|
+
return [];
|
|
1714
|
+
}
|
|
1715
|
+
const indexed = array.map((value, index) => ({ value, index }));
|
|
1716
|
+
indexed.sort((a, b) => a.value - b.value);
|
|
1717
|
+
const ranks = new Array(array.length);
|
|
1718
|
+
if (method === "ordinal") {
|
|
1719
|
+
for (let i = 0; i < indexed.length; i++) {
|
|
1720
|
+
ranks[indexed[i].index] = i + 1;
|
|
1721
|
+
}
|
|
1722
|
+
} else if (method === "dense") {
|
|
1723
|
+
let denseRank = 1;
|
|
1724
|
+
ranks[indexed[0].index] = denseRank;
|
|
1725
|
+
for (let i = 1; i < indexed.length; i++) {
|
|
1726
|
+
if (indexed[i].value !== indexed[i - 1].value) {
|
|
1727
|
+
denseRank++;
|
|
1728
|
+
}
|
|
1729
|
+
ranks[indexed[i].index] = denseRank;
|
|
1730
|
+
}
|
|
1731
|
+
} else {
|
|
1732
|
+
let i = 0;
|
|
1733
|
+
while (i < indexed.length) {
|
|
1734
|
+
let j = i;
|
|
1735
|
+
while (j < indexed.length && indexed[j].value === indexed[i].value) {
|
|
1736
|
+
j++;
|
|
1737
|
+
}
|
|
1738
|
+
const rankValue = i + 1;
|
|
1739
|
+
for (let k = i; k < j; k++) {
|
|
1740
|
+
ranks[indexed[k].index] = rankValue;
|
|
1741
|
+
}
|
|
1742
|
+
i = j;
|
|
1743
|
+
}
|
|
1744
|
+
}
|
|
1745
|
+
return ranks;
|
|
1746
|
+
}
|
|
1747
|
+
function percentile(array, percentile2) {
|
|
1748
|
+
if (!Array.isArray(array) || array.length === 0) {
|
|
1749
|
+
return void 0;
|
|
1750
|
+
}
|
|
1751
|
+
if (percentile2 < 0 || percentile2 > 100) {
|
|
1752
|
+
return void 0;
|
|
1753
|
+
}
|
|
1754
|
+
const sorted = [...array].sort((a, b) => a - b);
|
|
1755
|
+
const index = percentile2 / 100 * (sorted.length - 1);
|
|
1756
|
+
const lower = Math.floor(index);
|
|
1757
|
+
const upper = Math.ceil(index);
|
|
1758
|
+
const weight = index - lower;
|
|
1759
|
+
if (lower === upper) {
|
|
1760
|
+
return sorted[lower];
|
|
1761
|
+
}
|
|
1762
|
+
return sorted[lower] * (1 - weight) + sorted[upper] * weight;
|
|
1763
|
+
}
|
|
1764
|
+
function quartiles(array) {
|
|
1765
|
+
if (!Array.isArray(array) || array.length === 0) {
|
|
1766
|
+
return void 0;
|
|
1767
|
+
}
|
|
1768
|
+
const q1 = percentile(array, 25);
|
|
1769
|
+
const q2 = percentile(array, 50);
|
|
1770
|
+
const q3 = percentile(array, 75);
|
|
1771
|
+
return {
|
|
1772
|
+
q1,
|
|
1773
|
+
q2,
|
|
1774
|
+
q3,
|
|
1775
|
+
iqr: q3 - q1
|
|
1776
|
+
};
|
|
1777
|
+
}
|
|
1778
|
+
function standardDeviation(array, sample2 = false) {
|
|
1779
|
+
if (!Array.isArray(array) || array.length === 0) {
|
|
1780
|
+
return void 0;
|
|
1781
|
+
}
|
|
1782
|
+
if (sample2 && array.length === 1) {
|
|
1783
|
+
return void 0;
|
|
1784
|
+
}
|
|
1785
|
+
const avg = array.reduce((sum, val) => sum + val, 0) / array.length;
|
|
1786
|
+
const squareDiffs = array.map((value) => Math.pow(value - avg, 2));
|
|
1787
|
+
const avgSquareDiff = squareDiffs.reduce((sum, val) => sum + val, 0) / (sample2 ? array.length - 1 : array.length);
|
|
1788
|
+
return Math.sqrt(avgSquareDiff);
|
|
1789
|
+
}
|
|
1790
|
+
function variance(array, sample2 = false) {
|
|
1791
|
+
const std = standardDeviation(array, sample2);
|
|
1792
|
+
return std !== void 0 ? Math.pow(std, 2) : void 0;
|
|
1793
|
+
}
|
|
1794
|
+
function normalize(array) {
|
|
1795
|
+
if (!Array.isArray(array) || array.length === 0) {
|
|
1796
|
+
return [];
|
|
1797
|
+
}
|
|
1798
|
+
const min = Math.min(...array);
|
|
1799
|
+
const max = Math.max(...array);
|
|
1800
|
+
const range2 = max - min;
|
|
1801
|
+
if (range2 === 0) {
|
|
1802
|
+
return array.map(() => 0);
|
|
1803
|
+
}
|
|
1804
|
+
return array.map((value) => (value - min) / range2);
|
|
1805
|
+
}
|
|
1806
|
+
function standardize(array) {
|
|
1807
|
+
if (!Array.isArray(array) || array.length === 0) {
|
|
1808
|
+
return [];
|
|
1809
|
+
}
|
|
1810
|
+
const avg = array.reduce((sum, val) => sum + val, 0) / array.length;
|
|
1811
|
+
const std = standardDeviation(array);
|
|
1812
|
+
if (std === void 0 || std === 0) {
|
|
1813
|
+
return array.map(() => 0);
|
|
1814
|
+
}
|
|
1815
|
+
return array.map((value) => (value - avg) / std);
|
|
1816
|
+
}
|
|
1817
|
+
function scan(array, fn, initial) {
|
|
1818
|
+
if (!Array.isArray(array)) {
|
|
1819
|
+
return [];
|
|
1820
|
+
}
|
|
1821
|
+
const result = [];
|
|
1822
|
+
let accumulator = initial;
|
|
1823
|
+
for (let i = 0; i < array.length; i++) {
|
|
1824
|
+
accumulator = fn(accumulator, array[i], i);
|
|
1825
|
+
result.push(accumulator);
|
|
1826
|
+
}
|
|
1827
|
+
return result;
|
|
1828
|
+
}
|
|
1829
|
+
function cumulativeSum(array) {
|
|
1830
|
+
return scan(array, (acc, x) => acc + x, 0);
|
|
1831
|
+
}
|
|
1832
|
+
function diff(array) {
|
|
1833
|
+
if (!Array.isArray(array) || array.length < 2) {
|
|
1834
|
+
return [];
|
|
1835
|
+
}
|
|
1836
|
+
const result = [];
|
|
1837
|
+
for (let i = 1; i < array.length; i++) {
|
|
1838
|
+
result.push(array[i] - array[i - 1]);
|
|
1839
|
+
}
|
|
1840
|
+
return result;
|
|
1841
|
+
}
|
|
1842
|
+
function pad(array, length, value, position = "end") {
|
|
1843
|
+
if (!Array.isArray(array) || length <= array.length) {
|
|
1844
|
+
return [...array];
|
|
1845
|
+
}
|
|
1846
|
+
const padding = new Array(length - array.length).fill(value);
|
|
1847
|
+
return position === "start" ? [...padding, ...array] : [...array, ...padding];
|
|
1848
|
+
}
|
|
1849
|
+
function trim(array, predicate) {
|
|
1850
|
+
if (!Array.isArray(array) || array.length === 0) {
|
|
1851
|
+
return [];
|
|
1852
|
+
}
|
|
1853
|
+
let start = 0;
|
|
1854
|
+
let end = array.length - 1;
|
|
1855
|
+
while (start <= end && predicate(array[start])) {
|
|
1856
|
+
start++;
|
|
1857
|
+
}
|
|
1858
|
+
while (end >= start && predicate(array[end])) {
|
|
1859
|
+
end--;
|
|
1860
|
+
}
|
|
1861
|
+
return array.slice(start, end + 1);
|
|
1862
|
+
}
|
|
1863
|
+
function pairwise(array) {
|
|
1864
|
+
if (!Array.isArray(array) || array.length < 2) {
|
|
1865
|
+
return [];
|
|
1866
|
+
}
|
|
1867
|
+
const result = [];
|
|
1868
|
+
for (let i = 0; i < array.length - 1; i++) {
|
|
1869
|
+
result.push([array[i], array[i + 1]]);
|
|
1870
|
+
}
|
|
1871
|
+
return result;
|
|
1872
|
+
}
|
|
1873
|
+
function enumerate(array) {
|
|
1874
|
+
if (!Array.isArray(array)) {
|
|
1875
|
+
return [];
|
|
1876
|
+
}
|
|
1877
|
+
return array.map((value, index) => [index, value]);
|
|
1878
|
+
}
|
|
1879
|
+
function applyByIndex(array, functions) {
|
|
1880
|
+
if (!Array.isArray(array)) {
|
|
1881
|
+
return [];
|
|
1882
|
+
}
|
|
1883
|
+
return array.map((item, index) => {
|
|
1884
|
+
const fn = functions[index % functions.length];
|
|
1885
|
+
return fn(item);
|
|
1886
|
+
});
|
|
1887
|
+
}
|
|
1888
|
+
function rotateLeft(array, n = 1) {
|
|
1889
|
+
if (!Array.isArray(array) || array.length === 0) {
|
|
1890
|
+
return [];
|
|
1891
|
+
}
|
|
1892
|
+
const len = array.length;
|
|
1893
|
+
const rotations = (n % len + len) % len;
|
|
1894
|
+
return [...array.slice(rotations), ...array.slice(0, rotations)];
|
|
1895
|
+
}
|
|
1896
|
+
function rotateRight(array, n = 1) {
|
|
1897
|
+
return rotateLeft(array, -n);
|
|
1898
|
+
}
|
|
1899
|
+
function swap(array, i, j) {
|
|
1900
|
+
if (!Array.isArray(array) || i < 0 || j < 0 || i >= array.length || j >= array.length) {
|
|
1901
|
+
return [...array];
|
|
1902
|
+
}
|
|
1903
|
+
const result = [...array];
|
|
1904
|
+
[result[i], result[j]] = [result[j], result[i]];
|
|
1905
|
+
return result;
|
|
1906
|
+
}
|
|
1907
|
+
function reverseSegment(array, start, end) {
|
|
1908
|
+
if (!Array.isArray(array) || start < 0 || end >= array.length || start >= end) {
|
|
1909
|
+
return [...array];
|
|
1910
|
+
}
|
|
1911
|
+
const result = [...array];
|
|
1912
|
+
const segment2 = result.slice(start, end + 1).reverse();
|
|
1913
|
+
result.splice(start, end - start + 1, ...segment2);
|
|
1914
|
+
return result;
|
|
1915
|
+
}
|
|
1916
|
+
function isSorted(array, compareFn) {
|
|
1917
|
+
if (!Array.isArray(array) || array.length <= 1) {
|
|
1918
|
+
return true;
|
|
1919
|
+
}
|
|
1920
|
+
const compare = compareFn || ((a, b) => a < b ? -1 : a > b ? 1 : 0);
|
|
1921
|
+
for (let i = 1; i < array.length; i++) {
|
|
1922
|
+
if (compare(array[i - 1], array[i]) > 0) {
|
|
1923
|
+
return false;
|
|
1924
|
+
}
|
|
1925
|
+
}
|
|
1926
|
+
return true;
|
|
1927
|
+
}
|
|
1928
|
+
function hasDuplicates(array) {
|
|
1929
|
+
if (!Array.isArray(array)) {
|
|
1930
|
+
return false;
|
|
1931
|
+
}
|
|
1932
|
+
return new Set(array).size !== array.length;
|
|
1933
|
+
}
|
|
1934
|
+
function pickMany(array, keys) {
|
|
1935
|
+
if (!Array.isArray(array)) {
|
|
1936
|
+
return [];
|
|
1937
|
+
}
|
|
1938
|
+
return array.map((obj) => {
|
|
1939
|
+
const result = {};
|
|
1940
|
+
for (const key of keys) {
|
|
1941
|
+
if (key in obj) {
|
|
1942
|
+
result[key] = obj[key];
|
|
1943
|
+
}
|
|
1944
|
+
}
|
|
1945
|
+
return result;
|
|
1946
|
+
});
|
|
1947
|
+
}
|
|
1948
|
+
function toObject(array, keySelector, valueSelector) {
|
|
1949
|
+
if (!Array.isArray(array)) {
|
|
1950
|
+
return {};
|
|
1951
|
+
}
|
|
1952
|
+
const result = {};
|
|
1953
|
+
for (let i = 0; i < array.length; i++) {
|
|
1954
|
+
const key = keySelector(array[i], i);
|
|
1955
|
+
const value = valueSelector ? valueSelector(array[i], i) : array[i];
|
|
1956
|
+
result[key] = value;
|
|
1957
|
+
}
|
|
1958
|
+
return result;
|
|
1959
|
+
}
|
|
1960
|
+
async function mapAsync(array, fn) {
|
|
1961
|
+
if (!Array.isArray(array)) {
|
|
1962
|
+
return [];
|
|
1963
|
+
}
|
|
1964
|
+
const result = [];
|
|
1965
|
+
for (let i = 0; i < array.length; i++) {
|
|
1966
|
+
result.push(await fn(array[i], i));
|
|
1967
|
+
}
|
|
1968
|
+
return result;
|
|
1969
|
+
}
|
|
1970
|
+
async function mapAsyncParallel(array, fn) {
|
|
1971
|
+
if (!Array.isArray(array)) {
|
|
1972
|
+
return [];
|
|
1973
|
+
}
|
|
1974
|
+
return Promise.all(array.map((item, index) => fn(item, index)));
|
|
1975
|
+
}
|
|
1976
|
+
async function filterAsync(array, predicate) {
|
|
1977
|
+
if (!Array.isArray(array)) {
|
|
1978
|
+
return [];
|
|
1979
|
+
}
|
|
1980
|
+
const result = [];
|
|
1981
|
+
for (let i = 0; i < array.length; i++) {
|
|
1982
|
+
if (await predicate(array[i], i)) {
|
|
1983
|
+
result.push(array[i]);
|
|
1984
|
+
}
|
|
1985
|
+
}
|
|
1986
|
+
return result;
|
|
1987
|
+
}
|
|
1988
|
+
async function reduceAsync(array, fn, initial) {
|
|
1989
|
+
if (!Array.isArray(array)) {
|
|
1990
|
+
return initial;
|
|
1991
|
+
}
|
|
1992
|
+
let accumulator = initial;
|
|
1993
|
+
for (let i = 0; i < array.length; i++) {
|
|
1994
|
+
accumulator = await fn(accumulator, array[i], i);
|
|
1995
|
+
}
|
|
1996
|
+
return accumulator;
|
|
1997
|
+
}
|
|
1998
|
+
async function findAsync(array, predicate) {
|
|
1999
|
+
if (!Array.isArray(array)) {
|
|
2000
|
+
return void 0;
|
|
2001
|
+
}
|
|
2002
|
+
for (let i = 0; i < array.length; i++) {
|
|
2003
|
+
if (await predicate(array[i], i)) {
|
|
2004
|
+
return array[i];
|
|
2005
|
+
}
|
|
2006
|
+
}
|
|
2007
|
+
return void 0;
|
|
2008
|
+
}
|
|
2009
|
+
async function someAsync(array, predicate) {
|
|
2010
|
+
if (!Array.isArray(array)) {
|
|
2011
|
+
return false;
|
|
2012
|
+
}
|
|
2013
|
+
for (let i = 0; i < array.length; i++) {
|
|
2014
|
+
if (await predicate(array[i], i)) {
|
|
2015
|
+
return true;
|
|
2016
|
+
}
|
|
2017
|
+
}
|
|
2018
|
+
return false;
|
|
2019
|
+
}
|
|
2020
|
+
async function everyAsync(array, predicate) {
|
|
2021
|
+
if (!Array.isArray(array)) {
|
|
2022
|
+
return true;
|
|
2023
|
+
}
|
|
2024
|
+
for (let i = 0; i < array.length; i++) {
|
|
2025
|
+
if (!await predicate(array[i], i)) {
|
|
2026
|
+
return false;
|
|
2027
|
+
}
|
|
2028
|
+
}
|
|
2029
|
+
return true;
|
|
2030
|
+
}
|
|
2031
|
+
function weightedAverage(array, valueSelector, weightSelector) {
|
|
2032
|
+
if (!Array.isArray(array) || array.length === 0) {
|
|
2033
|
+
return 0;
|
|
2034
|
+
}
|
|
2035
|
+
let totalWeight = 0;
|
|
2036
|
+
let weightedSum = 0;
|
|
2037
|
+
for (const item of array) {
|
|
2038
|
+
const value = typeof valueSelector === "function" ? valueSelector(item) : get(item, valueSelector);
|
|
2039
|
+
const weight = typeof weightSelector === "function" ? weightSelector(item) : get(item, weightSelector);
|
|
2040
|
+
if (typeof value === "number" && typeof weight === "number") {
|
|
2041
|
+
weightedSum += value * weight;
|
|
2042
|
+
totalWeight += weight;
|
|
2043
|
+
}
|
|
2044
|
+
}
|
|
2045
|
+
return totalWeight === 0 ? 0 : weightedSum / totalWeight;
|
|
2046
|
+
}
|
|
2047
|
+
function weightedSample(array, weightSelector, count = 1) {
|
|
2048
|
+
if (!Array.isArray(array) || array.length === 0 || count < 1) {
|
|
2049
|
+
return [];
|
|
2050
|
+
}
|
|
2051
|
+
const weights = [];
|
|
2052
|
+
let totalWeight = 0;
|
|
2053
|
+
for (const item of array) {
|
|
2054
|
+
const weight = typeof weightSelector === "function" ? weightSelector(item) : get(item, weightSelector);
|
|
2055
|
+
totalWeight += typeof weight === "number" && weight > 0 ? weight : 0;
|
|
2056
|
+
weights.push(totalWeight);
|
|
2057
|
+
}
|
|
2058
|
+
if (totalWeight === 0) {
|
|
2059
|
+
return [];
|
|
2060
|
+
}
|
|
2061
|
+
const result = [];
|
|
2062
|
+
for (let i = 0; i < count; i++) {
|
|
2063
|
+
const random = Math.random() * totalWeight;
|
|
2064
|
+
const index = weights.findIndex((w) => random < w);
|
|
2065
|
+
if (index !== -1) {
|
|
2066
|
+
result.push(array[index]);
|
|
2067
|
+
}
|
|
2068
|
+
}
|
|
2069
|
+
return result;
|
|
2070
|
+
}
|
|
2071
|
+
function segment(array, sizes) {
|
|
2072
|
+
if (!Array.isArray(array) || !Array.isArray(sizes)) {
|
|
2073
|
+
return [];
|
|
2074
|
+
}
|
|
2075
|
+
const result = [];
|
|
2076
|
+
let offset = 0;
|
|
2077
|
+
for (const size of sizes) {
|
|
2078
|
+
if (offset >= array.length) break;
|
|
2079
|
+
result.push(array.slice(offset, offset + size));
|
|
2080
|
+
offset += size;
|
|
2081
|
+
}
|
|
2082
|
+
return result;
|
|
2083
|
+
}
|
|
2084
|
+
function selectByCount(array, count) {
|
|
2085
|
+
if (!Array.isArray(array) || count < 1) {
|
|
2086
|
+
return [];
|
|
2087
|
+
}
|
|
2088
|
+
const freq = frequencies(array);
|
|
2089
|
+
const result = [];
|
|
2090
|
+
for (const item of array) {
|
|
2091
|
+
if (freq.get(item) === count) {
|
|
2092
|
+
result.push(item);
|
|
2093
|
+
}
|
|
2094
|
+
}
|
|
2095
|
+
return result;
|
|
2096
|
+
}
|
|
2097
|
+
function filterConsecutive(array, n, predicate) {
|
|
2098
|
+
if (!Array.isArray(array) || n < 1) {
|
|
2099
|
+
return [];
|
|
2100
|
+
}
|
|
2101
|
+
const result = [];
|
|
2102
|
+
let consecutiveCount = 0;
|
|
2103
|
+
const buffer = [];
|
|
2104
|
+
for (const item of array) {
|
|
2105
|
+
if (predicate(item)) {
|
|
2106
|
+
consecutiveCount++;
|
|
2107
|
+
buffer.push(item);
|
|
2108
|
+
if (consecutiveCount >= n) {
|
|
2109
|
+
result.push(...buffer.splice(0, buffer.length - n + 1));
|
|
2110
|
+
}
|
|
2111
|
+
} else {
|
|
2112
|
+
consecutiveCount = 0;
|
|
2113
|
+
buffer.length = 0;
|
|
2114
|
+
}
|
|
2115
|
+
}
|
|
2116
|
+
if (consecutiveCount >= n) {
|
|
2117
|
+
result.push(...buffer);
|
|
2118
|
+
}
|
|
2119
|
+
return result;
|
|
2120
|
+
}
|
|
2121
|
+
function mapWithContext(array, fn, initialContext) {
|
|
2122
|
+
if (!Array.isArray(array)) {
|
|
2123
|
+
return [];
|
|
2124
|
+
}
|
|
2125
|
+
const result = [];
|
|
2126
|
+
let context = initialContext;
|
|
2127
|
+
for (let i = 0; i < array.length; i++) {
|
|
2128
|
+
const { value, newContext } = fn(array[i], context, i);
|
|
2129
|
+
result.push(value);
|
|
2130
|
+
context = newContext;
|
|
2131
|
+
}
|
|
2132
|
+
return result;
|
|
2133
|
+
}
|
|
2134
|
+
function batch(array, batchSize) {
|
|
2135
|
+
return chunk(array, batchSize);
|
|
2136
|
+
}
|
|
2137
|
+
function zipWith(array1, array2, fn) {
|
|
2138
|
+
if (!Array.isArray(array1) || !Array.isArray(array2)) {
|
|
2139
|
+
return [];
|
|
2140
|
+
}
|
|
2141
|
+
const length = Math.min(array1.length, array2.length);
|
|
2142
|
+
const result = [];
|
|
2143
|
+
for (let i = 0; i < length; i++) {
|
|
2144
|
+
result.push(fn(array1[i], array2[i]));
|
|
2145
|
+
}
|
|
2146
|
+
return result;
|
|
2147
|
+
}
|
|
2148
|
+
function mergeArraysByKey(arrays, key, mergeStrategy = "last") {
|
|
2149
|
+
if (!Array.isArray(arrays) || arrays.length === 0) {
|
|
2150
|
+
return [];
|
|
2151
|
+
}
|
|
2152
|
+
const map = /* @__PURE__ */ new Map();
|
|
2153
|
+
for (const array of arrays) {
|
|
2154
|
+
if (!Array.isArray(array)) continue;
|
|
2155
|
+
for (const item of array) {
|
|
2156
|
+
const keyValue = get(item, key);
|
|
2157
|
+
if (mergeStrategy === "first") {
|
|
2158
|
+
if (!map.has(keyValue)) {
|
|
2159
|
+
map.set(keyValue, item);
|
|
2160
|
+
}
|
|
2161
|
+
} else if (mergeStrategy === "last") {
|
|
2162
|
+
map.set(keyValue, item);
|
|
2163
|
+
} else if (mergeStrategy === "concat") {
|
|
2164
|
+
const existing = map.get(keyValue);
|
|
2165
|
+
if (Array.isArray(existing)) {
|
|
2166
|
+
existing.push(item);
|
|
2167
|
+
} else if (existing) {
|
|
2168
|
+
map.set(keyValue, [existing, item]);
|
|
2169
|
+
} else {
|
|
2170
|
+
map.set(keyValue, item);
|
|
2171
|
+
}
|
|
2172
|
+
} else if (typeof mergeStrategy === "function") {
|
|
2173
|
+
const existing = map.get(keyValue);
|
|
2174
|
+
if (existing && !Array.isArray(existing)) {
|
|
2175
|
+
map.set(keyValue, mergeStrategy(existing, item));
|
|
2176
|
+
} else {
|
|
2177
|
+
map.set(keyValue, item);
|
|
2178
|
+
}
|
|
2179
|
+
}
|
|
2180
|
+
}
|
|
2181
|
+
}
|
|
2182
|
+
const result = [];
|
|
2183
|
+
for (const value of map.values()) {
|
|
2184
|
+
if (Array.isArray(value) && mergeStrategy === "concat") {
|
|
2185
|
+
result.push(...value);
|
|
2186
|
+
} else if (!Array.isArray(value)) {
|
|
2187
|
+
result.push(value);
|
|
2188
|
+
}
|
|
2189
|
+
}
|
|
2190
|
+
return result;
|
|
2191
|
+
}
|
|
2192
|
+
function indexBy(array, key) {
|
|
2193
|
+
return toObject(array, (item) => String(get(item, key)));
|
|
2194
|
+
}
|
|
2195
|
+
function binarySearch(array, target, compareFn) {
|
|
2196
|
+
if (!Array.isArray(array) || array.length === 0) {
|
|
2197
|
+
return -1;
|
|
2198
|
+
}
|
|
2199
|
+
const compare = compareFn || ((a, b) => a < b ? -1 : a > b ? 1 : 0);
|
|
2200
|
+
let left = 0;
|
|
2201
|
+
let right = array.length - 1;
|
|
2202
|
+
while (left <= right) {
|
|
2203
|
+
const mid = Math.floor((left + right) / 2);
|
|
2204
|
+
const comparison = compare(array[mid], target);
|
|
2205
|
+
if (comparison === 0) {
|
|
2206
|
+
return mid;
|
|
2207
|
+
} else if (comparison < 0) {
|
|
2208
|
+
left = mid + 1;
|
|
2209
|
+
} else {
|
|
2210
|
+
right = mid - 1;
|
|
2211
|
+
}
|
|
2212
|
+
}
|
|
2213
|
+
return -1;
|
|
2214
|
+
}
|
|
2215
|
+
function insertSorted(array, value, compareFn) {
|
|
2216
|
+
if (!Array.isArray(array)) {
|
|
2217
|
+
return [value];
|
|
2218
|
+
}
|
|
2219
|
+
const compare = compareFn || ((a, b) => a < b ? -1 : a > b ? 1 : 0);
|
|
2220
|
+
const result = [...array];
|
|
2221
|
+
let insertIndex = 0;
|
|
2222
|
+
while (insertIndex < result.length && compare(result[insertIndex], value) < 0) {
|
|
2223
|
+
insertIndex++;
|
|
2224
|
+
}
|
|
2225
|
+
result.splice(insertIndex, 0, value);
|
|
2226
|
+
return result;
|
|
2227
|
+
}
|