-
Notifications
You must be signed in to change notification settings - Fork 23
/
export.mjs
27 lines (20 loc) · 13.5 KB
/
export.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
JSON-stat Javascript Toolkit v. 0.13.13 (JSON-stat v. 2.0 ready) (ES6 module)
https://json-stat.com
https://github.com/badosa/JSON-stat
Copyright 2019 Xavier Badosa (https://xavierbadosa.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing
permissions and limitations under the License.
*/
function isArray(t){return"[object Array]"===Object.prototype.toString.call(t)}function jsonstat(t){var e,n,i,r,s=function(t,e){var n,i=[];if("string"==typeof t&&(t=[t]),isArray(t)){if(t.length===e)return t;if(1===t.length){for(n=0;e>n;n++)i.push(t[0]);return i}}for(n=0;e>n;n++){var r=void 0===t[n]?null:t[n];i.push(r)}return i},l=function(t){var e=void 0===t.index?t.label:t.index;return isArray(e)?e.length:Object.keys(e).length};if(this.length=0,this.id=[],null!==t&&void 0!==t)switch(this["class"]=t["class"]||"bundle",this["class"]){case"bundle":var a=[],o=0;if(this.error=null,this.length=0,"string"==typeof t&&console.log("Module does not accept a URI string, must be an object."),null===t||"object"!=typeof t)return void(this["class"]=null);if(t.hasOwnProperty("error"))return void(this.error=t.error);if("dataset"===t["class"]||"collection"===t["class"]||"dimension"===t["class"])return JSONstat(t);for(n in t)o++,a.push(n);this.__tree__=t,this.length=o,this.id=a;break;case"dataset":t.hasOwnProperty("__tree__")?this.__tree__=e=t.__tree__:this.__tree__=e=t,this.label=e.label||null,this.note=e.note||null,this.link=e.link||null,this.href=e.href||null,this.updated=e.updated||null,this.source=e.source||null,this.extension=e.extension||null;var u,h=0,f=e.size||e.dimension&&e.dimension.size;if(this.size=f,e.hasOwnProperty("value")&&isArray(e.value))h=e.value.length;else{var c=1;for(u=f.length;u--;)c*=f[u];h=c}if(this.value=s(e.value,h),this.status=e.hasOwnProperty("status")?s(e.status,h):null,e.hasOwnProperty("dimension")){var d=e.dimension,v=e.role||!e.version&&d.role||null,p=e.id||d.id,y=f.length,g=function(t){v.hasOwnProperty(t)||(v[t]=null)};if(!isArray(p)||!isArray(f)||p.length!=y)return;if(this.length=y,this.id=p,v&&(g("time"),g("geo"),g("metric"),g("classification")),v&&null===v.classification){var b=[],_=["time","geo","metric"],m=function(t,e){for(var n=e.length;n--;)if(t===e[n])return!0;return!1};for(u=0;3>u;u++){var x=v[_[u]];null!==x&&(b=b.concat(x))}for(v.classification=[],u=0;y>u;u++)m(p[u],b)||v.classification.push(p[u]);0===v.classification.length&&(v.classification=null)}this.role=v,this.n=h;for(var j=0,O=this.length;O>j;j++)if(d[p[j]].category.hasOwnProperty("index")){if(isArray(d[p[j]].category.index)){var w={},k=d[p[j]].category.index;for(i=k.length,r=0;i>r;r++)w[k[r]]=r;d[p[j]].category.index=w}}else{var D=0;d[p[j]].category.index={};for(n in d[p[j]].category.label)d[p[j]].category.index[n]=D++}}else this.length=0;break;case"dimension":if(!t.hasOwnProperty("__tree__"))return JSONstat({version:"2.0","class":"dataset",dimension:{d:t},id:["d"],size:[l(t.category)],value:[null]}).Dimension(0);e=t.__tree__;var z=[],A=e.category;if(!e.hasOwnProperty("category"))return;if(!A.hasOwnProperty("label")){A.label={};for(n in A.index)A.label[n]=n}for(n in A.index)z[A.index[n]]=n;this.__tree__=e,this.label=e.label||null,this.note=e.note||null,this.link=e.link||null,this.href=e.href||null,this.id=z,this.length=z.length,this.role=t.role,this.hierarchy=A.hasOwnProperty("child"),this.extension=e.extension||null;break;case"category":var P=t.child;this.id=P,this.length=null===P?0:P.length,this.index=t.index,this.label=t.label,this.note=t.note||null,this.unit=t.unit,this.coordinates=t.coord;break;case"collection":if(this.length=0,this.label=t.label||null,this.note=t.note||null,this.link=t.link||null,this.href=t.href||null,this.updated=t.updated||null,this.source=t.source||null,this.extension=t.extension||null,null!==this.link&&t.link.item){var E=t.link.item;if(this.length=isArray(E)?E.length:0,this.length)for(r=0;r<this.length;r++)this.id[r]=E[r].href}}}jsonstat.prototype.Item=function(t){if(null===this||"collection"!==this["class"]||!this.length)return null;if("number"==typeof t)return t>this.length||0>t?null:this.link.item[t];var e,n=[];if("object"==typeof t){if(!t["class"]&&!t.follow)return null;t["class"]&&(e="dataset"===t["class"]&&"boolean"==typeof t.embedded?t.embedded===!0?function(t,e,i){var r=t.link.item[e];i["class"]===r["class"]&&r.id&&r.size&&r.dimension&&n.push(r)}:function(t,e,i){var r=t.link.item[e];i["class"]!==r["class"]||r.id&&r.size&&r.dimension||n.push(r)}:function(t,e,i){i["class"]===t.link.item[e]["class"]&&n.push(t.link.item[e])})}else e=function(t,e){n.push(t.link.item[e])};for(var i=0;i<this.length;i++)e(this,i,t);return n},jsonstat.prototype.Dataset=function(t){if(null===this)return null;if("dataset"===this["class"])return void 0!==t?this:[this];var e,n=[],i=0;if("collection"===this["class"]){var r=this.Item({"class":"dataset",embedded:!0});if(void 0===t){for(e=r.length;e>i;i++)n.push(JSONstat(r[i]));return n}if("number"==typeof t&&t>=0&&t<r.length)return JSONstat(r[t]);if("string"==typeof t)for(e=r.length;e>i;i++)if(r[i].href===t)return JSONstat(r[i]);return null}if("bundle"!==this["class"])return null;if(void 0===t){for(e=this.id.length;e>i;i++)n.push(this.Dataset(this.id[i]));return n}if("number"==typeof t){var s=this.id[t];return void 0!==s?this.Dataset(s):null}var l=this.__tree__[t];return void 0===l?null:new jsonstat({"class":"dataset",__tree__:l})},jsonstat.prototype.Dimension=function(t,e){e="boolean"==typeof e?e:!0;var n,i=[],r=this.id.length,s=function(t,e){if(null!==t)for(var n in t)for(var i=null!==t[n]?t[n].length:0;i--;)if(t[n][i]===e)return n;return null};if(null===this||"dataset"!==this["class"])return null;if(void 0===t){for(n=0;r>n;n++)i.push(this.Dimension(this.id[n]));return i}if("number"==typeof t){var l=this.id[t];return void 0!==l?this.Dimension(l,e):null}var a=this.role;if("object"==typeof t){if(t.hasOwnProperty("role")){for(n=0;r>n;n++){var o=this.id[n];s(a,o)===t.role&&i.push(this.Dimension(o,e))}return void 0===i[0]?null:i}return null}var u=this.__tree__.dimension;if(void 0===u)return null;var h=u[t];return void 0===h?null:e?new jsonstat({"class":"dimension",__tree__:h,role:s(a,t)}):function(t,e){var n=[];for(var i in t)n[t[i]]=e[i];return n}(h.category.index,h.category.label)},jsonstat.prototype.Category=function(t){if(null===this||"dimension"!==this["class"])return null;if(void 0===t){for(var e=[],n=0,i=this.id.length;i>n;n++)e.push(this.Category(this.id[n]));return e}if("number"==typeof t){var r=this.id[t];return void 0!==r?this.Category(r):null}var s=this.__tree__.category;if(void 0===s)return null;var l=s.index[t];if(void 0===l)return null;var a=s.unit&&s.unit[t]||null,o=s.coordinates&&s.coordinates[t]||null,u=s.child&&s.child[t]||null,h=s.note&&s.note[t]||null;return new jsonstat({"class":"category",index:l,label:s.label[t],note:h,child:u,unit:a,coord:o})},jsonstat.prototype.Slice=function(t){if(null===this||"dataset"!==this["class"])return null;if(void 0===t)return this;if(!isArray(t)){var e,n=[];for(e in t)n.push([e,t[e]]);t=n}var i=this,r=t.length,s=i.toTable({field:"id",content:"id",status:!0}),l=i.status,a=s.shift(),o=!1,u=[],h=[],f=[],c=[];return t.forEach(function(t){var e=i.Dimension(t[0]);if(null===e)return void(o=!0);var n=e.id.indexOf(t[1]);return-1===n?void(o=!0):(f.push([i.id.indexOf(t[0]),n]),void c.push(e.Category(n).label))}),o?null:(s.forEach(function(e){var n,i={},s=0;for(n=e.length;n--;)i[a[n]]=e[n];t.forEach(function(t){i[t[0]]===t[1]&&s++}),r===s&&(u.push(i.value),h.push(i.status))}),i.n=u.length,i.value=i.__tree__.value=u,i.status=i.__tree__.status=null!==l?h:null,t.forEach(function(t,e){i.size[f[e][0]]=1,i.__tree__.dimension[t[0]].category.index={},i.__tree__.dimension[t[0]].category.index[t[1]]=0,i.__tree__.dimension[t[0]].category.label={},i.__tree__.dimension[t[0]].category.label[t[1]]=c[e]}),i)},jsonstat.prototype.Data=function(t,e){var n,i,r=[],s=function(t){for(var e in t)if(t.hasOwnProperty(e))return e},l=function(t,e,n){var i,r=[],l={},a=t.dimension,o=t.id||a.id,u=t.size||a&&a.size;if("array"===n){for(i=e.length;i--;)l[e[i][0]]=e[i][1];e=l}for(var h=0,f=o.length;f>h;h++){var c=o[h],d=e[c];r.push("string"==typeof d?d:1===u[h]?s(a[c].category.index):null)}return r};if(null===this||"dataset"!==this["class"])return null;if(void 0===t){for(i=this.value.length,n=0;i>n;n++)r.push(this.Data(n));return r}if("boolean"!=typeof e&&(e=!0),"number"==typeof t){var a=this.value[t];return void 0===a?null:e?{value:a,status:this.status?this.status[t]:null}:a}var o="object",u=this.__tree__,h=u.size||u.dimension&&u.dimension.size,f=h.length;if(isArray(t)){if(!isArray(t[0])){if(this.length!==t.length)return null;var c=1,d=0,v=[],p=[];for(n=0;f>n;n++)if(void 0!==t[n]){if("number"!=typeof t[n]||t[n]>=h[n])return null;c*=n>0?h[f-n]:1,d+=c*t[f-n-1]}else v.push(n),p.push(h[n]);if(v.length>1)return null;if(1===v.length){for(var y=0,g=p[0];g>y;y++){var b=[];for(n=0;f>n;n++)n!==v[0]?b.push(t[n]):b.push(y);r.push(this.Data(b,e))}return r}return e?{value:this.value[d],status:this.status?this.status[d]:null}:this.value[d]}o="array"}var _=l(u,t,o),m=[],x=u.dimension,j=u.id||x.id;for(n=0,i=_.length;i>n;n++)m.push(x[j[n]].category.index[_[n]]);return this.Data(m,e)},jsonstat.prototype.toTable=function(t,e){if(null===this||"dataset"!==this["class"])return null;1==arguments.length&&"function"==typeof t&&(e=t,t=null),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};var n,i,r,s,l,a=this.__tree__,o=t.status===!0;if("function"==typeof e){n=this.toTable(t);var u=[],h="array"!==t.type?0:1,f="object"!==t.type?n.slice(h):n.rows.slice(0);for(l=f.length,i=0;l>i;i++){var c=e.call(this,f[i],i);void 0!==c&&u.push(c)}return"object"===t.type?{cols:n.cols,rows:u}:("array"===t.type&&u.unshift(n[0]),u)}if("arrobj"===t.type){n=this.toTable({field:"id",content:t.content,status:o});var d=[],v=n.shift(),p=a.role&&a.role.metric,y=function(){},g={},b=this,_=b.id,m=t.by&&-1!==_.indexOf(t.by)?t.by:null,x=t.meta===!0,j=void 0!==t.drop&&isArray(t.drop)?t.drop:[],O=t.comma===!0,w=t.bylabel===!0,k=function(e){if(x){var n={};return _.forEach(function(t){var e=b.Dimension(t);n[t]={label:e.label,role:e.role,categories:{id:e.id,label:b.Dimension(t,!1)}}}),{meta:{label:b.label,source:b.source,updated:b.updated,id:_,status:o,unit:t.unit,by:m,bylabel:w,drop:null!==m&&j.length>0?j:null,prefix:null!==m?J||"":null,comma:O,dimensions:n},data:e}}return e};if(null===m&&t.unit&&p){if("id"!==t.content)for(var D=p.length;D--;){var z=this.Dimension(p[D]);g[p[D]]={};for(var A=z.length;A--;)g[p[D]][z.Category(A).label]=z.id[A]}y=function(e,n){if(-1!==p.indexOf(e)){var i=a.dimension[e].category;i.unit?P.unit=i.unit["id"!==t.content?g[e][n]:n]:P.unit=null}},t.unit=!0}else t.unit=!1;for(l=n.length,i=0;l>i;i++){var P={};for(r=n[i].length;r--;)P[v[r]]=n[i][r],y(v[r],n[i][r]);d.push(P)}if(O&&d.forEach(function(t){null!==t.value&&(t.value=(""+t.value).replace(".",","))}),null!==m){var E,S={},f=[],C={},J=void 0!==t.prefix?t.prefix:"";j.forEach(function(t,e){(!b.Dimension(t)||b.Dimension(t).length>1)&&(j[e]="")});var N=_.filter(function(t){return t!==m&&-1===j.indexOf(t)}),T=b.Dimension(m),I=function(t,e){var n=[];return e.forEach(function(e){n.push(t[e])}),n.join(" ")},V=function(t,e){var n={};return e.forEach(function(e){n[e]=t[e]}),n};"id"!==t.content?w?E=function(t,e,n){t[e][J+n[m]]=n.value}:(T.Category().forEach(function(t,e){C[t.label]=T.id[e]}),E=function(t,e,n){t[e][J+C[n[m]]]=n.value}):E=function(t,e,n){t[e][J+n[m]]=n.value},d.forEach(function(t){var e=I(t,N);void 0===S[e]&&(S[e]=V(t,N)),E(S,e,t,m)});for(var M in S)f.push(S[M]);return o=!1,k(f)}return k(d)}var R,U,q,B,F="id"===t.field;if("object"===t.type){var G="number"==typeof this.value[0]||null===this.value[0]?"number":"string";R=function(t,e){var n=F&&t||e||t;et.push({id:t,label:n,type:"string"})},U=function(t,e,n){var i=F&&"value"||t||"Value",r=F&&"status"||e||"Status";n&&et.push({id:"status",label:r,type:"string"}),et.push({id:"value",label:i,type:G})},q=function(t){vt.push({v:t})},B=function(t){vt.push({v:t}),nt.push({c:vt})}}else R=function(t,e){var n=F&&t||e||t;et.push(n)},U=function(t,e,n){var i=F&&"value"||t||"Value",r=F&&"status"||e||"Status";n&&et.push(r),et.push(i),tt.push(et)},q=function(t){vt.push(t)},B=function(t){vt.push(t),tt.push(vt)};var H=a.dimension,K=a.id||H.id,L=a.size||H.size,Q=K.length;if(Q!=L.length)return!1;var W=[],X=1,D=1,Y=[],Z=[],$=[],tt=[],et=[],nt=[];for(i=0;Q>i;i++){var it=K[i],rt=H[it].label;R(it,rt),X*=L[i],D*=L[i];var st=[];for(r=0;r<L[i];r++)for(var lt in H[K[i]].category.index)if(H[K[i]].category.index[lt]===r){var at="id"!==t.content&&H[K[i]].category.label?H[K[i]].category.label[lt]:lt;st.push(at)}W.push(st),Y.push(D)}for(U(t.vlabel,t.slabel,o),l=W.length,i=0;l>i;i++){for(var ot=[],ut=0,ht=W[i].length;ht>ut;ut++)for(var ft=0;ft<X/Y[i];ft++)ot.push(W[i][ut]);Z.push(ot)}for(l=Z.length,i=0;l>i;i++){var ct=[],dt=0;for(s=0;X>s;s++)ct.push(Z[i][dt]),dt++,dt===Z[i].length&&(dt=0);$.push(ct)}for(s=0;X>s;s++){var vt=[];l=Z.length;for(var pt=0;l>pt;pt++)q($[pt][s]);o&&q(this.status?this.status[s]:null),B(this.value[s])}return"object"===t.type?{cols:et,rows:nt}:tt},jsonstat.prototype.node=function(){return this.__tree__},jsonstat.prototype.toString=function(){return this["class"]};
export const version="0.13.13";
export function JSONstat(resp){
return new jsonstat(resp);
}