-
Notifications
You must be signed in to change notification settings - Fork 24
/
module.js
23 lines (16 loc) · 13.4 KB
/
module.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) (Nodejs 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 JSONstat(t){return new JSONstat.jsonstat(t)}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){var n,i,r,l,s=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},o=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 a=[],u=0;if(this.error=null,this.length=0,"string"==typeof e&&console.log("Module does not accept a URI string, must be an object."),null===e||"object"!=typeof e)return void(this["class"]=null);if(e.hasOwnProperty("error"))return void(this.error=e.error);if("dataset"===e["class"]||"collection"===e["class"]||"dimension"===e["class"])return JSONstat(e);for(i in e)u++,a.push(i);this.__tree__=e,this.length=u,this.id=a;break;case"dataset":e.hasOwnProperty("__tree__")?this.__tree__=n=e.__tree__:this.__tree__=n=e,this.label=n.label||null,this.note=n.note||null,this.link=n.link||null,this.href=n.href||null,this.updated=n.updated||null,this.source=n.source||null,this.extension=n.extension||null;var h,f=0,c=n.size||n.dimension&&n.dimension.size;if(this.size=c,n.hasOwnProperty("value")&&t(n.value))f=n.value.length;else{var d=1;for(h=c.length;h--;)d*=c[h];f=d}if(this.value=s(n.value,f),this.status=n.hasOwnProperty("status")?s(n.status,f):null,n.hasOwnProperty("dimension")){var v=n.dimension,p=n.role||!n.version&&v.role||null,g=n.id||v.id,y=c.length,b=function(t){p.hasOwnProperty(t)||(p[t]=null)};if(!t(g)||!t(c)||g.length!=y)return;if(this.length=y,this.id=g,p&&(b("time"),b("geo"),b("metric"),b("classification")),p&&null===p.classification){var _=[],m=["time","geo","metric"],x=function(t,e){for(var n=e.length;n--;)if(t===e[n])return!0;return!1};for(h=0;3>h;h++){var O=p[m[h]];null!==O&&(_=_.concat(O))}for(p.classification=[],h=0;y>h;h++)x(g[h],_)||p.classification.push(g[h]);0===p.classification.length&&(p.classification=null)}this.role=p,this.n=f;for(var w=0,k=this.length;k>w;w++)if(v[g[w]].category.hasOwnProperty("index")){if(t(v[g[w]].category.index)){var D={},S=v[g[w]].category.index;for(r=S.length,l=0;r>l;l++)D[S[l]]=l;v[g[w]].category.index=D}}else{var j=0;v[g[w]].category.index={};for(i in v[g[w]].category.label)v[g[w]].category.index[i]=j++}}else this.length=0;break;case"dimension":if(!e.hasOwnProperty("__tree__"))return JSONstat({version:"2.0","class":"dataset",dimension:{d:e},id:["d"],size:[o(e.category)],value:[null]}).Dimension(0);n=e.__tree__;var z=[],P=n.category;if(!n.hasOwnProperty("category"))return;if(!P.hasOwnProperty("label")){P.label={};for(i in P.index)P.label[i]=i}for(i in P.index)z[P.index[i]]=i;this.__tree__=n,this.label=n.label||null,this.note=n.note||null,this.link=n.link||null,this.href=n.href||null,this.id=z,this.length=z.length,this.role=e.role,this.hierarchy=P.hasOwnProperty("child"),this.extension=n.extension||null;break;case"category":var J=e.child;this.id=J,this.length=null===J?0:J.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 N=e.link.item;if(this.length=t(N)?N.length:0,this.length)for(l=0;l<this.length;l++)this.id[l]=N[l].href}}}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])})}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=[],g=[];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),g.push(f[i]);if(p.length>1)return null;if(1===p.length){for(var y=0,b=g[0];b>y;y++){var _=[];for(i=0;c>i;i++)i!==p[0]?_.push(e[i]):_.push(y);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),x=[],O=h.dimension,w=h.id||O.id;for(i=0,r=m.length;r>i;i++)x.push(O[w[i]].category.index[m[i]]);return this.Data(x,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(),g=a.role&&a.role.metric,y=function(){},b={},_=this,m=_.id,x=e.by&&-1!==m.indexOf(e.by)?e.by:null,O=e.meta===!0,w=void 0!==e.drop&&t(e.drop)?e.drop:[],k=e.comma===!0,D=e.bylabel===!0,S=function(t){if(O){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:x,bylabel:D,drop:null!==x&&w.length>0?w:null,prefix:null!==x?T||"":null,comma:k,dimensions:n},data:t}}return t};if(null===x&&e.unit&&g){if("id"!==e.content)for(var j=g.length;j--;){var z=this.Dimension(g[j]);b[g[j]]={};for(var P=z.length;P--;)b[g[j]][z.Category(P).label]=z.id[P]}y=function(t,n){if(-1!==g.indexOf(t)){var i=a.dimension[t].category;i.unit?J.unit=i.unit["id"!==e.content?b[t][n]:n]:J.unit=null}},e.unit=!0}else e.unit=!1;for(o=i.length,r=0;o>r;r++){var J={};for(l=i[r].length;l--;)J[p[l]]=i[r][l],y(p[l],i[r][l]);v.push(J)}if(k&&v.forEach(function(t){null!==t.value&&(t.value=(""+t.value).replace(".",","))}),null!==x){var N,E={},c=[],C={},T=void 0!==e.prefix?e.prefix:"";w.forEach(function(t,e){(!_.Dimension(t)||_.Dimension(t).length>1)&&(w[e]="")});var I=m.filter(function(t){return t!==x&&-1===w.indexOf(t)}),V=_.Dimension(x),A=function(t,e){var n=[];return e.forEach(function(e){n.push(t[e])}),n.join(" ")},M=function(t,e){var n={};return e.forEach(function(e){n[e]=t[e]}),n};"id"!==e.content?D?N=function(t,e,n){t[e][T+n[x]]=n.value}:(V.Category().forEach(function(t,e){C[t.label]=V.id[e]}),N=function(t,e,n){t[e][T+C[n[x]]]=n.value}):N=function(t,e,n){t[e][T+n[x]]=n.value},v.forEach(function(t){var e=A(t,I);void 0===E[e]&&(E[e]=M(t,I)),N(E,e,t,x)});for(var R in E)c.push(E[R]);return u=!1,S(c)}return S(v)}var U,q,B,F,G="id"===e.field;if("object"===e.type){var H="number"==typeof this.value[0]||null===this.value[0]?"number":"string";U=function(t,e){var n=G&&t||e||t;nt.push({id:t,label:n,type:"string"})},q=function(t,e,n){var i=G&&"value"||t||"Value",r=G&&"status"||e||"Status";n&&nt.push({id:"status",label:r,type:"string"}),nt.push({id:"value",label:i,type:H})},B=function(t){pt.push({v:t})},F=function(t){pt.push({v:t}),it.push({c:pt})}}else U=function(t,e){var n=G&&t||e||t;nt.push(n)},q=function(t,e,n){var i=G&&"value"||t||"Value",r=G&&"status"||e||"Status";n&&nt.push(r),nt.push(i),et.push(nt)},B=function(t){pt.push(t)},F=function(t){pt.push(t),et.push(pt)};var K=a.dimension,L=a.id||K.id,Q=a.size||K.size,W=L.length;if(W!=Q.length)return!1;var X=[],Y=1,j=1,Z=[],$=[],tt=[],et=[],nt=[],it=[];for(r=0;W>r;r++){var rt=L[r],lt=K[rt].label;U(rt,lt),Y*=Q[r],j*=Q[r];var st=[];for(l=0;l<Q[r];l++)for(var ot in K[L[r]].category.index)if(K[L[r]].category.index[ot]===l){var at="id"!==e.content&&K[L[r]].category.label?K[L[r]].category.label[ot]:ot;st.push(at)}X.push(st),Z.push(j)}for(q(e.vlabel,e.slabel,u),o=X.length,r=0;o>r;r++){for(var ut=[],ht=0,ft=X[r].length;ft>ht;ht++)for(var ct=0;ct<Y/Z[r];ct++)ut.push(X[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 gt=0;o>gt;gt++)B(tt[gt][s]);u&&B(this.status?this.status[s]:null),F(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}(),module.exports=JSONstat;