-
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) · 18.1 KB
/
module.mjs
File metadata and controls
2 lines (2 loc) · 18.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])}for(s=0;s<l;s++)r.push(void 0===n[s]?null:n[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 h=o.size||o.dimension&&o.dimension.size;if(this.size=h,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:h.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=h.length,i=t=>{e.hasOwnProperty(t)||(e[t]=null)};if(!Array.isArray(n)||!Array.isArray(h)||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 c=[],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)c[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=c,this.length=c.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 h,c=[],f=[];const d=this.value,p=s?new n(JSON.parse(JSON.stringify(this))):this,y=p.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);return 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]=p.Dimension(n).id.filter(e=>-1===t[n].indexOf(e))}),e})(e)),p.Transform({type:"arrobj",content:"id",field:"id",status:!0}).forEach(t=>{let n=[];g.forEach(l=>{const i=e[l];let s=[];i.forEach(e=>{s.push(t[l]===e)}),n.push(-1!==s.indexOf(!0))}),-1===n.indexOf(!1)&&(c.push(t.value),f.push(t.status))}),g.forEach(t=>{const n=p.Dimension(t).id,l={};let i=0;p.size[p.id.indexOf(t)]=e[t].length,n.forEach(n=>{-1!==e[t].indexOf(n)&&(l[n]=i,i++)}),p.__tree__.dimension[t].category.index=l}),p.n=c.length,p.value=p.__tree__.value=t(d)?((t,e)=>e.from(t))(c,d.constructor):c,p.status=p.__tree__.status=null!==y?f:null),r?(h=p.__tree__,h.hasOwnProperty("id")||(h.version="2.0",h.hasOwnProperty("class")||(h.class="dataset"),h.id=h.dimension.id,h.size=h.dimension.size,delete h.dimension.id,delete h.dimension.size,h.dimension.hasOwnProperty("role")&&(h.role=h.dimension.role,delete h.dimension.role)),h.hasOwnProperty("status")&&-1!==["null","{}","[]"].indexOf(JSON.stringify(h.status))&&delete h.status,h.hasOwnProperty("role")&&(delete h.role.classification,["geo","time","metric"].forEach(t=>{null===h.role[t]&&delete h.role[t]})),o&&b(h,"value"),a&&h.hasOwnProperty("status")&&b(h,"status"),JSON.stringify(h)):p},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};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 r="object";const o=this.__tree__,a=o.size||o.dimension&&o.dimension.size,u=a.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<u;n++)if(void 0!==t[n]){if("number"!=typeof t[n]||t[n]>=a[n])return null;l*=n>0?a[u-n]:1,s+=l*t[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(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]}r="array"}let h=[];const c=((t,e,n)=>{let l,i=[],r={};const o=t.dimension,a=t.id||o.id,u=t.size||o&&o.size;if("array"===n){for(l=e.length;l--;)r[e[l][0]]=e[l][1];e=r}for(let t=0,n=a.length;t<n;t++){const n=a[t],l=e[n];i.push("string"==typeof l?l:1===u[t]?s(o[n].category.index):null)}return i})(o,t,r),f=o.dimension,d=o.id||f.id;for(n=0,l=c.length;n<l;n++)h.push(f[d[n]].category.index[c[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,h=t=>(u?"value":t)||"Value",c=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 c=f.role&&f.role.metric,p=this,y=p.id,b=t.by&&-1!==y.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,_=!0===t.bylabel,x=p.value.constructor,A=n=>{const i=h(t.vlabel);let s,r={};return"objarr"===t.type&&(s=null===b&&e(x.name)?t=>{r[t]=t===i?x.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={},y.forEach(t=>{const e=p.Dimension(t);r[t]={label:e.label,role:e.role,categories:{id:e.id,label:p.Dimension(t,!1)}}}),{meta:{label:p.label,source:p.source,updated:p.updated,id:y,status:d,unit:t.unit,by:b,bylabel:_,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 O=i.shift();let j;if(null===b&&t.unit&&c){if("id"!==t.content)for(let t=c.length;t--;){const e=this.Dimension(c[t]);u[c[t]]={};for(let n=e.length;n--;)u[c[t]][e.Category(n).label]=e.id[n]}o=function(e,n){if(-1!==c.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[O[r]]=i[s][r],o(O[r],i[s][r]);n.push(j)}if(v){const e=h(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){(!p.Dimension(t)||p.Dimension(t).length>1)&&(m[e]="")});const o=y.filter(function(t){return t!==b&&-1===m.indexOf(t)}),a=p.Dimension(b),u=function(t,e){let n=[];return e.forEach(function(e){n.push(t[e])}),n.join("\t")},h=function(t,e){const n={};return e.forEach(function(e){n[e]=t[e]}),n};"id"!==t.content?_?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]=h(t,o)),s(e,n,t,b)});for(let t in e)r.push(e[t]);return d=!1,A(r)}return A(n)}let p,y,b,g,m=[];if("object"===t.type){const t="number"==typeof this.value[0]||null===this.value[0]?"number":"string";p=function(t,e){const n=u&&t||e||t;z.push({id:t,label:n,type:"string"})},y=function(e,n,l){const i=h(e),s=c(n);l&&z.push({id:"status",label:s,type:"string"}),z.push({id:"value",label:i,type:t})},b=function(t){m.push({v:t})},g=function(t){m.push({v:t}),S.push({c:m})}}else p=function(t,e){const n=u&&t||e||t;z.push(n)},y=function(t,e,n){const l=h(t),i=c(e);n&&z.push(i),z.push(l),P.push(z)},b=function(t){m.push(t)},g=function(t){m.push(t),P.push(m)};const v=f.dimension,_=f.id||v.id,x=f.size||v.size,A=_.length;if(A!=x.length)return!1;let O=[],j=1,w=1,D=[],E=[],k=[],P=[],z=[],S=[];for(s=0;s<A;s++){const e=_[s];p(e,v[e].label),j*=x[s],w*=x[s];let n=[];for(r=0;r<x[s];r++)for(let e in v[_[s]].category.index)if(v[_[s]].category.index[e]===r){const l="id"!==t.content&&v[_[s]].category.label?v[_[s]].category.label[e]:e;n.push(l)}O.push(n),D.push(w)}for(y(t.vlabel,t.slabel,d),a=O.length,s=0;s<a;s++){let t=[];for(let e=0,n=O[s].length;e<n;e++)for(let n=0;n<j/D[s];n++)t.push(O[s][e]);E.push(t)}for(a=E.length,s=0;s<a;s++){let t=[],e=0;for(o=0;o<j;o++)t.push(E[s][e]),e++,e===E[s].length&&(e=0);k.push(t)}for(o=0;o<j;o++){m=[],a=E.length;for(let t=0;t<a;t++)b(k[t][o]);d&&b(this.status?this.status[o]:null),g(this.value[o])}return"object"===t.type?{cols:z,rows:S}:P},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=[],s=[],r=new Array(l).fill(0);for(let t=0;t<l;t++)s.push(this.Dimension(t).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 t=0;t<l;t++)a[e[t]]=s[t][r[t]];const h=t(a,u,o,i);void 0!==h&&i.push(h);for(let t=l-1;t>=0&&(r[t]++,!(r[t]<n[t]));t--)r[t]=0}return i},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",h="id"===t.field?"status":t.slabel||"Status",c=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}):[],p=l.filter(t=>!d.includes(t)),y=t=>null===t?null:String(t).replace(".",","),b=s?y:t=>t;let g,m,v;switch(n){case"array":{const t="label"===a?p.map(t=>e.Dimension(t).label):p,n=r?[h,u]:[u],l=p.map(t=>e.Dimension(t));m=t.concat(n);const i=r?(t,e)=>t.push(e.status):()=>{};g=(t,e)=>{const n=[];for(let e=0,i=p.length;e<i;e++){const i=p[e],s=l[e];n.push("label"===o?s.Category(t[i]).label:t[i])}return i(n,e),n.push(b(e.value)),n},v=e.Unflatten(g);break}case"object":{const t=p.map(t=>e.Dimension(t)),n="number"==typeof e.value[0]||null===e.value[0]?"number":"string",l=[];for(let e=0,n=p.length;e<n;e++){const n=p[e],i=t[e],s="id"===a?n:i.label||n;l.push({id:n,label:s,type:"string"})}r&&l.push({id:"status",label:h,type:"string"}),l.push({id:"value",label:u,type:n});const i=r?(t,e)=>t.push({v:e.status}):()=>{};return{cols:l,rows:e.Unflatten((e,n)=>{const l=[];for(let n=0,i=p.length;n<i;n++){const i=p[n],s=t[n],r=e[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 t=s?t=>t.map(y):t=>t,n="id"===a?t=>t:t=>e.Dimension(t).label,l="id"===o?(t,e)=>e:(t,n)=>e.Dimension(t).Category(n).label,c={};if(i){const t=p.filter(t=>t!==i),s=e.Dimension(i),r=new Map,o={};t.forEach(t=>{c[n(t)]=[]}),s.id.forEach(t=>{const e=f+("id"===a?t:s.Category(t).label);o[t]=e,c[e]=[]});let u=0;g=function(e,s){const a=t.map(t=>e[t]).join("\0");let h;if(r.has(a))h=r.get(a);else{h=u++,r.set(a,h),t.forEach(t=>{c[n(t)].push(l(t,e[t]))});for(const e in c)!t.includes(e)&&c[e].length;Object.values(o).forEach(t=>{c[t].push(null)})}const f=o[e[i]],d=s.value;null!==d&&(c[f][h]=b(d))},e.Unflatten(g)}else r?(c[u]=t(e.value),c[h]=e.status):c[u]=t(e.value),p.forEach(t=>{c[n(t)]=[]}),g=function(t,e){p.forEach(e=>{c[n(e)].push(l(e,t[e]))})},e.Unflatten(g);v=c;break}default:{const t=new Map,n=[];p.forEach(t=>{const l=e.Dimension(t);n.push({id:t,dim:l,prop:"label"===a?l.label:t,isBy:i&&t===i})});const l=r?(t,e)=>{t[h]=e.status}:()=>{};g=(e,s)=>{const r={};let a;for(let t=0,l=n.length;t<l;t++){const l=n[t],i=l.id,s=l.prop,u=l.isBy,h=e[i],c="label"===o?l.dim.Category(h).label:h;u?a=c:r[s]=c}if(i){l(r,s);const e=Object.values(r).join("|");let n=t.get(e),i=!1;n||(n=r,t.set(e,n),i=!0);return n[f+a]=b(s.value),i?n:void 0}return r[u]=b(s.value),l(r,s),r},v=e.Unflatten(g)}}if(m&&v.unshift(m),c){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:v}}return v};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}export{i as default};