@bicharts/shape-core 0.5.24 → 0.5.25
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.
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
/*! @bicharts/shape-core — Apache-2.0. Bundled reference data: GeoNames (https://www.geonames.org/) CC BY 4.0; US Census/TIGER (public domain). Full text: NOTICE in this package. */
|
|
2
|
+
import{a as j}from"./chunk-QEH7WTLB.mjs";import{a as q,b as J}from"./chunk-2LR73DNS.mjs";import{a as $,b as Q,c as L}from"./chunk-NWDPA7DG.mjs";import{c as X}from"./chunk-SAASSIJ7.mjs";import{b as Y,f as Z}from"./chunk-E6SVTFAW.mjs";import{a as K,b as k,c as H}from"./chunk-WBEROMBG.mjs";var ot=[[24.4,49.4,-125,-66.9],[51.2,71.5,-168,-129.9],[18.9,22.3,-160.3,-154.8]],at=[7,72,-172,-52];function tt(a,e,i){return a>=i[0]&&a<=i[1]&&e>=i[2]&&e<=i[3]}function W(a,e){if(!a.length)return NaN;if(a.length===1)return a[0];let i=(a.length-1)*e,o=Math.floor(i),m=Math.ceil(i);return o===m?a[o]:a[o]+(a[m]-a[o])*(i-o)}var x=a=>Math.round(a*10)/10;function nt(a,e=3){let i=[],o=[],m=0,n=0;for(let f of a){let g=f?.lat,h=f?.lon;if(typeof g!="number"||typeof h!="number"||!isFinite(g)||!isFinite(h)||g<-90||g>90||h<-180||h>180)continue;i.push(g),o.push(h);let S=f?.country?String(f.country).trim().toUpperCase():"",p=S?F(S):null;p?((S==="US"||S==="USA")&&m++,p==="north-america"&&n++):(ot.some(I=>tt(g,h,I))&&m++,tt(g,h,at)&&n++)}let l=i.length;return l<e?null:(i.sort((f,g)=>f-g),o.sort((f,g)=>f-g),{pctUsa:x(m/l*100),pctNa:x(n/l*100),latP5:x(W(i,.05)),latP95:x(W(i,.95)),lonP5:x(W(o,.05)),lonP95:x(W(o,.95)),n:l})}var et={"north-america":"AG AI AW BB BL BM BQ BS BZ CA CR CU CW DM DO GD GL GP GT HN HT JM KN KY LC MF MQ MS MX NI PA PM PR SV SX TC TT US VC VG VI","south-america":"AR BO BR CL CO EC FK GF GY PE PY SR UY VE",europe:"AD AL AT AX BA BE BG BY CH CY CZ DE DK EE ES FI FO FR GB GG GI GR HR HU IE IM IS IT JE LI LT LU LV MC MD ME MK MT NL NO PL PT RO RS RU SE SI SJ SK SM UA VA",africa:"AO BF BI BJ BW CD CF CG CI CM CV DJ DZ EG EH ER ET GA GH GM GN GQ GW KE KM LR LS LY MA MG ML MR MU MW MZ NA NE NG RE RW SC SD SH SL SN SO SS ST SZ TD TG TN TZ UG YT ZA ZM ZW",asia:"AE AF AM AZ BD BH BN BT CC CN CX GE HK ID IL IN IO IQ IR JO JP KG KH KP KR KW KZ LA LB LK MM MN MO MV MY NP OM PH PK PS QA SA SG SY TH TJ TL TM TR TW UZ VN YE",oceania:"AS AU CK FJ FM GU KI MH MP NC NF NR NU NZ PF PG PN PW SB TK TO TV UM VU WF WS",antarctica:"AQ BV GS HM TF"},O=(()=>{let a=new Map;for(let e of Object.keys(et))for(let i of et[e].split(/\s+/))i&&a.set(i,e);return a})();function ut(a){for(let[e,i]of a){let o=O.get(e);o&&!O.has(i)&&O.set(i,o)}}ut(Y);function F(a){if(!a)return null;let e=String(a).trim().toUpperCase();if(!e)return null;let i=O.get(e);if(i)return i;let o=Z(String(a));return o?O.get(o)??null:null}function At(a,e=2){return B(a.map(i=>[i,1]),e)}function B(a,e=2){let i=new Map,o=0;for(let[l,f]of a){let g=F(l);if(!g)continue;let h=typeof f=="number"&&isFinite(f)&&f>0?f:1;i.set(g,(i.get(g)??0)+h),o+=h}if(o<e||!i.size)return null;let m="europe",n=-1;for(let l of Array.from(i.keys()).sort()){let f=i.get(l);f>n&&(m=l,n=f)}return{dominantGeoRegion:m,dominantGeoRegionPct:x(n/o*100),regionCount:i.size,n:o}}import it from"papaparse";var lt=12,mt=100,ct=.3,ft=.9,gt=120,dt=1900,ht=2100,pt=0,bt=20,yt=new Set(["id","ids","uuid","guid","key","zip","zipcode","postal","postalcode","ssn","code","codes","sku","isbn","msisdn","imei"]);function Mt(a){if(!a)return!1;let e=a.replace(/([a-z0-9])([A-Z])/g,"$1 $2").replace(/[_\-./]+/g," ").toLowerCase().split(/\s+/).filter(i=>i.length>0);return e.length===0?!1:yt.has(e[e.length-1])}var Ct=/\b(year|yr|fy|fiscal\s*year|calendar\s*year)\b/i,St=/^(?:(?:19|20)\d{2}(?:[-/ ]?(?:q[1-4]|w(?:0?[1-9]|[1-4]\d|5[0-3])|(?:0?[1-9]|1[0-2])))?|q[1-4][-/ ](?:19|20)\d{2}|(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[a-z]*[-/ ]?(?:19|20)?\d{2})$/i;function U(a){if(!Number.isInteger(a)||a<190001||a>210012)return!1;let e=a%100;return e>=1&&e<=12}function z(a){if(!Number.isInteger(a)||a<19000101||a>21001231)return!1;let e=Math.floor(a/100)%100,i=a%100;return e>=1&&e<=12&&i>=1&&i<=31}function _t(a,e){if(!e||!/\b(?:19|20)\d{2}\b/.test(a))return!1;let i=a.replace(/\b(?:19|20)\d{2}\b/g,"").replace(/[-/.,]/g," ").trim();if(!i)return!1;let o=q(i);return o.length>0&&e[o]!==void 0}function Rt(a){let{dataType:e,name:i,isMeasure:o,distinctCount:m}=a;if(e==="DateTime")return!0;if(o)return!1;if(e==="Integer"||e==="Decimal"){let n=a.minNum,l=a.maxNum;if(!(n!=null&&l!=null&&Number.isFinite(n)&&Number.isFinite(l)&&Number.isInteger(n)&&Number.isInteger(l)&&l>=n))return!1;let g=n>=1900&&l<=2100;if(g&&m>=2&&Ct.test(i)||g&&m>=3&&m===l-n+1)return!0;let h=U(n)&&U(l),S=z(n)&&z(l);if(h||S){if(!a.sampleValues)return!0;let p=0,I=0;for(let d of a.sampleValues){if(d==null||d==="")continue;p++;let t=Number(String(d).trim());if(Number.isFinite(t)&&(h?U(t):z(t))&&I++,p>=60)break}if(p>=2&&I===p)return!0}return!1}if(a.sampleValues){let n=a.locale?J(a.locale):null,l=0,f=0;for(let g of a.sampleValues){if(g==null||g==="")continue;l++;let h=String(g).trim();if((St.test(h)||_t(h,n))&&f++,l>=60)break}if(l>=2&&f/l>=.8)return!0}return!1}function Tt(a){let e=(a||"").trim().toLowerCase();return e.startsWith("sum ")||e.startsWith("total ")?"sum":e.startsWith("average ")||e.startsWith("avg. ")?"avg":e.startsWith("count ")||e.startsWith("distinct count ")||e.startsWith("count (distinct) ")?"count":e.startsWith("min ")||e.startsWith("minimum ")?"min":e.startsWith("max ")||e.startsWith("maximum ")?"max":null}function Nt(a){let{rows:e,measureColIdx:i,measureName:o,candidateDims:m,minNum:n,maxNum:l,discourageAgg:f}=a,g=100,h=.01,S="";for(let I of m){if(I.distinct<2||I.distinct>g)continue;let d=m.filter(s=>s.idx!==I.idx).map(s=>s.idx),t=new Map;for(let s of e){let M=s[i];if(typeof M!="number"||!Number.isFinite(M))continue;let y=d.length?d.map(_=>String(s[_])).join(S):"",C=t.get(y);C||(C={sum:0,cset:new Set},t.set(y,C)),C.sum+=M,C.cset.add(String(s[I.idx]))}let r=0,c=0,u=0;for(let s of t.values())s.cset.size<2||(u++,Math.abs(s.sum-100)<=100*h?r++:Math.abs(s.sum-1)<=1*h&&c++);if(u>=2&&(r/u>=.9||c/u>=.9))return{additivity:"part_of_whole",partOfWholeDim:I.name}}if(f)return{additivity:"intensive_rate"};let p=Tt(o);return p==="avg"||p==="min"||p==="max"?{additivity:"intensive_rate"}:p==="count"?{additivity:"additive"}:p==="sum"?{additivity:"unknown"}:n!=null&&l!=null&&Number.isFinite(n)&&Number.isFinite(l)&&n>=0&&l<=1.0001&&l>0?{additivity:"intensive_rate"}:{additivity:"unknown"}}function rt(a){let{dataType:e,isMeasure:i,name:o,distinct:m,nonblank:n,prec:l,maxval:f,minval:g}=a,h=f-g+1,S=h>0?m/h:1,p=n>0?m/n:0,I=e==="Decimal"||l>0;return Mt(o)?"Categorical":S>=ft&&(K(o)&&m<=gt||g>=dt&&f<=ht||g>=pt&&f<=bt)?"Ordinal":i||m>lt&&(I||m>=mt||p>=ct)?"Continuous":"Categorical"}var st=class{_computedStatsForLevel=null;_cols=[];_rows=[];_origIndices=[];_rowHashes=new Set;dedupRows=!0;STR(e){return e==null?"":e.toString()}getPrecision(e){if(!isFinite(e))return 0;let o=e.toString().split(".")[1];return o?o.length:0}getColumns(){return this._cols}hasTimeComponent(e){return!(e instanceof Date)||isNaN(e.getTime())?!1:e.getHours()!==0||e.getMinutes()!==0||e.getSeconds()!==0||e.getMilliseconds()!==0}getColumnsWithStats(e,i){if(this._computedStatsForLevel==e)return this._cols;let o=0,m=parseInt(e),n=[];for(let t of this._cols){let r=new Map,c=[],u=0,s=0,M=0,y=null,C=null,_=null,E=!1,w=!0,D=!0,A=null,N=!1;for(let T of this._rows){let b=T[o],R=this.STR(b);if(R!=""){if(u+=R.length,r.set(R,(r.get(R)||0)+1),s++,t.dataType=="Integer"||t.dataType=="Decimal"||t.dataType=="DateTime")if((y===null||b<y)&&(y=b),(C===null||b>C)&&(C=b),t.dataType=="Integer"||t.dataType=="Decimal"){if(_===null?_=b:_+=b,t.dataType=="Decimal"){let v=this.getPrecision(b);v>M&&(M=v)}c.push(b)}else E=E||this.hasTimeComponent(b);N&&(b<A?w=!1:b>A&&(D=!1)),A=b,N=!0}}if(s>=3&&(w&&!D?t.sortedDirection="asc":D&&!w?t.sortedDirection="desc":t.sortedDirection="none"),s>0&&r.size>0&&!t.isMeasure){let T=0,b=Number.MAX_SAFE_INTEGER;for(let R of r.values())R>T&&(T=R),R<b&&(b=R);if(t.modalShare=T/s,t.minGroupCount=b===Number.MAX_SAFE_INTEGER?null:b,r.size===2){let R=[...r.keys()].map(V=>$(V).trim().toLowerCase()).sort(),G=[["false","true"],["no","yes"],["n","y"],["0","1"],["f","t"]].some(V=>R[0]===V[0]&&R[1]===V[1]),P=/^(is|has)[_a-z0-9]|(?:default|churn|fraud|approv|convert|active|cancel|delinquen|flag|paid|win|pass|fail)/i.test(t.name);(G||P)&&(t.isBinaryFlag=!0)}}t.dataType=="DateTime"&&(t.dateWithTime=E),t.isTemporal=Rt({dataType:t.dataType,name:t.name,isMeasure:t.isMeasure,distinctCount:r.size,minNum:typeof y=="number"?y:null,maxNum:typeof C=="number"?C:null,sampleValues:r.keys(),locale:i}),this.updateColumnStats10(m,t,s,r,M,C,y),this.updateColumnStats20(m,c,s,_,t,u,E,y,C,M,r,i),!t.isMeasure&&(t.dataType==="Integer"||t.dataType==="Decimal")&&rt({dataType:t.dataType,isMeasure:!1,name:t.name,distinct:r.size,nonblank:s,prec:M,maxval:typeof C=="number"?C:0,minval:typeof y=="number"?y:0})==="Continuous"&&(t.isMeasure=!0),n[o]=t.isMeasure||r.size>2e3?null:new Set(r.keys()),o++}let l=[];this._cols.forEach((t,r)=>{t.isMeasure||l.push({idx:r,distinct:t.distinctCount??0,name:t.name})}),this._cols.forEach((t,r)=>{if(!t.isMeasure)return;let c=typeof t.lowValue=="number"?t.lowValue:null,u=typeof t.highValue=="number"?t.highValue:null,s=Nt({rows:this._rows,measureColIdx:r,measureName:t.name,candidateDims:l,minNum:c,maxNum:u,discourageAgg:!!t.discourageAggregationAcrossGroups});t.additivity=s.additivity,s.partOfWholeDim&&(t.partOfWholeDim=s.partOfWholeDim)});let f=50,g=l.filter(t=>t.distinct>=2&&t.distinct<=f);this._cols.forEach((t,r)=>{if(!t.isMeasure)return;let c=[];for(let A of this._rows){let N=A[r];if(N==null||N==="")continue;let T=typeof N=="number"?N:parseFloat(N);isNaN(T)||c.push(T)}if(c.length===0)return;let u=[...c].sort((A,N)=>A-N),s=A=>u[Math.min(u.length-1,Math.max(0,Math.floor(A*(u.length-1))))],M=s(.5),y=s(.1),C=s(.9),_=Math.abs(M)>1e-9?Math.abs(M):Math.abs(u[u.length-1])>1e-9?Math.abs(u[u.length-1]):1;t.relativeDispersion=Math.round((C-y)/_*1e3)/1e3;let E=0;for(let A of c)E+=A;E/=c.length;let w=0;for(let A of c)w+=(A-E)*(A-E);if(w<=1e-12)return;let D=[];for(let A of g){let N=new Map;for(let b of this._rows){let R=b[r];if(R==null||R==="")continue;let v=typeof R=="number"?R:parseFloat(R);if(isNaN(v))continue;let G=this.STR(b[A.idx])+"",P=N.get(G);P?(P.s+=v,P.n++):N.set(G,{s:v,n:1})}let T=0;for(let b of N.values()){let R=b.s/b.n;T+=b.n*(R-E)*(R-E)}D.push({otherColumn:A.name,eta2:Math.round(T/w*1e4)/1e4})}D.length>0&&(t.groupDiscrimination=D)});let h=.97,S=5,p=this._cols.map((t,r)=>({c:t,i:r})).filter(t=>t.c.isMeasure);for(let t=0;t<p.length;t++)for(let r=t+1;r<p.length;r++){let c=p[t].i,u=p[r].i,s=0,M=0,y=0,C=0,_=0,E=0;for(let N of this._rows){let T=N[c],b=N[u];if(T==null||T===""||b===null||b===void 0||b==="")continue;let R=typeof T=="number"?T:parseFloat(T),v=typeof b=="number"?b:parseFloat(b);isNaN(R)||isNaN(v)||(s++,M+=R,y+=v,C+=R*R,_+=v*v,E+=R*v)}if(s<S)continue;let w=s*C-M*M,D=s*_-y*y;if(w<=1e-12||D<=1e-12)continue;let A=(s*E-M*y)/Math.sqrt(w*D);if(Math.abs(A)>=h){let N=p[t].c,T=p[r].c;N.collinearWithMeasures||(N.collinearWithMeasures=[]),T.collinearWithMeasures||(T.collinearWithMeasures=[]),N.collinearWithMeasures.push(T.name),T.collinearWithMeasures.push(N.name)}}for(let t=0;t<this._cols.length;t++){let r=n[t];if(!r||r.size===0)continue;let c=[];for(let u=0;u<this._cols.length;u++){if(u===t)continue;let s=n[u];if(!s||s.size===0)continue;let M=0;for(let C of r)s.has(C)&&M++;let y=Math.round(M/r.size*100);y>0&&c.push({otherColumn:this._cols[u].name,percentShared:y})}c.length>0&&(this._cols[t].sharedValueRatioWithOthers=c)}let I=50,d=[];for(let t=0;t<this._cols.length;t++){let r=n[t];r&&r.size>=2&&r.size<=I&&d.push(t)}if(d.length>=2){let t=new Map,r=(u,s)=>u+":"+s;for(let u=0;u<d.length;u++)for(let s=u+1;s<d.length;s++)t.set(r(d[u],d[s]),new Map);for(let u of this._rows)for(let s=0;s<d.length;s++){let M=this.STR(u[d[s]])+"";for(let y=s+1;y<d.length;y++){let C=""+this.STR(u[d[y]]),_=t.get(r(d[s],d[y])),E=M+""+C;_.set(E,(_.get(E)??0)+1)}}let c=new Map;for(let u=0;u<d.length;u++)for(let s=u+1;s<d.length;s++){let M=d[u],y=d[s],C=n[M].size,_=n[y].size,E=t.get(r(M,y)),w=E.size,D=0;for(let b of E.values())(D===0||b<D)&&(D=b);let A=Math.round(w/(C*_)*100),N=w===C,T=w===_;if((this._cols[M].categoricalPairStats||=[]).push({otherColumn:this._cols[y].name,fillPct:A,determinesOther:N,minCellCount:D,distinctCombinations:w}),(this._cols[y].categoricalPairStats||=[]).push({otherColumn:this._cols[M].name,fillPct:A,determinesOther:T,minCellCount:D,distinctCombinations:w}),N&&C>_){let b=c.get(M);(!b||_<b.card)&&c.set(M,{name:this._cols[y].name,card:_,ratio:_/C})}if(T&&_>C){let b=c.get(y);(!b||C<b.card)&&c.set(y,{name:this._cols[M].name,card:C,ratio:C/_})}}for(let[u,s]of c)this._cols[u].primaryParentColumn=s.name,this._cols[u].nestingRatio=Math.round(s.ratio*100)/100}return this._computedStatsForLevel=e,this._cols}updateColumnStats20(e,i,o,m,n,l,f,g,h,S,p,I){if(e>=20){let d=[...i].sort((c,u)=>c-u),t=Math.floor(d.length/2);if(o>0&&m!=null?n.avgValue=n.dataType=="Integer"?Math.round(m/o):m/o:n.avgValue=null,o>0&&(n.avgLength=l/o),this._rows.length>0&&(n.dataType!="DateTime"||f||!g?n.lowValue=g:n.lowValue=new Date(g).toISOString().slice(0,10)+"T00:00:00.000Z",n.dataType!="DateTime"||f||!h?n.highValue=h:n.highValue=new Date(h).toISOString().slice(0,10)+"T00:00:00.000Z",n.medianValue=d.length===0?null:d.length%2===0?n.dataType=="Integer"?Math.round((d[t-1]+d[t])/2):(d[t-1]+d[t])/2:d[t],n.numericPrecision=S),d.length>0){let c=n.avgValue??0,u=d.length,s=0,M=0;for(let C of d){let _=C-c;s+=_*_,M+=_*_*_}let y=s/u;n.skewness=y===0?0:M/u/Math.pow(Math.sqrt(y),3),n.stdDev=Math.sqrt(y)}m!==null&&(n.sum=m);let r=Array.from(p.entries()).sort((c,u)=>u[1]-c[1]);if(n.topCategories=r.slice(0,10).map(c=>c[1]),!n.isMeasure&&r.length>0){if(n.dataType==="String"){let c=r.map(s=>s[0]);n.formatSignature=j(c);let u=/^(UUID|EMAIL_LIKE|URL_LIKE|PHONE_LIKE|NUMERIC_ID|HEX_ID|OPAQUE_ID_ALPHANUMERIC|DATE_STR|BOOLEAN)$/;n.formatSignature&&!u.test(n.formatSignature)&&(n.avgLength===void 0||n.avgLength>=3)&&(n.isFreeText=!0)}else n.topCategoryValues=r.slice(0,5).map(c=>c[0]);if(e>=20){let c=r.map(s=>s[0]),u=H(c,I);if(u!==null)n.ordinalPattern=u.pattern,n.orderedDomain=u.orderedDomain;else if(n.dataType==="String"){let s=k(c);s!==null&&(n.safeDistinctValues=s)}}}if(d.length>=8&&(n.dataType==="Integer"||n.dataType==="Decimal")){let c=u=>d[Math.floor((d.length-1)*u)];n.quantiles={p05:c(.05),p25:c(.25),p75:c(.75),p95:c(.95)}}}if(!n.isMeasure&&p.size>0){let d=Array.from(p.entries()).sort((r,c)=>c[1]-r[1]),t=X(d.map(r=>r[0]),n.name,I);if(t!==null&&(n.geoKind=t.geoKind,n.geoMatchPct=t.geoMatchPct,t.geoAmbiguous&&(n.geoAmbiguous=!0),t.geoKind.indexOf("country")===0)){let r=B(d);r&&(n.dominantGeoRegion=r.dominantGeoRegion,n.dominantGeoRegionPct=r.dominantGeoRegionPct,n.geoRegionCount=r.regionCount)}}}updateColumnStats10(e,i,o,m,n,l,f){e>=10&&(i.blankCount=this._rows.length-o,i.distinctCount=m.size+(i.blankCount!=0?1:0),i.valueNature="Categorical",o>0&&(i.dataType==="Integer"||i.dataType==="Decimal"?i.valueNature=rt({dataType:i.dataType,isMeasure:!!i.isMeasure,name:i.name,distinct:m.size,nonblank:o,prec:n,maxval:l,minval:f}):i.dataType==="DateTime"&&(i.valueNature=i.dateWithTime?"Continuous":"Ordinal")))}getRowCount(){return this._rows.length}getLeafCardinality(){if(this._leafCardinality!==null)return this._leafCardinality;let e=[];if(this._cols.forEach((o,m)=>{o.isMeasure||e.push(m)}),e.length===0)return this._leafCardinality=this._rows.length,this._leafCardinality;let i=new Set;for(let o of this._rows)i.add(JSON.stringify(e.map(m=>o[m])));return this._leafCardinality=i.size,this._leafCardinality}_leafCardinality=null;getGeoExtent(){if(this._geoExtentComputed)return this._geoExtent;this._geoExtentComputed=!0,this._geoExtent=null;let e=l=>String(l||"").replace(/([a-z0-9])([A-Z])/g,"$1 $2").toLowerCase().split(/[^a-z]+/),i=-1,o=-1,m=-1;if(this._cols.forEach((l,f)=>{let g=e(l.name),h=l.dataType==="Integer"||l.dataType==="Decimal";h&&i<0&&g.some(S=>S==="lat"||S==="latitude")?i=f:h&&o<0&&g.some(S=>S==="lon"||S==="lng"||S==="long"||S==="longitude")&&(o=f),m<0&&!l.isMeasure&&typeof l.geoKind=="string"&&l.geoKind.indexOf("country")===0&&(m=f)}),i<0||o<0)return null;let n=[];for(let l of this._rows){let f=l[i],g=l[o],h=f==null||f===""?null:Number(f),S=g==null||g===""?null:Number(g);n.push({lat:h,lon:S,country:m>=0&&F(this.STR(l[m]))?this.STR(l[m]):null})}return this._geoExtent=nt(n),this._geoExtent}_geoExtent=null;_geoExtentComputed=!1;obfuscateString(e){let i="abcdefghijklmnopqrstuvwxyz",o=()=>i[Math.floor(L()*i.length)],m=()=>o().toUpperCase(),n=()=>Math.floor(L()*10).toString(),l=t=>/[A-Z]/.test(t)?m():/[a-z]/.test(t)?o():/[0-9]/.test(t)?n():t,f=e.includes("@")&&e.includes("."),g=f?e.split("@"):[e],h=f?g[1]:"",S=h.endsWith(".com"),p=S?".com":"",I=S?h.slice(0,-4):h,d=t=>{let r="";for(let c of t)r+=l(c),/[a-zA-Z0-9]/.test(c)&&L()<.2&&(r+=l(c));if(r.length>6){let c=Math.floor(L()*Math.min(3,r.length-6));for(let u=0;u<c;u++){let s=0;for(;s<5;){let M=Math.floor(L()*r.length);if(/[a-zA-Z0-9]/.test(r[M])){r=r.slice(0,M)+r.slice(M+1);break}s++}}}return r};if(f){let t=d(g[0]),r=d(I);return`${t}@${r}${p}`}else return d(e)}toObjectArray(){return this._rows.map(e=>{let i={};return this._cols.forEach((o,m)=>{i[o.name]=e[m]}),i})}async getCSVAsync(e){let i=[],o=e?25:1e7,m=e?4e3:1e7,n=new Map;for(let f of this._rows){let g={};for(let h=0;h<this._cols.length;h++){let S=this._cols[h],p=f[h];if(this.STR(p)!=""){if(e&&S.dataType=="String")if(n.has(p))p=n.get(p);else{let I=this.obfuscateString(p);n.set(p,I),p=I}else S.dataType=="DateTime"&&!S.dateWithTime&&(p=new Date(p).toISOString().slice(0,10)+"T00:00:00.000Z");m-=this.STR(p).length}else p=null;g[h]=p,--m}if(i.push(g),--o,o<=0||m<=0)break}return await it.unparse(i,{header:!1})}getCSVHeaderLine(){return it.unparse([this._cols.map(e=>this.STR(e.name))])}addRow(e,i){if(this.dedupRows){let o="";for(let n of e)o+=this.STR(n)+"~";let m=Q(o);if(this._rowHashes.has(m))return!1;this._rowHashes.add(m)}return this._rows.push(e),this._origIndices.push(i??-1),!0}getOriginalRowIndex(e){return e<0||e>=this._origIndices.length?-1:this._origIndices[e]}reset(){this._cols=[],this._rows=[],this._origIndices=[],this._rowHashes=new Set,this._leafCardinality=null}setColumns(e){this._cols=e}};export{nt as a,ut as b,F as c,At as d,Mt as e,Rt as f,Tt as g,Nt as h,rt as i,st as j};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! @bicharts/shape-core — Apache-2.0. Bundled reference data: GeoNames (https://www.geonames.org/) CC BY 4.0; US Census/TIGER (public domain). Full text: NOTICE in this package. */
|
|
2
|
-
import{e as w,j as b}from"./chunk-
|
|
2
|
+
import{e as w,j as b}from"./chunk-L2HRDGGX.mjs";import C from"papaparse";var D=500,S=/^[+-]?\d{1,15}$/,E=/^[+-]?(\d{1,3}(,\d{3})*|\d+)(\.\d+)?([eE][+-]?\d+)?$/,R=/^\d{4}-\d{2}-\d{2}([T ]\d{2}:\d{2}(:\d{2}(\.\d+)?)?(Z|[+-]\d{2}:?\d{2})?)?$/,L=/^\d{1,2}[\/\-.]\d{1,2}[\/\-.]\d{2,4}$/;function I(n){let i=0,e=0,r=0,o=0;for(let a of n){if(a==null)continue;if(typeof a=="number"){o++,e++,Number.isInteger(a)&&i++;continue}if(a instanceof Date){o++,r++;continue}if(typeof a=="boolean"){o++;continue}let c=String(a).trim();if(c!==""){if(o++,S.test(c)){i++,e++;continue}if(E.test(c)){e++;continue}if((R.test(c)||L.test(c))&&!isNaN(Date.parse(c))){r++;continue}}}return o===0?"String":r/o>.9?"DateTime":i===o?"Integer":e===o?"Decimal":"String"}function x(n,i){if(n==null)return null;if(typeof n=="number")return i==="String"?String(n):n;if(n instanceof Date)return i==="DateTime"?n:n.toISOString();if(typeof n=="boolean")return String(n);let e=String(n).trim();if(e==="")return null;switch(i){case"Integer":return S.test(e)?parseInt(e,10):null;case"Decimal":return E.test(e)?parseFloat(e.replace(/,/g,"")):null;case"DateTime":{let r=Date.parse(e);return isNaN(r)?null:new Date(r)}default:return e}}function M(n){let i=(n||"").toLowerCase().replace(/\(.*$/,"").trim();return/^(tinyint|smallint|int|integer|int2|int4|int8|bigint|serial|bigserial)$/.test(i)?"Integer":/^(decimal|numeric|money|smallmoney|float|real|double|double precision|number)$/.test(i)?"Decimal":/^(date|datetime|datetime2|smalldatetime|datetimeoffset|timestamp|timestamptz|time)$/.test(i)?"DateTime":(/^(bit|bool|boolean)$/.test(i),"String")}function g(n,i,e){if(n.length===0)throw new Error("ingest: no columns were resolved from the source. Supply column descriptors, or use a source shape that carries them (grid/table/objects/sql).");let r=n.length,o=n.map((t,s)=>t.dataType??I(i.slice(0,D).map(l=>l?.[s]))),a=new Set((e.measures??[]).map(t=>t.toLowerCase())),c=new Set((e.dimensions??[]).map(t=>t.toLowerCase())),d=new Set((e.nonAdditive??[]).map(t=>t.toLowerCase())),T=n.map((t,s)=>{let l=t.name.toLowerCase(),u=t.isMeasure??((o[s]==="Integer"||o[s]==="Decimal")&&!w(t.name));a.has(l)&&(u=!0),c.has(l)&&(u=!1);let h={name:t.name,dataType:o[s],isMeasure:u};return u&&d.has(l)&&(h.discourageAggregationAcrossGroups=!0),h}),m=new b;m.dedupRows=e.dedup!==!1,m.setColumns(T);for(let t=0;t<i.length;t++){let s=i[t];m.addRow(Array.from({length:r},(l,u)=>x(s?.[u],o[u])),t)}let y=m.getColumnsWithStats(e.privacyLevel??"20",e.locale??"en"),f=new Map(y.map(t=>[t.name,t]));n.forEach(t=>{let s=f.get(t.name);s&&(t.format&&(s.modelFormat=t.format),t.description&&(s.modelDesc=t.description))});for(let[t,s]of Object.entries(e.formats??{})){let l=f.get(t);l&&(l.modelFormat=s)}for(let[t,s]of Object.entries(e.descriptions??{})){let l=f.get(t);l&&(l.modelDesc=s)}for(let t of e.groupBy??[]){let s=f.get(t);s&&(s.isGrouping=!0)}return{columns:y,rows:m.toObjectArray(),totalRows:m.getRowCount(),index:m}}function p(n,i){return n.map(e=>Array.isArray(e)?e:i.map(r=>e[r]))}function A(n,i){let e=[],r=new Set,o=Math.min(n.length,Math.max(1,i));for(let a=0;a<o;a++){let c=n[a];if(!(!c||typeof c!="object"))for(let d of Object.keys(c))r.has(d)||(r.add(d),e.push(d))}return e}function $(n,i={}){switch(n.kind){case"csv":{let e=C.parse(n.text,{skipEmptyLines:!0});if(e.errors?.length&&e.data.length===0)throw new Error(`ingest: CSV parse failed - ${e.errors[0].message}`);let r=e.data;if(r.length<2)throw new Error("ingest: CSV needs a header line plus at least one data row.");let o=r[0].map(a=>(a??"").trim());return g(o.map(a=>({name:a})),r.slice(1),i)}case"grid":{if(!n.header?.length)throw new Error("ingest: 'grid' needs a non-empty header.");return g(n.header.map(e=>({name:(e??"").trim()})),n.rows??[],i)}case"table":{if(!n.columns?.length)throw new Error("ingest: 'table' needs at least one column descriptor.");let e=n.columns.map(r=>r.name);return g(n.columns,p(n.rows??[],e),i)}case"objects":{let e=n.rows??[],r=n.columns?.length?n.columns:A(e,i.columnScanLimit??D).map(o=>({name:o}));if(!r.length)throw new Error("ingest: 'objects' produced no columns - the rows array is empty or holds no object keys.");return g(r,p(e,r.map(o=>o.name)),i)}case"sql":{if(!n.schema?.length)throw new Error("ingest: 'sql' needs at least one schema column.");let e=n.schema.map(r=>{let o=M(r.dataType);return{name:r.name,dataType:o,isMeasure:r.isMeasure,format:r.format,description:r.description}});return g(e,p(n.rows??[],e.map(r=>r.name)),i)}default:{let e=n;throw new Error(`ingest: unknown source kind '${e?.kind}'. Expected one of: csv, grid, table, objects, sql.`)}}}export{M as a,$ as b};
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! @bicharts/shape-core — Apache-2.0. Bundled reference data: GeoNames (https://www.geonames.org/) CC BY 4.0; US Census/TIGER (public domain). Full text: NOTICE in this package. */
|
|
2
|
-
import{a as It,b as Lt}from"./chunk-
|
|
2
|
+
import{a as It,b as Lt}from"./chunk-SFNO5FEH.mjs";import{a as pt,b as yt,c as dt,d as gt,e as Mt,f as At,g as St,h as vt,i as Tt,j as kt}from"./chunk-L2HRDGGX.mjs";import{a as ht}from"./chunk-QEH7WTLB.mjs";import{a as Ct,b as Pt}from"./chunk-2LR73DNS.mjs";import{a as bt,b as Rt,c as xt}from"./chunk-NWDPA7DG.mjs";import{a as K,b as V,c as ut,d as ct,e as ft,f as mt}from"./chunk-SAASSIJ7.mjs";import{a as x,f as M,g,h as A,i as G,j as X,k as tt,l as H,m as p,n as nt,o as et,p as ot,q as S,r as it,s as rt,t as st,u as at,v as _,w as lt}from"./chunk-E6SVTFAW.mjs";import"./chunk-5MRZID36.mjs";import{a as Y,b as J,c as Q}from"./chunk-WBEROMBG.mjs";var w=2,j=400;function N(r){return!!M(r)}function P(r){let n=new Set;for(let e of r){if(e==null)continue;let s=x(String(e));if(!A(s)&&!n.has(s)&&(n.add(s),n.size>=j))break}return Array.from(n)}function C(r){let n=P(r);if(n.length===0||E(r)<95)return!1;let e=n.filter(l=>N(l)&&!p(l)).length,s=n.filter(l=>!N(l)&&!!p(l)).length;return e>=1&&e>=s}function q(r,n){if(!p(r))return!1;let e=M(r);return e?n==="world"?!0:H.has(e):!1}function E(r){let n=P(r);if(n.length===0)return 0;let e=0;for(let s of n)N(s)&&e++;return Math.round(e/n.length*1e3)/10}function v(r){let n=P(r);if(n.length===0)return 0;let e=0;for(let s of n)p(s)&&e++;return Math.round(e/n.length*1e3)/10}function B(r){let n=new Set;for(let e of r){if(e==null)continue;let s=String(e).trim();if(!A(x(s))&&!n.has(s)&&(n.add(s),n.size>=j))break}return Array.from(n)}function O(r){let n=B(r);if(n.length===0)return 0;let e=0;for(let s of n)S(s).length>0&&e++;return Math.round(e/n.length*1e3)/10}function Z(r,n){return V(n,K)?!0:B(r).some(e=>/^\d{5}(-\d{4})?$/.test(e.replace(/\.0+$/,"")))}function zt(r,n){let e=P(r);if(e.length===0)return 0;let s=0;for(let l of e)_(l,n)&&s++;return Math.round(s/e.length*1e3)/10}function $(r,n){let e=0;for(let s of P(r))n(s)&&e++;return e}function Gt(r,n){let e=0;for(let s of B(r))n(s)&&e++;return e}function _t(r,n,e,s){let l=[],c=[],o={},T={},k=new Set(r),u=t=>n.map(i=>i[t]);for(let t of["city","lat","lon","country"]){let i=e?.[t];i&&k.has(i)&&(o[t]=i)}let y=e?.zip;if(y&&k.has(y)){let t=u(y),i=O(t);i<95?c.push(`zip=${y} (only ${i}% of values read as ZIPs)`):Z(t,y)?o.zip=y:c.push(`zip=${y} (all values are 3-4 digits with nothing to say they are postal)`)}let m=e?.state;if(m&&k.has(m)){let t=u(m),i=P(t);i.length>0&&i.every(a=>q(a,s))?c.push(`state=${m} (every value is "CA", which is both Canada and California)`):C(t)?(c.push(`state=${m} (every value is a country, not a state)`),o.country||(o.country=m)):v(t)<95?c.push(`state=${m} (only ${v(t)}% of values are states/provinces)`):o.state=m}let d=new Set(Object.values(o).filter(Boolean)),b=r.filter(t=>!d.has(t)&&!t.startsWith("__"));if(!o.state){let t=null;for(let i of b){let a=u(i);if(C(a))continue;let f=v(a);if(f<95)continue;let z=$(a,R=>!!p(R)),W=$(a,R=>!!p(R)&&!q(R,s));z<w&&W<1||(!t||f>t.pct)&&(t={name:i,pct:f})}t&&(o.state=t.name,d.add(t.name),l.push(`state=${t.name} (${t.pct}% states/provinces)`))}if(!o.zip)for(let t of b){if(d.has(t))continue;let i=u(t),a=O(i);if(!(a<95)&&Z(i,t)&&!(Gt(i,f=>S(f).length>0)<w)){o.zip=t,d.add(t),l.push(`zip=${t} (${a}% ZIP codes)`);break}}let h=o.country;if(!h||!C(u(h))){let t=null;for(let i of b){if(d.has(i)||!C(u(i)))continue;let a=E(u(i));(!t||a>t.pct)&&(t={name:i,pct:a})}t&&(h&&(c.push(`country=${h} (only ${E(u(h))}% of values are country identifiers; using ${t.name} instead)`),T.country=h),o.country=t.name,d.add(t.name),l.push(`country=${t.name}`))}if(!o.city){let t=null;for(let i of b){if(d.has(i))continue;let a=u(i),f=zt(a,s);f<G||$(a,z=>_(z,s))<w||(!t||f>t.pct)&&(t={name:i,pct:f})}t&&(o.city=t.name,l.push(`city=${t.name} (${t.pct}% known cities)`))}let D=o.country?u(o.country):[],I=!!o.country&&C(D),U=I?new Set(D.map(t=>M(t==null?null:String(t))).filter(Boolean)).size:0,F=I&&U>=2,L=!!(o.lat&&o.lon)&&n.some(t=>{let i=+String(t[o.lat]??"").trim(),a=+String(t[o.lon]??"").trim();return isFinite(i)&&isFinite(a)&&i>=-90&&i<=90&&a>=-180&&a<=180&&String(t[o.lat]??"").trim()!==""&&String(t[o.lon]??"").trim()!==""});return o.lat&&o.lon&&!L&&c.push(`lat/lon=${o.lat}/${o.lon} (no row holds a usable coordinate pair)`),o.country&&!F&&!(o.city||o.state||o.zip||L)&&c.push(I?`country=${o.country} (every row resolves to the same country - nothing to place apart)`:`country=${o.country} (values are not country identifiers)`),{bind:!!(o.city||o.state||o.zip||F||L)?o:null,backfilled:l,refused:c,...Object.keys(T).length?{labelFallback:T}:{}}}export{G as CITY_ROLE_MATCH_PCT,xt as GET_RANDOM,kt as IndexedText,g as ROLE_MATCH_PCT,Rt as SIMPLE_STRING_HASH,bt as STR,v as admin1MatchPct,mt as buildGeoIsoColumn,st as buildGeoPointColumns,lt as cityMatchPct,at as cityTagsFor,vt as classifyAdditivity,Tt as classifyNumericValueNature,At as classifyTemporal,dt as countryRegion,ht as detectFormatSignature,ut as detectGeo,Q as detectOrdinalDomain,It as engineTypeForSqlType,St as hostAggHint,Lt as ingest,A as isBlankLike,tt as isCityTableLoaded,it as isGeoPointAmbiguity,Mt as isIdentifierName,ft as isJoinGeoKind,Y as isOrdinalFriendlyName,C as looksLikeCountryColumn,Pt as monthLookupFor,nt as normalizeCountry,Ct as normalizeMonthKey,x as normalizePlaceName,ot as normalizeZip5,X as registerCityTable,yt as registerIso3Regions,p as resolveAdmin1,rt as resolveGeoPoint,_t as resolvePointRoles,J as safeDistinctValuesToShip,gt as summarizeCountryRegions,pt as summarizeGeoExtent,ct as toGeoIso,O as zipMatchPct,S as zipPrefixCandidates,et as zipToPrefix3};
|
package/dist/indexedText.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! @bicharts/shape-core — Apache-2.0. Bundled reference data: GeoNames (https://www.geonames.org/) CC BY 4.0; US Census/TIGER (public domain). Full text: NOTICE in this package. */
|
|
2
|
-
import{e as a,f as b,g as c,h as d,i as e,j as f}from"./chunk-
|
|
2
|
+
import{e as a,f as b,g as c,h as d,i as e,j as f}from"./chunk-L2HRDGGX.mjs";import"./chunk-QEH7WTLB.mjs";import"./chunk-2LR73DNS.mjs";import"./chunk-NWDPA7DG.mjs";import"./chunk-SAASSIJ7.mjs";import"./chunk-E6SVTFAW.mjs";import"./chunk-5MRZID36.mjs";import"./chunk-WBEROMBG.mjs";export{f as IndexedText,d as classifyAdditivity,e as classifyNumericValueNature,b as classifyTemporal,c as hostAggHint,a as isIdentifierName};
|
package/dist/ingest.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! @bicharts/shape-core — Apache-2.0. Bundled reference data: GeoNames (https://www.geonames.org/) CC BY 4.0; US Census/TIGER (public domain). Full text: NOTICE in this package. */
|
|
2
|
-
import{a,b}from"./chunk-
|
|
2
|
+
import{a,b}from"./chunk-SFNO5FEH.mjs";import"./chunk-L2HRDGGX.mjs";import"./chunk-QEH7WTLB.mjs";import"./chunk-2LR73DNS.mjs";import"./chunk-NWDPA7DG.mjs";import"./chunk-SAASSIJ7.mjs";import"./chunk-E6SVTFAW.mjs";import"./chunk-5MRZID36.mjs";import"./chunk-WBEROMBG.mjs";export{a as engineTypeForSqlType,b as ingest};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bicharts/shape-core",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.25",
|
|
4
4
|
"description": "Host-agnostic data-shape profiler: ingest a dataset's rows through IValueCollection.addRow and read back the measured shape as an LLMColumnWithValue[] payload — types, cardinality, temporal/ordinal detection, geographic region and point resolution, cross-column signals. MEASUREMENT only: the policy that decides what a measured shape means for chart selection lives server-side.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
package/dist/chunk-YW3HUSG4.mjs
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
/*! @bicharts/shape-core — Apache-2.0. Bundled reference data: GeoNames (https://www.geonames.org/) CC BY 4.0; US Census/TIGER (public domain). Full text: NOTICE in this package. */
|
|
2
|
-
import{a as j}from"./chunk-QEH7WTLB.mjs";import{a as q,b as J}from"./chunk-2LR73DNS.mjs";import{a as $,b as Q,c as L}from"./chunk-NWDPA7DG.mjs";import{c as X}from"./chunk-SAASSIJ7.mjs";import{b as Y,f as Z}from"./chunk-E6SVTFAW.mjs";import{a as K,b as k,c as H}from"./chunk-WBEROMBG.mjs";var ot=[[24.4,49.4,-125,-66.9],[51.2,71.5,-168,-129.9],[18.9,22.3,-160.3,-154.8]],at=[7,72,-172,-52];function tt(u,t,n){return u>=n[0]&&u<=n[1]&&t>=n[2]&&t<=n[3]}function W(u,t){if(!u.length)return NaN;if(u.length===1)return u[0];let n=(u.length-1)*t,s=Math.floor(n),m=Math.ceil(n);return s===m?u[s]:u[s]+(u[m]-u[s])*(n-s)}var x=u=>Math.round(u*10)/10;function nt(u,t=3){let n=[],s=[],m=0,i=0;for(let f of u){let g=f?.lat,h=f?.lon;if(typeof g!="number"||typeof h!="number"||!isFinite(g)||!isFinite(h)||g<-90||g>90||h<-180||h>180)continue;n.push(g),s.push(h);let S=f?.country?String(f.country).trim().toUpperCase():"",y=S?F(S):null;y?((S==="US"||S==="USA")&&m++,y==="north-america"&&i++):(ot.some(I=>tt(g,h,I))&&m++,tt(g,h,at)&&i++)}let l=n.length;return l<t?null:(n.sort((f,g)=>f-g),s.sort((f,g)=>f-g),{pctUsa:x(m/l*100),pctNa:x(i/l*100),latP5:x(W(n,.05)),latP95:x(W(n,.95)),lonP5:x(W(s,.05)),lonP95:x(W(s,.95)),n:l})}var et={"north-america":"AG AI AW BB BL BM BQ BS BZ CA CR CU CW DM DO GD GL GP GT HN HT JM KN KY LC MF MQ MS MX NI PA PM PR SV SX TC TT US VC VG VI","south-america":"AR BO BR CL CO EC FK GF GY PE PY SR UY VE",europe:"AD AL AT AX BA BE BG BY CH CY CZ DE DK EE ES FI FO FR GB GG GI GR HR HU IE IM IS IT JE LI LT LU LV MC MD ME MK MT NL NO PL PT RO RS RU SE SI SJ SK SM UA VA",africa:"AO BF BI BJ BW CD CF CG CI CM CV DJ DZ EG EH ER ET GA GH GM GN GQ GW KE KM LR LS LY MA MG ML MR MU MW MZ NA NE NG RE RW SC SD SH SL SN SO SS ST SZ TD TG TN TZ UG YT ZA ZM ZW",asia:"AE AF AM AZ BD BH BN BT CC CN CX GE HK ID IL IN IO IQ IR JO JP KG KH KP KR KW KZ LA LB LK MM MN MO MV MY NP OM PH PK PS QA SA SG SY TH TJ TL TM TR TW UZ VN YE",oceania:"AS AU CK FJ FM GU KI MH MP NC NF NR NU NZ PF PG PN PW SB TK TO TV UM VU WF WS",antarctica:"AQ BV GS HM TF"},O=(()=>{let u=new Map;for(let t of Object.keys(et))for(let n of et[t].split(/\s+/))n&&u.set(n,t);return u})();function ut(u){for(let[t,n]of u){let s=O.get(t);s&&!O.has(n)&&O.set(n,s)}}ut(Y);function F(u){if(!u)return null;let t=String(u).trim().toUpperCase();if(!t)return null;let n=O.get(t);if(n)return n;let s=Z(String(u));return s?O.get(s)??null:null}function At(u,t=2){return B(u.map(n=>[n,1]),t)}function B(u,t=2){let n=new Map,s=0;for(let[l,f]of u){let g=F(l);if(!g)continue;let h=typeof f=="number"&&isFinite(f)&&f>0?f:1;n.set(g,(n.get(g)??0)+h),s+=h}if(s<t||!n.size)return null;let m="europe",i=-1;for(let l of Array.from(n.keys()).sort()){let f=n.get(l);f>i&&(m=l,i=f)}return{dominantGeoRegion:m,dominantGeoRegionPct:x(i/s*100),regionCount:n.size,n:s}}import it from"papaparse";var lt=12,mt=100,ct=.3,ft=.9,gt=120,dt=1900,ht=2100,pt=0,bt=20,yt=new Set(["id","ids","uuid","guid","key","zip","zipcode","postal","postalcode","ssn","code","codes","sku","isbn","msisdn","imei"]);function Mt(u){if(!u)return!1;let t=u.replace(/([a-z0-9])([A-Z])/g,"$1 $2").replace(/[_\-./]+/g," ").toLowerCase().split(/\s+/).filter(n=>n.length>0);return t.length===0?!1:yt.has(t[t.length-1])}var Ct=/\b(year|yr|fy|fiscal\s*year|calendar\s*year)\b/i,St=/^(?:(?:19|20)\d{2}(?:[-/ ]?(?:q[1-4]|w(?:0?[1-9]|[1-4]\d|5[0-3])|(?:0?[1-9]|1[0-2])))?|q[1-4][-/ ](?:19|20)\d{2}|(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[a-z]*[-/ ]?(?:19|20)?\d{2})$/i;function U(u){if(!Number.isInteger(u)||u<190001||u>210012)return!1;let t=u%100;return t>=1&&t<=12}function z(u){if(!Number.isInteger(u)||u<19000101||u>21001231)return!1;let t=Math.floor(u/100)%100,n=u%100;return t>=1&&t<=12&&n>=1&&n<=31}function _t(u,t){if(!t||!/\b(?:19|20)\d{2}\b/.test(u))return!1;let n=u.replace(/\b(?:19|20)\d{2}\b/g,"").replace(/[-/.,]/g," ").trim();if(!n)return!1;let s=q(n);return s.length>0&&t[s]!==void 0}function Rt(u){let{dataType:t,name:n,isMeasure:s,distinctCount:m}=u;if(t==="DateTime")return!0;if(s)return!1;if(t==="Integer"||t==="Decimal"){let i=u.minNum,l=u.maxNum;if(!(i!=null&&l!=null&&Number.isFinite(i)&&Number.isFinite(l)&&Number.isInteger(i)&&Number.isInteger(l)&&l>=i))return!1;let g=i>=1900&&l<=2100;if(g&&m>=2&&Ct.test(n)||g&&m>=3&&m===l-i+1)return!0;let h=U(i)&&U(l),S=z(i)&&z(l);if(h||S){if(!u.sampleValues)return!0;let y=0,I=0;for(let p of u.sampleValues){if(p==null||p==="")continue;y++;let e=Number(String(p).trim());if(Number.isFinite(e)&&(h?U(e):z(e))&&I++,y>=60)break}if(y>=2&&I===y)return!0}return!1}if(u.sampleValues){let i=u.locale?J(u.locale):null,l=0,f=0;for(let g of u.sampleValues){if(g==null||g==="")continue;l++;let h=String(g).trim();if((St.test(h)||_t(h,i))&&f++,l>=60)break}if(l>=2&&f/l>=.8)return!0}return!1}function Tt(u){let t=(u||"").trim().toLowerCase();return t.startsWith("sum ")||t.startsWith("total ")?"sum":t.startsWith("average ")||t.startsWith("avg. ")?"avg":t.startsWith("count ")||t.startsWith("distinct count ")||t.startsWith("count (distinct) ")?"count":t.startsWith("min ")||t.startsWith("minimum ")?"min":t.startsWith("max ")||t.startsWith("maximum ")?"max":null}function Nt(u){let{rows:t,measureColIdx:n,measureName:s,candidateDims:m,minNum:i,maxNum:l,discourageAgg:f}=u,g=100,h=.01,S="";for(let I of m){if(I.distinct<2||I.distinct>g)continue;let p=m.filter(o=>o.idx!==I.idx).map(o=>o.idx),e=new Map;for(let o of t){let d=o[n];if(typeof d!="number"||!Number.isFinite(d))continue;let M=p.length?p.map(_=>String(o[_])).join(S):"",C=e.get(M);C||(C={sum:0,cset:new Set},e.set(M,C)),C.sum+=d,C.cset.add(String(o[I.idx]))}let a=0,c=0,r=0;for(let o of e.values())o.cset.size<2||(r++,Math.abs(o.sum-100)<=100*h?a++:Math.abs(o.sum-1)<=1*h&&c++);if(r>=2&&(a/r>=.9||c/r>=.9))return{additivity:"part_of_whole",partOfWholeDim:I.name}}if(f)return{additivity:"intensive_rate"};let y=Tt(s);return y==="avg"||y==="min"||y==="max"?{additivity:"intensive_rate"}:y==="count"?{additivity:"additive"}:y==="sum"?{additivity:"unknown"}:i!=null&&l!=null&&Number.isFinite(i)&&Number.isFinite(l)&&i>=0&&l<=1.0001&&l>0?{additivity:"intensive_rate"}:{additivity:"unknown"}}function rt(u){let{dataType:t,isMeasure:n,name:s,distinct:m,nonblank:i,prec:l,maxval:f,minval:g}=u,h=f-g+1,S=h>0?m/h:1,y=i>0?m/i:0,I=t==="Decimal"||l>0;return Mt(s)?"Categorical":S>=ft&&(K(s)&&m<=gt||g>=dt&&f<=ht||g>=pt&&f<=bt)?"Ordinal":n||m>lt&&(I||m>=mt||y>=ct)?"Continuous":"Categorical"}var st=class{_computedStatsForLevel=null;_cols=[];_rows=[];_origIndices=[];_rowHashes=new Set;dedupRows=!0;STR(t){return t==null?"":t.toString()}getPrecision(t){if(!isFinite(t))return 0;let s=t.toString().split(".")[1];return s?s.length:0}getColumns(){return this._cols}hasTimeComponent(t){return!(t instanceof Date)||isNaN(t.getTime())?!1:t.getHours()!==0||t.getMinutes()!==0||t.getSeconds()!==0||t.getMilliseconds()!==0}getColumnsWithStats(t,n){if(this._computedStatsForLevel==t)return this._cols;let s=0,m=parseInt(t),i=[];for(let e of this._cols){let a=new Map,c=[],r=0,o=0,d=0,M=null,C=null,_=null,E=!1,v=!0,w=!0,A=null,N=!1;for(let T of this._rows){let b=T[s],R=this.STR(b);if(R!=""){if(r+=R.length,a.set(R,(a.get(R)||0)+1),o++,e.dataType=="Integer"||e.dataType=="Decimal"||e.dataType=="DateTime")if((M===null||b<M)&&(M=b),(C===null||b>C)&&(C=b),e.dataType=="Integer"||e.dataType=="Decimal"){if(_===null?_=b:_+=b,e.dataType=="Decimal"){let D=this.getPrecision(b);D>d&&(d=D)}c.push(b)}else E=E||this.hasTimeComponent(b);N&&(b<A?v=!1:b>A&&(w=!1)),A=b,N=!0}}if(o>=3&&(v&&!w?e.sortedDirection="asc":w&&!v?e.sortedDirection="desc":e.sortedDirection="none"),o>0&&a.size>0&&!e.isMeasure){let T=0,b=Number.MAX_SAFE_INTEGER;for(let R of a.values())R>T&&(T=R),R<b&&(b=R);if(e.modalShare=T/o,e.minGroupCount=b===Number.MAX_SAFE_INTEGER?null:b,a.size===2){let R=[...a.keys()].map(V=>$(V).trim().toLowerCase()).sort(),G=[["false","true"],["no","yes"],["n","y"],["0","1"],["f","t"]].some(V=>R[0]===V[0]&&R[1]===V[1]),P=/^(is|has)[_a-z0-9]|(?:default|churn|fraud|approv|convert|active|cancel|delinquen|flag|paid|win|pass|fail)/i.test(e.name);(G||P)&&(e.isBinaryFlag=!0)}}e.dataType=="DateTime"&&(e.dateWithTime=E),e.isTemporal=Rt({dataType:e.dataType,name:e.name,isMeasure:e.isMeasure,distinctCount:a.size,minNum:typeof M=="number"?M:null,maxNum:typeof C=="number"?C:null,sampleValues:a.keys(),locale:n}),this.updateColumnStats10(m,e,o,a,d,C,M),this.updateColumnStats20(m,c,o,_,e,r,E,M,C,d,a,n),!e.isMeasure&&(e.dataType==="Integer"||e.dataType==="Decimal")&&rt({dataType:e.dataType,isMeasure:!1,name:e.name,distinct:a.size,nonblank:o,prec:d,maxval:typeof C=="number"?C:0,minval:typeof M=="number"?M:0})==="Continuous"&&(e.isMeasure=!0),i[s]=e.isMeasure||a.size>2e3?null:new Set(a.keys()),s++}let l=[];this._cols.forEach((e,a)=>{e.isMeasure||l.push({idx:a,distinct:e.distinctCount??0,name:e.name})}),this._cols.forEach((e,a)=>{if(!e.isMeasure)return;let c=typeof e.lowValue=="number"?e.lowValue:null,r=typeof e.highValue=="number"?e.highValue:null,o=Nt({rows:this._rows,measureColIdx:a,measureName:e.name,candidateDims:l,minNum:c,maxNum:r,discourageAgg:!!e.discourageAggregationAcrossGroups});e.additivity=o.additivity,o.partOfWholeDim&&(e.partOfWholeDim=o.partOfWholeDim)});let f=50,g=l.filter(e=>e.distinct>=2&&e.distinct<=f);this._cols.forEach((e,a)=>{if(!e.isMeasure)return;let c=[];for(let A of this._rows){let N=A[a];if(N==null||N==="")continue;let T=typeof N=="number"?N:parseFloat(N);isNaN(T)||c.push(T)}if(c.length===0)return;let r=[...c].sort((A,N)=>A-N),o=A=>r[Math.min(r.length-1,Math.max(0,Math.floor(A*(r.length-1))))],d=o(.5),M=o(.1),C=o(.9),_=Math.abs(d)>1e-9?Math.abs(d):Math.abs(r[r.length-1])>1e-9?Math.abs(r[r.length-1]):1;e.relativeDispersion=Math.round((C-M)/_*1e3)/1e3;let E=0;for(let A of c)E+=A;E/=c.length;let v=0;for(let A of c)v+=(A-E)*(A-E);if(v<=1e-12)return;let w=[];for(let A of g){let N=new Map;for(let b of this._rows){let R=b[a];if(R==null||R==="")continue;let D=typeof R=="number"?R:parseFloat(R);if(isNaN(D))continue;let G=this.STR(b[A.idx])+"",P=N.get(G);P?(P.s+=D,P.n++):N.set(G,{s:D,n:1})}let T=0;for(let b of N.values()){let R=b.s/b.n;T+=b.n*(R-E)*(R-E)}w.push({otherColumn:A.name,eta2:Math.round(T/v*1e4)/1e4})}w.length>0&&(e.groupDiscrimination=w)});let h=.97,S=5,y=this._cols.map((e,a)=>({c:e,i:a})).filter(e=>e.c.isMeasure);for(let e=0;e<y.length;e++)for(let a=e+1;a<y.length;a++){let c=y[e].i,r=y[a].i,o=0,d=0,M=0,C=0,_=0,E=0;for(let N of this._rows){let T=N[c],b=N[r];if(T==null||T===""||b===null||b===void 0||b==="")continue;let R=typeof T=="number"?T:parseFloat(T),D=typeof b=="number"?b:parseFloat(b);isNaN(R)||isNaN(D)||(o++,d+=R,M+=D,C+=R*R,_+=D*D,E+=R*D)}if(o<S)continue;let v=o*C-d*d,w=o*_-M*M;if(v<=1e-12||w<=1e-12)continue;let A=(o*E-d*M)/Math.sqrt(v*w);if(Math.abs(A)>=h){let N=y[e].c,T=y[a].c;N.collinearWithMeasures||(N.collinearWithMeasures=[]),T.collinearWithMeasures||(T.collinearWithMeasures=[]),N.collinearWithMeasures.push(T.name),T.collinearWithMeasures.push(N.name)}}for(let e=0;e<this._cols.length;e++){let a=i[e];if(!a||a.size===0)continue;let c=[];for(let r=0;r<this._cols.length;r++){if(r===e)continue;let o=i[r];if(!o||o.size===0)continue;let d=0;for(let C of a)o.has(C)&&d++;let M=Math.round(d/a.size*100);M>0&&c.push({otherColumn:this._cols[r].name,percentShared:M})}c.length>0&&(this._cols[e].sharedValueRatioWithOthers=c)}let I=50,p=[];for(let e=0;e<this._cols.length;e++){let a=i[e];a&&a.size>=2&&a.size<=I&&p.push(e)}if(p.length>=2){let e=new Map,a=(r,o)=>r+":"+o;for(let r=0;r<p.length;r++)for(let o=r+1;o<p.length;o++)e.set(a(p[r],p[o]),new Map);for(let r of this._rows)for(let o=0;o<p.length;o++){let d=this.STR(r[p[o]])+"";for(let M=o+1;M<p.length;M++){let C=""+this.STR(r[p[M]]),_=e.get(a(p[o],p[M])),E=d+""+C;_.set(E,(_.get(E)??0)+1)}}let c=new Map;for(let r=0;r<p.length;r++)for(let o=r+1;o<p.length;o++){let d=p[r],M=p[o],C=i[d].size,_=i[M].size,E=e.get(a(d,M)),v=E.size,w=0;for(let b of E.values())(w===0||b<w)&&(w=b);let A=Math.round(v/(C*_)*100),N=v===C,T=v===_;if((this._cols[d].categoricalPairStats||=[]).push({otherColumn:this._cols[M].name,fillPct:A,determinesOther:N,minCellCount:w,distinctCombinations:v}),(this._cols[M].categoricalPairStats||=[]).push({otherColumn:this._cols[d].name,fillPct:A,determinesOther:T,minCellCount:w,distinctCombinations:v}),N&&C>_){let b=c.get(d);(!b||_<b.card)&&c.set(d,{name:this._cols[M].name,card:_,ratio:_/C})}if(T&&_>C){let b=c.get(M);(!b||C<b.card)&&c.set(M,{name:this._cols[d].name,card:C,ratio:C/_})}}for(let[r,o]of c)this._cols[r].primaryParentColumn=o.name,this._cols[r].nestingRatio=Math.round(o.ratio*100)/100}return this._computedStatsForLevel=t,this._cols}updateColumnStats20(t,n,s,m,i,l,f,g,h,S,y,I){if(t>=20){let p=[...n].sort((c,r)=>c-r),e=Math.floor(p.length/2);if(s>0&&m!=null?i.avgValue=i.dataType=="Integer"?Math.round(m/s):m/s:i.avgValue=null,s>0&&(i.avgLength=l/s),this._rows.length>0&&(i.dataType!="DateTime"||f||!g?i.lowValue=g:i.lowValue=new Date(g).toISOString().slice(0,10)+"T00:00:00.000Z",i.dataType!="DateTime"||f||!h?i.highValue=h:i.highValue=new Date(h).toISOString().slice(0,10)+"T00:00:00.000Z",i.medianValue=p.length===0?null:p.length%2===0?i.dataType=="Integer"?Math.round((p[e-1]+p[e])/2):(p[e-1]+p[e])/2:p[e],i.numericPrecision=S),p.length>0){let c=i.avgValue??0,r=p.length,o=0,d=0;for(let C of p){let _=C-c;o+=_*_,d+=_*_*_}let M=o/r;i.skewness=M===0?0:d/r/Math.pow(Math.sqrt(M),3),i.stdDev=Math.sqrt(M)}m!==null&&(i.sum=m);let a=Array.from(y.entries()).sort((c,r)=>r[1]-c[1]);if(i.topCategories=a.slice(0,10).map(c=>c[1]),!i.isMeasure&&a.length>0){if(i.dataType==="String"){let r=a.map(d=>d[0]);i.formatSignature=j(r);let o=/^(UUID|EMAIL_LIKE|URL_LIKE|PHONE_LIKE|NUMERIC_ID|HEX_ID|OPAQUE_ID_ALPHANUMERIC|DATE_STR|BOOLEAN)$/;i.formatSignature&&!o.test(i.formatSignature)&&(i.avgLength===void 0||i.avgLength>=3)&&(i.isFreeText=!0)}else i.topCategoryValues=a.slice(0,5).map(r=>r[0]);if(t>=20){let r=a.map(d=>d[0]),o=H(r,I);if(o!==null)i.ordinalPattern=o.pattern,i.orderedDomain=o.orderedDomain;else if(i.dataType==="String"){let d=k(r);d!==null&&(i.safeDistinctValues=d)}}let c=X(a.map(r=>r[0]),i.name,I);if(c!==null&&(i.geoKind=c.geoKind,i.geoMatchPct=c.geoMatchPct,c.geoAmbiguous&&(i.geoAmbiguous=!0),c.geoKind.indexOf("country")===0)){let r=B(a);r&&(i.dominantGeoRegion=r.dominantGeoRegion,i.dominantGeoRegionPct=r.dominantGeoRegionPct,i.geoRegionCount=r.regionCount)}}if(p.length>=8&&(i.dataType==="Integer"||i.dataType==="Decimal")){let c=r=>p[Math.floor((p.length-1)*r)];i.quantiles={p05:c(.05),p25:c(.25),p75:c(.75),p95:c(.95)}}}}updateColumnStats10(t,n,s,m,i,l,f){t>=10&&(n.blankCount=this._rows.length-s,n.distinctCount=m.size+(n.blankCount!=0?1:0),n.valueNature="Categorical",s>0&&(n.dataType==="Integer"||n.dataType==="Decimal"?n.valueNature=rt({dataType:n.dataType,isMeasure:!!n.isMeasure,name:n.name,distinct:m.size,nonblank:s,prec:i,maxval:l,minval:f}):n.dataType==="DateTime"&&(n.valueNature=n.dateWithTime?"Continuous":"Ordinal")))}getRowCount(){return this._rows.length}getLeafCardinality(){if(this._leafCardinality!==null)return this._leafCardinality;let t=[];if(this._cols.forEach((s,m)=>{s.isMeasure||t.push(m)}),t.length===0)return this._leafCardinality=this._rows.length,this._leafCardinality;let n=new Set;for(let s of this._rows)n.add(JSON.stringify(t.map(m=>s[m])));return this._leafCardinality=n.size,this._leafCardinality}_leafCardinality=null;getGeoExtent(){if(this._geoExtentComputed)return this._geoExtent;this._geoExtentComputed=!0,this._geoExtent=null;let t=l=>String(l||"").replace(/([a-z0-9])([A-Z])/g,"$1 $2").toLowerCase().split(/[^a-z]+/),n=-1,s=-1,m=-1;if(this._cols.forEach((l,f)=>{let g=t(l.name),h=l.dataType==="Integer"||l.dataType==="Decimal";h&&n<0&&g.some(S=>S==="lat"||S==="latitude")?n=f:h&&s<0&&g.some(S=>S==="lon"||S==="lng"||S==="long"||S==="longitude")&&(s=f),m<0&&!l.isMeasure&&typeof l.geoKind=="string"&&l.geoKind.indexOf("country")===0&&(m=f)}),n<0||s<0)return null;let i=[];for(let l of this._rows){let f=l[n],g=l[s],h=f==null||f===""?null:Number(f),S=g==null||g===""?null:Number(g);i.push({lat:h,lon:S,country:m>=0&&F(this.STR(l[m]))?this.STR(l[m]):null})}return this._geoExtent=nt(i),this._geoExtent}_geoExtent=null;_geoExtentComputed=!1;obfuscateString(t){let n="abcdefghijklmnopqrstuvwxyz",s=()=>n[Math.floor(L()*n.length)],m=()=>s().toUpperCase(),i=()=>Math.floor(L()*10).toString(),l=e=>/[A-Z]/.test(e)?m():/[a-z]/.test(e)?s():/[0-9]/.test(e)?i():e,f=t.includes("@")&&t.includes("."),g=f?t.split("@"):[t],h=f?g[1]:"",S=h.endsWith(".com"),y=S?".com":"",I=S?h.slice(0,-4):h,p=e=>{let a="";for(let c of e)a+=l(c),/[a-zA-Z0-9]/.test(c)&&L()<.2&&(a+=l(c));if(a.length>6){let c=Math.floor(L()*Math.min(3,a.length-6));for(let r=0;r<c;r++){let o=0;for(;o<5;){let d=Math.floor(L()*a.length);if(/[a-zA-Z0-9]/.test(a[d])){a=a.slice(0,d)+a.slice(d+1);break}o++}}}return a};if(f){let e=p(g[0]),a=p(I);return`${e}@${a}${y}`}else return p(t)}toObjectArray(){return this._rows.map(t=>{let n={};return this._cols.forEach((s,m)=>{n[s.name]=t[m]}),n})}async getCSVAsync(t){let n=[],s=t?25:1e7,m=t?4e3:1e7,i=new Map;for(let f of this._rows){let g={};for(let h=0;h<this._cols.length;h++){let S=this._cols[h],y=f[h];if(this.STR(y)!=""){if(t&&S.dataType=="String")if(i.has(y))y=i.get(y);else{let I=this.obfuscateString(y);i.set(y,I),y=I}else S.dataType=="DateTime"&&!S.dateWithTime&&(y=new Date(y).toISOString().slice(0,10)+"T00:00:00.000Z");m-=this.STR(y).length}else y=null;g[h]=y,--m}if(n.push(g),--s,s<=0||m<=0)break}return await it.unparse(n,{header:!1})}getCSVHeaderLine(){return it.unparse([this._cols.map(t=>this.STR(t.name))])}addRow(t,n){if(this.dedupRows){let s="";for(let i of t)s+=this.STR(i)+"~";let m=Q(s);if(this._rowHashes.has(m))return!1;this._rowHashes.add(m)}return this._rows.push(t),this._origIndices.push(n??-1),!0}getOriginalRowIndex(t){return t<0||t>=this._origIndices.length?-1:this._origIndices[t]}reset(){this._cols=[],this._rows=[],this._origIndices=[],this._rowHashes=new Set,this._leafCardinality=null}setColumns(t){this._cols=t}};export{nt as a,ut as b,F as c,At as d,Mt as e,Rt as f,Tt as g,Nt as h,rt as i,st as j};
|