-
Notifications
You must be signed in to change notification settings - Fork 24
/
json-stat.js
23 lines (16 loc) · 15.1 KB
/
json-stat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
JSON-stat Javascript Toolkit v. 0.13.13 (JSON-stat v. 2.0 ready)
https://json-stat.com
https://github.com/badosa/JSON-stat
Copyright 2019 Xavier Badosa (http://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 JSONstat(t,e,n){return window===this?new JSONstat.jsonstat(t,e,n):void 0}var JSONstat=JSONstat||{};JSONstat.version="0.13.13",function(){"use strict";function t(t){return"[object Array]"===Object.prototype.toString.call(t)}function e(e,n,i){var r,l,s,o,a=function(t,e,n){var i,r,l=e!==!1;if(n=e?n:!0,window.XDomainRequest&&/^(http(s)?:)?\/\//.test(t)){if(!l)return;r=new XDomainRequest,r.onload=function(){i=JSON.parse(r.responseText),n?e.call(JSONstat(i)):e.call(i)},r.open("GET",t),r.send()}else if(r=new XMLHttpRequest,r.onreadystatechange=function(){if(4===r.readyState){var t=r.status;i=t&&r.responseText&&(t>=200&&300>t||304===t)?JSON.parse(r.responseText):null,l&&(n?e.call(JSONstat(i)):e.call(i))}},r.open("GET",t,l),r.send(null),!l)return i},u=function(e,n){var i,r=[];if("string"==typeof e&&(e=[e]),t(e)){if(e.length===n)return e;if(1===e.length){for(i=0;n>i;i++)r.push(e[0]);return r}}for(i=0;n>i;i++){var l=void 0===e[i]?null:e[i];r.push(l)}return r},h=function(e){var n=void 0===e.index?e.label:e.index;return t(n)?n.length:Object.keys(n).length};if(this.length=0,this.id=[],null!==e&&void 0!==e)switch(this["class"]=e["class"]||"bundle",this["class"]){case"bundle":var f=[],c=0;if(this.error=null,this.length=0,"string"==typeof e&&e.length>0&&(e=a(e,"function"==typeof n?n:!1,void 0===i?!0:i)),null===e||"object"!=typeof e)return this["class"]=null,void(void 0===e&&(delete this.id,delete this["class"],delete this.error,delete this.length));if(e.hasOwnProperty("error"))return void(this.error=e.error);if("dataset"===e["class"]||"collection"===e["class"]||"dimension"===e["class"])return JSONstat(e);for(l in e)c++,f.push(l);this.__tree__=e,this.length=c,this.id=f;break;case"dataset":e.hasOwnProperty("__tree__")?this.__tree__=r=e.__tree__:this.__tree__=r=e,this.label=r.label||null,this.note=r.note||null,this.link=r.link||null,this.href=r.href||null,this.updated=r.updated||null,this.source=r.source||null,this.extension=r.extension||null;var d,v=0,p=r.size||r.dimension&&r.dimension.size;if(this.size=p,r.hasOwnProperty("value")&&t(r.value))v=r.value.length;else{var y=1;for(d=p.length;d--;)y*=p[d];v=y}if(this.value=u(r.value,v),this.status=r.hasOwnProperty("status")?u(r.status,v):null,r.hasOwnProperty("dimension")){var g=r.dimension,b=r.role||!r.version&&g.role||null,_=r.id||g.id,m=p.length,O=function(t){b.hasOwnProperty(t)||(b[t]=null)};if(!t(_)||!t(p)||_.length!=m)return;if(this.length=m,this.id=_,b&&(O("time"),O("geo"),O("metric"),O("classification")),b&&null===b.classification){var x=[],w=["time","geo","metric"],S=function(t,e){for(var n=e.length;n--;)if(t===e[n])return!0;return!1};for(d=0;3>d;d++){var k=b[w[d]];null!==k&&(x=x.concat(k))}for(b.classification=[],d=0;m>d;d++)S(_[d],x)||b.classification.push(_[d]);0===b.classification.length&&(b.classification=null)}this.role=b,this.n=v;for(var j=0,D=this.length;D>j;j++)if(g[_[j]].category.hasOwnProperty("index")){if(t(g[_[j]].category.index)){var E={},J=g[_[j]].category.index;for(s=J.length,o=0;s>o;o++)E[J[o]]=o;g[_[j]].category.index=E}}else{var N=0;g[_[j]].category.index={};for(l in g[_[j]].category.label)g[_[j]].category.index[l]=N++}}else this.length=0;break;case"dimension":if(!e.hasOwnProperty("__tree__"))return JSONstat({version:"2.0","class":"dataset",dimension:{d:e},id:["d"],size:[h(e.category)],value:[null]}).Dimension(0);r=e.__tree__;var P=[],T=r.category;if(!r.hasOwnProperty("category"))return;if(!T.hasOwnProperty("label")){T.label={};for(l in T.index)T.label[l]=l}for(l in T.index)P[T.index[l]]=l;this.__tree__=r,this.label=r.label||null,this.note=r.note||null,this.link=r.link||null,this.href=r.href||null,this.id=P,this.length=P.length,this.role=e.role,this.hierarchy=T.hasOwnProperty("child"),this.extension=r.extension||null;break;case"category":var z=e.child;this.id=z,this.length=null===z?0:z.length,this.index=e.index,this.label=e.label,this.note=e.note||null,this.unit=e.unit,this.coordinates=e.coord;break;case"collection":if(this.length=0,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,null!==this.link&&e.link.item){var A=e.link.item;if(this.length=t(A)?A.length:0,this.length)for(o=0;o<this.length;o++)this.id[o]=A[o].href}}}Array.prototype.indexOf||(Array.prototype.indexOf=function(t,e){var n;if(null==this)throw new TypeError('"this" is null or not defined');var i=Object(this),r=i.length>>>0;if(0===r)return-1;var l=+e||0;if(Math.abs(l)===1/0&&(l=0),l>=r)return-1;for(n=Math.max(l>=0?l:r-Math.abs(l),0);r>n;){if(n in i&&i[n]===t)return n;n++}return-1}),Array.prototype.forEach||(Array.prototype.forEach=function(t,e){var n,i;if(null==this)throw new TypeError(" this is null or not defined");var r=Object(this),l=r.length>>>0;if("function"!=typeof t)throw new TypeError(t+" is not a function");for(arguments.length>1&&(n=e),i=0;l>i;){var s;i in r&&(s=r[i],t.call(n,s,i,r)),i++}}),Object.keys||(Object.keys=function(t){var e,n=[];for(e in t)Object.prototype.hasOwnProperty.call(t,e)&&n.push(e);return n}),Array.prototype.filter||(Array.prototype.filter=function(t){if(void 0===this||null===this)throw new TypeError;var e=Object(this),n=e.length>>>0;if("function"!=typeof t)throw new TypeError;for(var i=[],r=arguments.length>=2?arguments[1]:void 0,l=0;n>l;l++)if(l in e){var s=e[l];t.call(r,s,l,e)&&i.push(s)}return i}),e.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])}:t.follow&&(e=function(t,e){n.push(JSONstat(t.id[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},e.prototype.Dataset=function(t){if(null===this)return null;if("dataset"===this["class"])return void 0!==t?this:[this];var n,i=[],r=0;if("collection"===this["class"]){var l=this.Item({"class":"dataset",embedded:!0});if(void 0===t){for(n=l.length;n>r;r++)i.push(JSONstat(l[r]));return i}if("number"==typeof t&&t>=0&&t<l.length)return JSONstat(l[t]);if("string"==typeof t)for(n=l.length;n>r;r++)if(l[r].href===t)return JSONstat(l[r]);return null}if("bundle"!==this["class"])return null;if(void 0===t){for(n=this.id.length;n>r;r++)i.push(this.Dataset(this.id[r]));return i}if("number"==typeof t){var s=this.id[t];return void 0!==s?this.Dataset(s):null}var o=this.__tree__[t];return void 0===o?null:new e({"class":"dataset",__tree__:o})},e.prototype.Dimension=function(t,n){n="boolean"==typeof n?n:!0;var i,r=[],l=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(i=0;l>i;i++)r.push(this.Dimension(this.id[i]));return r}if("number"==typeof t){var o=this.id[t];return void 0!==o?this.Dimension(o,n):null}var a=this.role;if("object"==typeof t){if(t.hasOwnProperty("role")){for(i=0;l>i;i++){var u=this.id[i];s(a,u)===t.role&&r.push(this.Dimension(u,n))}return void 0===r[0]?null:r}return null}var h=this.__tree__.dimension;if(void 0===h)return null;var f=h[t];return void 0===f?null:n?new e({"class":"dimension",__tree__:f,role:s(a,t)}):function(t,e){var n=[];for(var i in t)n[t[i]]=e[i];return n}(f.category.index,f.category.label)},e.prototype.Category=function(t){if(null===this||"dimension"!==this["class"])return null;if(void 0===t){for(var n=[],i=0,r=this.id.length;r>i;i++)n.push(this.Category(this.id[i]));return n}if("number"==typeof t){var l=this.id[t];return void 0!==l?this.Category(l):null}var s=this.__tree__.category;if(void 0===s)return null;var o=s.index[t];if(void 0===o)return null;var a=s.unit&&s.unit[t]||null,u=s.coordinates&&s.coordinates[t]||null,h=s.child&&s.child[t]||null,f=s.note&&s.note[t]||null;return new e({"class":"category",index:o,label:s.label[t],note:f,child:h,unit:a,coord:u})},e.prototype.Slice=function(e){if(null===this||"dataset"!==this["class"])return null;if(void 0===e)return this;if(!t(e)){var n,i=[];for(n in e)i.push([n,e[n]]);e=i}var r=this,l=e.length,s=r.toTable({field:"id",content:"id",status:!0}),o=r.status,a=s.shift(),u=!1,h=[],f=[],c=[],d=[];return e.forEach(function(t){var e=r.Dimension(t[0]);if(null===e)return void(u=!0);var n=e.id.indexOf(t[1]);return-1===n?void(u=!0):(c.push([r.id.indexOf(t[0]),n]),void d.push(e.Category(n).label))}),u?null:(s.forEach(function(t){var n,i={},r=0;for(n=t.length;n--;)i[a[n]]=t[n];e.forEach(function(t){i[t[0]]===t[1]&&r++}),l===r&&(h.push(i.value),f.push(i.status))}),r.n=h.length,r.value=r.__tree__.value=h,r.status=r.__tree__.status=null!==o?f:null,e.forEach(function(t,e){r.size[c[e][0]]=1,r.__tree__.dimension[t[0]].category.index={},r.__tree__.dimension[t[0]].category.index[t[1]]=0,r.__tree__.dimension[t[0]].category.label={},r.__tree__.dimension[t[0]].category.label[t[1]]=d[e]}),r)},e.prototype.Data=function(e,n){var i,r,l=[],s=function(t){for(var e in t)if(t.hasOwnProperty(e))return e},o=function(t,e,n){var i,r=[],l={},o=t.dimension,a=t.id||o.id,u=t.size||o&&o.size;if("array"===n){for(i=e.length;i--;)l[e[i][0]]=e[i][1];e=l}for(var h=0,f=a.length;f>h;h++){var c=a[h],d=e[c];r.push("string"==typeof d?d:1===u[h]?s(o[c].category.index):null)}return r};if(null===this||"dataset"!==this["class"])return null;if(void 0===e){for(r=this.value.length,i=0;r>i;i++)l.push(this.Data(i));return l}if("boolean"!=typeof n&&(n=!0),"number"==typeof e){var a=this.value[e];return void 0===a?null:n?{value:a,status:this.status?this.status[e]:null}:a}var u="object",h=this.__tree__,f=h.size||h.dimension&&h.dimension.size,c=f.length;if(t(e)){if(!t(e[0])){if(this.length!==e.length)return null;var d=1,v=0,p=[],y=[];for(i=0;c>i;i++)if(void 0!==e[i]){if("number"!=typeof e[i]||e[i]>=f[i])return null;d*=i>0?f[c-i]:1,v+=d*e[c-i-1]}else p.push(i),y.push(f[i]);if(p.length>1)return null;if(1===p.length){for(var g=0,b=y[0];b>g;g++){var _=[];for(i=0;c>i;i++)i!==p[0]?_.push(e[i]):_.push(g);l.push(this.Data(_,n))}return l}return n?{value:this.value[v],status:this.status?this.status[v]:null}:this.value[v]}u="array"}var m=o(h,e,u),O=[],x=h.dimension,w=h.id||x.id;for(i=0,r=m.length;r>i;i++)O.push(x[w[i]].category.index[m[i]]);return this.Data(O,n)},e.prototype.toTable=function(e,n){if(null===this||"dataset"!==this["class"])return null;1==arguments.length&&"function"==typeof e&&(n=e,e=null),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};var i,r,l,s,o,a=this.__tree__,u=e.status===!0;if("function"==typeof n){i=this.toTable(e);var h=[],f="array"!==e.type?0:1,c="object"!==e.type?i.slice(f):i.rows.slice(0);for(o=c.length,r=0;o>r;r++){var d=n.call(this,c[r],r);void 0!==d&&h.push(d)}return"object"===e.type?{cols:i.cols,rows:h}:("array"===e.type&&h.unshift(i[0]),h)}if("arrobj"===e.type){i=this.toTable({field:"id",content:e.content,status:u});var v=[],p=i.shift(),y=a.role&&a.role.metric,g=function(){},b={},_=this,m=_.id,O=e.by&&-1!==m.indexOf(e.by)?e.by:null,x=e.meta===!0,w=void 0!==e.drop&&t(e.drop)?e.drop:[],S=e.comma===!0,k=e.bylabel===!0,j=function(t){if(x){var n={};return m.forEach(function(t){var e=_.Dimension(t);n[t]={label:e.label,role:e.role,categories:{id:e.id,label:_.Dimension(t,!1)}}}),{meta:{label:_.label,source:_.source,updated:_.updated,id:m,status:u,unit:e.unit,by:O,bylabel:k,drop:null!==O&&w.length>0?w:null,prefix:null!==O?A||"":null,comma:S,dimensions:n},data:t}}return t};if(null===O&&e.unit&&y){if("id"!==e.content)for(var D=y.length;D--;){var E=this.Dimension(y[D]);b[y[D]]={};for(var J=E.length;J--;)b[y[D]][E.Category(J).label]=E.id[J]}g=function(t,n){if(-1!==y.indexOf(t)){var i=a.dimension[t].category;i.unit?N.unit=i.unit["id"!==e.content?b[t][n]:n]:N.unit=null}},e.unit=!0}else e.unit=!1;for(o=i.length,r=0;o>r;r++){var N={};for(l=i[r].length;l--;)N[p[l]]=i[r][l],g(p[l],i[r][l]);v.push(N)}if(S&&v.forEach(function(t){null!==t.value&&(t.value=(""+t.value).replace(".",","))}),null!==O){var P,T={},c=[],z={},A=void 0!==e.prefix?e.prefix:"";w.forEach(function(t,e){(!_.Dimension(t)||_.Dimension(t).length>1)&&(w[e]="")});var C=m.filter(function(t){return t!==O&&-1===w.indexOf(t)}),M=_.Dimension(O),q=function(t,e){var n=[];return e.forEach(function(e){n.push(t[e])}),n.join(" ")},R=function(t,e){var n={};return e.forEach(function(e){n[e]=t[e]}),n};"id"!==e.content?k?P=function(t,e,n){t[e][A+n[O]]=n.value}:(M.Category().forEach(function(t,e){z[t.label]=M.id[e]}),P=function(t,e,n){t[e][A+z[n[O]]]=n.value}):P=function(t,e,n){t[e][A+n[O]]=n.value},v.forEach(function(t){var e=q(t,C);void 0===T[e]&&(T[e]=R(t,C)),P(T,e,t,O)});for(var V in T)c.push(T[V]);return u=!1,j(c)}return j(v)}var X,G,I,H,L="id"===e.field;if("object"===e.type){var B="number"==typeof this.value[0]||null===this.value[0]?"number":"string";X=function(t,e){var n=L&&t||e||t;nt.push({id:t,label:n,type:"string"})},G=function(t,e,n){var i=L&&"value"||t||"Value",r=L&&"status"||e||"Status";n&&nt.push({id:"status",label:r,type:"string"}),nt.push({id:"value",label:i,type:B})},I=function(t){pt.push({v:t})},H=function(t){pt.push({v:t}),it.push({c:pt})}}else X=function(t,e){var n=L&&t||e||t;nt.push(n)},G=function(t,e,n){var i=L&&"value"||t||"Value",r=L&&"status"||e||"Status";n&&nt.push(r),nt.push(i),et.push(nt)},I=function(t){pt.push(t)},H=function(t){pt.push(t),et.push(pt)};var F=a.dimension,K=a.id||F.id,Q=a.size||F.size,U=K.length;if(U!=Q.length)return!1;var W=[],Y=1,D=1,Z=[],$=[],tt=[],et=[],nt=[],it=[];for(r=0;U>r;r++){var rt=K[r],lt=F[rt].label;X(rt,lt),Y*=Q[r],D*=Q[r];var st=[];for(l=0;l<Q[r];l++)for(var ot in F[K[r]].category.index)if(F[K[r]].category.index[ot]===l){var at="id"!==e.content&&F[K[r]].category.label?F[K[r]].category.label[ot]:ot;st.push(at)}W.push(st),Z.push(D)}for(G(e.vlabel,e.slabel,u),o=W.length,r=0;o>r;r++){for(var ut=[],ht=0,ft=W[r].length;ft>ht;ht++)for(var ct=0;ct<Y/Z[r];ct++)ut.push(W[r][ht]);$.push(ut)}for(o=$.length,r=0;o>r;r++){var dt=[],vt=0;for(s=0;Y>s;s++)dt.push($[r][vt]),vt++,vt===$[r].length&&(vt=0);tt.push(dt)}for(s=0;Y>s;s++){var pt=[];o=$.length;for(var yt=0;o>yt;yt++)I(tt[yt][s]);u&&I(this.status?this.status[s]:null),H(this.value[s])}return"object"===e.type?{cols:nt,rows:it}:et},e.prototype.node=function(){return this.__tree__},e.prototype.toString=function(){return this["class"]},JSONstat.jsonstat=e}();