forked from jsonstat/toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.mjs
More file actions
2 lines (2 loc) · 21.1 KB
/
module.mjs
File metadata and controls
2 lines (2 loc) · 21.1 KB
1
2
// jsonstat-toolkit v2.2.2 Copyright 2026 Xavier Badosa https://jsonstat.com
const t=t=>"[object ArrayBuffer]"===Object.prototype.toString.call(t.buffer),e=t=>["Int8Array","Uint8Array","Uint8ClampedArray","Int16Array","Uint16Array","Int32Array","Uint32Array","Float32Array","Float64Array","BigInt64Array","BigUint64Array"].includes(t);function n(l,i){const s=(n,l,i)=>{if(0===Object.entries(n).length)return null;let s,r=[];if(i&&!e(i.name)&&(i=null),"string"==typeof n&&(n=[n]),Array.isArray(n)||t(n)){if(n.length===l)return i&&-1===n.findIndex(t=>null==t)?i.from(n):n;if(1===n.length)return Array(l).fill(n[0])}r=new Array(l).fill(null);const o=Object.keys(n);for(s=0;s<o.length;s++){const t=Number(o[s]);t<l&&(r[t]=n[o[s]])}return r},r=t=>{const e=void 0===t.index?t.label:t.index;return Array.isArray(e)?e.length:Object.keys(e).length};let o,a;if(this.length=0,this.id=[],null!=l)switch(this.class=l.class||"bundle",this.class){case"bundle":if(this.error=null,this.length=0,null===l||"object"!=typeof l)return void(this.class=null);if(l.hasOwnProperty("error"))return void(this.error=l.error);if("dataset"===l.class||"collection"===l.class||"dimension"===l.class)return new n(l);const e=Object.keys(l);this.__tree__=l,this.length=e.length,this.id=e;break;case"dataset":this.__tree__=o=l.hasOwnProperty("__tree__")?l.__tree__:l,this.label=o.label||null,this.note=o.note||null,this.link=o.link||null,this.href=o.href||null,this.updated=o.updated||null,this.source=o.source||null,this.extension=o.extension||null;let u=0;const c=o.size||o.dimension&&o.dimension.size;if(this.size=c,this.value=o.hasOwnProperty("value")&&null!==o.value&&0!==o.value.length?o.value:{},u=Array.isArray(this.value)||t(this.value)?this.value.length:c.reduce((t,e)=>t*e,1),this.value=s(this.value,u,i),this.status=o.hasOwnProperty("status")&&null!==o.status?s(o.status,u):null,o.hasOwnProperty("dimension")){const t=o.dimension,e=o.role||!o.version&&t.role||null,n=o.id||t.id,l=c.length,i=t=>{e.hasOwnProperty(t)||(e[t]=null)};if(!Array.isArray(n)||!Array.isArray(c)||n.length!=l)return;if(this.length=l,this.id=n,e&&(i("time"),i("geo"),i("metric"),i("classification")),e&&null===e.classification){let t=[];for(const n of["time","geo","metric"]){const l=e[n];null!==l&&(t=t.concat(l))}const l=n.filter(e=>!t.includes(e));e.classification=l.length?l:null}this.role=e,this.n=u;for(let e=0,l=this.length;e<l;e++)if(t[n[e]].category.hasOwnProperty("index")){if(Array.isArray(t[n[e]].category.index)){const l={};t[n[e]].category.index.forEach((t,e)=>{l[t]=e}),t[n[e]].category.index=l}}else{let l=0;for(a in t[n[e]].category.index={},t[n[e]].category.label)t[n[e]].category.index[a]=l++}}else this.length=0;break;case"dimension":if(!l.hasOwnProperty("__tree__"))return new n({version:"2.0",class:"dataset",dimension:{d:l},id:["d"],size:[r(l.category)],value:[null]}).Dimension(0);o=l.__tree__;const h=[],f=o.category;if(!o.hasOwnProperty("category"))return;if(!f.hasOwnProperty("label"))for(a in f.label={},f.index)f.label[a]=a;for(a in f.index)h[f.index[a]]=a;this.__tree__=o,this.label=o.label||null,this.note=o.note||null,this.link=o.link||null,this.href=o.href||null,this.id=h,this.length=h.length,this.role=l.role,this.hierarchy=f.hasOwnProperty("child"),this.extension=o.extension||null;break;case"category":const d=l.child;this.id=d,this.length=null===d?0:d.length,this.index=l.index,this.label=l.label,this.note=l.note||null,this.unit=l.unit,this.coordinates=l.coord;break;case"collection":if(this.length=0,this.label=l.label||null,this.note=l.note||null,this.link=l.link||null,this.href=l.href||null,this.updated=l.updated||null,this.source=l.source||null,this.extension=l.extension||null,null!==this.link&&l.link.item){const t=l.link.item;this.length=Array.isArray(t)?t.length:0,this.length&&(this.id=t.map(t=>t.href))}}}n.prototype.Item=function(t){return null!==this&&"collection"===this.class&&this.length?"number"==typeof t?t>this.length||t<0?null:this.link.item[t]:"object"!=typeof t||null===t?this.link.item:t.class?this.link.item.filter(e=>{if(e.class!==t.class)return!1;if("dataset"===t.class&&"boolean"==typeof t.embedded){const n=e.id&&e.size&&e.dimension;return t.embedded?n:!n}return!0}):null:null},n.prototype.Dataset=function(t){if(null===this)return null;if("dataset"===this.class)return void 0!==t?this:[this];if("collection"===this.class){const e=this.Item({class:"dataset",embedded:!0});if(void 0===t)return e.map(t=>new n(t));if("number"==typeof t&&t>=0&&t<e.length)return new n(e[t]);if("string"==typeof t){const l=e.find(e=>e.href===t);if(l)return new n(l)}return null}if("bundle"!==this.class)return null;if(void 0===t)return this.id.map(t=>this.Dataset(t));if("number"==typeof t){const e=this.id[t];return void 0!==e?this.Dataset(e):null}const e=this.__tree__[t];return void 0===e?null:new n({class:"dataset",__tree__:e})},n.prototype.Dimension=function(t,e){e="boolean"!=typeof e||e;const l=(t,e)=>{if(null!==t)for(let n in t)for(let l=null!==t[n]?t[n].length:0;l--;)if(t[n][l]===e)return n;return null};if(null===this||"dataset"!==this.class)return null;if(void 0===t)return this.id.map(t=>this.Dimension(t));if("number"==typeof t){const n=this.id[t];return void 0!==n?this.Dimension(n,e):null}const i=this.role;if("object"==typeof t){if(t.hasOwnProperty("role")){const n=[];for(let s=0,r=this.id.length;s<r;s++){const r=this.id[s];l(i,r)===t.role&&n.push(this.Dimension(r,e))}return void 0===n[0]?null:n}return null}const s=this.__tree__.dimension;if(void 0===s)return null;const r=s[t];return void 0===r?null:e?new n({class:"dimension",__tree__:r,role:l(i,t)}):((t,e)=>{let n=[];for(let l in t)n[t[l]]=e[l];return n})(r.category.index,r.category.label)},n.prototype.Category=function(t){if(null===this||"dimension"!==this.class)return null;if(void 0===t)return this.id.map(t=>this.Category(t));if("number"==typeof t){const e=this.id[t];return void 0!==e?this.Category(e):null}const e=this.__tree__.category;if(void 0===e)return null;const l=e.index[t];if(void 0===l)return null;const i=e.unit&&e.unit[t]||null,s=e.coordinates&&e.coordinates[t]||null,r=e.child&&e.child[t]||null,o=e.note&&e.note[t]||null;return new n({class:"category",index:l,label:e.label[t],note:o,child:r,unit:i,coord:s})},n.prototype.Dice=function(e,l,i){let s,r,o,a;const u=(t,e)=>t.hasOwnProperty(e)&&!!t[e];if(null===this||"dataset"!==this.class||null===this.value)return null;if("object"!=typeof e)return this;"object"!=typeof l?("boolean"==typeof l&&!0===l&&(s=!0),"boolean"==typeof i&&!0===i||(i=!1)):(s=u(l,"clone"),i=u(l,"drop"),r=u(l,"stringify"),o=u(l,"ovalue"),a=u(l,"ostatus"));let c,h=[],f=[];const d=this.value,y=s?new n(structuredClone(this)):this,p=y.status,b=(t,e)=>{const n=((t,e)=>{const n={};return Array.isArray(t[e])?(t[e].forEach((t,e)=>{null!==t&&(n[String(e)]=t)}),n):t[e]})(t,e);delete t[e],t[e]=n};Array.isArray(e)&&(e=(t=>{const e={};return t.forEach(t=>{e[t[0]]=t[1]}),e})(e)),null===e&&(e={});const g=Object.keys(e);if(g.length>0){g.forEach(t=>{const n=e[t];Array.isArray(n)||(e[t]=[n]),0===e[t].length&&delete e[t]}),i&&(e=(t=>{const e={};return Object.keys(t).forEach(n=>{e[n]=y.Dimension(n).id.filter(e=>-1===t[n].indexOf(e))}),e})(e));const n={};g.forEach(t=>{n[t]=new Set(e[t])}),y.Unflatten((t,e)=>{for(let e=0;e<g.length;e++)if(!n[g[e]].has(t[g[e]]))return;h.push(e.value),f.push(e.status)}),g.forEach(t=>{const n=y.Dimension(t).id,l={};let i=0;y.size[y.id.indexOf(t)]=e[t].length,n.forEach(n=>{-1!==e[t].indexOf(n)&&(l[n]=i,i++)}),y.__tree__.dimension[t].category.index=l}),y.n=h.length,y.value=y.__tree__.value=t(d)?((t,e)=>e.from(t))(h,d.constructor):h,y.status=y.__tree__.status=null!==p?f:null}return r?(c=y.__tree__,c.hasOwnProperty("id")||(c.version="2.0",c.hasOwnProperty("class")||(c.class="dataset"),c.id=c.dimension.id,c.size=c.dimension.size,delete c.dimension.id,delete c.dimension.size,c.dimension.hasOwnProperty("role")&&(c.role=c.dimension.role,delete c.dimension.role)),c.hasOwnProperty("status")&&-1!==["null","{}","[]"].indexOf(JSON.stringify(c.status))&&delete c.status,c.hasOwnProperty("role")&&(delete c.role.classification,["geo","time","metric"].forEach(t=>{null===c.role[t]&&delete c.role[t]})),o&&b(c,"value"),a&&c.hasOwnProperty("status")&&b(c,"status"),JSON.stringify(c)):y},n.prototype.Slice=function(t){return null===this||"dataset"!==this.class||0===Object.entries(this.value).length?null:void 0===t?this:(Array.isArray(t)||(t=Object.keys(t).map(e=>[e,t[e]])),this.Dice(t.map(t=>[t[0],[t[1]]])))},n.prototype.Data=function(t,e){let n,l,i=[],s=t=>{for(let e in t)if(t.hasOwnProperty(e))return e},r={};if(null===this||"dataset"!==this.class||null===this.value)return null;if(void 0===t)return this.value.map((t,e)=>this.Data(e));if("boolean"!=typeof e&&(e=!0),"number"==typeof t){const n=this.value[t];return void 0===n?null:e?{value:n,status:this.status?this.status[t]:null}:n}let o="object";const a=this.__tree__,u=a.size||a.dimension&&a.dimension.size,c=u.length;if(Array.isArray(t)){if(!Array.isArray(t[0])){if(this.length!==t.length)return null;let l=1,s=0,r=[],o=[];for(n=0;n<c;n++)if(void 0!==t[n]){if("number"!=typeof t[n]||t[n]>=u[n])return null;l*=n>0?u[c-n]:1,s+=l*t[c-n-1]}else r.push(n),o.push(u[n]);if(r.length>1)return null;if(1===r.length){for(let l=0,s=o[0];l<s;l++){let s=[];for(n=0;n<c;n++)n!==r[0]?s.push(t[n]):s.push(l);i.push(this.Data(s,e))}return i}return e?{value:this.value[s],status:this.status?this.status[s]:null}:this.value[s]}o="array"}let h=[];const f=((t,e,n)=>{let l,i=[],o={};const a=t.dimension,u=t.id||a.id,c=t.size||a&&a.size;if("array"===n){for(l=e.length;l--;)o[e[l][0]]=e[l][1];e=o}for(let t=0,n=u.length;t<n;t++){const n=u[t],l=e[n];"string"==typeof l?i.push(l):1===c[t]?(n in r||(r[n]=s(a[n].category.index)),i.push(r[n])):i.push(null)}return i})(a,t,o),d=a.dimension,y=a.id||d.id;for(n=0,l=f.length;n<l;n++)h.push(d[y[n]].category.index[f[n]]);return this.Data(h,e)},n.prototype.toTable=function(t,n){if(null===this||"dataset"!==this.class||null===this.value)return null;1==arguments.length&&"function"==typeof t&&(n=t,t=null);const l=void 0!==(t=t||{field:"label",content:"label",vlabel:"Value",slabel:"Status",type:"array",status:!1,unit:!1,by:null,prefix:"",drop:[],meta:!1,comma:!1,bylabel:!1}).prefix?t.prefix:"";let i,s,r,o,a;"arrobj"!==t.type&&"objarr"!==t.type||void 0!==t.field||(t.field="id");const u="id"===t.field,c=t=>(u?"value":t)||"Value",h=t=>(u?"status":t)||"Status",f=this.__tree__;let d=!0===t.status;if("function"==typeof n){i=this.toTable(t);let e=[];const l="array"!==t.type?0:1,r="object"!==t.type?i.slice(l):i.rows.slice(0);for(a=r.length,s=0;s<a;s++){const t=n.call(this,r[s],s);void 0!==t&&e.push(t)}return"object"===t.type?{cols:i.cols,rows:e}:("array"===t.type&&e.unshift(i[0]),e)}if("arrobj"===t.type||"objarr"===t.type){let n=[],o=function(){},u={};const h=f.role&&f.role.metric,y=this,p=y.id,b=t.by&&-1!==p.indexOf(t.by)?t.by:null,g=!0===t.meta,m=void 0!==t.drop&&Array.isArray(t.drop)?t.drop:[],v=!0===t.comma,w=!0===t.bylabel,A=y.value.constructor,_=n=>{const i=c(t.vlabel);let s,r={};return"objarr"===t.type&&(s=null===b&&e(A.name)?t=>{r[t]=t===i?A.from(n,e=>e[t]):n.map(e=>e[t])}:t=>{r[t]=n.map(e=>e[t])},Object.keys(n[0]).forEach(s),n=r),g?(r={},p.forEach(t=>{const e=y.Dimension(t);r[t]={label:e.label,role:e.role,categories:{id:e.id,label:y.Dimension(t,!1)}}}),{meta:{label:y.label,source:y.source,updated:y.updated,id:p,status:d,unit:t.unit,by:b,bylabel:w,drop:null!==b&&m.length>0?m:null,prefix:null!==b?l||"":null,comma:v,dimensions:r},data:n}):n};b&&(t.field="id"),i=this.toTable({field:t.field,vlabel:t.vlabel,slabel:t.slabel,content:t.content,status:d});const x=i.shift();let j;if(null===b&&t.unit&&h){if("id"!==t.content)for(let t=h.length;t--;){const e=this.Dimension(h[t]);u[h[t]]={};for(let n=e.length;n--;)u[h[t]][e.Category(n).label]=e.id[n]}o=function(e,n){if(-1!==h.indexOf(e)){const l=f.dimension[e].category;l.unit?j.unit=l.unit["id"!==t.content?u[e][n]:n]:j.unit=null}},t.unit=!0}else t.unit=!1;for(a=i.length,s=0;s<a;s++){for(j={},r=i[s].length;r--;)j[x[r]]=i[s][r],o(x[r],i[s][r]);n.push(j)}if(v){const e=c(t.vlabel);n.forEach(function(t){null!==t[e]&&(t[e]=String(t[e]).replace(".",","))})}if(null!==b){const e={},i={};let s,r=[];m.forEach(function(t,e){(!y.Dimension(t)||y.Dimension(t).length>1)&&(m[e]="")});const o=p.filter(function(t){return t!==b&&-1===m.indexOf(t)}),a=y.Dimension(b),u=function(t,e){let n=[];return e.forEach(function(e){n.push(t[e])}),n.join("\t")},c=function(t,e){const n={};return e.forEach(function(e){n[e]=t[e]}),n};"id"!==t.content?w?s=function(t,e,n){t[e][`${l}${n[b]}`]=n.value}:(a.Category().forEach(function(t,e){i[t.label]=a.id[e]}),s=function(t,e,n){t[e][`${l}${i[n[b]]}`]=n.value}):s=function(t,e,n){t[e][`${l}${n[b]}`]=n.value},n.forEach(function(t){const n=u(t,o);void 0===e[n]&&(e[n]=c(t,o)),s(e,n,t,b)});for(let t in e)r.push(e[t]);return d=!1,_(r)}return _(n)}let y,p,b,g,m=[];if("object"===t.type){const t="number"==typeof this.value[0]||null===this.value[0]?"number":"string";y=function(t,e){const n=u&&t||e||t;k.push({id:t,label:n,type:"string"})},p=function(e,n,l){const i=c(e),s=h(n);l&&k.push({id:"status",label:s,type:"string"}),k.push({id:"value",label:i,type:t})},b=function(t){m.push({v:t})},g=function(t){m.push({v:t}),z.push({c:m})}}else y=function(t,e){const n=u&&t||e||t;k.push(n)},p=function(t,e,n){const l=c(t),i=h(e);n&&k.push(i),k.push(l),D.push(k)},b=function(t){m.push(t)},g=function(t){m.push(t),D.push(m)};const v=f.dimension,w=f.id||v.id,A=f.size||v.size,_=w.length;if(_!=A.length)return!1;let x=[],j=1,O=1,D=[],k=[],z=[];for(s=0;s<_;s++){const e=w[s];y(e,v[e].label),j*=A[s],O*=A[s];const n=new Array(A[s]),l=v[w[s]].category.index,i=new Array(A[s]),o=v[w[s]].category.label,a="id"!==t.content&&o;for(let t in l)i[l[t]]=t;for(r=0;r<A[s];r++){const t=i[r];n[r]=a?o[t]:t}x.push(n)}p(t.vlabel,t.slabel,d),a=x.length;const E=new Array(a);let P=1;for(s=a-1;s>=0;s--)E[s]=P,P*=A[s];const S=new Array(a);for(s=0;s<a;s++)S[s]=x[s].length;const I=this.value,C=this.status;for(o=0;o<j;o++){m=[];for(let t=0;t<a;t++)b(x[t][(o/E[t]|0)%S[t]]);d&&b(C?C[o]:null),g(I[o])}return"object"===t.type?{cols:k,rows:z}:D},n.prototype.node=function(){return this.__tree__},n.prototype.toString=function(){return this.class},n.prototype.Unflatten=function(t){if(null===this||"dataset"!==this.class||null===this.value||"function"!=typeof t)return null;const e=this.id,n=this.size,l=e.length,i=this.n,s=this.value,r=this.status,o=[],a=new Array(l),u=new Array(l);let c=1;for(let t=l-1;t>=0;t--)u[t]=c,c*=n[t],a[t]=this.Dimension(t).id;for(let c=0;c<i;c++){const i={};for(let t=0;t<l;t++)i[e[t]]=a[t][(c/u[t]|0)%n[t]];const h=t(i,{value:s[c],status:r?r[c]:null},c,o);void 0!==h&&o.push(h)}return o},n.prototype.Transform=function(t){if(null===this||"dataset"!==this.class||null===this.value)return null;if(null!=t){if("object"!=typeof t||void 0!==t.type&&"string"!=typeof t.type)return null}else t={};"arrobj"!==t.type&&"objarr"!==t.type||void 0!==t.field||(t.field="id");const e=this,n="arrobj"!==t.type&&"objarr"!==t.type&&"object"!==t.type?"array":t.type,l=e.id,i="array"!==n&&"object"!==n&&t.by&&l.includes(t.by)?t.by:null,s=t.comma||!1,r=null===i&&t.status||!1,o=t.content||"label",a=null===i?t.field||"label":"id",u="id"===t.field?"value":t.vlabel||"Value",c="id"===t.field?"status":t.slabel||"Status",h=t.meta||!1,f=i&&"string"==typeof t.prefix?t.prefix:"",d=Array.isArray(t.drop)?t.drop.filter(t=>{const n=e.Dimension(t);return n&&1===n.length}):[],y=l.filter(t=>!d.includes(t)),p=t=>null===t?null:String(t).replace(".",","),b=s?p:t=>t,g=y.length,m=new Array(g),v="label"===o?new Array(g):null;for(let t=0;t<g;t++){const n=y[t],l=e.Dimension(n),i=l.id;if(m[t]=i,v){const e=l.__tree__.category.label;if(e){const n=new Array(i.length);for(let t=0,l=i.length;t<l;t++)n[t]=e[i[t]];v[t]=n}else v[t]=null}}const w=e.size,A=l.length,_=e.n,x=e.value,j=e.status,O=new Array(A);let D=1;for(let t=A-1;t>=0;t--)O[t]=D,D*=w[t];const k=new Array(g);for(let t=0;t<g;t++)k[t]=l.indexOf(y[t]);const z=new Array(g),E=new Array(g);for(let t=0;t<g;t++){const e=k[t];z[t]=O[e],E[t]=w[e]}const P=v?(t,e)=>v[t]?v[t][e]:m[t][e]:(t,e)=>m[t][e];let S,I;switch(n){case"array":{const t="label"===a?y.map(t=>e.Dimension(t).label):y,n=r?[c,u]:[u];S=t.concat(n);const l=new Array(_);for(let t=0;t<_;t++){const e=new Array(g+(r?2:1));for(let n=0;n<g;n++){const l=(t/z[n]|0)%E[n];e[n]=P(n,l)}r?(e[g]=j?j[t]:null,e[g+1]=b(x[t])):e[g]=b(x[t]),l[t]=e}I=l;break}case"object":{const t="number"==typeof x[0]||null===x[0]?"number":"string",n=new Array(g+(r?2:1));for(let t=0;t<g;t++){const l=y[t],i=e.Dimension(l),s="id"===a?l:i.label||l;n[t]={id:l,label:s,type:"string"}}let l=g;r&&(n[l++]={id:"status",label:c,type:"string"}),n[l]={id:"value",label:u,type:t};const i=new Array(_);for(let t=0;t<_;t++){const e=new Array(g+(r?2:1));for(let n=0;n<g;n++){const l=(t/z[n]|0)%E[n];e[n]={v:P(n,l)}}let n=g;r&&(e[n++]={v:j?j[t]:null}),e[n]={v:x[t]},i[t]={c:e}}return{cols:n,rows:i}}case"objarr":{const t=s?t=>t.map(p):t=>t,n="id"===a?t=>t:t=>e.Dimension(t).label,o={},h=new Array(g);for(let t=0;t<g;t++)h[t]=n(y[t]);if(i){const t=l.indexOf(i),n=O[t],s=w[t],r=[],u=[],c=e.Dimension(i),d=new Map,p=new Array(s);for(let t=0;t<g;t++)y[t]!==i&&(r.push(t),u.push(h[t]));const v=r.length;for(let t=0;t<v;t++)o[u[t]]=[];const A=new Array(s);for(let t=0;t<s;t++){const e=c.id[t],n=f+("id"===a?e:c.Category(e).label);A[t]=n,p[t]=n,o[n]=[]}let j=0;for(let t=0;t<_;t++){let e,l="";for(let e=0;e<v;e++){const n=r[e],i=(t/z[n]|0)%E[n];e>0&&(l+="\0"),l+=m[n][i]}if(d.has(l))e=d.get(l);else{e=j++,d.set(l,e);for(let e=0;e<v;e++){const n=r[e],l=(t/z[n]|0)%E[n];o[u[e]].push(P(n,l))}for(let t=0;t<s;t++)o[A[t]].push(null)}const i=(t/n|0)%s,a=x[t];null!==a&&(o[p[i]][e]=b(a))}}else{r?(o[u]=t(e.value),o[c]=e.status):o[u]=t(e.value);for(let t=0;t<g;t++)o[h[t]]=new Array(_);for(let t=0;t<_;t++)for(let e=0;e<g;e++){const n=(t/z[e]|0)%E[e];o[h[e]][t]=P(e,n)}}I=o;break}default:{const t=i?new Map:null,n=new Array(g);for(let t=0;t<g;t++){const l=y[t],s=e.Dimension(l);n[t]={prop:"label"===a?s.label:l,isBy:i&&l===i}}const l=r?(t,e)=>{t[c]=e}:()=>{};if(i){const e=[];for(let i=0;i<_;i++){const s={};let r;for(let t=0;t<g;t++){const e=P(t,(i/z[t]|0)%E[t]);n[t].isBy?r=e:s[n[t].prop]=e}l(s,j?j[i]:null);const o=Object.values(s).join("|");let a=t.get(o),u=!1;a||(a=s,t.set(o,a),u=!0),a[f+r]=b(x[i]),u&&e.push(a)}I=e}else{const t=new Array(_);for(let e=0;e<_;e++){const i={};for(let t=0;t<g;t++){const l=(e/z[t]|0)%E[t];i[n[t].prop]=P(t,l)}i[u]=b(x[e]),l(i,j?j[e]:null),t[e]=i}I=t}}}if(S&&I.unshift(S),h){const t={};return l.forEach(n=>{const l=e.Dimension(n);t[n]={label:l.label,role:l.role,categories:{id:l.id,label:e.Dimension(n,!1)}}}),{meta:{type:n,label:e.label,source:e.source,updated:e.updated,id:l,status:r,by:i,drop:d.length>0?d:null,prefix:null!==i?f||"":null,comma:s,dimensions:t},data:I}}return I},n.prototype.toCSV=function(t){if(null===this||"dataset"!==this.class||null===this.value)return null;const e=(t=t||{}).delimiter||",",n=!1!==t.header,l=this.id,i=this.size,s=l.length,r=this.n,o=this.value,a=new Array(s),u=new Array(s);let c=1;for(let t=s-1;t>=0;t--)u[t]=c,c*=i[t],a[t]=this.Dimension(t).id;const h=new Array(n?r+1:r);let f=0;n&&(h[f++]=l.concat("value").join(e));for(let t=0;t<r;t++){let n=a[0][(t/u[0]|0)%i[0]];for(let l=1;l<s;l++)n+=e+a[l][(t/u[l]|0)%i[l]];n+=e+o[t],h[f++]=n}return h.join("\n")},n.prototype.unflattenIterator=function*(){if(null===this||"dataset"!==this.class||null===this.value)return;const t=this.id,e=this.size,n=t.length,l=this.n,i=this.value,s=this.status,r=new Array(n),o=new Array(n);let a=1;for(let t=n-1;t>=0;t--)o[t]=a,a*=e[t],r[t]=this.Dimension(t).id;for(let a=0;a<l;a++){const l={};for(let i=0;i<n;i++)l[t[i]]=r[i][(a/o[i]|0)%e[i]];l.value=i[a],l.status=s?s[a]:null,yield l}};const l=t=>{if(!t.ok)throw new Error(`${t.status} ${t.statusText}`);return t.json()};function i(t,e,i){const s="object"==typeof e?e:{method:"GET"};return"function"!=typeof i&&(i=null),i||"function"!=typeof e||(i=e),"object"==typeof t?new n(t,i):"version"===t?"2.2.2":fetch?fetch(t,s).then(l).then(t=>new n(t,i)).catch(t=>{throw t}):void 0}i.fromRows=function(t,e,l){const i=l&&l.measures?l.measures:[],s=l&&l.timeDimensions?l.timeDimensions:[],r=i.length,o=new Map;for(let e=0;e<t.length;e++)o.set(t[e],e);const a=new Set(i),u=[];for(let e=0;e<t.length;e++)a.has(t[e])||u.push(t[e]);const c=u.length,h=new Int32Array(c);for(let t=0;t<c;t++)h[t]=o.get(u[t]);const f=new Int32Array(r);for(let t=0;t<r;t++)f[t]=o.get(i[t]);const d=new Array(c),y=new Array(c);for(let t=0;t<c;t++)d[t]=[],y[t]=new Map;const p=e.length;for(let t=0;t<p;t++){const n=e[t];for(let t=0;t<c;t++){const e=String(n[h[t]]);y[t].has(e)||(y[t].set(e,d[t].length),d[t].push(e))}}const b=new Array(c+(r>0?1:0)),g=new Array(b.length);for(let t=0;t<c;t++)b[t]=u[t],g[t]=d[t].length;r>0&&(b[c]="metric",g[c]=r);const m=new Array(c);if(c>0){m[c-1]=r>0?r:1;for(let t=c-2;t>=0;t--)m[t]=m[t+1]*g[t+1]}let v=1;for(let t=0;t<g.length;t++)v*=g[t];const w=new Array(v).fill(null);for(let t=0;t<p;t++){const n=e[t];let l=0;for(let t=0;t<c;t++){const e=String(n[h[t]]);l+=y[t].get(e)*m[t]}if(r>0)for(let t=0;t<r;t++)w[l+t]=n[f[t]]}const A={};for(let t=0;t<c;t++){const e=d[t],n={},l={};for(let t=0;t<e.length;t++)n[e[t]]=t,l[e[t]]=e[t];A[u[t]]={category:{index:n,label:l}}}if(r>0){const t={},e={};for(let n=0;n<r;n++)t[i[n]]=n,e[i[n]]=i[n];A.metric={category:{index:t,label:e}}}const _={metric:r>0?["metric"]:null};if(s.length>0){const t=new Set(b),e=[];for(let n=0;n<s.length;n++)t.has(s[n])&&e.push(s[n]);_.time=e.length>0?e:null}return new n({version:"2.0",class:"dataset",id:b,size:g,dimension:A,role:_,value:w})};export{i as default};