-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport.mjs
More file actions
2 lines (2 loc) · 35.9 KB
/
import.mjs
File metadata and controls
2 lines (2 loc) · 35.9 KB
1
2
// jsonstat-suite v4.0.1 Copyright 2026 Xavier Badosa https://jsonstat.com
const e=e=>"[object ArrayBuffer]"===Object.prototype.toString.call(e.buffer),t=e=>["Int8Array","Uint8Array","Uint8ClampedArray","Int16Array","Uint16Array","Int32Array","Uint32Array","Float32Array","Float64Array","BigInt64Array","BigUint64Array"].includes(e);function n(l,i){const s=(n,l,i)=>{if(0===Object.entries(n).length)return null;let s,r=[];if(i&&!t(i.name)&&(i=null),"string"==typeof n&&(n=[n]),Array.isArray(n)||e(n)){if(n.length===l)return i&&-1===n.findIndex(e=>null==e)?i.from(n):n;if(1===n.length)return Array(l).fill(n[0])}for(s=0;s<l;s++)r.push(void 0===n[s]?null:n[s]);return r},r=e=>{const t=void 0===e.index?e.label:e.index;return Array.isArray(t)?t.length:Object.keys(t).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 t=Object.keys(l);this.__tree__=l,this.length=t.length,this.id=t;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)||e(this.value)?this.value.length:c.reduce((e,t)=>e*t,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 e=o.dimension,t=o.role||!o.version&&e.role||null,n=o.id||e.id,l=c.length,i=e=>{t.hasOwnProperty(e)||(t[e]=null)};if(!Array.isArray(n)||!Array.isArray(c)||n.length!=l)return;if(this.length=l,this.id=n,t&&(i("time"),i("geo"),i("metric"),i("classification")),t&&null===t.classification){let e=[];for(const n of["time","geo","metric"]){const l=t[n];null!==l&&(e=e.concat(l))}const l=n.filter(t=>!e.includes(t));t.classification=l.length?l:null}this.role=t,this.n=u;for(let t=0,l=this.length;t<l;t++)if(e[n[t]].category.hasOwnProperty("index")){if(Array.isArray(e[n[t]].category.index)){const l={};e[n[t]].category.index.forEach((e,t)=>{l[e]=t}),e[n[t]].category.index=l}}else{let l=0;for(a in e[n[t]].category.index={},e[n[t]].category.label)e[n[t]].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 f=[],d=o.category;if(!o.hasOwnProperty("category"))return;if(!d.hasOwnProperty("label"))for(a in d.label={},d.index)d.label[a]=a;for(a in d.index)f[d.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=f,this.length=f.length,this.role=l.role,this.hierarchy=d.hasOwnProperty("child"),this.extension=o.extension||null;break;case"category":const h=l.child;this.id=h,this.length=null===h?0:h.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 e=l.link.item;this.length=Array.isArray(e)?e.length:0,this.length&&(this.id=e.map(e=>e.href))}}}n.prototype.Item=function(e){return null!==this&&"collection"===this.class&&this.length?"number"==typeof e?e>this.length||e<0?null:this.link.item[e]:"object"!=typeof e||null===e?this.link.item:e.class?this.link.item.filter(t=>{if(t.class!==e.class)return!1;if("dataset"===e.class&&"boolean"==typeof e.embedded){const n=t.id&&t.size&&t.dimension;return e.embedded?n:!n}return!0}):null:null},n.prototype.Dataset=function(e){if(null===this)return null;if("dataset"===this.class)return void 0!==e?this:[this];if("collection"===this.class){const t=this.Item({class:"dataset",embedded:!0});if(void 0===e)return t.map(e=>new n(e));if("number"==typeof e&&e>=0&&e<t.length)return new n(t[e]);if("string"==typeof e){const l=t.find(t=>t.href===e);if(l)return new n(l)}return null}if("bundle"!==this.class)return null;if(void 0===e)return this.id.map(e=>this.Dataset(e));if("number"==typeof e){const t=this.id[e];return void 0!==t?this.Dataset(t):null}const t=this.__tree__[e];return void 0===t?null:new n({class:"dataset",__tree__:t})},n.prototype.Dimension=function(e,t){t="boolean"!=typeof t||t;const l=(e,t)=>{if(null!==e)for(let n in e)for(let l=null!==e[n]?e[n].length:0;l--;)if(e[n][l]===t)return n;return null};if(null===this||"dataset"!==this.class)return null;if(void 0===e)return this.id.map(e=>this.Dimension(e));if("number"==typeof e){const n=this.id[e];return void 0!==n?this.Dimension(n,t):null}const i=this.role;if("object"==typeof e){if(e.hasOwnProperty("role")){const n=[];for(let s=0,r=this.id.length;s<r;s++){const r=this.id[s];l(i,r)===e.role&&n.push(this.Dimension(r,t))}return void 0===n[0]?null:n}return null}const s=this.__tree__.dimension;if(void 0===s)return null;const r=s[e];return void 0===r?null:t?new n({class:"dimension",__tree__:r,role:l(i,e)}):((e,t)=>{let n=[];for(let l in e)n[e[l]]=t[l];return n})(r.category.index,r.category.label)},n.prototype.Category=function(e){if(null===this||"dimension"!==this.class)return null;if(void 0===e)return this.id.map(e=>this.Category(e));if("number"==typeof e){const t=this.id[e];return void 0!==t?this.Category(t):null}const t=this.__tree__.category;if(void 0===t)return null;const l=t.index[e];if(void 0===l)return null;const i=t.unit&&t.unit[e]||null,s=t.coordinates&&t.coordinates[e]||null,r=t.child&&t.child[e]||null,o=t.note&&t.note[e]||null;return new n({class:"category",index:l,label:t.label[e],note:o,child:r,unit:i,coord:s})},n.prototype.Dice=function(t,l,i){let s,r,o,a;const u=(e,t)=>e.hasOwnProperty(t)&&!!e[t];if(null===this||"dataset"!==this.class||null===this.value)return null;if("object"!=typeof t)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,f=[],d=[];const h=this.value,p=s?new n(JSON.parse(JSON.stringify(this))):this,y=p.status,b=(e,t)=>{const n=((e,t)=>{const n={};return Array.isArray(e[t])?(e[t].forEach((e,t)=>{null!==e&&(n[String(t)]=e)}),n):e[t]})(e,t);delete e[t],e[t]=n};Array.isArray(t)&&(t=(e=>{const t={};return e.forEach(e=>{t[e[0]]=e[1]}),t})(t)),null===t&&(t={});const g=Object.keys(t);return g.length>0&&(g.forEach(e=>{const n=t[e];Array.isArray(n)||(t[e]=[n]),0===t[e].length&&delete t[e]}),i&&(t=(e=>{const t={};return Object.keys(e).forEach(n=>{t[n]=p.Dimension(n).id.filter(t=>-1===e[n].indexOf(t))}),t})(t)),p.Transform({type:"arrobj",content:"id",field:"id",status:!0}).forEach(e=>{let n=[];g.forEach(l=>{const i=t[l];let s=[];i.forEach(t=>{s.push(e[l]===t)}),n.push(-1!==s.indexOf(!0))}),-1===n.indexOf(!1)&&(f.push(e.value),d.push(e.status))}),g.forEach(e=>{const n=p.Dimension(e).id,l={};let i=0;p.size[p.id.indexOf(e)]=t[e].length,n.forEach(n=>{-1!==t[e].indexOf(n)&&(l[n]=i,i++)}),p.__tree__.dimension[e].category.index=l}),p.n=f.length,p.value=p.__tree__.value=e(h)?((e,t)=>t.from(e))(f,h.constructor):f,p.status=p.__tree__.status=null!==y?d:null),r?(c=p.__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(e=>{null===c.role[e]&&delete c.role[e]})),o&&b(c,"value"),a&&c.hasOwnProperty("status")&&b(c,"status"),JSON.stringify(c)):p},n.prototype.Slice=function(e){return null===this||"dataset"!==this.class||0===Object.entries(this.value).length?null:void 0===e?this:(Array.isArray(e)||(e=Object.keys(e).map(t=>[t,e[t]])),this.Dice(e.map(e=>[e[0],[e[1]]])))},n.prototype.Data=function(e,t){let n,l,i=[],s=e=>{for(let t in e)if(e.hasOwnProperty(t))return t};if(null===this||"dataset"!==this.class||null===this.value)return null;if(void 0===e)return this.value.map((e,t)=>this.Data(t));if("boolean"!=typeof t&&(t=!0),"number"==typeof e){const n=this.value[e];return void 0===n?null:t?{value:n,status:this.status?this.status[e]:null}:n}let r="object";const o=this.__tree__,a=o.size||o.dimension&&o.dimension.size,u=a.length;if(Array.isArray(e)){if(!Array.isArray(e[0])){if(this.length!==e.length)return null;let l=1,s=0,r=[],o=[];for(n=0;n<u;n++)if(void 0!==e[n]){if("number"!=typeof e[n]||e[n]>=a[n])return null;l*=n>0?a[u-n]:1,s+=l*e[u-n-1]}else r.push(n),o.push(a[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<u;n++)n!==r[0]?s.push(e[n]):s.push(l);i.push(this.Data(s,t))}return i}return t?{value:this.value[s],status:this.status?this.status[s]:null}:this.value[s]}r="array"}let c=[];const f=((e,t,n)=>{let l,i=[],r={};const o=e.dimension,a=e.id||o.id,u=e.size||o&&o.size;if("array"===n){for(l=t.length;l--;)r[t[l][0]]=t[l][1];t=r}for(let e=0,n=a.length;e<n;e++){const n=a[e],l=t[n];i.push("string"==typeof l?l:1===u[e]?s(o[n].category.index):null)}return i})(o,e,r),d=o.dimension,h=o.id||d.id;for(n=0,l=f.length;n<l;n++)c.push(d[h[n]].category.index[f[n]]);return this.Data(c,t)},n.prototype.toTable=function(e,n){if(null===this||"dataset"!==this.class||null===this.value)return null;1==arguments.length&&"function"==typeof e&&(n=e,e=null);const l=void 0!==(e=e||{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?e.prefix:"";let i,s,r,o,a;"arrobj"!==e.type&&"objarr"!==e.type||void 0!==e.field||(e.field="id");const u="id"===e.field,c=e=>(u?"value":e)||"Value",f=e=>(u?"status":e)||"Status",d=this.__tree__;let h=!0===e.status;if("function"==typeof n){i=this.toTable(e);let t=[];const l="array"!==e.type?0:1,r="object"!==e.type?i.slice(l):i.rows.slice(0);for(a=r.length,s=0;s<a;s++){const e=n.call(this,r[s],s);void 0!==e&&t.push(e)}return"object"===e.type?{cols:i.cols,rows:t}:("array"===e.type&&t.unshift(i[0]),t)}if("arrobj"===e.type||"objarr"===e.type){let n=[],o=function(){},u={};const f=d.role&&d.role.metric,p=this,y=p.id,b=e.by&&-1!==y.indexOf(e.by)?e.by:null,g=!0===e.meta,m=void 0!==e.drop&&Array.isArray(e.drop)?e.drop:[],v=!0===e.comma,O=!0===e.bylabel,w=p.value.constructor,_=n=>{const i=c(e.vlabel);let s,r={};return"objarr"===e.type&&(s=null===b&&t(w.name)?e=>{r[e]=e===i?w.from(n,t=>t[e]):n.map(t=>t[e])}:e=>{r[e]=n.map(t=>t[e])},Object.keys(n[0]).forEach(s),n=r),g?(r={},y.forEach(e=>{const t=p.Dimension(e);r[e]={label:t.label,role:t.role,categories:{id:t.id,label:p.Dimension(e,!1)}}}),{meta:{label:p.label,source:p.source,updated:p.updated,id:y,status:h,unit:e.unit,by:b,bylabel:O,drop:null!==b&&m.length>0?m:null,prefix:null!==b?l||"":null,comma:v,dimensions:r},data:n}):n};b&&(e.field="id"),i=this.toTable({field:e.field,vlabel:e.vlabel,slabel:e.slabel,content:e.content,status:h});const x=i.shift();let A;if(null===b&&e.unit&&f){if("id"!==e.content)for(let e=f.length;e--;){const t=this.Dimension(f[e]);u[f[e]]={};for(let n=t.length;n--;)u[f[e]][t.Category(n).label]=t.id[n]}o=function(t,n){if(-1!==f.indexOf(t)){const l=d.dimension[t].category;l.unit?A.unit=l.unit["id"!==e.content?u[t][n]:n]:A.unit=null}},e.unit=!0}else e.unit=!1;for(a=i.length,s=0;s<a;s++){for(A={},r=i[s].length;r--;)A[x[r]]=i[s][r],o(x[r],i[s][r]);n.push(A)}if(v){const t=c(e.vlabel);n.forEach(function(e){null!==e[t]&&(e[t]=String(e[t]).replace(".",","))})}if(null!==b){const t={},i={};let s,r=[];m.forEach(function(e,t){(!p.Dimension(e)||p.Dimension(e).length>1)&&(m[t]="")});const o=y.filter(function(e){return e!==b&&-1===m.indexOf(e)}),a=p.Dimension(b),u=function(e,t){let n=[];return t.forEach(function(t){n.push(e[t])}),n.join("\t")},c=function(e,t){const n={};return t.forEach(function(t){n[t]=e[t]}),n};"id"!==e.content?O?s=function(e,t,n){e[t][`${l}${n[b]}`]=n.value}:(a.Category().forEach(function(e,t){i[e.label]=a.id[t]}),s=function(e,t,n){e[t][`${l}${i[n[b]]}`]=n.value}):s=function(e,t,n){e[t][`${l}${n[b]}`]=n.value},n.forEach(function(e){const n=u(e,o);void 0===t[n]&&(t[n]=c(e,o)),s(t,n,e,b)});for(let e in t)r.push(t[e]);return h=!1,_(r)}return _(n)}let p,y,b,g,m=[];if("object"===e.type){const e="number"==typeof this.value[0]||null===this.value[0]?"number":"string";p=function(e,t){const n=u&&e||t||e;k.push({id:e,label:n,type:"string"})},y=function(t,n,l){const i=c(t),s=f(n);l&&k.push({id:"status",label:s,type:"string"}),k.push({id:"value",label:i,type:e})},b=function(e){m.push({v:e})},g=function(e){m.push({v:e}),z.push({c:m})}}else p=function(e,t){const n=u&&e||t||e;k.push(n)},y=function(e,t,n){const l=c(e),i=f(t);n&&k.push(i),k.push(l),P.push(k)},b=function(e){m.push(e)},g=function(e){m.push(e),P.push(m)};const v=d.dimension,O=d.id||v.id,w=d.size||v.size,_=O.length;if(_!=w.length)return!1;let x=[],A=1,E=1,j=[],D=[],S=[],P=[],k=[],z=[];for(s=0;s<_;s++){const t=O[s];p(t,v[t].label),A*=w[s],E*=w[s];let n=[];for(r=0;r<w[s];r++)for(let t in v[O[s]].category.index)if(v[O[s]].category.index[t]===r){const l="id"!==e.content&&v[O[s]].category.label?v[O[s]].category.label[t]:t;n.push(l)}x.push(n),j.push(E)}for(y(e.vlabel,e.slabel,h),a=x.length,s=0;s<a;s++){let e=[];for(let t=0,n=x[s].length;t<n;t++)for(let n=0;n<A/j[s];n++)e.push(x[s][t]);D.push(e)}for(a=D.length,s=0;s<a;s++){let e=[],t=0;for(o=0;o<A;o++)e.push(D[s][t]),t++,t===D[s].length&&(t=0);S.push(e)}for(o=0;o<A;o++){m=[],a=D.length;for(let e=0;e<a;e++)b(S[e][o]);h&&b(this.status?this.status[o]:null),g(this.value[o])}return"object"===e.type?{cols:k,rows:z}:P},n.prototype.node=function(){return this.__tree__},n.prototype.toString=function(){return this.class},n.prototype.Unflatten=function(e){if(null===this||"dataset"!==this.class||null===this.value||"function"!=typeof e)return null;const t=this.id,n=this.size,l=t.length,i=[],s=[],r=new Array(l).fill(0);for(let e=0;e<l;e++)s.push(this.Dimension(e).id);for(let o=0;o<this.n;o++){const a={},u={value:this.value[o],status:this.status?this.status[o]:null};for(let e=0;e<l;e++)a[t[e]]=s[e][r[e]];const c=e(a,u,o,i);void 0!==c&&i.push(c);for(let e=l-1;e>=0&&(r[e]++,!(r[e]<n[e]));e--)r[e]=0}return i},n.prototype.Transform=function(e){if(null===this||"dataset"!==this.class||null===this.value)return null;if(null!=e){if("object"!=typeof e||void 0!==e.type&&"string"!=typeof e.type)return null}else e={};"arrobj"!==e.type&&"objarr"!==e.type||void 0!==e.field||(e.field="id");const t=this,n="arrobj"!==e.type&&"objarr"!==e.type&&"object"!==e.type?"array":e.type,l=t.id,i="array"!==n&&"object"!==n&&e.by&&l.includes(e.by)?e.by:null,s=e.comma||!1,r=null===i&&e.status||!1,o=e.content||"label",a=null===i?e.field||"label":"id",u="id"===e.field?"value":e.vlabel||"Value",c="id"===e.field?"status":e.slabel||"Status",f=e.meta||!1,d=i&&"string"==typeof e.prefix?e.prefix:"",h=Array.isArray(e.drop)?e.drop.filter(e=>{const n=t.Dimension(e);return n&&1===n.length}):[],p=l.filter(e=>!h.includes(e)),y=e=>null===e?null:String(e).replace(".",","),b=s?y:e=>e;let g,m,v;switch(n){case"array":{const e="label"===a?p.map(e=>t.Dimension(e).label):p,n=r?[c,u]:[u],l=p.map(e=>t.Dimension(e));m=e.concat(n);const i=r?(e,t)=>e.push(t.status):()=>{};g=(e,t)=>{const n=[];for(let t=0,i=p.length;t<i;t++){const i=p[t],s=l[t];n.push("label"===o?s.Category(e[i]).label:e[i])}return i(n,t),n.push(b(t.value)),n},v=t.Unflatten(g);break}case"object":{const e=p.map(e=>t.Dimension(e)),n="number"==typeof t.value[0]||null===t.value[0]?"number":"string",l=[];for(let t=0,n=p.length;t<n;t++){const n=p[t],i=e[t],s="id"===a?n:i.label||n;l.push({id:n,label:s,type:"string"})}r&&l.push({id:"status",label:c,type:"string"}),l.push({id:"value",label:u,type:n});const i=r?(e,t)=>e.push({v:t.status}):()=>{};return{cols:l,rows:t.Unflatten((t,n)=>{const l=[];for(let n=0,i=p.length;n<i;n++){const i=p[n],s=e[n],r=t[i];l.push({v:"label"===o?s.Category(r).label:r})}return i(l,n),l.push({v:n.value}),{c:l}})}}case"objarr":{const e=s?e=>e.map(y):e=>e,n="id"===a?e=>e:e=>t.Dimension(e).label,l="id"===o?(e,t)=>t:(e,n)=>t.Dimension(e).Category(n).label,f={};if(i){const e=p.filter(e=>e!==i),s=t.Dimension(i),r=new Map,o={};e.forEach(e=>{f[n(e)]=[]}),s.id.forEach(e=>{const t=d+("id"===a?e:s.Category(e).label);o[e]=t,f[t]=[]});let u=0;g=function(t,s){const a=e.map(e=>t[e]).join("\0");let c;if(r.has(a))c=r.get(a);else{c=u++,r.set(a,c),e.forEach(e=>{f[n(e)].push(l(e,t[e]))});for(const t in f)!e.includes(t)&&f[t].length;Object.values(o).forEach(e=>{f[e].push(null)})}const d=o[t[i]],h=s.value;null!==h&&(f[d][c]=b(h))},t.Unflatten(g)}else r?(f[u]=e(t.value),f[c]=t.status):f[u]=e(t.value),p.forEach(e=>{f[n(e)]=[]}),g=function(e,t){p.forEach(t=>{f[n(t)].push(l(t,e[t]))})},t.Unflatten(g);v=f;break}default:{const e=new Map,n=[];p.forEach(e=>{const l=t.Dimension(e);n.push({id:e,dim:l,prop:"label"===a?l.label:e,isBy:i&&e===i})});const l=r?(e,t)=>{e[c]=t.status}:()=>{};g=(t,s)=>{const r={};let a;for(let e=0,l=n.length;e<l;e++){const l=n[e],i=l.id,s=l.prop,u=l.isBy,c=t[i],f="label"===o?l.dim.Category(c).label:c;u?a=f:r[s]=f}if(i){l(r,s);const t=Object.values(r).join("|");let n=e.get(t),i=!1;return n||(n=r,e.set(t,n),i=!0),n[d+a]=b(s.value),i?n:void 0}return r[u]=b(s.value),l(r,s),r},v=t.Unflatten(g)}}if(m&&v.unshift(m),f){const e={};return l.forEach(n=>{const l=t.Dimension(n);e[n]={label:l.label,role:l.role,categories:{id:l.id,label:t.Dimension(n,!1)}}}),{meta:{type:n,label:t.label,source:t.source,updated:t.updated,id:l,status:r,by:i,drop:h.length>0?h:null,prefix:null!==i?d||"":null,comma:s,dimensions:e},data:v}}return v};const l=e=>{if(!e.ok)throw new Error(`${e.status} ${e.statusText}`);return e.json()};function i(e,t,i){return"function"!=typeof i&&(i=null),"object"==typeof e?new n(e,i):"version"===e?"2.2.2":fetch?fetch(e,{method:"GET"}).then(l).then(e=>new n(e,i)).catch(e=>{throw e}):void 0}function s(e,t){return null==e?null:("string"!=typeof e&&void 0!==e.length||(e=i(e)),0===e.length||"dataset"!==e.class&&"collection"!==e.class&&"bundle"!==e.class?null:(null===e.value&&(e.value=Array(e.n).fill(null)),"dataset"===e.class?e:e.Dataset(t)))}function r(e){if(null===e||0===e.length||"dataset"!==e.class)return!1;let t=1;for(let n=e.length;n--;)t*=e.Dimension(n).length;return t===e.n}function o(e,t,n){return(t.label||n).capitalize()+function(e,t){let n,l,i="";return e&&"metric"===e.role&&t.unit&&(n=t.unit.hasOwnProperty("label")?t.unit.label:"",l=t.unit.hasOwnProperty("symbol")?t.unit.symbol:"",n+l!==""&&(i=""===l?n:""===n?l:"start"===t.unit.position?l+n:n+" "+l,i=" ("+i+")")),i}(e,t)}function a(e,t,n){let l,i,s='<select name="'+t+'">',r=[];if(null!==n[1]){if(l=e.id,r=e.Dimension(),2===l.length)return(e.Dimension(n[0]).label||n[0]).capitalize()}else if(i=e.Dimension(t),l=i.id,r=i.Category(),1===l.length)return;return l.forEach(function(e,t){const l=e!==n[0]?"":'selected="selected" ';null!==n[1]&&e===n[1]||(s+="<option "+l+'value="'+e+'">'+o(i,r[t],e)+"</option>")}),s+="</select>",s}function u(e,t,n){function l(e){void 0!==t?t.innerHTML=i[e]:window.alert(i[e])}if(void 0===e)return void l("urierror");if(void 0===t)return void l("selerror");void 0===n&&(n={});const i=void 0===n.i18n||void 0===n.i18n.msgs?{urierror:"tbrowser: A valid JSON-stat input must be specified.",selerror:"tbrowser: A valid selector must be specified.",jsonerror:"The request did not return a valid JSON-stat dataset.",dimerror:"Only one dimension was found in the dataset. At least two are required.",dataerror:"Selection returned no data!",source:"Source",filters:"Filters",constants:"Constants",rc:"Rows & Columns",na:"n/a"}:n.i18n.msgs,u=void 0===n.i18n||void 0===n.i18n.locale?"en-US":n.i18n.locale,c=n.dsid||0,f=n.status||!1,d=n.tblclass||"",h=n.nonconst||!1,p=s(e,c);if(!r(p))return void l("jsonerror");let y;h&&(y=function(e){let t=0;const n=e.size.slice(0),l=[];return n.forEach(function(n,i){const s=i-t,r=e.Dimension(s);1===n&&(delete e.__tree__.dimension[e.id[s]],e.size.splice(s,1),e.id.splice(s,1),e.length--,t++,l.push(r.label.capitalize()+": "+r.Category(0).label.capitalize()))}),l}(p)),1!==p.length?function e(t,l,s,r){"function"==typeof n.callback&&n.callback(s);let c="",d="",p="",b="",g="",m="",v=null!==l.label?'<span class="label">'+l.label.capitalize()+"</span>":"",O=l.source?i.source+": "+l.source:"";const w=s.rows,_=l.Dimension(w),x=_.id,A=s.cols,E=l.Dimension(A),j=E.id,D=l.role&&l.role.metric?l.role.metric[0]:null,S=null!==D?l.Dimension(D):null,P=function(e){return e.hasOwnProperty("unit")&&e.unit&&e.unit.hasOwnProperty("decimals")?e.unit.decimals:null},k=s.filters,z=JSON.parse(JSON.stringify(k)),C=[];h&&y.length&&(v='<span class="label">'+y.join(". ")+"</span>"),""!==O&&"."!==O.slice(-1)&&(O+="."),p+="<caption>"+v+"<form>";for(const e in k){const t=l.Dimension(e),n=t.label?t.label.capitalize():e.capitalize();t.length>1?g+="<p>"+a(l,e,[k[e],null])+" <strong>"+n+"</strong></p>":C.push({label:n,value:o(t,t.Category(0)),name:e,id:t.id[0]})}""!==g&&(g='<fieldset id="filters"><legend>'+i.filters+"</legend>"+g+"</fieldset>"),C.forEach(function(e){m+="<p>"+e.value+" <strong>"+e.label+'</strong></p><input type="hidden" name="'+e.name+'" value="'+e.id+'" />'}),""!==m&&(m='<fieldset id="constants"><legend>'+i.constants+"</legend>"+m+"</fieldset>"),p+=m+g+'<fieldset id="rowscols"><legend>'+i.rc+"</legend>"+a(l,"rows",[w,A])+" <a>↔</a> "+a(l,"cols",[A,w])+"</fieldset></form></caption>",b+="<tbody>";const N=Number.toLocaleString&&"none"!==u?function(e,t){return null===t?e.toLocaleString(u):e.toLocaleString(u,{minimumFractionDigits:t,maximumFractionDigits:t})}:function(e,t){return null===t?e:e.toFixed(t)};if(x.forEach(function(e){z[w]=e;const t=l.Data(z),n=function(e,t){let n;const l=A!==D?null===S?null:P(S.Category(z[D])):P(E.Category(t));null!==e.value?(n=N(e.value,l),f&&null!==e.status&&(n+=" ("+e.status+")")):n=e.status||i.na,b+="<td>"+n+"</td>"};null!==t?(b+='<tr><th scope="row">'+o(_,_.Category(e))+"</th>",Array.isArray(t)?t.forEach(function(e,t){n(e,t)}):n(t,0),b+="</tr>"):b="ERROR"}),"ERROR"===b)return i.dataerror;b+="</tbody>",c+="<thead><tr><th></th>",j.forEach(function(e){c+='<th scope="col">'+o(E,E.Category(e))+"</th>"}),c+="</tr></thead>",""!==O&&(d='<tfoot><tr><td colspan="'+(j.length+1)+'">'+O+"</td></tr></tfoot>"),t.innerHTML='<table class="'+r+'">'+p+c+b+d+"</table>",[].slice.call(t.querySelectorAll("select")).forEach(function(n){n.addEventListener("change",function(n){e(t,l,function(e,t,n){const l={filters:{}};return n.forEach(function(e){"rows"===e.name||"cols"===e.name?l[e.name]=e.value:l.filters[e.name]=e.value}),"rowscols"===t&&e.id.forEach(function(t,n){t!==l.rows&&t!==l.cols?void 0===l.filters[t]&&(l.filters[t]=e.Dimension(n).id[0]):delete l.filters[t]}),l}(l,n.target.parentElement.getAttribute("id"),function(e){const t=[];return[].slice.call(e.querySelectorAll("select, input")).forEach(function(e){t.push({name:e.name,value:e.value})}),t}(t)),r)},!1)}),t.querySelector("a").addEventListener("click",function(){s.cols=w,s.rows=A,e(t,l,s,r)},!1)}(t,p,function(e,t){if("object"==typeof t)return{rows:t.rows,cols:t.cols,filters:t.filters};const n={},l=[],i=e.id;let s,r;if(t){const n="bigger"===t?function(e,t){return e.len<t.len?1:-1}:function(e,t){return e.len>t.len?1:-1};e.Dimension().forEach(function(e,t){l.push({id:i[t],len:e.length})}),l.sort(n),s=l[0].id,r=l[1].id}else s=i[0],r=i[1];return e.Dimension(s).length<e.Dimension(r).length&&(s=r+(r=s,"")),i.forEach(function(t){t!==s&&t!==r&&(n[t]=e.Dimension(t).id[0])}),{rows:s,cols:r,filters:n}}(p,n.preset),d):l("dimerror")}function c(e,t){if(void 0===e)return null;void 0===t&&(t={});let n="",l="",i="",o=0,a=t.na||"n/a",u=t.dsid||0,c=t.vlabel||null,f=t.slabel||null,d=t.counter||!1,h=t.tblclass||"",p=t.numclass||"",y=t.valclass||"",b=t.status||!1,g=t.locale||"en-US",m=t.source||"Source",v=s(e,u),O=v.role&&v.role.metric?v.id.indexOf(v.role.metric[0]):null,w=v.Dimension({role:"metric"}),_={},x=Number.toLocaleString&&"none"!==g?function(e,t){return null===t?e.toLocaleString(g):e.toLocaleString(g,{minimumFractionDigits:t,maximumFractionDigits:t})}:function(e,t){return null===t?e:e.toFixed(t)},A=function(e,t){let i=null;e.forEach(function(e,s){const r=D===s?' class="'+p+" "+y+'"':"";let o=a;t?(O===s&&(i=_[e]),null!==e&&(o=D===s?x(e,i):e),l+="<td"+r+">"+o+"</td>"):n+="<th"+r+">"+e+"</th>"})},E=d?function(e,t){t?l+='<tr><td class="'+p+'">'+t+"</td>":n+='<th class="'+p+'">#</th>',A(e,t),l+="</tr>"}:function(e,t){l+="<tr>",A(e,t),l+="</tr>"};if(!r(v))return null;w&&w[0].Category().forEach(function(e){const t=e.unit&&e.unit.hasOwnProperty("decimals")?e.unit.decimals:null;_[e.label]=t}),console.log(v),console.log(v.Transform({status:b,vlabel:c,slabel:f}));const j=v.Transform({status:b,vlabel:c,slabel:f}),D=j[0].length-1;return j.forEach(function(e,t){E(e,t)}),v.source&&(o=v.length+1,d&&o++,b&&o++,m+=": "+v.source,"."!==m.slice(-1)&&(m+="."),i='<tfoot><td colspan="'+o+'">'+m+"</td></tfoot>"),'<table class="'+h+'"><caption>'+(t.caption||v.label||"")+"</caption><thead><tr>"+n+"</tr></thead><tbody>"+l+"</tbody>"+i+"</table>"}function f(e,t){const n={};return Array.isArray(e[t])?(e[t].forEach(function(e,t){null!==e&&(n[String(t)]=e)}),n):e[t]}function d(e,t){if(void 0===e)return null;void 0===t&&(t={}),"boolean"!=typeof t.ovalue&&(t.ovalue=!1),"boolean"!=typeof t.ostatus&&(t.ostatus=!1),"boolean"!=typeof t.instance&&(t.instance=!1);const n=t.vlabel||"Value",l=t.slabel||"Status",s=t.type||"array",r=t.label||"",o=t.header||null,a=[],u=[],c=[],d=[],h={},p={},y=function(e,t){let n=1,l=0;for(let i=0;i<v;i++)n*=i>0?t[v-i]:1,l+=n*e[v-i-1];return l};let b,g=function(t,l){const i=e[t][n];c[y(l,u)]=isNaN(i)?null:i};switch(s){case"array":e=function(e){const t=e[0],n=e.slice(1),l=[];for(let e=0,i=n.length;e<i;e++){let i={};for(let l=0,s=t.length;l<s;l++)i[t[l]]=n[e][l];l.push(i)}return l}(e);break;case"object":e=function(e){const t=e.cols.map(function(e){return e.id}),n=e.rows,l=[];for(let e=0,i=n.length;e<i;e++){let i={};for(let l=0,s=t.length;l<s;l++)i[t[l]]=n[e].c[l].v;l.push(i)}return l}(e)}const m=e.length;t.hasOwnProperty("drop")&&Array.isArray(t.drop)&&t.drop.length&&e.forEach(function(e){t.drop.forEach(function(t){delete e[t]})});for(const t in e[0])if(t!==n)if(t!==l){if(a.push(t),o)b=o.dimension[t],h[t]=b.category.index;else{h[t]=[];for(let n=0;n<m;n++){const l=e[n][t];-1===h[t].indexOf(l)&&h[t].push(l)}}u.push(h[t].length),p[t]={label:o?b.label:t,category:{index:h[t]}},o&&(p[t].category.label=b.category.label,b.category.unit&&(p[t].category.unit=b.category.unit))}else g=function(t,i){const s=e[t][n],r=e[t][l];c[y(i,u)]=isNaN(s)?null:s,d[y(i,u)]=""===r?null:r};const v=a.length;for(let t=0;t<m;t++){const n=[];for(let l=0;l<v;l++){const i=a[l];n.push(h[i].indexOf(e[t][i]))}g(t,n)}const O={version:"2.0",class:"dataset",value:c,dimension:p,id:a,size:u};return r&&(O.label=r),d.length&&(O.status=d),o&&(o.label&&(O.label=o.label),o.source&&(O.source=o.source),o.updated&&(O.updated=o.updated),o.href&&(O.href=o.href),o.role&&(O.role=o.role)),t.ovalue&&(O.value=f(O,"value")),t.ostatus&&O.hasOwnProperty("status")&&(O.status=f(O,"status")),t.instance?i(O):O}function h(e,t){if(void 0===e)return null;void 0===t&&(t={});const n="jsonstat"===e.substring(0,8),l={time:[],geo:[],metric:[]},i=n?e.substring(8,9):t.delimiter||",";let s,r,o,a=[],u=null,c=null,f=!1,h=n?"value":t.vlabel,p=n?"status":t.slabel,y=";"===i?t.decimal||",":t.decimal||".";const b=function(e,t){t=t||",";const n=new RegExp("(\\"+t+'|\\r?\\n|\\r|^)(?:"([^"]*(?:""[^"]*)*)"|([^"\\'+t+"\\r\\n]*))","gi"),l=[[]];let i,s,r=null;for(;r=n.exec(e);)s=r[1],s.length&&s!=t&&l.push([]),i=r[2]?r[2].replace(new RegExp('""',"g"),'"'):r[3],l[l.length-1].push(i);return l}(e.trim(),i);if(n){for(y=b[0][1],o=b[0][2],b.shift();"data"!==b[0][0];)a.push(b.shift());b.shift(),c={dimension:{}},a.forEach(function(e){let t,n,i,s,r,a,u,d;switch(e[0]){case"dimension":if(c.dimension[e[1]]={},s=c.dimension[e[1]],s.label=e[2],s.category={},r=s.category,r.index=[],n={},i=2*e[3]+3,e.length>=i){for(t=4;t<i;t++)u=e[t],d=e[++t],Object.defineProperty(n,u,{value:d,writable:!0,configurable:!0,enumerable:!0}),r.label=n,r.index.push(u);"string"==typeof e[t]&&-1!==["time","geo","metric"].indexOf(e[t])&&(l[e[t]].push(e[1]),f=!0,"metric"===e[t]&&"string"==typeof e[++t]&&(r.unit={},r.index.forEach(function(n,l){const i=e[t+l].split(o);r.unit[n]={},a=r.unit[n],void 0!==i[0]&&""!==i[0]&&(a.decimals=1*i[0]),void 0!==i[1]&&""!==i[1]&&(a.label=i[1]),void 0!==i[2]&&""!==i[2]&&(a.symbol=i[2]),void 0!==i[1]&&-1!==["start","end"].indexOf(i[3])&&(a.position=i[3])})))}break;case"label":case"source":case"updated":case"href":c[e[0]]=e[1]||null}}),f&&(l.time.length||delete l.time,l.geo.length||delete l.geo,l.metric.length||delete l.metric,c.role=l)}if(s=b.length,r=b[0].length,void 0!==h){for(;r--;)if(b[0][r]===h){u=r;break}if(null===u)return null}else u=r-1,h=b[0][u];if(","===y)for(r=1;r<s;r++)b[r][u]=Number(b[r][u].replace(",","."));else for(r=1;r<s;r++)b[r][u]=Number(b[r][u]);return d(b,{header:c,vlabel:h,slabel:p,type:"array",label:t.label||"",ovalue:t.ovalue||!1,ostatus:t.ostatus||!1,instance:t.instance||!1})}function p(e,t){if("object"!=typeof e)return null;if(e.hasOwnProperty("data")&&e.hasOwnProperty("meta")&&e.data.hasOwnProperty("dataSets")&&e.data.hasOwnProperty("structures")&&Array.isArray(e.data.structures)&&(e.dataSets=e.data.dataSets,e.structure=e.data.structures[0],e.header=e.meta),!e.hasOwnProperty("dataSets")||!Array.isArray(e.dataSets))return null;if(1!==e.dataSets.length)return null;e.dataSets[0].hasOwnProperty("observations")||function(e){const t=e.dataSets[0],n=t.series,l=e.structure,i=l.dimensions,s={};Object.keys(n).forEach(function(e){const t=n[e].observations;Object.keys(t).forEach(function(l){s[e+":"+l]=t[l].concat(n[e].attributes)})}),t.observations=s,delete t.series,i.observation=i.series.concat(i.observation),delete i.series,l.attributes.observation=l.attributes.observation.concat(l.attributes.series),delete l.attributes.series}(e),void 0===t?t={ovalue:!1,ostatus:!1,instance:!1}:("boolean"!=typeof t.ovalue&&(t.ovalue=!1),"boolean"!=typeof t.ostatus&&(t.ostatus=!1),"boolean"!=typeof t.instance&&(t.instance=!1));const n=e.structure,l=e.dataSets[0].observations,s=n.attributes.observation,r=n.dimensions;if(!r.hasOwnProperty("observation"))return null;if(r.hasOwnProperty("series")&&Object.keys(r.series).length)return null;let o=1,a=[],u=function(){},c=s.findIndex(function(e){return"OBS_STATUS"===e.id});const f=[],d=[],h={},p={time:[],geo:[]},y=function(e,t){const n=e.size;for(let e=n.length-t.length;e--;)t.push(0);let l=0,i=1;for(let e=0,s=n.length;e<s;e++)i*=e>0?n[s-e]:1,l+=i*t[s-e-1];return l},b=function(e){if(h[e.id]={label:e.name},e.hasOwnProperty("role"))switch(e.role){case"REF_AREA":p.geo.push(e.id);break;case"TIME_PERIOD":p.time.push(e.id)}Object.defineProperty(h[e.id],"category",{value:{index:[],label:{}},writable:!0,enumerable:!0}),f.push(e.id),d.push(e.values.length),o*=e.values.length;const t=h[e.id].category;e.values.forEach(function(e){t.index.push(e.id),Object.defineProperty(t.label,e.id,{value:e.name,writable:!0,enumerable:!0})})},g=e.header.links?e.header.links.find(function(e){return"request"===e.rel}):null;-1!==c&&(s[c].values.length?a=s[c].values:c=-1),r.observation.forEach(b),r.hasOwnProperty("dataSet")&&r.dataSet.forEach(b);const m={version:"2.0",class:"dataset",updated:e.header.prepared||null,source:e.header.sender.name||null,label:n.name||null,id:f,size:d,dimension:h,value:t.ovalue?{}:new Array(o).fill(null)};g&&(m.link={alternate:[{type:"application/vnd.sdmx.data+json",href:g.href}]}),p.geo.length+p.time.length>0&&(p.time.length||delete p.time,p.geo.length||delete p.geo,m.role=p),-1!==c&&(m.status=t.ostatus?{}:new Array(o).fill(null),m.extension={status:{label:{}}},a.forEach(function(e){m.extension.status.label[e.id]=e.name}),u=t.ostatus?function(e,t){const n=l[e][c];null!==n&&(m.status[y(m,t)]=a[n].id)}:function(e,t){const n=l[e][c];m.status[y(m,t)]=null===n?null:a[n].id}),c++;for(let e in l){const n=e.split(":");t.ovalue&&null===l[e][0]||(m.value[y(m,n)]=l[e][0]),u(e,n)}return t.instance?i(m):m}function y(e,t){if(void 0===e||!Array.isArray(e))return null;const n=JSON.parse(JSON.stringify(e)),l=n[0];if(!l.hasOwnProperty("version")||!l.hasOwnProperty("class")||"dataset"!==l.class)return null;void 0===t&&(t={});const s=void 0===t.label?null:t.label,r=void 0===t.by?null:t.by;let o,a,u,c=[];if(null===r){for(let e=1,t=n.length;e<t;e++)c=c.concat(n[e].value);return l.value=c,null!==s&&(l.label=s),l}const f=function(e,t,n){if(Array.isArray(e))e=e.concat(t);else for(const l in t)e[l]=0===t[l]?n:t[l];return e};n.forEach(function(e,t){const n=i(e).Transform({status:!0}),l=e.dimension[r].category;0===t?(c=[n[0]],o=l.index,a=l.label,u=l.unit):(o=function(e,t,n){if(Array.isArray(e))e=e.concat(t);else for(const l in t)e[l]=t[l]+n;return e}(o,l.index,Object.keys(o).length),a=f(a,l.label,t),u=f(u,l.unit,t)),c=c.concat(n.slice(1))});const h=d(c);return l.value=h.value,l.size=h.size,l.status=h.status||null,l.label=s||"",l.href=null,l.dimension[r].category.index=o||null,l.dimension[r].category.label=a||null,l.dimension[r].category.unit=u||null,l}function b(e,t){return null==e?"":-1!==e.indexOf(t)?'"'+e+'"':e}function g(e,t){if(void 0===e)return null;void 0===t&&(t={});const n={content:!1,field:!1},l=!0===t.rich,i=t.na||"n/a",o=t.delimiter||",",a=t.separator||"|",u=";"===o?t.decimal||",":t.decimal||".",c=!0===t.array,f=s(e,t.dsid||0),d=c?function(e){O.push(e)}:function(e){O+=e+"\n"};let h=t.content||"label",p=t.field||"label",y=l?"value":t.vlabel||"Value",g=l?"status":t.slabel||"Status",m=!0===t.status,v=c?[]:"",O=c?[]:"",w=c?function(e){v.push(e)}:function(e){v+=e+"\n"};if(!r(f))return null;l?m=null!==f.status:("[id] label"===h&&(n.content=!0,h="id"),"[id] label"===p&&(n.field=!0,p="id"));const _=!l&&t.by&&-1!==f.id.indexOf(t.by)?t.by:null,x=_&&void 0!==t.drop&&Array.isArray(t.drop)?t.drop:null;if(_)return function(e,t){const n=[],l=t.by,i=t.idlabel,s=t.drop,r=t.na,o=t.delimiter,a=t.decimal,u=i.content?"id":t.content,c=i.field?"label":t.field,f=e.toTable({drop:s,content:u,by:l,type:"arrobj"}),d=Object.keys(f[0]),h=function(e,t,n,l,s){return e.map(e=>"label"===n?-1!==l.id.indexOf(e)?b((i.field?"["+e+"] ":"")+l.Dimension(e).label,t):b((i.field?"["+e+"] ":"")+l.Dimension(s).Category(e).label,t):b(e,t)).join(t)+"\n"}(d,o,c,e,l),p="."!==a?function(e){return null===e?b(r,o):String(e).replace(".",a)}:function(e){return null===e?b(r,o):e};return f.forEach(t=>{let l=[];d.forEach(n=>{const s=t[n],r="string"==typeof s?b(i.content?"["+s+"] "+e.Dimension(n).Category(s).label:s,o):p(s);l.push(r)}),n.push(l.join(o))}),h+n.join("\n")}(f,{drop:x,na:i,delimiter:o,decimal:u,content:h,field:p,by:_,idlabel:n});const A=f.toTable({vlabel:y,slabel:g,status:m,field:l||n.field?"id":p,content:l||n.content?"id":h,type:"array"}),E=A[0].indexOf("id"===p?"value":y),j=m?A[0].indexOf("id"===p?"status":g):-1;return A.forEach(function(e,t){e.forEach(function(l,s){const r=f.Dimension(s);t&&s===E?null===l?e[s]=b(i,o):"."!==u&&(e[s]=String(e[s]).replace(".",u)):t&&s===j&&null===l?e[s]="":(n.content&&t&&r?e[s]="["+e[s]+"] "+r.Category(e[s]).label:n.field&&0===t&&(e[s]=r?"["+e[s]+"] "+r.label:"value"===e[s]?y:g),e[s]=b(e[s],o))}),w(e.join(o))}),l&&(d("jsonstat"+o+u+o+a),["label","source","updated","href"].forEach(function(e){f[e]&&d(e+o+b(f[e],o))}),f.id.forEach(function(e,t){const n=[],l=f.Dimension(t),i=l.role;let s=!1,r="";r+="dimension"+o+b(e,o)+o+b(l.label,o)+o+l.length,"metric"===i&&l.__tree__.category.unit&&(s=!0),l.id.forEach(function(e,t){const i=[],u=l.Category(t);r+=o+b(e,o)+o+b(u.label,o),s&&(i.push(u.unit.hasOwnProperty("decimals")?u.unit.decimals:""),i.push(u.unit.label||""),u.unit.symbol&&(i.push(u.unit.symbol),i.push(u.unit.position)),n.push(b(i.join(a),o)))}),null!==i&&"classification"!==i&&(r+=o+l.role,s&&(r+=o+n.join(o))),d(r)}),v=c?O.concat(["data"],v):O+"data\n"+v),v}String.prototype.capitalize=function(){return this.charAt(0).toUpperCase()+this.slice(1)};var m="4.0.1";export{c as datalist,h as fromCSV,p as fromSDMX,d as fromTable,y as join,u as tbrowser,g as toCSV,m as version};