jsonstat-suite 3.4.1 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/iife.js +2 -2
- package/import.mjs +2 -2
- package/main.cjs +2 -2
- package/module.mjs +2 -2
- package/package.json +7 -8
- package/rollup.config.mjs +0 -13
package/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
# JSON-stat
|
|
1
|
+
# JSON-stat JavaScript Utilities Suite
|
|
2
2
|
|
|
3
3
|
<blockquote>
|
|
4
|
-
|
|
5
|
-
</blockquote>
|
|
4
|
+
The JSON-stat JavaScript Utilities Suite is a set of high level functions built on top of the [JSON-stat JavaScript Toolkit](https://www.npmjs.com/package/jsonstat-toolkit).
|
|
6
5
|
|
|
7
|
-
The JSON-stat
|
|
6
|
+
The <strong>jsonstat-suite</strong> package contains versions 3 and above of the JSON-stat JavaScript Utilities Suite. Version 2 of this Suite can be found in the <a href="https://www.npmjs.com/package/jsonstat-utils">jsonstat-utils</a> package. Version 4 is the current release and is built upon <a href="https://www.npmjs.com/package/jsonstat-toolkit">jsonstat-toolkit</a> version 2.2.2 and subsequent releases.
|
|
7
|
+
</blockquote>
|
|
8
8
|
|
|
9
9
|
This suite includes 7 functions:
|
|
10
10
|
|
package/iife.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
// jsonstat-suite
|
|
2
|
-
var JSONstatUtils=function(e,t){"use strict";function n(e,n){return null==e?null:("string"!=typeof e&&void 0!==e.length||(e=t(e)),0===e.length||"dataset"!==e.class&&"collection"!==e.class&&"bundle"!==e.class?null:"dataset"===e.class?e:e.Dataset(n))}function r(e){if(null===e||0===e.length||"dataset"!==e.class)return!1;for(var t=e.length,n=1;t--;)n*=e.Dimension(t).length;return n===e.n}function a(e,t,n){var r,a,l,i,o;return(t.label||n).capitalize()+(a=t,o="",(r=e)&&"metric"===r.role&&a.unit&&(l=a.unit.hasOwnProperty("label")?a.unit.label:"")+(i=a.unit.hasOwnProperty("symbol")?a.unit.symbol:"")!==""&&(o=" ("+(o=""===i?l:""===l?i:"start"===a.unit.position?i+l:l+" "+i)+")"),o)}function l(e,t,n){var r,l='<select name="'+t+'">',i=[];if(null!==n[1]){if(r=e.id,i=e.Dimension(),2===r.length)return(e.Dimension(n[0]).label||n[0]).capitalize()}else{var o=e.Dimension(t);if(r=o.id,i=o.Category(),1===r.length)return}return r.forEach((function(e,t){var r=e!==n[0]?"":'selected="selected" ';null!==n[1]&&e===n[1]||(l+="<option "+r+'value="'+e+'">'+a(o,i[t],e)+"</option>")})),l+="</select>"}function i(e,t){var n={};return Array.isArray(e[t])?(e[t].forEach((function(e,t){null!==e&&(n[String(t)]=e)})),n):e[t]}function o(e,n){if(void 0===e)return null;void 0===n&&(n={}),"boolean"!=typeof n.ovalue&&(n.ovalue=!1),"boolean"!=typeof n.ostatus&&(n.ostatus=!1),"boolean"!=typeof n.instance&&(n.instance=!1);var r=n.vlabel||"Value",a=n.slabel||"Status",l=n.type||"array",o=n.label||"",s=n.header||null,u=[],c=[],f=[],d=[],h={},v={},p=function(e,t){for(var n=1,r=0,a=0;a<E;a++)r+=(n*=a>0?t[E-a]:1)*e[E-a-1];return r},b=function(){var t=e[S][r];f[p(A,c)]=isNaN(t)?null:t};switch(l){case"array":e=function(e){for(var t=e[0],n=e.slice(1),r=[],a=0,l=n.length;a<l;a++){for(var i=0,o=t.length,s={};i<o;i++)s[t[i]]=n[a][i];r.push(s)}return r}(e);break;case"object":e=function(e){for(var t=e.cols.map((function(e){return e.id})),n=e.rows,r=[],a=0,l=n.length;a<l;a++){for(var i=0,o=t.length,s={};i<o;i++)s[t[i]]=n[a].c[i].v;r.push(s)}return r}(e)}var y,g=e.length;for(var m in n.hasOwnProperty("drop")&&Array.isArray(n.drop)&&n.drop.length&&e.forEach((function(e){n.drop.forEach((function(t){delete e[t]}))})),e[0])if(m!==r)if(m!==a){if(u.push(m),s)y=s.dimension[m],h[m]=y.category.index;else{h[m]=[];for(var w=0;w<g;w++){var O=e[w][m];-1===h[m].indexOf(O)&&h[m].push(O)}}c.push(h[m].length),v[m]={label:s?y.label:m,category:{index:h[m]}},s&&(v[m].category.label=y.category.label,y.category.unit&&(v[m].category.unit=y.category.unit))}else b=function(){var t=e[S][r],n=e[S][a];f[p(A,c)]=isNaN(t)?null:t,d[p(A,c)]=""===n?null:n};for(var E=u.length,S=0;S<g;S++){for(var A=[],x=0;x<E;x++){var P=u[x];A.push(h[P].indexOf(e[S][P]))}b()}var j={version:"2.0",class:"dataset",value:f,dimension:v,id:u,size:c};return o&&(j.label=o),d.length&&(j.status=d),s&&(s.label&&(j.label=s.label),s.source&&(j.source=s.source),s.updated&&(j.updated=s.updated),s.href&&(j.href=s.href),s.role&&(j.role=s.role)),n.ovalue&&(j.value=i(j,"value")),n.ostatus&&j.hasOwnProperty("status")&&(j.status=i(j,"status")),n.instance?t(j):j}function s(e,t){return null==e?"":-1!==e.indexOf(t)?'"'+e+'"':e}String.prototype.capitalize=function(){return this.charAt(0).toUpperCase()+this.slice(1)};return e.datalist=function(e,t){if(void 0===e)return null;void 0===t&&(t={});var a="",l="",i="",o=0,s=t.na||"n/a",u=t.dsid||0,c=t.vlabel||null,f=t.slabel||null,d=t.counter||!1,h=t.tblclass||"",v=t.numclass||"",p=t.valclass||"",b=t.status||!1,y=t.locale||"en-US",g=t.source||"Source",m=n(e,u),w=m.role&&m.role.metric?m.id.indexOf(m.role.metric[0]):null,O=m.Dimension({role:"metric"}),E={},S=Number.toLocaleString&&"none"!==y?function(e,t){return null===t?e.toLocaleString(y):e.toLocaleString(y,{minimumFractionDigits:t,maximumFractionDigits:t})}:function(e,t){return null===t?e:e.toFixed(t)},A=function(e,t){var n=null;e.forEach((function(e,r){var i=j===r?' class="'+v+" "+p+'"':"",o=s;t?(w===r&&(n=E[e]),null!==e&&(o=j===r?S(e,n):e),l+="<td"+i+">"+o+"</td>"):a+="<th"+i+">"+e+"</th>"}))},x=d?function(e,t){t?l+='<tr><td class="'+v+'">'+t+"</td>":a+='<th class="'+v+'">#</th>',A(e,t),l+="</tr>"}:function(e,t){l+="<tr>",A(e,t),l+="</tr>"};if(!r(m))return null;O&&O[0].Category().forEach((function(e){var t=e.unit&&e.unit.hasOwnProperty("decimals")?e.unit.decimals:null;E[e.label]=t}));var P=m.toTable({status:b,vlabel:c,slabel:f}),j=P[0].length-1;return P.forEach((function(e,t){x(e,t)})),m.source&&(o=m.length+1,d&&o++,b&&o++,"."!==(g+=": "+m.source).slice(-1)&&(g+="."),i='<tfoot><td colspan="'+o+'">'+g+"</td></tfoot>"),'<table class="'+h+'"><caption>'+(t.caption||m.label||"")+"</caption><thead><tr>"+a+"</tr></thead><tbody>"+l+"</tbody>"+i+"</table>"},e.fromCSV=function(e,t){if(void 0===e)return null;void 0===t&&(t={});var n,r,a,l=[],i=null,s=null,u=!1,c={time:[],geo:[],metric:[]},f="jsonstat"===e.substring(0,8),d=f?"value":t.vlabel,h=f?"status":t.slabel,v=f?e.substring(8,9):t.delimiter||",",p=";"===v?t.decimal||",":t.decimal||".",b=function(e,t){t=t||",";for(var n,r,a=new RegExp("(\\"+t+'|\\r?\\n|\\r|^)(?:"([^"]*(?:""[^"]*)*)"|([^"\\'+t+"\\r\\n]*))","gi"),l=[[]],i=null;i=a.exec(e);)(r=i[1]).length&&r!=t&&l.push([]),n=i[2]?i[2].replace(new RegExp('""',"g"),'"'):i[3],l[l.length-1].push(n);return l}(e.trim(),v);if(f){for(p=b[0][1],a=b[0][2],b.shift();"data"!==b[0][0];)l.push(b.shift());b.shift(),s={dimension:{}},l.forEach((function(e){var t,n,r,l,i,o,f,d;switch(e[0]){case"dimension":if(s.dimension[e[1]]={},(l=s.dimension[e[1]]).label=e[2],l.category={},(i=l.category).index=[],n={},r=2*e[3]+3,e.length>=r){for(t=4;t<r;t++)f=e[t],d=e[++t],Object.defineProperty(n,f,{value:d,writable:!0,configurable:!0,enumerable:!0}),i.label=n,i.index.push(f);"string"==typeof e[t]&&-1!==["time","geo","metric"].indexOf(e[t])&&(c[e[t]].push(e[1]),u=!0,"metric"===e[t]&&"string"==typeof e[++t]&&(i.unit={},i.index.forEach((function(n,r){var l=e[t+r].split(a);i.unit[n]={},o=i.unit[n],void 0!==l[0]&&""!==l[0]&&(o.decimals=1*l[0]),void 0!==l[1]&&""!==l[1]&&(o.label=l[1]),void 0!==l[2]&&""!==l[2]&&(o.symbol=l[2]),void 0!==l[1]&&-1!==["start","end"].indexOf(l[3])&&(o.position=l[3])}))))}break;case"label":case"source":case"updated":case"href":s[e[0]]=e[1]||null}})),u&&(c.time.length||delete c.time,c.geo.length||delete c.geo,c.metric.length||delete c.metric,s.role=c)}if(n=b.length,r=b[0].length,void 0!==d){for(;r--;)if(b[0][r]===d){i=r;break}if(null===i)return null}else i=r-1,d=b[0][i];if(","===p)for(r=1;r<n;r++)b[r][i]=Number(b[r][i].replace(",","."));else for(r=1;r<n;r++)b[r][i]=Number(b[r][i]);return o(b,{header:s,vlabel:d,slabel:h,type:"array",label:t.label||"",ovalue:t.ovalue||!1,ostatus:t.ostatus||!1,instance:t.instance||!1})},e.fromSDMX=function(e,n){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){var t=e.dataSets[0],n=t.series,r=e.structure,a=r.dimensions,l={};Object.keys(n).forEach((function(e){var t=n[e].observations;Object.keys(t).forEach((function(r){l[e+":"+r]=t[r].concat(n[e].attributes)}))})),t.observations=l,delete t.series,a.observation=a.series.concat(a.observation),delete a.series,r.attributes.observation=r.attributes.observation.concat(r.attributes.series),delete r.attributes.series}(e),void 0===n?n={ovalue:!1,ostatus:!1,instance:!1}:("boolean"!=typeof n.ovalue&&(n.ovalue=!1),"boolean"!=typeof n.ostatus&&(n.ostatus=!1),"boolean"!=typeof n.instance&&(n.instance=!1));var r=e.structure,a=e.dataSets[0].observations,l=r.attributes.observation,i=r.dimensions;if(!i.hasOwnProperty("observation"))return null;if(i.hasOwnProperty("series")&&Object.keys(i.series).length)return null;Array.prototype.reduce||Object.defineProperty(Array.prototype,"reduce",{value:function(e){if(null===this)throw new TypeError("Array.prototype.reduce called on null or undefined");if("function"!=typeof e)throw new TypeError(e+" is not a function");var t,n=Object(this),r=n.length>>>0,a=0;if(arguments.length>=2)t=arguments[1];else{for(;a<r&&!(a in n);)a++;if(a>=r)throw new TypeError("Reduce of empty array with no initial value");t=n[a++]}for(;a<r;)a in n&&(t=e(t,n[a],a,n)),a++;return t}}),Array.prototype.find||Object.defineProperty(Array.prototype,"find",{value:function(e){if(null==this)throw TypeError('"this" is null or not defined');var t=Object(this),n=t.length>>>0;if("function"!=typeof e)throw TypeError("predicate must be a function");for(var r=arguments[1],a=0;a<n;){var l=t[a];if(e.call(r,l,a,t))return l;a++}},configurable:!0,writable:!0}),Array.prototype.findIndex||Object.defineProperty(Array.prototype,"findIndex",{value:function(e){if(null==this)throw new TypeError('"this" is null or not defined');var t=Object(this),n=t.length>>>0;if("function"!=typeof e)throw new TypeError("predicate must be a function");for(var r=arguments[1],a=0;a<n;){var l=t[a];if(e.call(r,l,a,t))return a;a++}return-1},configurable:!0,writable:!0});var o=1,s=[],u=[],c={},f=[],d={time:[],geo:[]},h=function(){},v=function(e,t){for(var n=e.size,r=n.length-t.length;r--;)t.push(0);for(var a=0,l=n.length,i=0,o=1;a<l;a++)i+=(o*=a>0?n[l-a]:1)*t[l-a-1];return i},p=function(e){if(c[e.id]={label:e.name},e.hasOwnProperty("role"))switch(e.role){case"REF_AREA":d.geo.push(e.id);break;case"TIME_PERIOD":d.time.push(e.id)}Object.defineProperty(c[e.id],"category",{value:{index:[],label:{}},writable:!0,enumerable:!0}),s.push(e.id),u.push(e.values.length),o*=e.values.length;var t=c[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})}))},b=e.header.links?e.header.links.find((function(e){return"request"===e.rel})):null,y=l.findIndex((function(e){return"OBS_STATUS"===e.id}));-1!==y&&(l[y].values.length?f=l[y].values:y=-1),i.observation.forEach(p),i.hasOwnProperty("dataSet")&&i.dataSet.forEach(p);var g={version:"2.0",class:"dataset",updated:e.header.prepared||null,source:e.header.sender.name||null,label:r.name||null,id:s,size:u,dimension:c,value:n.ovalue?{}:new Array(o).fill(null)};for(var m in b&&(g.link={alternate:[{type:"application/vnd.sdmx.data+json",href:b.href}]}),d.geo.length+d.time.length>0&&(d.time.length||delete d.time,d.geo.length||delete d.geo,g.role=d),-1!==y&&(g.status=n.ostatus?{}:new Array(o).fill(null),g.extension={status:{label:{}}},f.forEach((function(e){g.extension.status.label[e.id]=e.name})),h=n.ostatus?function(){var e=a[m][y];null!==e&&(g.status[v(g,w)]=f[e].id)}:function(){var e=a[m][y];g.status[v(g,w)]=null===e?null:f[e].id}),y++,a){var w=m.split(":");n.ovalue&&null===a[m][0]||(g.value[v(g,w)]=a[m][0]),h()}return n.instance?t(g):g},e.fromTable=o,e.join=function(e,n){if(void 0===e||!Array.isArray(e))return null;var r=JSON.parse(JSON.stringify(e)),a=r[0];if(!a.hasOwnProperty("version")||!a.hasOwnProperty("class")||"dataset"!==a.class)return null;void 0===n&&(n={});var l=void 0===n.label?null:n.label,i=void 0===n.by?null:n.by,s=[];if(null===i){for(var u=1,c=r.length;u<c;u++)s=s.concat(r[u].value);return a.value=s,null!==l&&(a.label=l),a}var f,d,h,v=function(e,t,n){if(Array.isArray(e))e=e.concat(t);else for(var r in t)e[r]=0===t[r]?n:t[r];return e};r.forEach((function(e,n){var r=t(e).toTable({status:!0}),a=e.dimension[i].category;0===n?(s=[r[0]],f=a.index,d=a.label,h=a.unit):(f=function(e,t,n){if(Array.isArray(e))e=e.concat(t);else for(var r in t)e[r]=t[r]+n;return e}(f,a.index,Object.keys(f).length),d=v(d,a.label,n),h=v(h,a.unit,n)),s=s.concat(r.slice(1))}));var p=o(s);return a.value=p.value,a.size=p.size,a.status=p.status||null,a.label=l||"",a.href=null,a.dimension[i].category.index=f||null,a.dimension[i].category.label=d||null,a.dimension[i].category.unit=h||null,a},e.tbrowser=function(e,t,i){function o(e){void 0!==t?t.innerHTML=s[e]:window.alert(s[e])}if(void 0!==e)if(void 0!==t){void 0===i&&(i={});var s=void 0===i.i18n||void 0===i.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"}:i.i18n.msgs,u=void 0===i.i18n||void 0===i.i18n.locale?"en-US":i.i18n.locale,c=i.dsid||0,f=i.status||!1,d=i.tblclass||"",h=i.nonconst||!1,v=n(e,c);if(r(v)){if(h)var p=function(e){var t=0,n=e.size.slice(0),r=[];return n.forEach((function(n,a){var l=a-t,i=e.Dimension(l);1===n&&(delete e.__tree__.dimension[e.id[l]],e.size.splice(l,1),e.id.splice(l,1),e.length--,t++,r.push(i.label.capitalize()+": "+i.Category(0).label.capitalize()))})),r}(v);1!==v.length?function e(t,n,r,o){"function"==typeof i.callback&&i.callback(r);var c="",d="",v="",b="",y=r.rows,g=n.Dimension(y),m=g.id,w=r.cols,O=n.Dimension(w),E=O.id,S=n.role&&n.role.metric?n.role.metric[0]:null,A=null!==S?n.Dimension(S):null,x=function(e){return e.hasOwnProperty("unit")&&e.unit&&e.unit.hasOwnProperty("decimals")?e.unit.decimals:null},P=r.filters,j=JSON.parse(JSON.stringify(P)),D=[],T="",N="",z=n.source?s.source+": "+n.source:"",C=null!==n.label?'<span class="label">'+n.label.capitalize()+"</span>":"";for(var k in h&&p.length&&(C='<span class="label">'+p.join(". ")+"</span>"),""!==z&&"."!==z.slice(-1)&&(z+="."),v+="<caption>"+C+"<form>",P){var R=n.Dimension(k),_=R.label?R.label.capitalize():k.capitalize();R.length>1?T+="<p>"+l(n,k,[P[k],null])+" <strong>"+_+"</strong></p>":D.push({label:_,value:a(R,R.Category(0)),name:k,id:R.id[0]})}""!==T&&(T='<fieldset id="filters"><legend>'+s.filters+"</legend>"+T+"</fieldset>"),D.forEach((function(e){N+="<p>"+e.value+" <strong>"+e.label+'</strong></p><input type="hidden" name="'+e.name+'" value="'+e.id+'" />'})),""!==N&&(N='<fieldset id="constants"><legend>'+s.constants+"</legend>"+N+"</fieldset>"),v+=N+T+'<fieldset id="rowscols"><legend>'+s.rc+"</legend>"+l(n,"rows",[y,w])+" <a>↔</a> "+l(n,"cols",[w,y])+"</fieldset></form></caption>",b+="<tbody>";var L=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(m.forEach((function(e){j[y]=e;var t=n.Data(j),r=function(e,t){var n,r=w!==S?null===A?null:x(A.Category(j[S])):x(O.Category(t));null!==e.value?(n=L(e.value,r),f&&null!==e.status&&(n+=" ("+e.status+")")):n=e.status||s.na,b+="<td>"+n+"</td>"};null!==t?(b+='<tr><th scope="row">'+a(g,g.Category(e))+"</th>",Array.isArray(t)?t.forEach((function(e,t){r(e,t)})):r(t,0),b+="</tr>"):b="ERROR"})),"ERROR"===b)return s.dataerror;b+="</tbody>",c+="<thead><tr><th></th>",E.forEach((function(e){c+='<th scope="col">'+a(O,O.Category(e))+"</th>"})),c+="</tr></thead>",""!==z&&(d='<tfoot><tr><td colspan="'+(E.length+1)+'">'+z+"</td></tr></tfoot>"),t.innerHTML='<table class="'+o+'">'+v+c+b+d+"</table>",[].slice.call(t.querySelectorAll("select")).forEach((function(r){r.addEventListener("change",(function(r){var a,l;e(t,n,function(e,t,n){var r={filters:{}};return n.forEach((function(e){"rows"===e.name||"cols"===e.name?r[e.name]=e.value:r.filters[e.name]=e.value})),"rowscols"===t&&e.id.forEach((function(t,n){t!==r.rows&&t!==r.cols?void 0===r.filters[t]&&(r.filters[t]=e.Dimension(n).id[0]):delete r.filters[t]})),r}(n,r.target.parentElement.getAttribute("id"),(a=t,l=[],[].slice.call(a.querySelectorAll("select, input")).forEach((function(e){l.push({name:e.name,value:e.value})})),l)),o)}),!1)})),t.querySelector("a").addEventListener("click",(function(){r.cols=y,r.rows=w,e(t,n,r,o)}),!1)}(t,v,function(e,t){if("object"==typeof t)return{rows:t.rows,cols:t.cols,filters:t.filters};var n,r,a={},l=[],i=e.id;if(t){var o="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(o),n=l[0].id,r=l[1].id}else n=i[0],r=i[1];return e.Dimension(n).length<e.Dimension(r).length&&(n=r+(r=n,"")),i.forEach((function(t){t!==n&&t!==r&&(a[t]=e.Dimension(t).id[0])})),{rows:n,cols:r,filters:a}}(v,i.preset),d):o("dimerror")}else o("jsonerror")}else o("selerror");else o("urierror")},e.toCSV=function(e,t){if(void 0===e)return null;void 0===t&&(t={});var a={content:!1,field:!1},l=!0===t.rich,i=t.content||"label",o=t.field||"label",u=l?"value":t.vlabel||"Value",c=l?"status":t.slabel||"Status",f=!0===t.status,d=t.na||"n/a",h=t.delimiter||",",v=t.separator||"|",p=";"===h?t.decimal||",":t.decimal||".",b=!0===t.array,y=n(e,t.dsid||0),g=b?[]:"",m=b?[]:"",w=b?function(e){g.push(e)}:function(e){g+=e+"\n"},O=b?function(e){m.push(e)}:function(e){m+=e+"\n"};if(!r(y))return null;l?f=null!==y.status:("[id] label"===i&&(a.content=!0,i="id"),"[id] label"===o&&(a.field=!0,o="id"));var E=y.toTable({vlabel:u,slabel:c,status:f,field:l||a.field?"id":o,content:l||a.content?"id":i,type:"array"}),S=E[0].indexOf("id"===o?"value":u),A=f?E[0].indexOf("id"===o?"status":c):-1;return E.forEach((function(e,t){e.forEach((function(n,r){var l=y.Dimension(r);t&&r===S?null===n?e[r]=s(d,h):"."!==p&&(e[r]=String(e[r]).replace(".",p)):t&&r===A&&null===n?e[r]="":(a.content&&t&&l&&(e[r]="["+e[r]+"] "+l.Category(e[r]).label),a.field&&0===t&&(e[r]=l?"["+e[r]+"] "+l.label:"value"===e[r]?u:c),e[r]=s(e[r],h))})),w(e.join(h))})),l&&(O("jsonstat"+h+p+h+v),["label","source","updated","href"].forEach((function(e){y[e]&&O(e+h+s(y[e],h))})),y.id.forEach((function(e,t){var n=[],r=y.Dimension(t),a=r.role,l=!1,i="";i+="dimension"+h+s(e,h)+h+s(r.label,h)+h+r.length,"metric"===a&&r.__tree__.category.unit&&(l=!0),r.id.forEach((function(e,t){var a=[],o=r.Category(t);i+=h+s(e,h)+h+s(o.label,h),l&&(a.push(o.unit.hasOwnProperty("decimals")?o.unit.decimals:""),a.push(o.unit.label||""),o.unit.symbol&&(a.push(o.unit.symbol),a.push(o.unit.position)),n.push(s(a.join(v),h)))})),null!==a&&"classification"!==a&&(i+=h+r.role,l&&(i+=h+n.join(h))),O(i)})),g=b?m.concat(["data"],g):m+"data\n"+g),g},e.version="3.4.1",e}({},JSONstat);
|
|
1
|
+
// jsonstat-suite v4.0.0 Copyright 2026 Xavier Badosa https://jsonstat.com
|
|
2
|
+
var JSONstatUtils=function(e,t){"use strict";function n(e,n){return null==e?null:("string"!=typeof e&&void 0!==e.length||(e=t(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(n)))}function l(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,o="";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!==""&&(o=""===l?n:""===n?l:"start"===t.unit.position?l+n:n+" "+l,o=" ("+o+")")),o}(e,t)}function i(e,t,n){let l,i,r='<select name="'+t+'">',a=[];if(null!==n[1]){if(l=e.id,a=e.Dimension(),2===l.length)return(e.Dimension(n[0]).label||n[0]).capitalize()}else if(i=e.Dimension(t),l=i.id,a=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]||(r+="<option "+l+'value="'+e+'">'+o(i,a[t],e)+"</option>")}),r+="</select>",r}function r(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 a(e,n){if(void 0===e)return null;void 0===n&&(n={}),"boolean"!=typeof n.ovalue&&(n.ovalue=!1),"boolean"!=typeof n.ostatus&&(n.ostatus=!1),"boolean"!=typeof n.instance&&(n.instance=!1);const l=n.vlabel||"Value",o=n.slabel||"Status",i=n.type||"array",a=n.label||"",s=n.header||null,u=[],c=[],d=[],f=[],h={},b={},p=function(e,t){let n=1,l=0;for(let o=0;o<v;o++)n*=o>0?t[v-o]:1,l+=n*e[v-o-1];return l};let m,g=function(t,n){const o=e[t][l];d[p(n,c)]=isNaN(o)?null:o};switch(i){case"array":e=function(e){const t=e[0],n=e.slice(1),l=[];for(let e=0,o=n.length;e<o;e++){let o={};for(let l=0,i=t.length;l<i;l++)o[t[l]]=n[e][l];l.push(o)}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,o=n.length;e<o;e++){let o={};for(let l=0,i=t.length;l<i;l++)o[t[l]]=n[e].c[l].v;l.push(o)}return l}(e)}const y=e.length;n.hasOwnProperty("drop")&&Array.isArray(n.drop)&&n.drop.length&&e.forEach(function(e){n.drop.forEach(function(t){delete e[t]})});for(const t in e[0])if(t!==l)if(t!==o){if(u.push(t),s)m=s.dimension[t],h[t]=m.category.index;else{h[t]=[];for(let n=0;n<y;n++){const l=e[n][t];-1===h[t].indexOf(l)&&h[t].push(l)}}c.push(h[t].length),b[t]={label:s?m.label:t,category:{index:h[t]}},s&&(b[t].category.label=m.category.label,m.category.unit&&(b[t].category.unit=m.category.unit))}else g=function(t,n){const i=e[t][l],r=e[t][o];d[p(n,c)]=isNaN(i)?null:i,f[p(n,c)]=""===r?null:r};const v=u.length;for(let t=0;t<y;t++){const n=[];for(let l=0;l<v;l++){const o=u[l];n.push(h[o].indexOf(e[t][o]))}g(t,n)}const O={version:"2.0",class:"dataset",value:d,dimension:b,id:u,size:c};return a&&(O.label=a),f.length&&(O.status=f),s&&(s.label&&(O.label=s.label),s.source&&(O.source=s.source),s.updated&&(O.updated=s.updated),s.href&&(O.href=s.href),s.role&&(O.role=s.role)),n.ovalue&&(O.value=r(O,"value")),n.ostatus&&O.hasOwnProperty("status")&&(O.status=r(O,"status")),n.instance?t(O):O}function s(e,t){return null==e?"":-1!==e.indexOf(t)?'"'+e+'"':e}String.prototype.capitalize=function(){return this.charAt(0).toUpperCase()+this.slice(1)};return e.datalist=function(e,t){if(void 0===e)return null;void 0===t&&(t={});let o="",i="",r="",a=0,s=t.na||"n/a",u=t.dsid||0,c=t.vlabel||null,d=t.slabel||null,f=t.counter||!1,h=t.tblclass||"",b=t.numclass||"",p=t.valclass||"",m=t.status||!1,g=t.locale||"en-US",y=t.source||"Source",v=n(e,u),O=v.role&&v.role.metric?v.id.indexOf(v.role.metric[0]):null,w=v.Dimension({role:"metric"}),E={},S=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)},x=function(e,t){let n=null;e.forEach(function(e,l){const r=j===l?' class="'+b+" "+p+'"':"";let a=s;t?(O===l&&(n=E[e]),null!==e&&(a=j===l?S(e,n):e),i+="<td"+r+">"+a+"</td>"):o+="<th"+r+">"+e+"</th>"})},A=f?function(e,t){t?i+='<tr><td class="'+b+'">'+t+"</td>":o+='<th class="'+b+'">#</th>',x(e,t),i+="</tr>"}:function(e,t){i+="<tr>",x(e,t),i+="</tr>"};if(!l(v))return null;w&&w[0].Category().forEach(function(e){const t=e.unit&&e.unit.hasOwnProperty("decimals")?e.unit.decimals:null;E[e.label]=t}),console.log(v),console.log(v.Transform({status:m,vlabel:c,slabel:d}));const D=v.Transform({status:m,vlabel:c,slabel:d}),j=D[0].length-1;return D.forEach(function(e,t){A(e,t)}),v.source&&(a=v.length+1,f&&a++,m&&a++,y+=": "+v.source,"."!==y.slice(-1)&&(y+="."),r='<tfoot><td colspan="'+a+'">'+y+"</td></tfoot>"),'<table class="'+h+'"><caption>'+(t.caption||v.label||"")+"</caption><thead><tr>"+o+"</tr></thead><tbody>"+i+"</tbody>"+r+"</table>"},e.fromCSV=function(e,t){if(void 0===e)return null;void 0===t&&(t={});const n="jsonstat"===e.substring(0,8),l={time:[],geo:[],metric:[]},o=n?e.substring(8,9):t.delimiter||",";let i,r,s,u=[],c=null,d=null,f=!1,h=n?"value":t.vlabel,b=n?"status":t.slabel,p=";"===o?t.decimal||",":t.decimal||".";const m=function(e,t){t=t||",";const n=new RegExp("(\\"+t+'|\\r?\\n|\\r|^)(?:"([^"]*(?:""[^"]*)*)"|([^"\\'+t+"\\r\\n]*))","gi"),l=[[]];let o,i,r=null;for(;r=n.exec(e);)i=r[1],i.length&&i!=t&&l.push([]),o=r[2]?r[2].replace(new RegExp('""',"g"),'"'):r[3],l[l.length-1].push(o);return l}(e.trim(),o);if(n){for(p=m[0][1],s=m[0][2],m.shift();"data"!==m[0][0];)u.push(m.shift());m.shift(),d={dimension:{}},u.forEach(function(e){let t,n,o,i,r,a,u,c;switch(e[0]){case"dimension":if(d.dimension[e[1]]={},i=d.dimension[e[1]],i.label=e[2],i.category={},r=i.category,r.index=[],n={},o=2*e[3]+3,e.length>=o){for(t=4;t<o;t++)u=e[t],c=e[++t],Object.defineProperty(n,u,{value:c,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 o=e[t+l].split(s);r.unit[n]={},a=r.unit[n],void 0!==o[0]&&""!==o[0]&&(a.decimals=1*o[0]),void 0!==o[1]&&""!==o[1]&&(a.label=o[1]),void 0!==o[2]&&""!==o[2]&&(a.symbol=o[2]),void 0!==o[1]&&-1!==["start","end"].indexOf(o[3])&&(a.position=o[3])})))}break;case"label":case"source":case"updated":case"href":d[e[0]]=e[1]||null}}),f&&(l.time.length||delete l.time,l.geo.length||delete l.geo,l.metric.length||delete l.metric,d.role=l)}if(i=m.length,r=m[0].length,void 0!==h){for(;r--;)if(m[0][r]===h){c=r;break}if(null===c)return null}else c=r-1,h=m[0][c];if(","===p)for(r=1;r<i;r++)m[r][c]=Number(m[r][c].replace(",","."));else for(r=1;r<i;r++)m[r][c]=Number(m[r][c]);return a(m,{header:d,vlabel:h,slabel:b,type:"array",label:t.label||"",ovalue:t.ovalue||!1,ostatus:t.ostatus||!1,instance:t.instance||!1})},e.fromSDMX=function(e,n){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,o=l.dimensions,i={};Object.keys(n).forEach(function(e){const t=n[e].observations;Object.keys(t).forEach(function(l){i[e+":"+l]=t[l].concat(n[e].attributes)})}),t.observations=i,delete t.series,o.observation=o.series.concat(o.observation),delete o.series,l.attributes.observation=l.attributes.observation.concat(l.attributes.series),delete l.attributes.series}(e),void 0===n?n={ovalue:!1,ostatus:!1,instance:!1}:("boolean"!=typeof n.ovalue&&(n.ovalue=!1),"boolean"!=typeof n.ostatus&&(n.ostatus=!1),"boolean"!=typeof n.instance&&(n.instance=!1));const l=e.structure,o=e.dataSets[0].observations,i=l.attributes.observation,r=l.dimensions;if(!r.hasOwnProperty("observation"))return null;if(r.hasOwnProperty("series")&&Object.keys(r.series).length)return null;let a=1,s=[],u=function(){},c=i.findIndex(function(e){return"OBS_STATUS"===e.id});const d=[],f=[],h={},b={time:[],geo:[]},p=function(e,t){const n=e.size;for(let e=n.length-t.length;e--;)t.push(0);let l=0,o=1;for(let e=0,i=n.length;e<i;e++)o*=e>0?n[i-e]:1,l+=o*t[i-e-1];return l},m=function(e){if(h[e.id]={label:e.name},e.hasOwnProperty("role"))switch(e.role){case"REF_AREA":b.geo.push(e.id);break;case"TIME_PERIOD":b.time.push(e.id)}Object.defineProperty(h[e.id],"category",{value:{index:[],label:{}},writable:!0,enumerable:!0}),d.push(e.id),f.push(e.values.length),a*=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&&(i[c].values.length?s=i[c].values:c=-1),r.observation.forEach(m),r.hasOwnProperty("dataSet")&&r.dataSet.forEach(m);const y={version:"2.0",class:"dataset",updated:e.header.prepared||null,source:e.header.sender.name||null,label:l.name||null,id:d,size:f,dimension:h,value:n.ovalue?{}:new Array(a).fill(null)};g&&(y.link={alternate:[{type:"application/vnd.sdmx.data+json",href:g.href}]}),b.geo.length+b.time.length>0&&(b.time.length||delete b.time,b.geo.length||delete b.geo,y.role=b),-1!==c&&(y.status=n.ostatus?{}:new Array(a).fill(null),y.extension={status:{label:{}}},s.forEach(function(e){y.extension.status.label[e.id]=e.name}),u=n.ostatus?function(e,t){const n=o[e][c];null!==n&&(y.status[p(y,t)]=s[n].id)}:function(e,t){const n=o[e][c];y.status[p(y,t)]=null===n?null:s[n].id}),c++;for(let e in o){const t=e.split(":");n.ovalue&&null===o[e][0]||(y.value[p(y,t)]=o[e][0]),u(e,t)}return n.instance?t(y):y},e.fromTable=a,e.join=function(e,n){if(void 0===e||!Array.isArray(e))return null;const l=JSON.parse(JSON.stringify(e)),o=l[0];if(!o.hasOwnProperty("version")||!o.hasOwnProperty("class")||"dataset"!==o.class)return null;void 0===n&&(n={});const i=void 0===n.label?null:n.label,r=void 0===n.by?null:n.by;let s,u,c,d=[];if(null===r){for(let e=1,t=l.length;e<t;e++)d=d.concat(l[e].value);return o.value=d,null!==i&&(o.label=i),o}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};l.forEach(function(e,n){const l=t(e).Transform({status:!0}),o=e.dimension[r].category;0===n?(d=[l[0]],s=o.index,u=o.label,c=o.unit):(s=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}(s,o.index,Object.keys(s).length),u=f(u,o.label,n),c=f(c,o.unit,n)),d=d.concat(l.slice(1))});const h=a(d);return o.value=h.value,o.size=h.size,o.status=h.status||null,o.label=i||"",o.href=null,o.dimension[r].category.index=s||null,o.dimension[r].category.label=u||null,o.dimension[r].category.unit=c||null,o},e.tbrowser=function(e,t,r){function a(e){void 0!==t?t.innerHTML=s[e]:window.alert(s[e])}if(void 0===e)return void a("urierror");if(void 0===t)return void a("selerror");void 0===r&&(r={});const s=void 0===r.i18n||void 0===r.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"}:r.i18n.msgs,u=void 0===r.i18n||void 0===r.i18n.locale?"en-US":r.i18n.locale,c=r.dsid||0,d=r.status||!1,f=r.tblclass||"",h=r.nonconst||!1,b=n(e,c);if(!l(b))return void a("jsonerror");let p;h&&(p=function(e){let t=0;const n=e.size.slice(0),l=[];return n.forEach(function(n,o){const i=o-t,r=e.Dimension(i);1===n&&(delete e.__tree__.dimension[e.id[i]],e.size.splice(i,1),e.id.splice(i,1),e.length--,t++,l.push(r.label.capitalize()+": "+r.Category(0).label.capitalize()))}),l}(b)),1!==b.length?function e(t,n,l,a){"function"==typeof r.callback&&r.callback(l);let c="",f="",b="",m="",g="",y="",v=null!==n.label?'<span class="label">'+n.label.capitalize()+"</span>":"",O=n.source?s.source+": "+n.source:"";const w=l.rows,E=n.Dimension(w),S=E.id,x=l.cols,A=n.Dimension(x),D=A.id,j=n.role&&n.role.metric?n.role.metric[0]:null,P=null!==j?n.Dimension(j):null,C=function(e){return e.hasOwnProperty("unit")&&e.unit&&e.unit.hasOwnProperty("decimals")?e.unit.decimals:null},N=l.filters,k=JSON.parse(JSON.stringify(N)),z=[];h&&p.length&&(v='<span class="label">'+p.join(". ")+"</span>"),""!==O&&"."!==O.slice(-1)&&(O+="."),b+="<caption>"+v+"<form>";for(const e in N){const t=n.Dimension(e),l=t.label?t.label.capitalize():e.capitalize();t.length>1?g+="<p>"+i(n,e,[N[e],null])+" <strong>"+l+"</strong></p>":z.push({label:l,value:o(t,t.Category(0)),name:e,id:t.id[0]})}""!==g&&(g='<fieldset id="filters"><legend>'+s.filters+"</legend>"+g+"</fieldset>"),z.forEach(function(e){y+="<p>"+e.value+" <strong>"+e.label+'</strong></p><input type="hidden" name="'+e.name+'" value="'+e.id+'" />'}),""!==y&&(y='<fieldset id="constants"><legend>'+s.constants+"</legend>"+y+"</fieldset>"),b+=y+g+'<fieldset id="rowscols"><legend>'+s.rc+"</legend>"+i(n,"rows",[w,x])+" <a>↔</a> "+i(n,"cols",[x,w])+"</fieldset></form></caption>",m+="<tbody>";const R=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(S.forEach(function(e){k[w]=e;const t=n.Data(k),l=function(e,t){let n;const l=x!==j?null===P?null:C(P.Category(k[j])):C(A.Category(t));null!==e.value?(n=R(e.value,l),d&&null!==e.status&&(n+=" ("+e.status+")")):n=e.status||s.na,m+="<td>"+n+"</td>"};null!==t?(m+='<tr><th scope="row">'+o(E,E.Category(e))+"</th>",Array.isArray(t)?t.forEach(function(e,t){l(e,t)}):l(t,0),m+="</tr>"):m="ERROR"}),"ERROR"===m)return s.dataerror;m+="</tbody>",c+="<thead><tr><th></th>",D.forEach(function(e){c+='<th scope="col">'+o(A,A.Category(e))+"</th>"}),c+="</tr></thead>",""!==O&&(f='<tfoot><tr><td colspan="'+(D.length+1)+'">'+O+"</td></tr></tfoot>"),t.innerHTML='<table class="'+a+'">'+b+c+m+f+"</table>",[].slice.call(t.querySelectorAll("select")).forEach(function(l){l.addEventListener("change",function(l){e(t,n,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}(n,l.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)),a)},!1)}),t.querySelector("a").addEventListener("click",function(){l.cols=w,l.rows=x,e(t,n,l,a)},!1)}(t,b,function(e,t){if("object"==typeof t)return{rows:t.rows,cols:t.cols,filters:t.filters};const n={},l=[],o=e.id;let i,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:o[t],len:e.length})}),l.sort(n),i=l[0].id,r=l[1].id}else i=o[0],r=o[1];return e.Dimension(i).length<e.Dimension(r).length&&(i=r+(r=i,"")),o.forEach(function(t){t!==i&&t!==r&&(n[t]=e.Dimension(t).id[0])}),{rows:i,cols:r,filters:n}}(b,r.preset),f):a("dimerror")},e.toCSV=function(e,t){if(void 0===e)return null;void 0===t&&(t={});const o={content:!1,field:!1},i=!0===t.rich,r=t.na||"n/a",a=t.delimiter||",",u=t.separator||"|",c=";"===a?t.decimal||",":t.decimal||".",d=!0===t.array,f=n(e,t.dsid||0),h=d?function(e){O.push(e)}:function(e){O+=e+"\n"};let b=t.content||"label",p=t.field||"label",m=i?"value":t.vlabel||"Value",g=i?"status":t.slabel||"Status",y=!0===t.status,v=d?[]:"",O=d?[]:"",w=d?function(e){v.push(e)}:function(e){v+=e+"\n"};if(!l(f))return null;i?y=null!==f.status:("[id] label"===b&&(o.content=!0,b="id"),"[id] label"===p&&(o.field=!0,p="id"));const E=!i&&t.by&&-1!==f.id.indexOf(t.by)?t.by:null,S=E&&void 0!==t.drop&&Array.isArray(t.drop)?t.drop:null;if(E)return function(e,t){const n=[],l=t.by,o=t.idlabel,i=t.drop,r=t.na,a=t.delimiter,u=t.decimal,c=o.content?"id":t.content,d=o.field?"label":t.field,f=e.Transform({drop:i,content:c,by:l,type:"arrobj"}),h=Object.keys(f[0]),b=function(e,t,n,l,i){return e.map(e=>"label"===n?-1!==l.id.indexOf(e)?s((o.field?"["+e+"] ":"")+l.Dimension(e).label,t):s((o.field?"["+e+"] ":"")+l.Dimension(i).Category(e).label,t):s(e,t)).join(t)+"\n"}(h,a,d,e,l),p="."!==u?function(e){return null===e?s(r,a):String(e).replace(".",u)}:function(e){return null===e?s(r,a):e};return f.forEach(t=>{let l=[];h.forEach(n=>{const i=t[n],r="string"==typeof i?s(o.content?"["+i+"] "+e.Dimension(n).Category(i).label:i,a):p(i);l.push(r)}),n.push(l.join(a))}),b+n.join("\n")}(f,{drop:S,na:r,delimiter:a,decimal:c,content:b,field:p,by:E,idlabel:o});const x=f.Transform({vlabel:m,slabel:g,status:y,field:i||o.field?"id":p,content:i||o.content?"id":b,type:"array"}),A=x[0].indexOf("id"===p?"value":m),D=y?x[0].indexOf("id"===p?"status":g):-1;return x.forEach(function(e,t){e.forEach(function(n,l){const i=f.Dimension(l);t&&l===A?null===n?e[l]=s(r,a):"."!==c&&(e[l]=String(e[l]).replace(".",c)):t&&l===D&&null===n?e[l]="":(o.content&&t&&i?e[l]="["+e[l]+"] "+i.Category(e[l]).label:o.field&&0===t&&(e[l]=i?"["+e[l]+"] "+i.label:"value"===e[l]?m:g),e[l]=s(e[l],a))}),w(e.join(a))}),i&&(h("jsonstat"+a+c+a+u),["label","source","updated","href"].forEach(function(e){f[e]&&h(e+a+s(f[e],a))}),f.id.forEach(function(e,t){const n=[],l=f.Dimension(t),o=l.role;let i=!1,r="";r+="dimension"+a+s(e,a)+a+s(l.label,a)+a+l.length,"metric"===o&&l.__tree__.category.unit&&(i=!0),l.id.forEach(function(e,t){const o=[],c=l.Category(t);r+=a+s(e,a)+a+s(c.label,a),i&&(o.push(c.unit.hasOwnProperty("decimals")?c.unit.decimals:""),o.push(c.unit.label||""),c.unit.symbol&&(o.push(c.unit.symbol),o.push(c.unit.position)),n.push(s(o.join(u),a)))}),null!==o&&"classification"!==o&&(r+=a+l.role,i&&(r+=a+n.join(a))),h(r)}),v=d?O.concat(["data"],v):O+"data\n"+v),v},e.version="4.0.0",e}({},JSONstat);
|
package/import.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
// jsonstat-suite
|
|
2
|
-
function e(r,i){var l,a,s,o,u=function(e,r,i){var l,a=[];if(i&&!n(i.name)&&(i=null),"string"==typeof e&&(e=[e]),Array.isArray(e)||t(e)){if(e.length===r)return i&&-1===e.findIndex((function(e){return null==e}))?i.from(e):e;if(1===e.length){for(l=0;l<r;l++)a.push(e[0]);return a}}for(l=0;l<r;l++){var s=void 0===e[l]?null:e[l];a.push(s)}return a};if(this.length=0,this.id=[],null!=r)switch(this.class=r.class||"bundle",this.class){case"bundle":var c=[],f=0;if(this.error=null,this.length=0,null===r||"object"!=typeof r)return void(this.class=null);if(r.hasOwnProperty("error"))return void(this.error=r.error);if("dataset"===r.class||"collection"===r.class||"dimension"===r.class)return new e(r);for(a in r)f++,c.push(a);this.__tree__=r,this.length=f,this.id=c;break;case"dataset":r.hasOwnProperty("__tree__")?this.__tree__=l=r.__tree__:this.__tree__=l=r,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;var h,d=0,v=l.size||l.dimension&&l.dimension.size;if(this.size=v,this.value=l.hasOwnProperty("value")&&null!==l.value&&0!==l.value.length?l.value:{},Array.isArray(this.value)||t(this.value))d=this.value.length;else{var p=1;for(h=v.length;h--;)p*=v[h];d=p}if(this.value=u(this.value,d,i),this.status=l.hasOwnProperty("status")?u(l.status,d):null,l.hasOwnProperty("dimension")){var y=l.dimension,b=l.role||!l.version&&y.role||null,g=l.id||y.id,m=v.length,_=function(e){b.hasOwnProperty(e)||(b[e]=null)};if(!Array.isArray(g)||!Array.isArray(v)||g.length!=m)return;if(this.length=m,this.id=g,b&&(_("time"),_("geo"),_("metric"),_("classification")),b&&null===b.classification){var w=[],O=["time","geo","metric"],x=function(e,t){for(var n=t.length;n--;)if(e===t[n])return!0;return!1};for(h=0;h<3;h++){var A=b[O[h]];null!==A&&(w=w.concat(A))}for(b.classification=[],h=0;h<m;h++)x(g[h],w)||b.classification.push(g[h]);0===b.classification.length&&(b.classification=null)}this.role=b,this.n=d;for(var E=0,S=this.length;E<S;E++)if(y[g[E]].category.hasOwnProperty("index")){if(Array.isArray(y[g[E]].category.index)){var D={},j=y[g[E]].category.index;for(s=j.length,o=0;o<s;o++)D[j[o]]=o;y[g[E]].category.index=D}}else{var P=0;for(a in y[g[E]].category.index={},y[g[E]].category.label)y[g[E]].category.index[a]=P++}}else this.length=0;break;case"dimension":if(!r.hasOwnProperty("__tree__"))return new e({version:"2.0",class:"dataset",dimension:{d:r},id:["d"],size:[function(e){var t=void 0===e.index?e.label:e.index;return Array.isArray(t)?t.length:Object.keys(t).length}(r.category)],value:[null]}).Dimension(0);var k=[],z=(l=r.__tree__).category;if(!l.hasOwnProperty("category"))return;if(!z.hasOwnProperty("label"))for(a in z.label={},z.index)z.label[a]=a;for(a in z.index)k[z.index[a]]=a;this.__tree__=l,this.label=l.label||null,this.note=l.note||null,this.link=l.link||null,this.href=l.href||null,this.id=k,this.length=k.length,this.role=r.role,this.hierarchy=z.hasOwnProperty("child"),this.extension=l.extension||null;break;case"category":var C=r.child;this.id=C,this.length=null===C?0:C.length,this.index=r.index,this.label=r.label,this.note=r.note||null,this.unit=r.unit,this.coordinates=r.coord;break;case"collection":if(this.length=0,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,null!==this.link&&r.link.item){var N=r.link.item;if(this.length=Array.isArray(N)?N.length:0,this.length)for(o=0;o<this.length;o++)this.id[o]=N[o].href}}}function t(e){return"[object ArrayBuffer]"===Object.prototype.toString.call(e.buffer)}function n(e){return-1!==["Int8Array","Uint8Array","Uint8ClampedArray","Int16Array","Uint16Array","Int32Array","Uint32Array","Float32Array","Float64Array","BigInt64Array","BigUint64Array"].indexOf(e)}function r(e){if(!e.ok)throw new Error(e.status+" "+e.statusText);return e.json()}function i(t,n,i){var l="object"==typeof n?n:null;return"function"!=typeof i&&(i=null),i||"function"!=typeof n||(i=n),"object"==typeof t?new e(t,i):"version"===t?"1.4.2":fetch?fetch(t,l).then(r).then((function(t){return new e(t,i)})):void 0}function l(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:"dataset"===e.class?e:e.Dataset(t))}function a(e){if(null===e||0===e.length||"dataset"!==e.class)return!1;for(var t=e.length,n=1;t--;)n*=e.Dimension(t).length;return n===e.n}function s(e,t,n){var r,i,l,a,s;return(t.label||n).capitalize()+(i=t,s="",(r=e)&&"metric"===r.role&&i.unit&&(l=i.unit.hasOwnProperty("label")?i.unit.label:"")+(a=i.unit.hasOwnProperty("symbol")?i.unit.symbol:"")!==""&&(s=" ("+(s=""===a?l:""===l?a:"start"===i.unit.position?a+l:l+" "+a)+")"),s)}function o(e,t,n){var r,i='<select name="'+t+'">',l=[];if(null!==n[1]){if(r=e.id,l=e.Dimension(),2===r.length)return(e.Dimension(n[0]).label||n[0]).capitalize()}else{var a=e.Dimension(t);if(r=a.id,l=a.Category(),1===r.length)return}return r.forEach((function(e,t){var r=e!==n[0]?"":'selected="selected" ';null!==n[1]&&e===n[1]||(i+="<option "+r+'value="'+e+'">'+s(a,l[t],e)+"</option>")})),i+="</select>"}function u(e,t,n){function r(e){void 0!==t?t.innerHTML=i[e]:window.alert(i[e])}if(void 0!==e)if(void 0!==t){void 0===n&&(n={});var 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,h=n.tblclass||"",d=n.nonconst||!1,v=l(e,c);if(a(v)){if(d)var p=function(e){var t=0,n=e.size.slice(0),r=[];return n.forEach((function(n,i){var l=i-t,a=e.Dimension(l);1===n&&(delete e.__tree__.dimension[e.id[l]],e.size.splice(l,1),e.id.splice(l,1),e.length--,t++,r.push(a.label.capitalize()+": "+a.Category(0).label.capitalize()))})),r}(v);1!==v.length?function e(t,r,l,a){"function"==typeof n.callback&&n.callback(l);var c="",h="",v="",y="",b=l.rows,g=r.Dimension(b),m=g.id,_=l.cols,w=r.Dimension(_),O=w.id,x=r.role&&r.role.metric?r.role.metric[0]:null,A=null!==x?r.Dimension(x):null,E=function(e){return e.hasOwnProperty("unit")&&e.unit&&e.unit.hasOwnProperty("decimals")?e.unit.decimals:null},S=l.filters,D=JSON.parse(JSON.stringify(S)),j=[],P="",k="",z=r.source?i.source+": "+r.source:"",C=null!==r.label?'<span class="label">'+r.label.capitalize()+"</span>":"";for(var N in d&&p.length&&(C='<span class="label">'+p.join(". ")+"</span>"),""!==z&&"."!==z.slice(-1)&&(z+="."),v+="<caption>"+C+"<form>",S){var T=r.Dimension(N),R=T.label?T.label.capitalize():N.capitalize();T.length>1?P+="<p>"+o(r,N,[S[N],null])+" <strong>"+R+"</strong></p>":j.push({label:R,value:s(T,T.Category(0)),name:N,id:T.id[0]})}""!==P&&(P='<fieldset id="filters"><legend>'+i.filters+"</legend>"+P+"</fieldset>"),j.forEach((function(e){k+="<p>"+e.value+" <strong>"+e.label+'</strong></p><input type="hidden" name="'+e.name+'" value="'+e.id+'" />'})),""!==k&&(k='<fieldset id="constants"><legend>'+i.constants+"</legend>"+k+"</fieldset>"),v+=k+P+'<fieldset id="rowscols"><legend>'+i.rc+"</legend>"+o(r,"rows",[b,_])+" <a>↔</a> "+o(r,"cols",[_,b])+"</fieldset></form></caption>",y+="<tbody>";var F=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(m.forEach((function(e){D[b]=e;var t=r.Data(D),n=function(e,t){var n,r=_!==x?null===A?null:E(A.Category(D[x])):E(w.Category(t));null!==e.value?(n=F(e.value,r),f&&null!==e.status&&(n+=" ("+e.status+")")):n=e.status||i.na,y+="<td>"+n+"</td>"};null!==t?(y+='<tr><th scope="row">'+s(g,g.Category(e))+"</th>",Array.isArray(t)?t.forEach((function(e,t){n(e,t)})):n(t,0),y+="</tr>"):y="ERROR"})),"ERROR"===y)return i.dataerror;y+="</tbody>",c+="<thead><tr><th></th>",O.forEach((function(e){c+='<th scope="col">'+s(w,w.Category(e))+"</th>"})),c+="</tr></thead>",""!==z&&(h='<tfoot><tr><td colspan="'+(O.length+1)+'">'+z+"</td></tr></tfoot>"),t.innerHTML='<table class="'+a+'">'+v+c+y+h+"</table>",[].slice.call(t.querySelectorAll("select")).forEach((function(n){n.addEventListener("change",(function(n){var i,l;e(t,r,function(e,t,n){var r={filters:{}};return n.forEach((function(e){"rows"===e.name||"cols"===e.name?r[e.name]=e.value:r.filters[e.name]=e.value})),"rowscols"===t&&e.id.forEach((function(t,n){t!==r.rows&&t!==r.cols?void 0===r.filters[t]&&(r.filters[t]=e.Dimension(n).id[0]):delete r.filters[t]})),r}(r,n.target.parentElement.getAttribute("id"),(i=t,l=[],[].slice.call(i.querySelectorAll("select, input")).forEach((function(e){l.push({name:e.name,value:e.value})})),l)),a)}),!1)})),t.querySelector("a").addEventListener("click",(function(){l.cols=b,l.rows=_,e(t,r,l,a)}),!1)}(t,v,function(e,t){if("object"==typeof t)return{rows:t.rows,cols:t.cols,filters:t.filters};var n,r,i={},l=[],a=e.id;if(t){var s="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:a[t],len:e.length})})),l.sort(s),n=l[0].id,r=l[1].id}else n=a[0],r=a[1];return e.Dimension(n).length<e.Dimension(r).length&&(n=r+(r=n,"")),a.forEach((function(t){t!==n&&t!==r&&(i[t]=e.Dimension(t).id[0])})),{rows:n,cols:r,filters:i}}(v,n.preset),h):r("dimerror")}else r("jsonerror")}else r("selerror");else r("urierror")}function c(e,t){if(void 0===e)return null;void 0===t&&(t={});var n="",r="",i="",s=0,o=t.na||"n/a",u=t.dsid||0,c=t.vlabel||null,f=t.slabel||null,h=t.counter||!1,d=t.tblclass||"",v=t.numclass||"",p=t.valclass||"",y=t.status||!1,b=t.locale||"en-US",g=t.source||"Source",m=l(e,u),_=m.role&&m.role.metric?m.id.indexOf(m.role.metric[0]):null,w=m.Dimension({role:"metric"}),O={},x=Number.toLocaleString&&"none"!==b?function(e,t){return null===t?e.toLocaleString(b):e.toLocaleString(b,{minimumFractionDigits:t,maximumFractionDigits:t})}:function(e,t){return null===t?e:e.toFixed(t)},A=function(e,t){var i=null;e.forEach((function(e,l){var a=D===l?' class="'+v+" "+p+'"':"",s=o;t?(_===l&&(i=O[e]),null!==e&&(s=D===l?x(e,i):e),r+="<td"+a+">"+s+"</td>"):n+="<th"+a+">"+e+"</th>"}))},E=h?function(e,t){t?r+='<tr><td class="'+v+'">'+t+"</td>":n+='<th class="'+v+'">#</th>',A(e,t),r+="</tr>"}:function(e,t){r+="<tr>",A(e,t),r+="</tr>"};if(!a(m))return null;w&&w[0].Category().forEach((function(e){var t=e.unit&&e.unit.hasOwnProperty("decimals")?e.unit.decimals:null;O[e.label]=t}));var S=m.toTable({status:y,vlabel:c,slabel:f}),D=S[0].length-1;return S.forEach((function(e,t){E(e,t)})),m.source&&(s=m.length+1,h&&s++,y&&s++,"."!==(g+=": "+m.source).slice(-1)&&(g+="."),i='<tfoot><td colspan="'+s+'">'+g+"</td></tfoot>"),'<table class="'+d+'"><caption>'+(t.caption||m.label||"")+"</caption><thead><tr>"+n+"</tr></thead><tbody>"+r+"</tbody>"+i+"</table>"}function f(e,t){var n={};return Array.isArray(e[t])?(e[t].forEach((function(e,t){null!==e&&(n[String(t)]=e)})),n):e[t]}function h(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);var n=t.vlabel||"Value",r=t.slabel||"Status",l=t.type||"array",a=t.label||"",s=t.header||null,o=[],u=[],c=[],h=[],d={},v={},p=function(e,t){for(var n=1,r=0,i=0;i<O;i++)r+=(n*=i>0?t[O-i]:1)*e[O-i-1];return r},y=function(){var t=e[x][n];c[p(A,u)]=isNaN(t)?null:t};switch(l){case"array":e=function(e){for(var t=e[0],n=e.slice(1),r=[],i=0,l=n.length;i<l;i++){for(var a=0,s=t.length,o={};a<s;a++)o[t[a]]=n[i][a];r.push(o)}return r}(e);break;case"object":e=function(e){for(var t=e.cols.map((function(e){return e.id})),n=e.rows,r=[],i=0,l=n.length;i<l;i++){for(var a=0,s=t.length,o={};a<s;a++)o[t[a]]=n[i].c[a].v;r.push(o)}return r}(e)}var b,g=e.length;for(var m in t.hasOwnProperty("drop")&&Array.isArray(t.drop)&&t.drop.length&&e.forEach((function(e){t.drop.forEach((function(t){delete e[t]}))})),e[0])if(m!==n)if(m!==r){if(o.push(m),s)b=s.dimension[m],d[m]=b.category.index;else{d[m]=[];for(var _=0;_<g;_++){var w=e[_][m];-1===d[m].indexOf(w)&&d[m].push(w)}}u.push(d[m].length),v[m]={label:s?b.label:m,category:{index:d[m]}},s&&(v[m].category.label=b.category.label,b.category.unit&&(v[m].category.unit=b.category.unit))}else y=function(){var t=e[x][n],i=e[x][r];c[p(A,u)]=isNaN(t)?null:t,h[p(A,u)]=""===i?null:i};for(var O=o.length,x=0;x<g;x++){for(var A=[],E=0;E<O;E++){var S=o[E];A.push(d[S].indexOf(e[x][S]))}y()}var D={version:"2.0",class:"dataset",value:c,dimension:v,id:o,size:u};return a&&(D.label=a),h.length&&(D.status=h),s&&(s.label&&(D.label=s.label),s.source&&(D.source=s.source),s.updated&&(D.updated=s.updated),s.href&&(D.href=s.href),s.role&&(D.role=s.role)),t.ovalue&&(D.value=f(D,"value")),t.ostatus&&D.hasOwnProperty("status")&&(D.status=f(D,"status")),t.instance?i(D):D}function d(e,t){if(void 0===e)return null;void 0===t&&(t={});var n,r,i,l=[],a=null,s=null,o=!1,u={time:[],geo:[],metric:[]},c="jsonstat"===e.substring(0,8),f=c?"value":t.vlabel,d=c?"status":t.slabel,v=c?e.substring(8,9):t.delimiter||",",p=";"===v?t.decimal||",":t.decimal||".",y=function(e,t){t=t||",";for(var n,r,i=new RegExp("(\\"+t+'|\\r?\\n|\\r|^)(?:"([^"]*(?:""[^"]*)*)"|([^"\\'+t+"\\r\\n]*))","gi"),l=[[]],a=null;a=i.exec(e);)(r=a[1]).length&&r!=t&&l.push([]),n=a[2]?a[2].replace(new RegExp('""',"g"),'"'):a[3],l[l.length-1].push(n);return l}(e.trim(),v);if(c){for(p=y[0][1],i=y[0][2],y.shift();"data"!==y[0][0];)l.push(y.shift());y.shift(),s={dimension:{}},l.forEach((function(e){var t,n,r,l,a,c,f,h;switch(e[0]){case"dimension":if(s.dimension[e[1]]={},(l=s.dimension[e[1]]).label=e[2],l.category={},(a=l.category).index=[],n={},r=2*e[3]+3,e.length>=r){for(t=4;t<r;t++)f=e[t],h=e[++t],Object.defineProperty(n,f,{value:h,writable:!0,configurable:!0,enumerable:!0}),a.label=n,a.index.push(f);"string"==typeof e[t]&&-1!==["time","geo","metric"].indexOf(e[t])&&(u[e[t]].push(e[1]),o=!0,"metric"===e[t]&&"string"==typeof e[++t]&&(a.unit={},a.index.forEach((function(n,r){var l=e[t+r].split(i);a.unit[n]={},c=a.unit[n],void 0!==l[0]&&""!==l[0]&&(c.decimals=1*l[0]),void 0!==l[1]&&""!==l[1]&&(c.label=l[1]),void 0!==l[2]&&""!==l[2]&&(c.symbol=l[2]),void 0!==l[1]&&-1!==["start","end"].indexOf(l[3])&&(c.position=l[3])}))))}break;case"label":case"source":case"updated":case"href":s[e[0]]=e[1]||null}})),o&&(u.time.length||delete u.time,u.geo.length||delete u.geo,u.metric.length||delete u.metric,s.role=u)}if(n=y.length,r=y[0].length,void 0!==f){for(;r--;)if(y[0][r]===f){a=r;break}if(null===a)return null}else a=r-1,f=y[0][a];if(","===p)for(r=1;r<n;r++)y[r][a]=Number(y[r][a].replace(",","."));else for(r=1;r<n;r++)y[r][a]=Number(y[r][a]);return h(y,{header:s,vlabel:f,slabel:d,type:"array",label:t.label||"",ovalue:t.ovalue||!1,ostatus:t.ostatus||!1,instance:t.instance||!1})}function v(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){var t=e.dataSets[0],n=t.series,r=e.structure,i=r.dimensions,l={};Object.keys(n).forEach((function(e){var t=n[e].observations;Object.keys(t).forEach((function(r){l[e+":"+r]=t[r].concat(n[e].attributes)}))})),t.observations=l,delete t.series,i.observation=i.series.concat(i.observation),delete i.series,r.attributes.observation=r.attributes.observation.concat(r.attributes.series),delete r.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));var n=e.structure,r=e.dataSets[0].observations,l=n.attributes.observation,a=n.dimensions;if(!a.hasOwnProperty("observation"))return null;if(a.hasOwnProperty("series")&&Object.keys(a.series).length)return null;var s=1,o=[],u=[],c={},f=[],h={time:[],geo:[]},d=function(){},v=function(e,t){for(var n=e.size,r=n.length-t.length;r--;)t.push(0);for(var i=0,l=n.length,a=0,s=1;i<l;i++)a+=(s*=i>0?n[l-i]:1)*t[l-i-1];return a},p=function(e){if(c[e.id]={label:e.name},e.hasOwnProperty("role"))switch(e.role){case"REF_AREA":h.geo.push(e.id);break;case"TIME_PERIOD":h.time.push(e.id)}Object.defineProperty(c[e.id],"category",{value:{index:[],label:{}},writable:!0,enumerable:!0}),o.push(e.id),u.push(e.values.length),s*=e.values.length;var t=c[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})}))},y=e.header.links?e.header.links.find((function(e){return"request"===e.rel})):null,b=l.findIndex((function(e){return"OBS_STATUS"===e.id}));-1!==b&&(l[b].values.length?f=l[b].values:b=-1),a.observation.forEach(p),a.hasOwnProperty("dataSet")&&a.dataSet.forEach(p);var g={version:"2.0",class:"dataset",updated:e.header.prepared||null,source:e.header.sender.name||null,label:n.name||null,id:o,size:u,dimension:c,value:t.ovalue?{}:new Array(s).fill(null)};for(var m in y&&(g.link={alternate:[{type:"application/vnd.sdmx.data+json",href:y.href}]}),h.geo.length+h.time.length>0&&(h.time.length||delete h.time,h.geo.length||delete h.geo,g.role=h),-1!==b&&(g.status=t.ostatus?{}:new Array(s).fill(null),g.extension={status:{label:{}}},f.forEach((function(e){g.extension.status.label[e.id]=e.name})),d=t.ostatus?function(){var e=r[m][b];null!==e&&(g.status[v(g,_)]=f[e].id)}:function(){var e=r[m][b];g.status[v(g,_)]=null===e?null:f[e].id}),b++,r){var _=m.split(":");t.ovalue&&null===r[m][0]||(g.value[v(g,_)]=r[m][0]),d()}return t.instance?i(g):g}function p(e,t){if(void 0===e||!Array.isArray(e))return null;var n=JSON.parse(JSON.stringify(e)),r=n[0];if(!r.hasOwnProperty("version")||!r.hasOwnProperty("class")||"dataset"!==r.class)return null;void 0===t&&(t={});var l=void 0===t.label?null:t.label,a=void 0===t.by?null:t.by,s=[];if(null===a){for(var o=1,u=n.length;o<u;o++)s=s.concat(n[o].value);return r.value=s,null!==l&&(r.label=l),r}var c,f,d,v=function(e,t,n){if(Array.isArray(e))e=e.concat(t);else for(var r in t)e[r]=0===t[r]?n:t[r];return e};n.forEach((function(e,t){var n=i(e).toTable({status:!0}),r=e.dimension[a].category;0===t?(s=[n[0]],c=r.index,f=r.label,d=r.unit):(c=function(e,t,n){if(Array.isArray(e))e=e.concat(t);else for(var r in t)e[r]=t[r]+n;return e}(c,r.index,Object.keys(c).length),f=v(f,r.label,t),d=v(d,r.unit,t)),s=s.concat(n.slice(1))}));var p=h(s);return r.value=p.value,r.size=p.size,r.status=p.status||null,r.label=l||"",r.href=null,r.dimension[a].category.index=c||null,r.dimension[a].category.label=f||null,r.dimension[a].category.unit=d||null,r}function y(e,t){return null==e?"":-1!==e.indexOf(t)?'"'+e+'"':e}function b(e,t){if(void 0===e)return null;void 0===t&&(t={});var n={content:!1,field:!1},r=!0===t.rich,i=t.content||"label",s=t.field||"label",o=r?"value":t.vlabel||"Value",u=r?"status":t.slabel||"Status",c=!0===t.status,f=t.na||"n/a",h=t.delimiter||",",d=t.separator||"|",v=";"===h?t.decimal||",":t.decimal||".",p=!0===t.array,b=l(e,t.dsid||0),g=p?[]:"",m=p?[]:"",_=p?function(e){g.push(e)}:function(e){g+=e+"\n"},w=p?function(e){m.push(e)}:function(e){m+=e+"\n"};if(!a(b))return null;r?c=null!==b.status:("[id] label"===i&&(n.content=!0,i="id"),"[id] label"===s&&(n.field=!0,s="id"));var O=b.toTable({vlabel:o,slabel:u,status:c,field:r||n.field?"id":s,content:r||n.content?"id":i,type:"array"}),x=O[0].indexOf("id"===s?"value":o),A=c?O[0].indexOf("id"===s?"status":u):-1;return O.forEach((function(e,t){e.forEach((function(r,i){var l=b.Dimension(i);t&&i===x?null===r?e[i]=y(f,h):"."!==v&&(e[i]=String(e[i]).replace(".",v)):t&&i===A&&null===r?e[i]="":(n.content&&t&&l&&(e[i]="["+e[i]+"] "+l.Category(e[i]).label),n.field&&0===t&&(e[i]=l?"["+e[i]+"] "+l.label:"value"===e[i]?o:u),e[i]=y(e[i],h))})),_(e.join(h))})),r&&(w("jsonstat"+h+v+h+d),["label","source","updated","href"].forEach((function(e){b[e]&&w(e+h+y(b[e],h))})),b.id.forEach((function(e,t){var n=[],r=b.Dimension(t),i=r.role,l=!1,a="";a+="dimension"+h+y(e,h)+h+y(r.label,h)+h+r.length,"metric"===i&&r.__tree__.category.unit&&(l=!0),r.id.forEach((function(e,t){var i=[],s=r.Category(t);a+=h+y(e,h)+h+y(s.label,h),l&&(i.push(s.unit.hasOwnProperty("decimals")?s.unit.decimals:""),i.push(s.unit.label||""),s.unit.symbol&&(i.push(s.unit.symbol),i.push(s.unit.position)),n.push(y(i.join(d),h)))})),null!==i&&"classification"!==i&&(a+=h+r.role,l&&(a+=h+n.join(h))),w(a)})),g=p?m.concat(["data"],g):m+"data\n"+g),g}e.prototype.Item=function(e){if(null===this||"collection"!==this.class||!this.length)return null;if("number"==typeof e)return e>this.length||e<0?null:this.link.item[e];var t,n=[];if("object"==typeof e){if(!e.class&&!e.follow)return null;e.class&&(t="dataset"===e.class&&"boolean"==typeof e.embedded?!0===e.embedded?function(e,t,r){var i=e.link.item[t];r.class===i.class&&i.id&&i.size&&i.dimension&&n.push(i)}:function(e,t,r){var i=e.link.item[t];r.class!==i.class||i.id&&i.size&&i.dimension||n.push(i)}:function(e,t,r){r.class===e.link.item[t].class&&n.push(e.link.item[t])})}else t=function(e,t){n.push(e.link.item[t])};for(var r=0;r<this.length;r++)t(this,r,e);return n},e.prototype.Dataset=function(t){if(null===this)return null;if("dataset"===this.class)return void 0!==t?this:[this];var n,r=[],i=0;if("collection"===this.class){var l=this.Item({class:"dataset",embedded:!0});if(void 0===t){for(n=l.length;i<n;i++)r.push(new e(l[i]));return r}if("number"==typeof t&&t>=0&&t<l.length)return new e(l[t]);if("string"==typeof t)for(n=l.length;i<n;i++)if(l[i].href===t)return new e(l[i]);return null}if("bundle"!==this.class)return null;if(void 0===t){for(n=this.id.length;i<n;i++)r.push(this.Dataset(this.id[i]));return r}if("number"==typeof t){var a=this.id[t];return void 0!==a?this.Dataset(a):null}var s=this.__tree__[t];return void 0===s?null:new e({class:"dataset",__tree__:s})},e.prototype.Dimension=function(t,n){n="boolean"!=typeof n||n;var r,i=[],l=this.id.length,a=function(e,t){if(null!==e)for(var n in e)for(var r=null!==e[n]?e[n].length:0;r--;)if(e[n][r]===t)return n;return null};if(null===this||"dataset"!==this.class)return null;if(void 0===t){for(r=0;r<l;r++)i.push(this.Dimension(this.id[r]));return i}if("number"==typeof t){var s=this.id[t];return void 0!==s?this.Dimension(s,n):null}var o=this.role;if("object"==typeof t){if(t.hasOwnProperty("role")){for(r=0;r<l;r++){var u=this.id[r];a(o,u)===t.role&&i.push(this.Dimension(u,n))}return void 0===i[0]?null:i}return null}var c=this.__tree__.dimension;if(void 0===c)return null;var f=c[t];return void 0===f?null:n?new e({class:"dimension",__tree__:f,role:a(o,t)}):function(e,t){var n=[];for(var r in e)n[e[r]]=t[r];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=[],r=0,i=this.id.length;r<i;r++)n.push(this.Category(this.id[r]));return n}if("number"==typeof t){var l=this.id[t];return void 0!==l?this.Category(l):null}var a=this.__tree__.category;if(void 0===a)return null;var s=a.index[t];if(void 0===s)return null;var o=a.unit&&a.unit[t]||null,u=a.coordinates&&a.coordinates[t]||null,c=a.child&&a.child[t]||null,f=a.note&&a.note[t]||null;return new e({class:"category",index:s,label:a.label[t],note:f,child:c,unit:o,coord:u})},e.prototype.Dice=function(n,r,i){var l,a,s,o,u=function(e,t){return e.hasOwnProperty(t)&&!!e[t]};if(null===this||"dataset"!==this.class)return null;if("object"!=typeof n)return this;"object"!=typeof r?("boolean"==typeof r&&!0===r&&(l=!0),"boolean"==typeof i&&!0===i||(i=!1)):(l=u(r,"clone"),i=u(r,"drop"),a=u(r,"stringify"),s=u(r,"ovalue"),o=u(r,"ostatus"));var c,f=this.value,h=l?new e(JSON.parse(JSON.stringify(this))):this,d=h.status,v=[],p=[],y=function(e,t){var n,r,i,l=(n=e,r=t,i={},Array.isArray(n[r])?(n[r].forEach((function(e,t){null!==e&&(i[String(t)]=e)})),i):n[r]);delete e[t],e[t]=l};Array.isArray(n)&&(n=function(e){var t={};return e.forEach((function(e){t[e[0]]=e[1]})),t}(n)),null===n&&(n={});var b=Object.keys(n);return b.length>0&&(b.forEach((function(e){var t=n[e];Array.isArray(t)||(n[e]=[t]),0===n[e].length&&delete n[e]})),i&&(n=function(e){var t={};return Object.keys(e).forEach((function(n){t[n]=h.Dimension(n).id.filter((function(t){return-1===e[n].indexOf(t)}))})),t}(n)),h.toTable({type:"arrobj",content:"id",status:!0}).forEach((function(e,t){var r=[];b.forEach((function(t){var i=n[t],l=[];i.forEach((function(n){l.push(e[t]===n)})),r.push(-1!==l.indexOf(!0))})),-1===r.indexOf(!1)&&(v.push(e.value),p.push(e.status))})),b.forEach((function(e){var t=h.Dimension(e).id,r=0,i={};h.size[h.id.indexOf(e)]=n[e].length,t.forEach((function(t){-1!==n[e].indexOf(t)&&(i[t]=r,r++)})),h.__tree__.dimension[e].category.index=i})),h.n=v.length,h.value=h.__tree__.value=t(f)?function(e,t){return t.from(e)}(v,f.constructor):v,h.status=h.__tree__.status=null!==d?p:null),a?((c=h.__tree__).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((function(e){null===c.role[e]&&delete c.role[e]}))),s&&y(c,"value"),o&&c.hasOwnProperty("status")&&y(c,"status"),JSON.stringify(c)):h},e.prototype.Slice=function(e){if(null===this||"dataset"!==this.class)return null;if(void 0===e)return this;if(!Array.isArray(e)){var t,n=[];for(t in e)n.push([t,e[t]]);e=n}return this.Dice(e.map((function(e){return[e[0],[e[1]]]})))},e.prototype.Data=function(e,t){var n,r,i=[],l=function(e){for(var t in e)if(e.hasOwnProperty(t))return t};if(null===this||"dataset"!==this.class)return null;if(void 0===e){for(r=this.value.length,n=0;n<r;n++)i.push(this.Data(n));return i}if("boolean"!=typeof t&&(t=!0),"number"==typeof e){var a=this.value[e];return void 0===a?null:t?{value:a,status:this.status?this.status[e]:null}:a}var s="object",o=this.__tree__,u=o.size||o.dimension&&o.dimension.size,c=u.length;if(Array.isArray(e)){if(!Array.isArray(e[0])){if(this.length!==e.length)return null;var f=1,h=0,d=[],v=[];for(n=0;n<c;n++)if(void 0!==e[n]){if("number"!=typeof e[n]||e[n]>=u[n])return null;h+=(f*=n>0?u[c-n]:1)*e[c-n-1]}else d.push(n),v.push(u[n]);if(d.length>1)return null;if(1===d.length){for(var p=0,y=v[0];p<y;p++){var b=[];for(n=0;n<c;n++)n!==d[0]?b.push(e[n]):b.push(p);i.push(this.Data(b,t))}return i}return t?{value:this.value[h],status:this.status?this.status[h]:null}:this.value[h]}s="array"}var g=function(e,t,n){var r,i=[],a={},s=e.dimension,o=e.id||s.id,u=e.size||s&&s.size;if("array"===n){for(r=t.length;r--;)a[t[r][0]]=t[r][1];t=a}for(var c=0,f=o.length;c<f;c++){var h=o[c],d=t[h];i.push("string"==typeof d?d:1===u[c]?l(s[h].category.index):null)}return i}(o,e,s),m=[],_=o.dimension,w=o.id||_.id;for(n=0,r=g.length;n<r;n++)m.push(_[w[n]].category.index[g[n]]);return this.Data(m,t)},e.prototype.toTable=function(e,t){if(null===this||"dataset"!==this.class)return null;1==arguments.length&&"function"==typeof e&&(t=e,e=null),"arrobj"!==(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}).type&&"objarr"!==e.type||void 0!==e.field||(e.field="id");var r,i,l,a,s,o,u,c,f,h="id"===e.field,d=function(e){return(h?"value":e)||"Value"},v=function(e){return(h?"status":e)||"Status"},p=this.__tree__,y=!0===e.status;if("function"==typeof t){r=this.toTable(e);var b=[],g="array"!==e.type?0:1;for(s=(U="object"!==e.type?r.slice(g):r.rows.slice(0)).length,i=0;i<s;i++){var m=t.call(this,U[i],i);void 0!==m&&b.push(m)}return"object"===e.type?{cols:r.cols,rows:b}:("array"===e.type&&b.unshift(r[0]),b)}if("arrobj"===e.type||"objarr"===e.type){var _=[],w=p.role&&p.role.metric,O=function(){},x={},A=this,E=A.id,S=e.by&&-1!==E.indexOf(e.by)?e.by:null,D=!0===e.meta,j=void 0!==e.drop&&Array.isArray(e.drop)?e.drop:[],P=!0===e.comma,k=!0===e.bylabel,z=A.value.constructor,C=function(t){var r,i={},l=d(e.vlabel);return"objarr"===e.type&&(r=null===S&&n(z.name)?function(e){i[e]=e===l?z.from(t,(function(t){return t[e]})):t.map((function(t){return t[e]}))}:function(e){i[e]=t.map((function(t){return t[e]}))},Object.keys(t[0]).forEach(r),t=i),D?(i={},E.forEach((function(e){var t=A.Dimension(e);i[e]={label:t.label,role:t.role,categories:{id:t.id,label:A.Dimension(e,!1)}}})),{meta:{label:A.label,source:A.source,updated:A.updated,id:E,status:y,unit:e.unit,by:S,bylabel:k,drop:null!==S&&j.length>0?j:null,prefix:null!==S?B||"":null,comma:P,dimensions:i},data:t}):t};S&&(e.field="id");var N=(r=this.toTable({field:e.field,vlabel:e.vlabel,slabel:e.slabel,content:e.content,status:y})).shift();if(null===S&&e.unit&&w){if("id"!==e.content)for(var T=w.length;T--;){var R=this.Dimension(w[T]);x[w[T]]={};for(var F=R.length;F--;)x[w[T]][R.Category(F).label]=R.id[F]}O=function(t,n){if(-1!==w.indexOf(t)){var r=p.dimension[t].category;r.unit?I.unit=r.unit["id"!==e.content?x[t][n]:n]:I.unit=null}},e.unit=!0}else e.unit=!1;for(s=r.length,i=0;i<s;i++){var I={};for(l=r[i].length;l--;)I[N[l]]=r[i][l],O(N[l],r[i][l]);_.push(I)}if(P&&_.forEach((function(e){null!==e.value&&(e.value=(""+e.value).replace(".",","))})),null!==S){var J,L={},U=[],q={},B=void 0!==e.prefix?e.prefix:"";j.forEach((function(e,t){(!A.Dimension(e)||A.Dimension(e).length>1)&&(j[t]="")}));var V=E.filter((function(e){return e!==S&&-1===j.indexOf(e)})),M=A.Dimension(S);for(var H in"id"!==e.content?k?J=function(e,t,n){e[t][B+n[S]]=n.value}:(M.Category().forEach((function(e,t){q[e.label]=M.id[t]})),J=function(e,t,n){e[t][B+q[n[S]]]=n.value}):J=function(e,t,n){e[t][B+n[S]]=n.value},_.forEach((function(e){var t=function(e,t){var n=[];return t.forEach((function(t){n.push(e[t])})),n.join("\t")}(e,V);void 0===L[t]&&(L[t]=function(e,t){var n={};return t.forEach((function(t){n[t]=e[t]})),n}(e,V)),J(L,t,e,S)})),L)U.push(L[H]);return y=!1,C(U)}return C(_)}if("object"===e.type){var G="number"==typeof this.value[0]||null===this.value[0]?"number":"string";o=function(e,t){var n=h&&e||t||e;re.push({id:e,label:n,type:"string"})},u=function(e,t,n){var r=d(e),i=v(t);n&&re.push({id:"status",label:i,type:"string"}),re.push({id:"value",label:r,type:G})},c=function(e){pe.push({v:e})},f=function(e){pe.push({v:e}),ie.push({c:pe})}}else o=function(e,t){var n=h&&e||t||e;re.push(n)},u=function(e,t,n){var r=d(e),i=v(t);n&&re.push(i),re.push(r),ne.push(re)},c=function(e){pe.push(e)},f=function(e){pe.push(e),ne.push(pe)};var K=p.dimension,Q=p.id||K.id,W=p.size||K.size,X=Q.length;if(X!=W.length)return!1;var Y=[],Z=1,$=(T=1,[]),ee=[],te=[],ne=[],re=[],ie=[];for(i=0;i<X;i++){var le=Q[i];o(le,K[le].label),Z*=W[i],T*=W[i];var ae=[];for(l=0;l<W[i];l++)for(var se in K[Q[i]].category.index)if(K[Q[i]].category.index[se]===l){var oe="id"!==e.content&&K[Q[i]].category.label?K[Q[i]].category.label[se]:se;ae.push(oe)}Y.push(ae),$.push(T)}for(u(e.vlabel,e.slabel,y),s=Y.length,i=0;i<s;i++){for(var ue=[],ce=0,fe=Y[i].length;ce<fe;ce++)for(var he=0;he<Z/$[i];he++)ue.push(Y[i][ce]);ee.push(ue)}for(s=ee.length,i=0;i<s;i++){var de=[],ve=0;for(a=0;a<Z;a++)de.push(ee[i][ve]),++ve===ee[i].length&&(ve=0);te.push(de)}for(a=0;a<Z;a++){var pe=[];s=ee.length;for(var ye=0;ye<s;ye++)c(te[ye][a]);y&&c(this.status?this.status[a]:null),f(this.value[a])}return"object"===e.type?{cols:re,rows:ie}:ne},e.prototype.node=function(){return this.__tree__},e.prototype.toString=function(){return this.class},String.prototype.capitalize=function(){return this.charAt(0).toUpperCase()+this.slice(1)};var g="3.4.1";export{c as datalist,d as fromCSV,v as fromSDMX,h as fromTable,p as join,u as tbrowser,b as toCSV,g as version};
|
|
1
|
+
// jsonstat-suite v4.0.0 Copyright 2026 Xavier Badosa https://jsonstat.com
|
|
2
|
+
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.Transform({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.Transform({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.0";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};
|
package/main.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
// jsonstat-suite
|
|
2
|
-
"use strict";var e=require("jsonstat-toolkit");function t(t,n){return null==t?null:("string"!=typeof t&&void 0!==t.length||(t=e(t)),0===t.length||"dataset"!==t.class&&"collection"!==t.class&&"bundle"!==t.class?null:"dataset"===t.class?t:t.Dataset(n))}function n(e){if(null===e||0===e.length||"dataset"!==e.class)return!1;for(var t=e.length,n=1;t--;)n*=e.Dimension(t).length;return n===e.n}function r(e,t,n){var r,a,l,i,o;return(t.label||n).capitalize()+(a=t,o="",(r=e)&&"metric"===r.role&&a.unit&&(l=a.unit.hasOwnProperty("label")?a.unit.label:"")+(i=a.unit.hasOwnProperty("symbol")?a.unit.symbol:"")!==""&&(o=" ("+(o=""===i?l:""===l?i:"start"===a.unit.position?i+l:l+" "+i)+")"),o)}function a(e,t,n){var a,l='<select name="'+t+'">',i=[];if(null!==n[1]){if(a=e.id,i=e.Dimension(),2===a.length)return(e.Dimension(n[0]).label||n[0]).capitalize()}else{var o=e.Dimension(t);if(a=o.id,i=o.Category(),1===a.length)return}return a.forEach((function(e,t){var a=e!==n[0]?"":'selected="selected" ';null!==n[1]&&e===n[1]||(l+="<option "+a+'value="'+e+'">'+r(o,i[t],e)+"</option>")})),l+="</select>"}function l(e,t){var n={};return Array.isArray(e[t])?(e[t].forEach((function(e,t){null!==e&&(n[String(t)]=e)})),n):e[t]}function i(t,n){if(void 0===t)return null;void 0===n&&(n={}),"boolean"!=typeof n.ovalue&&(n.ovalue=!1),"boolean"!=typeof n.ostatus&&(n.ostatus=!1),"boolean"!=typeof n.instance&&(n.instance=!1);var r=n.vlabel||"Value",a=n.slabel||"Status",i=n.type||"array",o=n.label||"",s=n.header||null,u=[],c=[],d=[],f=[],h={},v={},b=function(e,t){for(var n=1,r=0,a=0;a<E;a++)r+=(n*=a>0?t[E-a]:1)*e[E-a-1];return r},p=function(){var e=t[S][r];d[b(x,c)]=isNaN(e)?null:e};switch(i){case"array":t=function(e){for(var t=e[0],n=e.slice(1),r=[],a=0,l=n.length;a<l;a++){for(var i=0,o=t.length,s={};i<o;i++)s[t[i]]=n[a][i];r.push(s)}return r}(t);break;case"object":t=function(e){for(var t=e.cols.map((function(e){return e.id})),n=e.rows,r=[],a=0,l=n.length;a<l;a++){for(var i=0,o=t.length,s={};i<o;i++)s[t[i]]=n[a].c[i].v;r.push(s)}return r}(t)}var g,m=t.length;for(var y in n.hasOwnProperty("drop")&&Array.isArray(n.drop)&&n.drop.length&&t.forEach((function(e){n.drop.forEach((function(t){delete e[t]}))})),t[0])if(y!==r)if(y!==a){if(u.push(y),s)g=s.dimension[y],h[y]=g.category.index;else{h[y]=[];for(var w=0;w<m;w++){var O=t[w][y];-1===h[y].indexOf(O)&&h[y].push(O)}}c.push(h[y].length),v[y]={label:s?g.label:y,category:{index:h[y]}},s&&(v[y].category.label=g.category.label,g.category.unit&&(v[y].category.unit=g.category.unit))}else p=function(){var e=t[S][r],n=t[S][a];d[b(x,c)]=isNaN(e)?null:e,f[b(x,c)]=""===n?null:n};for(var E=u.length,S=0;S<m;S++){for(var x=[],A=0;A<E;A++){var D=u[A];x.push(h[D].indexOf(t[S][D]))}p()}var P={version:"2.0",class:"dataset",value:d,dimension:v,id:u,size:c};return o&&(P.label=o),f.length&&(P.status=f),s&&(s.label&&(P.label=s.label),s.source&&(P.source=s.source),s.updated&&(P.updated=s.updated),s.href&&(P.href=s.href),s.role&&(P.role=s.role)),n.ovalue&&(P.value=l(P,"value")),n.ostatus&&P.hasOwnProperty("status")&&(P.status=l(P,"status")),n.instance?e(P):P}function o(e,t){return null==e?"":-1!==e.indexOf(t)?'"'+e+'"':e}String.prototype.capitalize=function(){return this.charAt(0).toUpperCase()+this.slice(1)};exports.datalist=function(e,r){if(void 0===e)return null;void 0===r&&(r={});var a="",l="",i="",o=0,s=r.na||"n/a",u=r.dsid||0,c=r.vlabel||null,d=r.slabel||null,f=r.counter||!1,h=r.tblclass||"",v=r.numclass||"",b=r.valclass||"",p=r.status||!1,g=r.locale||"en-US",m=r.source||"Source",y=t(e,u),w=y.role&&y.role.metric?y.id.indexOf(y.role.metric[0]):null,O=y.Dimension({role:"metric"}),E={},S=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)},x=function(e,t){var n=null;e.forEach((function(e,r){var i=P===r?' class="'+v+" "+b+'"':"",o=s;t?(w===r&&(n=E[e]),null!==e&&(o=P===r?S(e,n):e),l+="<td"+i+">"+o+"</td>"):a+="<th"+i+">"+e+"</th>"}))},A=f?function(e,t){t?l+='<tr><td class="'+v+'">'+t+"</td>":a+='<th class="'+v+'">#</th>',x(e,t),l+="</tr>"}:function(e,t){l+="<tr>",x(e,t),l+="</tr>"};if(!n(y))return null;O&&O[0].Category().forEach((function(e){var t=e.unit&&e.unit.hasOwnProperty("decimals")?e.unit.decimals:null;E[e.label]=t}));var D=y.toTable({status:p,vlabel:c,slabel:d}),P=D[0].length-1;return D.forEach((function(e,t){A(e,t)})),y.source&&(o=y.length+1,f&&o++,p&&o++,"."!==(m+=": "+y.source).slice(-1)&&(m+="."),i='<tfoot><td colspan="'+o+'">'+m+"</td></tfoot>"),'<table class="'+h+'"><caption>'+(r.caption||y.label||"")+"</caption><thead><tr>"+a+"</tr></thead><tbody>"+l+"</tbody>"+i+"</table>"},exports.fromCSV=function(e,t){if(void 0===e)return null;void 0===t&&(t={});var n,r,a,l=[],o=null,s=null,u=!1,c={time:[],geo:[],metric:[]},d="jsonstat"===e.substring(0,8),f=d?"value":t.vlabel,h=d?"status":t.slabel,v=d?e.substring(8,9):t.delimiter||",",b=";"===v?t.decimal||",":t.decimal||".",p=function(e,t){t=t||",";for(var n,r,a=new RegExp("(\\"+t+'|\\r?\\n|\\r|^)(?:"([^"]*(?:""[^"]*)*)"|([^"\\'+t+"\\r\\n]*))","gi"),l=[[]],i=null;i=a.exec(e);)(r=i[1]).length&&r!=t&&l.push([]),n=i[2]?i[2].replace(new RegExp('""',"g"),'"'):i[3],l[l.length-1].push(n);return l}(e.trim(),v);if(d){for(b=p[0][1],a=p[0][2],p.shift();"data"!==p[0][0];)l.push(p.shift());p.shift(),s={dimension:{}},l.forEach((function(e){var t,n,r,l,i,o,d,f;switch(e[0]){case"dimension":if(s.dimension[e[1]]={},(l=s.dimension[e[1]]).label=e[2],l.category={},(i=l.category).index=[],n={},r=2*e[3]+3,e.length>=r){for(t=4;t<r;t++)d=e[t],f=e[++t],Object.defineProperty(n,d,{value:f,writable:!0,configurable:!0,enumerable:!0}),i.label=n,i.index.push(d);"string"==typeof e[t]&&-1!==["time","geo","metric"].indexOf(e[t])&&(c[e[t]].push(e[1]),u=!0,"metric"===e[t]&&"string"==typeof e[++t]&&(i.unit={},i.index.forEach((function(n,r){var l=e[t+r].split(a);i.unit[n]={},o=i.unit[n],void 0!==l[0]&&""!==l[0]&&(o.decimals=1*l[0]),void 0!==l[1]&&""!==l[1]&&(o.label=l[1]),void 0!==l[2]&&""!==l[2]&&(o.symbol=l[2]),void 0!==l[1]&&-1!==["start","end"].indexOf(l[3])&&(o.position=l[3])}))))}break;case"label":case"source":case"updated":case"href":s[e[0]]=e[1]||null}})),u&&(c.time.length||delete c.time,c.geo.length||delete c.geo,c.metric.length||delete c.metric,s.role=c)}if(n=p.length,r=p[0].length,void 0!==f){for(;r--;)if(p[0][r]===f){o=r;break}if(null===o)return null}else o=r-1,f=p[0][o];if(","===b)for(r=1;r<n;r++)p[r][o]=Number(p[r][o].replace(",","."));else for(r=1;r<n;r++)p[r][o]=Number(p[r][o]);return i(p,{header:s,vlabel:f,slabel:h,type:"array",label:t.label||"",ovalue:t.ovalue||!1,ostatus:t.ostatus||!1,instance:t.instance||!1})},exports.fromSDMX=function(t,n){if("object"!=typeof t)return null;if(t.hasOwnProperty("data")&&t.hasOwnProperty("meta")&&t.data.hasOwnProperty("dataSets")&&t.data.hasOwnProperty("structures")&&Array.isArray(t.data.structures)&&(t.dataSets=t.data.dataSets,t.structure=t.data.structures[0],t.header=t.meta),!t.hasOwnProperty("dataSets")||!Array.isArray(t.dataSets))return null;if(1!==t.dataSets.length)return null;t.dataSets[0].hasOwnProperty("observations")||function(e){var t=e.dataSets[0],n=t.series,r=e.structure,a=r.dimensions,l={};Object.keys(n).forEach((function(e){var t=n[e].observations;Object.keys(t).forEach((function(r){l[e+":"+r]=t[r].concat(n[e].attributes)}))})),t.observations=l,delete t.series,a.observation=a.series.concat(a.observation),delete a.series,r.attributes.observation=r.attributes.observation.concat(r.attributes.series),delete r.attributes.series}(t),void 0===n?n={ovalue:!1,ostatus:!1,instance:!1}:("boolean"!=typeof n.ovalue&&(n.ovalue=!1),"boolean"!=typeof n.ostatus&&(n.ostatus=!1),"boolean"!=typeof n.instance&&(n.instance=!1));var r=t.structure,a=t.dataSets[0].observations,l=r.attributes.observation,i=r.dimensions;if(!i.hasOwnProperty("observation"))return null;if(i.hasOwnProperty("series")&&Object.keys(i.series).length)return null;var o=1,s=[],u=[],c={},d=[],f={time:[],geo:[]},h=function(){},v=function(e,t){for(var n=e.size,r=n.length-t.length;r--;)t.push(0);for(var a=0,l=n.length,i=0,o=1;a<l;a++)i+=(o*=a>0?n[l-a]:1)*t[l-a-1];return i},b=function(e){if(c[e.id]={label:e.name},e.hasOwnProperty("role"))switch(e.role){case"REF_AREA":f.geo.push(e.id);break;case"TIME_PERIOD":f.time.push(e.id)}Object.defineProperty(c[e.id],"category",{value:{index:[],label:{}},writable:!0,enumerable:!0}),s.push(e.id),u.push(e.values.length),o*=e.values.length;var t=c[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})}))},p=t.header.links?t.header.links.find((function(e){return"request"===e.rel})):null,g=l.findIndex((function(e){return"OBS_STATUS"===e.id}));-1!==g&&(l[g].values.length?d=l[g].values:g=-1),i.observation.forEach(b),i.hasOwnProperty("dataSet")&&i.dataSet.forEach(b);var m={version:"2.0",class:"dataset",updated:t.header.prepared||null,source:t.header.sender.name||null,label:r.name||null,id:s,size:u,dimension:c,value:n.ovalue?{}:new Array(o).fill(null)};for(var y in p&&(m.link={alternate:[{type:"application/vnd.sdmx.data+json",href:p.href}]}),f.geo.length+f.time.length>0&&(f.time.length||delete f.time,f.geo.length||delete f.geo,m.role=f),-1!==g&&(m.status=n.ostatus?{}:new Array(o).fill(null),m.extension={status:{label:{}}},d.forEach((function(e){m.extension.status.label[e.id]=e.name})),h=n.ostatus?function(){var e=a[y][g];null!==e&&(m.status[v(m,w)]=d[e].id)}:function(){var e=a[y][g];m.status[v(m,w)]=null===e?null:d[e].id}),g++,a){var w=y.split(":");n.ovalue&&null===a[y][0]||(m.value[v(m,w)]=a[y][0]),h()}return n.instance?e(m):m},exports.fromTable=i,exports.join=function(t,n){if(void 0===t||!Array.isArray(t))return null;var r=JSON.parse(JSON.stringify(t)),a=r[0];if(!a.hasOwnProperty("version")||!a.hasOwnProperty("class")||"dataset"!==a.class)return null;void 0===n&&(n={});var l=void 0===n.label?null:n.label,o=void 0===n.by?null:n.by,s=[];if(null===o){for(var u=1,c=r.length;u<c;u++)s=s.concat(r[u].value);return a.value=s,null!==l&&(a.label=l),a}var d,f,h,v=function(e,t,n){if(Array.isArray(e))e=e.concat(t);else for(var r in t)e[r]=0===t[r]?n:t[r];return e};r.forEach((function(t,n){var r=e(t).toTable({status:!0}),a=t.dimension[o].category;0===n?(s=[r[0]],d=a.index,f=a.label,h=a.unit):(d=function(e,t,n){if(Array.isArray(e))e=e.concat(t);else for(var r in t)e[r]=t[r]+n;return e}(d,a.index,Object.keys(d).length),f=v(f,a.label,n),h=v(h,a.unit,n)),s=s.concat(r.slice(1))}));var b=i(s);return a.value=b.value,a.size=b.size,a.status=b.status||null,a.label=l||"",a.href=null,a.dimension[o].category.index=d||null,a.dimension[o].category.label=f||null,a.dimension[o].category.unit=h||null,a},exports.tbrowser=function(e,l,i){function o(e){void 0!==l?l.innerHTML=s[e]:window.alert(s[e])}if(void 0!==e)if(void 0!==l){void 0===i&&(i={});var s=void 0===i.i18n||void 0===i.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"}:i.i18n.msgs,u=void 0===i.i18n||void 0===i.i18n.locale?"en-US":i.i18n.locale,c=i.dsid||0,d=i.status||!1,f=i.tblclass||"",h=i.nonconst||!1,v=t(e,c);if(n(v)){if(h)var b=function(e){var t=0,n=e.size.slice(0),r=[];return n.forEach((function(n,a){var l=a-t,i=e.Dimension(l);1===n&&(delete e.__tree__.dimension[e.id[l]],e.size.splice(l,1),e.id.splice(l,1),e.length--,t++,r.push(i.label.capitalize()+": "+i.Category(0).label.capitalize()))})),r}(v);1!==v.length?function e(t,n,l,o){"function"==typeof i.callback&&i.callback(l);var c="",f="",v="",p="",g=l.rows,m=n.Dimension(g),y=m.id,w=l.cols,O=n.Dimension(w),E=O.id,S=n.role&&n.role.metric?n.role.metric[0]:null,x=null!==S?n.Dimension(S):null,A=function(e){return e.hasOwnProperty("unit")&&e.unit&&e.unit.hasOwnProperty("decimals")?e.unit.decimals:null},D=l.filters,P=JSON.parse(JSON.stringify(D)),j=[],k="",z="",C=n.source?s.source+": "+n.source:"",N=null!==n.label?'<span class="label">'+n.label.capitalize()+"</span>":"";for(var R in h&&b.length&&(N='<span class="label">'+b.join(". ")+"</span>"),""!==C&&"."!==C.slice(-1)&&(C+="."),v+="<caption>"+N+"<form>",D){var _=n.Dimension(R),L=_.label?_.label.capitalize():R.capitalize();_.length>1?k+="<p>"+a(n,R,[D[R],null])+" <strong>"+L+"</strong></p>":j.push({label:L,value:r(_,_.Category(0)),name:R,id:_.id[0]})}""!==k&&(k='<fieldset id="filters"><legend>'+s.filters+"</legend>"+k+"</fieldset>"),j.forEach((function(e){z+="<p>"+e.value+" <strong>"+e.label+'</strong></p><input type="hidden" name="'+e.name+'" value="'+e.id+'" />'})),""!==z&&(z='<fieldset id="constants"><legend>'+s.constants+"</legend>"+z+"</fieldset>"),v+=z+k+'<fieldset id="rowscols"><legend>'+s.rc+"</legend>"+a(n,"rows",[g,w])+" <a>↔</a> "+a(n,"cols",[w,g])+"</fieldset></form></caption>",p+="<tbody>";var T=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(y.forEach((function(e){P[g]=e;var t=n.Data(P),a=function(e,t){var n,r=w!==S?null===x?null:A(x.Category(P[S])):A(O.Category(t));null!==e.value?(n=T(e.value,r),d&&null!==e.status&&(n+=" ("+e.status+")")):n=e.status||s.na,p+="<td>"+n+"</td>"};null!==t?(p+='<tr><th scope="row">'+r(m,m.Category(e))+"</th>",Array.isArray(t)?t.forEach((function(e,t){a(e,t)})):a(t,0),p+="</tr>"):p="ERROR"})),"ERROR"===p)return s.dataerror;p+="</tbody>",c+="<thead><tr><th></th>",E.forEach((function(e){c+='<th scope="col">'+r(O,O.Category(e))+"</th>"})),c+="</tr></thead>",""!==C&&(f='<tfoot><tr><td colspan="'+(E.length+1)+'">'+C+"</td></tr></tfoot>"),t.innerHTML='<table class="'+o+'">'+v+c+p+f+"</table>",[].slice.call(t.querySelectorAll("select")).forEach((function(r){r.addEventListener("change",(function(r){var a,l;e(t,n,function(e,t,n){var r={filters:{}};return n.forEach((function(e){"rows"===e.name||"cols"===e.name?r[e.name]=e.value:r.filters[e.name]=e.value})),"rowscols"===t&&e.id.forEach((function(t,n){t!==r.rows&&t!==r.cols?void 0===r.filters[t]&&(r.filters[t]=e.Dimension(n).id[0]):delete r.filters[t]})),r}(n,r.target.parentElement.getAttribute("id"),(a=t,l=[],[].slice.call(a.querySelectorAll("select, input")).forEach((function(e){l.push({name:e.name,value:e.value})})),l)),o)}),!1)})),t.querySelector("a").addEventListener("click",(function(){l.cols=g,l.rows=w,e(t,n,l,o)}),!1)}(l,v,function(e,t){if("object"==typeof t)return{rows:t.rows,cols:t.cols,filters:t.filters};var n,r,a={},l=[],i=e.id;if(t){var o="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(o),n=l[0].id,r=l[1].id}else n=i[0],r=i[1];return e.Dimension(n).length<e.Dimension(r).length&&(n=r+(r=n,"")),i.forEach((function(t){t!==n&&t!==r&&(a[t]=e.Dimension(t).id[0])})),{rows:n,cols:r,filters:a}}(v,i.preset),f):o("dimerror")}else o("jsonerror")}else o("selerror");else o("urierror")},exports.toCSV=function(e,r){if(void 0===e)return null;void 0===r&&(r={});var a={content:!1,field:!1},l=!0===r.rich,i=r.content||"label",s=r.field||"label",u=l?"value":r.vlabel||"Value",c=l?"status":r.slabel||"Status",d=!0===r.status,f=r.na||"n/a",h=r.delimiter||",",v=r.separator||"|",b=";"===h?r.decimal||",":r.decimal||".",p=!0===r.array,g=t(e,r.dsid||0),m=p?[]:"",y=p?[]:"",w=p?function(e){m.push(e)}:function(e){m+=e+"\n"},O=p?function(e){y.push(e)}:function(e){y+=e+"\n"};if(!n(g))return null;l?d=null!==g.status:("[id] label"===i&&(a.content=!0,i="id"),"[id] label"===s&&(a.field=!0,s="id"));var E=g.toTable({vlabel:u,slabel:c,status:d,field:l||a.field?"id":s,content:l||a.content?"id":i,type:"array"}),S=E[0].indexOf("id"===s?"value":u),x=d?E[0].indexOf("id"===s?"status":c):-1;return E.forEach((function(e,t){e.forEach((function(n,r){var l=g.Dimension(r);t&&r===S?null===n?e[r]=o(f,h):"."!==b&&(e[r]=String(e[r]).replace(".",b)):t&&r===x&&null===n?e[r]="":(a.content&&t&&l&&(e[r]="["+e[r]+"] "+l.Category(e[r]).label),a.field&&0===t&&(e[r]=l?"["+e[r]+"] "+l.label:"value"===e[r]?u:c),e[r]=o(e[r],h))})),w(e.join(h))})),l&&(O("jsonstat"+h+b+h+v),["label","source","updated","href"].forEach((function(e){g[e]&&O(e+h+o(g[e],h))})),g.id.forEach((function(e,t){var n=[],r=g.Dimension(t),a=r.role,l=!1,i="";i+="dimension"+h+o(e,h)+h+o(r.label,h)+h+r.length,"metric"===a&&r.__tree__.category.unit&&(l=!0),r.id.forEach((function(e,t){var a=[],s=r.Category(t);i+=h+o(e,h)+h+o(s.label,h),l&&(a.push(s.unit.hasOwnProperty("decimals")?s.unit.decimals:""),a.push(s.unit.label||""),s.unit.symbol&&(a.push(s.unit.symbol),a.push(s.unit.position)),n.push(o(a.join(v),h)))})),null!==a&&"classification"!==a&&(i+=h+r.role,l&&(i+=h+n.join(h))),O(i)})),m=p?y.concat(["data"],m):y+"data\n"+m),m},exports.version="3.4.1";
|
|
1
|
+
// jsonstat-suite v4.0.0 Copyright 2026 Xavier Badosa https://jsonstat.com
|
|
2
|
+
"use strict";var e=require("jsonstat-toolkit");function t(t,n){return null==t?null:("string"!=typeof t&&void 0!==t.length||(t=e(t)),0===t.length||"dataset"!==t.class&&"collection"!==t.class&&"bundle"!==t.class?null:(null===t.value&&(t.value=Array(t.n).fill(null)),"dataset"===t.class?t:t.Dataset(n)))}function n(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 l(e,t,n){return(t.label||n).capitalize()+function(e,t){let n,l,o="";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!==""&&(o=""===l?n:""===n?l:"start"===t.unit.position?l+n:n+" "+l,o=" ("+o+")")),o}(e,t)}function o(e,t,n){let o,i,r='<select name="'+t+'">',s=[];if(null!==n[1]){if(o=e.id,s=e.Dimension(),2===o.length)return(e.Dimension(n[0]).label||n[0]).capitalize()}else if(i=e.Dimension(t),o=i.id,s=i.Category(),1===o.length)return;return o.forEach(function(e,t){const o=e!==n[0]?"":'selected="selected" ';null!==n[1]&&e===n[1]||(r+="<option "+o+'value="'+e+'">'+l(i,s[t],e)+"</option>")}),r+="</select>",r}function i(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 r(t,n){if(void 0===t)return null;void 0===n&&(n={}),"boolean"!=typeof n.ovalue&&(n.ovalue=!1),"boolean"!=typeof n.ostatus&&(n.ostatus=!1),"boolean"!=typeof n.instance&&(n.instance=!1);const l=n.vlabel||"Value",o=n.slabel||"Status",r=n.type||"array",s=n.label||"",a=n.header||null,u=[],c=[],d=[],f=[],h={},b={},p=function(e,t){let n=1,l=0;for(let o=0;o<v;o++)n*=o>0?t[v-o]:1,l+=n*e[v-o-1];return l};let m,g=function(e,n){const o=t[e][l];d[p(n,c)]=isNaN(o)?null:o};switch(r){case"array":t=function(e){const t=e[0],n=e.slice(1),l=[];for(let e=0,o=n.length;e<o;e++){let o={};for(let l=0,i=t.length;l<i;l++)o[t[l]]=n[e][l];l.push(o)}return l}(t);break;case"object":t=function(e){const t=e.cols.map(function(e){return e.id}),n=e.rows,l=[];for(let e=0,o=n.length;e<o;e++){let o={};for(let l=0,i=t.length;l<i;l++)o[t[l]]=n[e].c[l].v;l.push(o)}return l}(t)}const y=t.length;n.hasOwnProperty("drop")&&Array.isArray(n.drop)&&n.drop.length&&t.forEach(function(e){n.drop.forEach(function(t){delete e[t]})});for(const e in t[0])if(e!==l)if(e!==o){if(u.push(e),a)m=a.dimension[e],h[e]=m.category.index;else{h[e]=[];for(let n=0;n<y;n++){const l=t[n][e];-1===h[e].indexOf(l)&&h[e].push(l)}}c.push(h[e].length),b[e]={label:a?m.label:e,category:{index:h[e]}},a&&(b[e].category.label=m.category.label,m.category.unit&&(b[e].category.unit=m.category.unit))}else g=function(e,n){const i=t[e][l],r=t[e][o];d[p(n,c)]=isNaN(i)?null:i,f[p(n,c)]=""===r?null:r};const v=u.length;for(let e=0;e<y;e++){const n=[];for(let l=0;l<v;l++){const o=u[l];n.push(h[o].indexOf(t[e][o]))}g(e,n)}const w={version:"2.0",class:"dataset",value:d,dimension:b,id:u,size:c};return s&&(w.label=s),f.length&&(w.status=f),a&&(a.label&&(w.label=a.label),a.source&&(w.source=a.source),a.updated&&(w.updated=a.updated),a.href&&(w.href=a.href),a.role&&(w.role=a.role)),n.ovalue&&(w.value=i(w,"value")),n.ostatus&&w.hasOwnProperty("status")&&(w.status=i(w,"status")),n.instance?e(w):w}function s(e,t){return null==e?"":-1!==e.indexOf(t)?'"'+e+'"':e}String.prototype.capitalize=function(){return this.charAt(0).toUpperCase()+this.slice(1)};exports.datalist=function(e,l){if(void 0===e)return null;void 0===l&&(l={});let o="",i="",r="",s=0,a=l.na||"n/a",u=l.dsid||0,c=l.vlabel||null,d=l.slabel||null,f=l.counter||!1,h=l.tblclass||"",b=l.numclass||"",p=l.valclass||"",m=l.status||!1,g=l.locale||"en-US",y=l.source||"Source",v=t(e,u),w=v.role&&v.role.metric?v.id.indexOf(v.role.metric[0]):null,O=v.Dimension({role:"metric"}),E={},S=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)},x=function(e,t){let n=null;e.forEach(function(e,l){const r=j===l?' class="'+b+" "+p+'"':"";let s=a;t?(w===l&&(n=E[e]),null!==e&&(s=j===l?S(e,n):e),i+="<td"+r+">"+s+"</td>"):o+="<th"+r+">"+e+"</th>"})},A=f?function(e,t){t?i+='<tr><td class="'+b+'">'+t+"</td>":o+='<th class="'+b+'">#</th>',x(e,t),i+="</tr>"}:function(e,t){i+="<tr>",x(e,t),i+="</tr>"};if(!n(v))return null;O&&O[0].Category().forEach(function(e){const t=e.unit&&e.unit.hasOwnProperty("decimals")?e.unit.decimals:null;E[e.label]=t}),console.log(v),console.log(v.Transform({status:m,vlabel:c,slabel:d}));const D=v.Transform({status:m,vlabel:c,slabel:d}),j=D[0].length-1;return D.forEach(function(e,t){A(e,t)}),v.source&&(s=v.length+1,f&&s++,m&&s++,y+=": "+v.source,"."!==y.slice(-1)&&(y+="."),r='<tfoot><td colspan="'+s+'">'+y+"</td></tfoot>"),'<table class="'+h+'"><caption>'+(l.caption||v.label||"")+"</caption><thead><tr>"+o+"</tr></thead><tbody>"+i+"</tbody>"+r+"</table>"},exports.fromCSV=function(e,t){if(void 0===e)return null;void 0===t&&(t={});const n="jsonstat"===e.substring(0,8),l={time:[],geo:[],metric:[]},o=n?e.substring(8,9):t.delimiter||",";let i,s,a,u=[],c=null,d=null,f=!1,h=n?"value":t.vlabel,b=n?"status":t.slabel,p=";"===o?t.decimal||",":t.decimal||".";const m=function(e,t){t=t||",";const n=new RegExp("(\\"+t+'|\\r?\\n|\\r|^)(?:"([^"]*(?:""[^"]*)*)"|([^"\\'+t+"\\r\\n]*))","gi"),l=[[]];let o,i,r=null;for(;r=n.exec(e);)i=r[1],i.length&&i!=t&&l.push([]),o=r[2]?r[2].replace(new RegExp('""',"g"),'"'):r[3],l[l.length-1].push(o);return l}(e.trim(),o);if(n){for(p=m[0][1],a=m[0][2],m.shift();"data"!==m[0][0];)u.push(m.shift());m.shift(),d={dimension:{}},u.forEach(function(e){let t,n,o,i,r,s,u,c;switch(e[0]){case"dimension":if(d.dimension[e[1]]={},i=d.dimension[e[1]],i.label=e[2],i.category={},r=i.category,r.index=[],n={},o=2*e[3]+3,e.length>=o){for(t=4;t<o;t++)u=e[t],c=e[++t],Object.defineProperty(n,u,{value:c,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 o=e[t+l].split(a);r.unit[n]={},s=r.unit[n],void 0!==o[0]&&""!==o[0]&&(s.decimals=1*o[0]),void 0!==o[1]&&""!==o[1]&&(s.label=o[1]),void 0!==o[2]&&""!==o[2]&&(s.symbol=o[2]),void 0!==o[1]&&-1!==["start","end"].indexOf(o[3])&&(s.position=o[3])})))}break;case"label":case"source":case"updated":case"href":d[e[0]]=e[1]||null}}),f&&(l.time.length||delete l.time,l.geo.length||delete l.geo,l.metric.length||delete l.metric,d.role=l)}if(i=m.length,s=m[0].length,void 0!==h){for(;s--;)if(m[0][s]===h){c=s;break}if(null===c)return null}else c=s-1,h=m[0][c];if(","===p)for(s=1;s<i;s++)m[s][c]=Number(m[s][c].replace(",","."));else for(s=1;s<i;s++)m[s][c]=Number(m[s][c]);return r(m,{header:d,vlabel:h,slabel:b,type:"array",label:t.label||"",ovalue:t.ovalue||!1,ostatus:t.ostatus||!1,instance:t.instance||!1})},exports.fromSDMX=function(t,n){if("object"!=typeof t)return null;if(t.hasOwnProperty("data")&&t.hasOwnProperty("meta")&&t.data.hasOwnProperty("dataSets")&&t.data.hasOwnProperty("structures")&&Array.isArray(t.data.structures)&&(t.dataSets=t.data.dataSets,t.structure=t.data.structures[0],t.header=t.meta),!t.hasOwnProperty("dataSets")||!Array.isArray(t.dataSets))return null;if(1!==t.dataSets.length)return null;t.dataSets[0].hasOwnProperty("observations")||function(e){const t=e.dataSets[0],n=t.series,l=e.structure,o=l.dimensions,i={};Object.keys(n).forEach(function(e){const t=n[e].observations;Object.keys(t).forEach(function(l){i[e+":"+l]=t[l].concat(n[e].attributes)})}),t.observations=i,delete t.series,o.observation=o.series.concat(o.observation),delete o.series,l.attributes.observation=l.attributes.observation.concat(l.attributes.series),delete l.attributes.series}(t),void 0===n?n={ovalue:!1,ostatus:!1,instance:!1}:("boolean"!=typeof n.ovalue&&(n.ovalue=!1),"boolean"!=typeof n.ostatus&&(n.ostatus=!1),"boolean"!=typeof n.instance&&(n.instance=!1));const l=t.structure,o=t.dataSets[0].observations,i=l.attributes.observation,r=l.dimensions;if(!r.hasOwnProperty("observation"))return null;if(r.hasOwnProperty("series")&&Object.keys(r.series).length)return null;let s=1,a=[],u=function(){},c=i.findIndex(function(e){return"OBS_STATUS"===e.id});const d=[],f=[],h={},b={time:[],geo:[]},p=function(e,t){const n=e.size;for(let e=n.length-t.length;e--;)t.push(0);let l=0,o=1;for(let e=0,i=n.length;e<i;e++)o*=e>0?n[i-e]:1,l+=o*t[i-e-1];return l},m=function(e){if(h[e.id]={label:e.name},e.hasOwnProperty("role"))switch(e.role){case"REF_AREA":b.geo.push(e.id);break;case"TIME_PERIOD":b.time.push(e.id)}Object.defineProperty(h[e.id],"category",{value:{index:[],label:{}},writable:!0,enumerable:!0}),d.push(e.id),f.push(e.values.length),s*=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=t.header.links?t.header.links.find(function(e){return"request"===e.rel}):null;-1!==c&&(i[c].values.length?a=i[c].values:c=-1),r.observation.forEach(m),r.hasOwnProperty("dataSet")&&r.dataSet.forEach(m);const y={version:"2.0",class:"dataset",updated:t.header.prepared||null,source:t.header.sender.name||null,label:l.name||null,id:d,size:f,dimension:h,value:n.ovalue?{}:new Array(s).fill(null)};g&&(y.link={alternate:[{type:"application/vnd.sdmx.data+json",href:g.href}]}),b.geo.length+b.time.length>0&&(b.time.length||delete b.time,b.geo.length||delete b.geo,y.role=b),-1!==c&&(y.status=n.ostatus?{}:new Array(s).fill(null),y.extension={status:{label:{}}},a.forEach(function(e){y.extension.status.label[e.id]=e.name}),u=n.ostatus?function(e,t){const n=o[e][c];null!==n&&(y.status[p(y,t)]=a[n].id)}:function(e,t){const n=o[e][c];y.status[p(y,t)]=null===n?null:a[n].id}),c++;for(let e in o){const t=e.split(":");n.ovalue&&null===o[e][0]||(y.value[p(y,t)]=o[e][0]),u(e,t)}return n.instance?e(y):y},exports.fromTable=r,exports.join=function(t,n){if(void 0===t||!Array.isArray(t))return null;const l=JSON.parse(JSON.stringify(t)),o=l[0];if(!o.hasOwnProperty("version")||!o.hasOwnProperty("class")||"dataset"!==o.class)return null;void 0===n&&(n={});const i=void 0===n.label?null:n.label,s=void 0===n.by?null:n.by;let a,u,c,d=[];if(null===s){for(let e=1,t=l.length;e<t;e++)d=d.concat(l[e].value);return o.value=d,null!==i&&(o.label=i),o}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};l.forEach(function(t,n){const l=e(t).Transform({status:!0}),o=t.dimension[s].category;0===n?(d=[l[0]],a=o.index,u=o.label,c=o.unit):(a=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}(a,o.index,Object.keys(a).length),u=f(u,o.label,n),c=f(c,o.unit,n)),d=d.concat(l.slice(1))});const h=r(d);return o.value=h.value,o.size=h.size,o.status=h.status||null,o.label=i||"",o.href=null,o.dimension[s].category.index=a||null,o.dimension[s].category.label=u||null,o.dimension[s].category.unit=c||null,o},exports.tbrowser=function(e,i,r){function s(e){void 0!==i?i.innerHTML=a[e]:window.alert(a[e])}if(void 0===e)return void s("urierror");if(void 0===i)return void s("selerror");void 0===r&&(r={});const a=void 0===r.i18n||void 0===r.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"}:r.i18n.msgs,u=void 0===r.i18n||void 0===r.i18n.locale?"en-US":r.i18n.locale,c=r.dsid||0,d=r.status||!1,f=r.tblclass||"",h=r.nonconst||!1,b=t(e,c);if(!n(b))return void s("jsonerror");let p;h&&(p=function(e){let t=0;const n=e.size.slice(0),l=[];return n.forEach(function(n,o){const i=o-t,r=e.Dimension(i);1===n&&(delete e.__tree__.dimension[e.id[i]],e.size.splice(i,1),e.id.splice(i,1),e.length--,t++,l.push(r.label.capitalize()+": "+r.Category(0).label.capitalize()))}),l}(b)),1!==b.length?function e(t,n,i,s){"function"==typeof r.callback&&r.callback(i);let c="",f="",b="",m="",g="",y="",v=null!==n.label?'<span class="label">'+n.label.capitalize()+"</span>":"",w=n.source?a.source+": "+n.source:"";const O=i.rows,E=n.Dimension(O),S=E.id,x=i.cols,A=n.Dimension(x),D=A.id,j=n.role&&n.role.metric?n.role.metric[0]:null,P=null!==j?n.Dimension(j):null,C=function(e){return e.hasOwnProperty("unit")&&e.unit&&e.unit.hasOwnProperty("decimals")?e.unit.decimals:null},k=i.filters,z=JSON.parse(JSON.stringify(k)),N=[];h&&p.length&&(v='<span class="label">'+p.join(". ")+"</span>"),""!==w&&"."!==w.slice(-1)&&(w+="."),b+="<caption>"+v+"<form>";for(const e in k){const t=n.Dimension(e),i=t.label?t.label.capitalize():e.capitalize();t.length>1?g+="<p>"+o(n,e,[k[e],null])+" <strong>"+i+"</strong></p>":N.push({label:i,value:l(t,t.Category(0)),name:e,id:t.id[0]})}""!==g&&(g='<fieldset id="filters"><legend>'+a.filters+"</legend>"+g+"</fieldset>"),N.forEach(function(e){y+="<p>"+e.value+" <strong>"+e.label+'</strong></p><input type="hidden" name="'+e.name+'" value="'+e.id+'" />'}),""!==y&&(y='<fieldset id="constants"><legend>'+a.constants+"</legend>"+y+"</fieldset>"),b+=y+g+'<fieldset id="rowscols"><legend>'+a.rc+"</legend>"+o(n,"rows",[O,x])+" <a>↔</a> "+o(n,"cols",[x,O])+"</fieldset></form></caption>",m+="<tbody>";const R=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(S.forEach(function(e){z[O]=e;const t=n.Data(z),o=function(e,t){let n;const l=x!==j?null===P?null:C(P.Category(z[j])):C(A.Category(t));null!==e.value?(n=R(e.value,l),d&&null!==e.status&&(n+=" ("+e.status+")")):n=e.status||a.na,m+="<td>"+n+"</td>"};null!==t?(m+='<tr><th scope="row">'+l(E,E.Category(e))+"</th>",Array.isArray(t)?t.forEach(function(e,t){o(e,t)}):o(t,0),m+="</tr>"):m="ERROR"}),"ERROR"===m)return a.dataerror;m+="</tbody>",c+="<thead><tr><th></th>",D.forEach(function(e){c+='<th scope="col">'+l(A,A.Category(e))+"</th>"}),c+="</tr></thead>",""!==w&&(f='<tfoot><tr><td colspan="'+(D.length+1)+'">'+w+"</td></tr></tfoot>"),t.innerHTML='<table class="'+s+'">'+b+c+m+f+"</table>",[].slice.call(t.querySelectorAll("select")).forEach(function(l){l.addEventListener("change",function(l){e(t,n,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}(n,l.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)),s)},!1)}),t.querySelector("a").addEventListener("click",function(){i.cols=O,i.rows=x,e(t,n,i,s)},!1)}(i,b,function(e,t){if("object"==typeof t)return{rows:t.rows,cols:t.cols,filters:t.filters};const n={},l=[],o=e.id;let i,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:o[t],len:e.length})}),l.sort(n),i=l[0].id,r=l[1].id}else i=o[0],r=o[1];return e.Dimension(i).length<e.Dimension(r).length&&(i=r+(r=i,"")),o.forEach(function(t){t!==i&&t!==r&&(n[t]=e.Dimension(t).id[0])}),{rows:i,cols:r,filters:n}}(b,r.preset),f):s("dimerror")},exports.toCSV=function(e,l){if(void 0===e)return null;void 0===l&&(l={});const o={content:!1,field:!1},i=!0===l.rich,r=l.na||"n/a",a=l.delimiter||",",u=l.separator||"|",c=";"===a?l.decimal||",":l.decimal||".",d=!0===l.array,f=t(e,l.dsid||0),h=d?function(e){w.push(e)}:function(e){w+=e+"\n"};let b=l.content||"label",p=l.field||"label",m=i?"value":l.vlabel||"Value",g=i?"status":l.slabel||"Status",y=!0===l.status,v=d?[]:"",w=d?[]:"",O=d?function(e){v.push(e)}:function(e){v+=e+"\n"};if(!n(f))return null;i?y=null!==f.status:("[id] label"===b&&(o.content=!0,b="id"),"[id] label"===p&&(o.field=!0,p="id"));const E=!i&&l.by&&-1!==f.id.indexOf(l.by)?l.by:null,S=E&&void 0!==l.drop&&Array.isArray(l.drop)?l.drop:null;if(E)return function(e,t){const n=[],l=t.by,o=t.idlabel,i=t.drop,r=t.na,a=t.delimiter,u=t.decimal,c=o.content?"id":t.content,d=o.field?"label":t.field,f=e.Transform({drop:i,content:c,by:l,type:"arrobj"}),h=Object.keys(f[0]),b=function(e,t,n,l,i){return e.map(e=>"label"===n?-1!==l.id.indexOf(e)?s((o.field?"["+e+"] ":"")+l.Dimension(e).label,t):s((o.field?"["+e+"] ":"")+l.Dimension(i).Category(e).label,t):s(e,t)).join(t)+"\n"}(h,a,d,e,l),p="."!==u?function(e){return null===e?s(r,a):String(e).replace(".",u)}:function(e){return null===e?s(r,a):e};return f.forEach(t=>{let l=[];h.forEach(n=>{const i=t[n],r="string"==typeof i?s(o.content?"["+i+"] "+e.Dimension(n).Category(i).label:i,a):p(i);l.push(r)}),n.push(l.join(a))}),b+n.join("\n")}(f,{drop:S,na:r,delimiter:a,decimal:c,content:b,field:p,by:E,idlabel:o});const x=f.Transform({vlabel:m,slabel:g,status:y,field:i||o.field?"id":p,content:i||o.content?"id":b,type:"array"}),A=x[0].indexOf("id"===p?"value":m),D=y?x[0].indexOf("id"===p?"status":g):-1;return x.forEach(function(e,t){e.forEach(function(n,l){const i=f.Dimension(l);t&&l===A?null===n?e[l]=s(r,a):"."!==c&&(e[l]=String(e[l]).replace(".",c)):t&&l===D&&null===n?e[l]="":(o.content&&t&&i?e[l]="["+e[l]+"] "+i.Category(e[l]).label:o.field&&0===t&&(e[l]=i?"["+e[l]+"] "+i.label:"value"===e[l]?m:g),e[l]=s(e[l],a))}),O(e.join(a))}),i&&(h("jsonstat"+a+c+a+u),["label","source","updated","href"].forEach(function(e){f[e]&&h(e+a+s(f[e],a))}),f.id.forEach(function(e,t){const n=[],l=f.Dimension(t),o=l.role;let i=!1,r="";r+="dimension"+a+s(e,a)+a+s(l.label,a)+a+l.length,"metric"===o&&l.__tree__.category.unit&&(i=!0),l.id.forEach(function(e,t){const o=[],c=l.Category(t);r+=a+s(e,a)+a+s(c.label,a),i&&(o.push(c.unit.hasOwnProperty("decimals")?c.unit.decimals:""),o.push(c.unit.label||""),c.unit.symbol&&(o.push(c.unit.symbol),o.push(c.unit.position)),n.push(s(o.join(u),a)))}),null!==o&&"classification"!==o&&(r+=a+l.role,i&&(r+=a+n.join(a))),h(r)}),v=d?w.concat(["data"],v):w+"data\n"+v),v},exports.version="4.0.0";
|
package/module.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
// jsonstat-suite
|
|
2
|
-
import e from"jsonstat-toolkit";function t(t,n){return null==t?null:("string"!=typeof t&&void 0!==t.length||(t=e(t)),0===t.length||"dataset"!==t.class&&"collection"!==t.class&&"bundle"!==t.class?null:"dataset"===t.class?t:t.Dataset(n))}function n(e){if(null===e||0===e.length||"dataset"!==e.class)return!1;for(var t=e.length,n=1;t--;)n*=e.Dimension(t).length;return n===e.n}function a(e,t,n){var a,r,l,i,o;return(t.label||n).capitalize()+(r=t,o="",(a=e)&&"metric"===a.role&&r.unit&&(l=r.unit.hasOwnProperty("label")?r.unit.label:"")+(i=r.unit.hasOwnProperty("symbol")?r.unit.symbol:"")!==""&&(o=" ("+(o=""===i?l:""===l?i:"start"===r.unit.position?i+l:l+" "+i)+")"),o)}function r(e,t,n){var r,l='<select name="'+t+'">',i=[];if(null!==n[1]){if(r=e.id,i=e.Dimension(),2===r.length)return(e.Dimension(n[0]).label||n[0]).capitalize()}else{var o=e.Dimension(t);if(r=o.id,i=o.Category(),1===r.length)return}return r.forEach((function(e,t){var r=e!==n[0]?"":'selected="selected" ';null!==n[1]&&e===n[1]||(l+="<option "+r+'value="'+e+'">'+a(o,i[t],e)+"</option>")})),l+="</select>"}function l(e,l,i){function o(e){void 0!==l?l.innerHTML=s[e]:window.alert(s[e])}if(void 0!==e)if(void 0!==l){void 0===i&&(i={});var s=void 0===i.i18n||void 0===i.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"}:i.i18n.msgs,u=void 0===i.i18n||void 0===i.i18n.locale?"en-US":i.i18n.locale,c=i.dsid||0,d=i.status||!1,f=i.tblclass||"",h=i.nonconst||!1,v=t(e,c);if(n(v)){if(h)var b=function(e){var t=0,n=e.size.slice(0),a=[];return n.forEach((function(n,r){var l=r-t,i=e.Dimension(l);1===n&&(delete e.__tree__.dimension[e.id[l]],e.size.splice(l,1),e.id.splice(l,1),e.length--,t++,a.push(i.label.capitalize()+": "+i.Category(0).label.capitalize()))})),a}(v);1!==v.length?function e(t,n,l,o){"function"==typeof i.callback&&i.callback(l);var c="",f="",v="",p="",g=l.rows,m=n.Dimension(g),y=m.id,w=l.cols,O=n.Dimension(w),E=O.id,S=n.role&&n.role.metric?n.role.metric[0]:null,x=null!==S?n.Dimension(S):null,A=function(e){return e.hasOwnProperty("unit")&&e.unit&&e.unit.hasOwnProperty("decimals")?e.unit.decimals:null},D=l.filters,P=JSON.parse(JSON.stringify(D)),j=[],k="",z="",N=n.source?s.source+": "+n.source:"",C=null!==n.label?'<span class="label">'+n.label.capitalize()+"</span>":"";for(var R in h&&b.length&&(C='<span class="label">'+b.join(". ")+"</span>"),""!==N&&"."!==N.slice(-1)&&(N+="."),v+="<caption>"+C+"<form>",D){var _=n.Dimension(R),L=_.label?_.label.capitalize():R.capitalize();_.length>1?k+="<p>"+r(n,R,[D[R],null])+" <strong>"+L+"</strong></p>":j.push({label:L,value:a(_,_.Category(0)),name:R,id:_.id[0]})}""!==k&&(k='<fieldset id="filters"><legend>'+s.filters+"</legend>"+k+"</fieldset>"),j.forEach((function(e){z+="<p>"+e.value+" <strong>"+e.label+'</strong></p><input type="hidden" name="'+e.name+'" value="'+e.id+'" />'})),""!==z&&(z='<fieldset id="constants"><legend>'+s.constants+"</legend>"+z+"</fieldset>"),v+=z+k+'<fieldset id="rowscols"><legend>'+s.rc+"</legend>"+r(n,"rows",[g,w])+" <a>↔</a> "+r(n,"cols",[w,g])+"</fieldset></form></caption>",p+="<tbody>";var T=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(y.forEach((function(e){P[g]=e;var t=n.Data(P),r=function(e,t){var n,a=w!==S?null===x?null:A(x.Category(P[S])):A(O.Category(t));null!==e.value?(n=T(e.value,a),d&&null!==e.status&&(n+=" ("+e.status+")")):n=e.status||s.na,p+="<td>"+n+"</td>"};null!==t?(p+='<tr><th scope="row">'+a(m,m.Category(e))+"</th>",Array.isArray(t)?t.forEach((function(e,t){r(e,t)})):r(t,0),p+="</tr>"):p="ERROR"})),"ERROR"===p)return s.dataerror;p+="</tbody>",c+="<thead><tr><th></th>",E.forEach((function(e){c+='<th scope="col">'+a(O,O.Category(e))+"</th>"})),c+="</tr></thead>",""!==N&&(f='<tfoot><tr><td colspan="'+(E.length+1)+'">'+N+"</td></tr></tfoot>"),t.innerHTML='<table class="'+o+'">'+v+c+p+f+"</table>",[].slice.call(t.querySelectorAll("select")).forEach((function(a){a.addEventListener("change",(function(a){var r,l;e(t,n,function(e,t,n){var a={filters:{}};return n.forEach((function(e){"rows"===e.name||"cols"===e.name?a[e.name]=e.value:a.filters[e.name]=e.value})),"rowscols"===t&&e.id.forEach((function(t,n){t!==a.rows&&t!==a.cols?void 0===a.filters[t]&&(a.filters[t]=e.Dimension(n).id[0]):delete a.filters[t]})),a}(n,a.target.parentElement.getAttribute("id"),(r=t,l=[],[].slice.call(r.querySelectorAll("select, input")).forEach((function(e){l.push({name:e.name,value:e.value})})),l)),o)}),!1)})),t.querySelector("a").addEventListener("click",(function(){l.cols=g,l.rows=w,e(t,n,l,o)}),!1)}(l,v,function(e,t){if("object"==typeof t)return{rows:t.rows,cols:t.cols,filters:t.filters};var n,a,r={},l=[],i=e.id;if(t){var o="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(o),n=l[0].id,a=l[1].id}else n=i[0],a=i[1];return e.Dimension(n).length<e.Dimension(a).length&&(n=a+(a=n,"")),i.forEach((function(t){t!==n&&t!==a&&(r[t]=e.Dimension(t).id[0])})),{rows:n,cols:a,filters:r}}(v,i.preset),f):o("dimerror")}else o("jsonerror")}else o("selerror");else o("urierror")}function i(e,a){if(void 0===e)return null;void 0===a&&(a={});var r="",l="",i="",o=0,s=a.na||"n/a",u=a.dsid||0,c=a.vlabel||null,d=a.slabel||null,f=a.counter||!1,h=a.tblclass||"",v=a.numclass||"",b=a.valclass||"",p=a.status||!1,g=a.locale||"en-US",m=a.source||"Source",y=t(e,u),w=y.role&&y.role.metric?y.id.indexOf(y.role.metric[0]):null,O=y.Dimension({role:"metric"}),E={},S=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)},x=function(e,t){var n=null;e.forEach((function(e,a){var i=P===a?' class="'+v+" "+b+'"':"",o=s;t?(w===a&&(n=E[e]),null!==e&&(o=P===a?S(e,n):e),l+="<td"+i+">"+o+"</td>"):r+="<th"+i+">"+e+"</th>"}))},A=f?function(e,t){t?l+='<tr><td class="'+v+'">'+t+"</td>":r+='<th class="'+v+'">#</th>',x(e,t),l+="</tr>"}:function(e,t){l+="<tr>",x(e,t),l+="</tr>"};if(!n(y))return null;O&&O[0].Category().forEach((function(e){var t=e.unit&&e.unit.hasOwnProperty("decimals")?e.unit.decimals:null;E[e.label]=t}));var D=y.toTable({status:p,vlabel:c,slabel:d}),P=D[0].length-1;return D.forEach((function(e,t){A(e,t)})),y.source&&(o=y.length+1,f&&o++,p&&o++,"."!==(m+=": "+y.source).slice(-1)&&(m+="."),i='<tfoot><td colspan="'+o+'">'+m+"</td></tfoot>"),'<table class="'+h+'"><caption>'+(a.caption||y.label||"")+"</caption><thead><tr>"+r+"</tr></thead><tbody>"+l+"</tbody>"+i+"</table>"}function o(e,t){var n={};return Array.isArray(e[t])?(e[t].forEach((function(e,t){null!==e&&(n[String(t)]=e)})),n):e[t]}function s(t,n){if(void 0===t)return null;void 0===n&&(n={}),"boolean"!=typeof n.ovalue&&(n.ovalue=!1),"boolean"!=typeof n.ostatus&&(n.ostatus=!1),"boolean"!=typeof n.instance&&(n.instance=!1);var a=n.vlabel||"Value",r=n.slabel||"Status",l=n.type||"array",i=n.label||"",s=n.header||null,u=[],c=[],d=[],f=[],h={},v={},b=function(e,t){for(var n=1,a=0,r=0;r<E;r++)a+=(n*=r>0?t[E-r]:1)*e[E-r-1];return a},p=function(){var e=t[S][a];d[b(x,c)]=isNaN(e)?null:e};switch(l){case"array":t=function(e){for(var t=e[0],n=e.slice(1),a=[],r=0,l=n.length;r<l;r++){for(var i=0,o=t.length,s={};i<o;i++)s[t[i]]=n[r][i];a.push(s)}return a}(t);break;case"object":t=function(e){for(var t=e.cols.map((function(e){return e.id})),n=e.rows,a=[],r=0,l=n.length;r<l;r++){for(var i=0,o=t.length,s={};i<o;i++)s[t[i]]=n[r].c[i].v;a.push(s)}return a}(t)}var g,m=t.length;for(var y in n.hasOwnProperty("drop")&&Array.isArray(n.drop)&&n.drop.length&&t.forEach((function(e){n.drop.forEach((function(t){delete e[t]}))})),t[0])if(y!==a)if(y!==r){if(u.push(y),s)g=s.dimension[y],h[y]=g.category.index;else{h[y]=[];for(var w=0;w<m;w++){var O=t[w][y];-1===h[y].indexOf(O)&&h[y].push(O)}}c.push(h[y].length),v[y]={label:s?g.label:y,category:{index:h[y]}},s&&(v[y].category.label=g.category.label,g.category.unit&&(v[y].category.unit=g.category.unit))}else p=function(){var e=t[S][a],n=t[S][r];d[b(x,c)]=isNaN(e)?null:e,f[b(x,c)]=""===n?null:n};for(var E=u.length,S=0;S<m;S++){for(var x=[],A=0;A<E;A++){var D=u[A];x.push(h[D].indexOf(t[S][D]))}p()}var P={version:"2.0",class:"dataset",value:d,dimension:v,id:u,size:c};return i&&(P.label=i),f.length&&(P.status=f),s&&(s.label&&(P.label=s.label),s.source&&(P.source=s.source),s.updated&&(P.updated=s.updated),s.href&&(P.href=s.href),s.role&&(P.role=s.role)),n.ovalue&&(P.value=o(P,"value")),n.ostatus&&P.hasOwnProperty("status")&&(P.status=o(P,"status")),n.instance?e(P):P}function u(e,t){if(void 0===e)return null;void 0===t&&(t={});var n,a,r,l=[],i=null,o=null,u=!1,c={time:[],geo:[],metric:[]},d="jsonstat"===e.substring(0,8),f=d?"value":t.vlabel,h=d?"status":t.slabel,v=d?e.substring(8,9):t.delimiter||",",b=";"===v?t.decimal||",":t.decimal||".",p=function(e,t){t=t||",";for(var n,a,r=new RegExp("(\\"+t+'|\\r?\\n|\\r|^)(?:"([^"]*(?:""[^"]*)*)"|([^"\\'+t+"\\r\\n]*))","gi"),l=[[]],i=null;i=r.exec(e);)(a=i[1]).length&&a!=t&&l.push([]),n=i[2]?i[2].replace(new RegExp('""',"g"),'"'):i[3],l[l.length-1].push(n);return l}(e.trim(),v);if(d){for(b=p[0][1],r=p[0][2],p.shift();"data"!==p[0][0];)l.push(p.shift());p.shift(),o={dimension:{}},l.forEach((function(e){var t,n,a,l,i,s,d,f;switch(e[0]){case"dimension":if(o.dimension[e[1]]={},(l=o.dimension[e[1]]).label=e[2],l.category={},(i=l.category).index=[],n={},a=2*e[3]+3,e.length>=a){for(t=4;t<a;t++)d=e[t],f=e[++t],Object.defineProperty(n,d,{value:f,writable:!0,configurable:!0,enumerable:!0}),i.label=n,i.index.push(d);"string"==typeof e[t]&&-1!==["time","geo","metric"].indexOf(e[t])&&(c[e[t]].push(e[1]),u=!0,"metric"===e[t]&&"string"==typeof e[++t]&&(i.unit={},i.index.forEach((function(n,a){var l=e[t+a].split(r);i.unit[n]={},s=i.unit[n],void 0!==l[0]&&""!==l[0]&&(s.decimals=1*l[0]),void 0!==l[1]&&""!==l[1]&&(s.label=l[1]),void 0!==l[2]&&""!==l[2]&&(s.symbol=l[2]),void 0!==l[1]&&-1!==["start","end"].indexOf(l[3])&&(s.position=l[3])}))))}break;case"label":case"source":case"updated":case"href":o[e[0]]=e[1]||null}})),u&&(c.time.length||delete c.time,c.geo.length||delete c.geo,c.metric.length||delete c.metric,o.role=c)}if(n=p.length,a=p[0].length,void 0!==f){for(;a--;)if(p[0][a]===f){i=a;break}if(null===i)return null}else i=a-1,f=p[0][i];if(","===b)for(a=1;a<n;a++)p[a][i]=Number(p[a][i].replace(",","."));else for(a=1;a<n;a++)p[a][i]=Number(p[a][i]);return s(p,{header:o,vlabel:f,slabel:h,type:"array",label:t.label||"",ovalue:t.ovalue||!1,ostatus:t.ostatus||!1,instance:t.instance||!1})}function c(t,n){if("object"!=typeof t)return null;if(t.hasOwnProperty("data")&&t.hasOwnProperty("meta")&&t.data.hasOwnProperty("dataSets")&&t.data.hasOwnProperty("structures")&&Array.isArray(t.data.structures)&&(t.dataSets=t.data.dataSets,t.structure=t.data.structures[0],t.header=t.meta),!t.hasOwnProperty("dataSets")||!Array.isArray(t.dataSets))return null;if(1!==t.dataSets.length)return null;t.dataSets[0].hasOwnProperty("observations")||function(e){var t=e.dataSets[0],n=t.series,a=e.structure,r=a.dimensions,l={};Object.keys(n).forEach((function(e){var t=n[e].observations;Object.keys(t).forEach((function(a){l[e+":"+a]=t[a].concat(n[e].attributes)}))})),t.observations=l,delete t.series,r.observation=r.series.concat(r.observation),delete r.series,a.attributes.observation=a.attributes.observation.concat(a.attributes.series),delete a.attributes.series}(t),void 0===n?n={ovalue:!1,ostatus:!1,instance:!1}:("boolean"!=typeof n.ovalue&&(n.ovalue=!1),"boolean"!=typeof n.ostatus&&(n.ostatus=!1),"boolean"!=typeof n.instance&&(n.instance=!1));var a=t.structure,r=t.dataSets[0].observations,l=a.attributes.observation,i=a.dimensions;if(!i.hasOwnProperty("observation"))return null;if(i.hasOwnProperty("series")&&Object.keys(i.series).length)return null;var o=1,s=[],u=[],c={},d=[],f={time:[],geo:[]},h=function(){},v=function(e,t){for(var n=e.size,a=n.length-t.length;a--;)t.push(0);for(var r=0,l=n.length,i=0,o=1;r<l;r++)i+=(o*=r>0?n[l-r]:1)*t[l-r-1];return i},b=function(e){if(c[e.id]={label:e.name},e.hasOwnProperty("role"))switch(e.role){case"REF_AREA":f.geo.push(e.id);break;case"TIME_PERIOD":f.time.push(e.id)}Object.defineProperty(c[e.id],"category",{value:{index:[],label:{}},writable:!0,enumerable:!0}),s.push(e.id),u.push(e.values.length),o*=e.values.length;var t=c[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})}))},p=t.header.links?t.header.links.find((function(e){return"request"===e.rel})):null,g=l.findIndex((function(e){return"OBS_STATUS"===e.id}));-1!==g&&(l[g].values.length?d=l[g].values:g=-1),i.observation.forEach(b),i.hasOwnProperty("dataSet")&&i.dataSet.forEach(b);var m={version:"2.0",class:"dataset",updated:t.header.prepared||null,source:t.header.sender.name||null,label:a.name||null,id:s,size:u,dimension:c,value:n.ovalue?{}:new Array(o).fill(null)};for(var y in p&&(m.link={alternate:[{type:"application/vnd.sdmx.data+json",href:p.href}]}),f.geo.length+f.time.length>0&&(f.time.length||delete f.time,f.geo.length||delete f.geo,m.role=f),-1!==g&&(m.status=n.ostatus?{}:new Array(o).fill(null),m.extension={status:{label:{}}},d.forEach((function(e){m.extension.status.label[e.id]=e.name})),h=n.ostatus?function(){var e=r[y][g];null!==e&&(m.status[v(m,w)]=d[e].id)}:function(){var e=r[y][g];m.status[v(m,w)]=null===e?null:d[e].id}),g++,r){var w=y.split(":");n.ovalue&&null===r[y][0]||(m.value[v(m,w)]=r[y][0]),h()}return n.instance?e(m):m}function d(t,n){if(void 0===t||!Array.isArray(t))return null;var a=JSON.parse(JSON.stringify(t)),r=a[0];if(!r.hasOwnProperty("version")||!r.hasOwnProperty("class")||"dataset"!==r.class)return null;void 0===n&&(n={});var l=void 0===n.label?null:n.label,i=void 0===n.by?null:n.by,o=[];if(null===i){for(var u=1,c=a.length;u<c;u++)o=o.concat(a[u].value);return r.value=o,null!==l&&(r.label=l),r}var d,f,h,v=function(e,t,n){if(Array.isArray(e))e=e.concat(t);else for(var a in t)e[a]=0===t[a]?n:t[a];return e};a.forEach((function(t,n){var a=e(t).toTable({status:!0}),r=t.dimension[i].category;0===n?(o=[a[0]],d=r.index,f=r.label,h=r.unit):(d=function(e,t,n){if(Array.isArray(e))e=e.concat(t);else for(var a in t)e[a]=t[a]+n;return e}(d,r.index,Object.keys(d).length),f=v(f,r.label,n),h=v(h,r.unit,n)),o=o.concat(a.slice(1))}));var b=s(o);return r.value=b.value,r.size=b.size,r.status=b.status||null,r.label=l||"",r.href=null,r.dimension[i].category.index=d||null,r.dimension[i].category.label=f||null,r.dimension[i].category.unit=h||null,r}function f(e,t){return null==e?"":-1!==e.indexOf(t)?'"'+e+'"':e}function h(e,a){if(void 0===e)return null;void 0===a&&(a={});var r={content:!1,field:!1},l=!0===a.rich,i=a.content||"label",o=a.field||"label",s=l?"value":a.vlabel||"Value",u=l?"status":a.slabel||"Status",c=!0===a.status,d=a.na||"n/a",h=a.delimiter||",",v=a.separator||"|",b=";"===h?a.decimal||",":a.decimal||".",p=!0===a.array,g=t(e,a.dsid||0),m=p?[]:"",y=p?[]:"",w=p?function(e){m.push(e)}:function(e){m+=e+"\n"},O=p?function(e){y.push(e)}:function(e){y+=e+"\n"};if(!n(g))return null;l?c=null!==g.status:("[id] label"===i&&(r.content=!0,i="id"),"[id] label"===o&&(r.field=!0,o="id"));var E=g.toTable({vlabel:s,slabel:u,status:c,field:l||r.field?"id":o,content:l||r.content?"id":i,type:"array"}),S=E[0].indexOf("id"===o?"value":s),x=c?E[0].indexOf("id"===o?"status":u):-1;return E.forEach((function(e,t){e.forEach((function(n,a){var l=g.Dimension(a);t&&a===S?null===n?e[a]=f(d,h):"."!==b&&(e[a]=String(e[a]).replace(".",b)):t&&a===x&&null===n?e[a]="":(r.content&&t&&l&&(e[a]="["+e[a]+"] "+l.Category(e[a]).label),r.field&&0===t&&(e[a]=l?"["+e[a]+"] "+l.label:"value"===e[a]?s:u),e[a]=f(e[a],h))})),w(e.join(h))})),l&&(O("jsonstat"+h+b+h+v),["label","source","updated","href"].forEach((function(e){g[e]&&O(e+h+f(g[e],h))})),g.id.forEach((function(e,t){var n=[],a=g.Dimension(t),r=a.role,l=!1,i="";i+="dimension"+h+f(e,h)+h+f(a.label,h)+h+a.length,"metric"===r&&a.__tree__.category.unit&&(l=!0),a.id.forEach((function(e,t){var r=[],o=a.Category(t);i+=h+f(e,h)+h+f(o.label,h),l&&(r.push(o.unit.hasOwnProperty("decimals")?o.unit.decimals:""),r.push(o.unit.label||""),o.unit.symbol&&(r.push(o.unit.symbol),r.push(o.unit.position)),n.push(f(r.join(v),h)))})),null!==r&&"classification"!==r&&(i+=h+a.role,l&&(i+=h+n.join(h))),O(i)})),m=p?y.concat(["data"],m):y+"data\n"+m),m}String.prototype.capitalize=function(){return this.charAt(0).toUpperCase()+this.slice(1)};var v="3.4.1";export{i as datalist,u as fromCSV,c as fromSDMX,s as fromTable,d as join,l as tbrowser,h as toCSV,v as version};
|
|
1
|
+
// jsonstat-suite v4.0.0 Copyright 2026 Xavier Badosa https://jsonstat.com
|
|
2
|
+
import e from"jsonstat-toolkit";function t(t,n){return null==t?null:("string"!=typeof t&&void 0!==t.length||(t=e(t)),0===t.length||"dataset"!==t.class&&"collection"!==t.class&&"bundle"!==t.class?null:(null===t.value&&(t.value=Array(t.n).fill(null)),"dataset"===t.class?t:t.Dataset(n)))}function n(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 l(e,t,n){return(t.label||n).capitalize()+function(e,t){let n,l,o="";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!==""&&(o=""===l?n:""===n?l:"start"===t.unit.position?l+n:n+" "+l,o=" ("+o+")")),o}(e,t)}function o(e,t,n){let o,i,r='<select name="'+t+'">',a=[];if(null!==n[1]){if(o=e.id,a=e.Dimension(),2===o.length)return(e.Dimension(n[0]).label||n[0]).capitalize()}else if(i=e.Dimension(t),o=i.id,a=i.Category(),1===o.length)return;return o.forEach(function(e,t){const o=e!==n[0]?"":'selected="selected" ';null!==n[1]&&e===n[1]||(r+="<option "+o+'value="'+e+'">'+l(i,a[t],e)+"</option>")}),r+="</select>",r}function i(e,i,r){function a(e){void 0!==i?i.innerHTML=s[e]:window.alert(s[e])}if(void 0===e)return void a("urierror");if(void 0===i)return void a("selerror");void 0===r&&(r={});const s=void 0===r.i18n||void 0===r.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"}:r.i18n.msgs,u=void 0===r.i18n||void 0===r.i18n.locale?"en-US":r.i18n.locale,c=r.dsid||0,d=r.status||!1,f=r.tblclass||"",h=r.nonconst||!1,b=t(e,c);if(!n(b))return void a("jsonerror");let p;h&&(p=function(e){let t=0;const n=e.size.slice(0),l=[];return n.forEach(function(n,o){const i=o-t,r=e.Dimension(i);1===n&&(delete e.__tree__.dimension[e.id[i]],e.size.splice(i,1),e.id.splice(i,1),e.length--,t++,l.push(r.label.capitalize()+": "+r.Category(0).label.capitalize()))}),l}(b)),1!==b.length?function e(t,n,i,a){"function"==typeof r.callback&&r.callback(i);let c="",f="",b="",m="",g="",y="",v=null!==n.label?'<span class="label">'+n.label.capitalize()+"</span>":"",w=n.source?s.source+": "+n.source:"";const O=i.rows,E=n.Dimension(O),S=E.id,x=i.cols,A=n.Dimension(x),D=A.id,j=n.role&&n.role.metric?n.role.metric[0]:null,P=null!==j?n.Dimension(j):null,k=function(e){return e.hasOwnProperty("unit")&&e.unit&&e.unit.hasOwnProperty("decimals")?e.unit.decimals:null},z=i.filters,C=JSON.parse(JSON.stringify(z)),N=[];h&&p.length&&(v='<span class="label">'+p.join(". ")+"</span>"),""!==w&&"."!==w.slice(-1)&&(w+="."),b+="<caption>"+v+"<form>";for(const e in z){const t=n.Dimension(e),i=t.label?t.label.capitalize():e.capitalize();t.length>1?g+="<p>"+o(n,e,[z[e],null])+" <strong>"+i+"</strong></p>":N.push({label:i,value:l(t,t.Category(0)),name:e,id:t.id[0]})}""!==g&&(g='<fieldset id="filters"><legend>'+s.filters+"</legend>"+g+"</fieldset>"),N.forEach(function(e){y+="<p>"+e.value+" <strong>"+e.label+'</strong></p><input type="hidden" name="'+e.name+'" value="'+e.id+'" />'}),""!==y&&(y='<fieldset id="constants"><legend>'+s.constants+"</legend>"+y+"</fieldset>"),b+=y+g+'<fieldset id="rowscols"><legend>'+s.rc+"</legend>"+o(n,"rows",[O,x])+" <a>↔</a> "+o(n,"cols",[x,O])+"</fieldset></form></caption>",m+="<tbody>";const R=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(S.forEach(function(e){C[O]=e;const t=n.Data(C),o=function(e,t){let n;const l=x!==j?null===P?null:k(P.Category(C[j])):k(A.Category(t));null!==e.value?(n=R(e.value,l),d&&null!==e.status&&(n+=" ("+e.status+")")):n=e.status||s.na,m+="<td>"+n+"</td>"};null!==t?(m+='<tr><th scope="row">'+l(E,E.Category(e))+"</th>",Array.isArray(t)?t.forEach(function(e,t){o(e,t)}):o(t,0),m+="</tr>"):m="ERROR"}),"ERROR"===m)return s.dataerror;m+="</tbody>",c+="<thead><tr><th></th>",D.forEach(function(e){c+='<th scope="col">'+l(A,A.Category(e))+"</th>"}),c+="</tr></thead>",""!==w&&(f='<tfoot><tr><td colspan="'+(D.length+1)+'">'+w+"</td></tr></tfoot>"),t.innerHTML='<table class="'+a+'">'+b+c+m+f+"</table>",[].slice.call(t.querySelectorAll("select")).forEach(function(l){l.addEventListener("change",function(l){e(t,n,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}(n,l.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)),a)},!1)}),t.querySelector("a").addEventListener("click",function(){i.cols=O,i.rows=x,e(t,n,i,a)},!1)}(i,b,function(e,t){if("object"==typeof t)return{rows:t.rows,cols:t.cols,filters:t.filters};const n={},l=[],o=e.id;let i,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:o[t],len:e.length})}),l.sort(n),i=l[0].id,r=l[1].id}else i=o[0],r=o[1];return e.Dimension(i).length<e.Dimension(r).length&&(i=r+(r=i,"")),o.forEach(function(t){t!==i&&t!==r&&(n[t]=e.Dimension(t).id[0])}),{rows:i,cols:r,filters:n}}(b,r.preset),f):a("dimerror")}function r(e,l){if(void 0===e)return null;void 0===l&&(l={});let o="",i="",r="",a=0,s=l.na||"n/a",u=l.dsid||0,c=l.vlabel||null,d=l.slabel||null,f=l.counter||!1,h=l.tblclass||"",b=l.numclass||"",p=l.valclass||"",m=l.status||!1,g=l.locale||"en-US",y=l.source||"Source",v=t(e,u),w=v.role&&v.role.metric?v.id.indexOf(v.role.metric[0]):null,O=v.Dimension({role:"metric"}),E={},S=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)},x=function(e,t){let n=null;e.forEach(function(e,l){const r=j===l?' class="'+b+" "+p+'"':"";let a=s;t?(w===l&&(n=E[e]),null!==e&&(a=j===l?S(e,n):e),i+="<td"+r+">"+a+"</td>"):o+="<th"+r+">"+e+"</th>"})},A=f?function(e,t){t?i+='<tr><td class="'+b+'">'+t+"</td>":o+='<th class="'+b+'">#</th>',x(e,t),i+="</tr>"}:function(e,t){i+="<tr>",x(e,t),i+="</tr>"};if(!n(v))return null;O&&O[0].Category().forEach(function(e){const t=e.unit&&e.unit.hasOwnProperty("decimals")?e.unit.decimals:null;E[e.label]=t}),console.log(v),console.log(v.Transform({status:m,vlabel:c,slabel:d}));const D=v.Transform({status:m,vlabel:c,slabel:d}),j=D[0].length-1;return D.forEach(function(e,t){A(e,t)}),v.source&&(a=v.length+1,f&&a++,m&&a++,y+=": "+v.source,"."!==y.slice(-1)&&(y+="."),r='<tfoot><td colspan="'+a+'">'+y+"</td></tfoot>"),'<table class="'+h+'"><caption>'+(l.caption||v.label||"")+"</caption><thead><tr>"+o+"</tr></thead><tbody>"+i+"</tbody>"+r+"</table>"}function a(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 s(t,n){if(void 0===t)return null;void 0===n&&(n={}),"boolean"!=typeof n.ovalue&&(n.ovalue=!1),"boolean"!=typeof n.ostatus&&(n.ostatus=!1),"boolean"!=typeof n.instance&&(n.instance=!1);const l=n.vlabel||"Value",o=n.slabel||"Status",i=n.type||"array",r=n.label||"",s=n.header||null,u=[],c=[],d=[],f=[],h={},b={},p=function(e,t){let n=1,l=0;for(let o=0;o<v;o++)n*=o>0?t[v-o]:1,l+=n*e[v-o-1];return l};let m,g=function(e,n){const o=t[e][l];d[p(n,c)]=isNaN(o)?null:o};switch(i){case"array":t=function(e){const t=e[0],n=e.slice(1),l=[];for(let e=0,o=n.length;e<o;e++){let o={};for(let l=0,i=t.length;l<i;l++)o[t[l]]=n[e][l];l.push(o)}return l}(t);break;case"object":t=function(e){const t=e.cols.map(function(e){return e.id}),n=e.rows,l=[];for(let e=0,o=n.length;e<o;e++){let o={};for(let l=0,i=t.length;l<i;l++)o[t[l]]=n[e].c[l].v;l.push(o)}return l}(t)}const y=t.length;n.hasOwnProperty("drop")&&Array.isArray(n.drop)&&n.drop.length&&t.forEach(function(e){n.drop.forEach(function(t){delete e[t]})});for(const e in t[0])if(e!==l)if(e!==o){if(u.push(e),s)m=s.dimension[e],h[e]=m.category.index;else{h[e]=[];for(let n=0;n<y;n++){const l=t[n][e];-1===h[e].indexOf(l)&&h[e].push(l)}}c.push(h[e].length),b[e]={label:s?m.label:e,category:{index:h[e]}},s&&(b[e].category.label=m.category.label,m.category.unit&&(b[e].category.unit=m.category.unit))}else g=function(e,n){const i=t[e][l],r=t[e][o];d[p(n,c)]=isNaN(i)?null:i,f[p(n,c)]=""===r?null:r};const v=u.length;for(let e=0;e<y;e++){const n=[];for(let l=0;l<v;l++){const o=u[l];n.push(h[o].indexOf(t[e][o]))}g(e,n)}const w={version:"2.0",class:"dataset",value:d,dimension:b,id:u,size:c};return r&&(w.label=r),f.length&&(w.status=f),s&&(s.label&&(w.label=s.label),s.source&&(w.source=s.source),s.updated&&(w.updated=s.updated),s.href&&(w.href=s.href),s.role&&(w.role=s.role)),n.ovalue&&(w.value=a(w,"value")),n.ostatus&&w.hasOwnProperty("status")&&(w.status=a(w,"status")),n.instance?e(w):w}function u(e,t){if(void 0===e)return null;void 0===t&&(t={});const n="jsonstat"===e.substring(0,8),l={time:[],geo:[],metric:[]},o=n?e.substring(8,9):t.delimiter||",";let i,r,a,u=[],c=null,d=null,f=!1,h=n?"value":t.vlabel,b=n?"status":t.slabel,p=";"===o?t.decimal||",":t.decimal||".";const m=function(e,t){t=t||",";const n=new RegExp("(\\"+t+'|\\r?\\n|\\r|^)(?:"([^"]*(?:""[^"]*)*)"|([^"\\'+t+"\\r\\n]*))","gi"),l=[[]];let o,i,r=null;for(;r=n.exec(e);)i=r[1],i.length&&i!=t&&l.push([]),o=r[2]?r[2].replace(new RegExp('""',"g"),'"'):r[3],l[l.length-1].push(o);return l}(e.trim(),o);if(n){for(p=m[0][1],a=m[0][2],m.shift();"data"!==m[0][0];)u.push(m.shift());m.shift(),d={dimension:{}},u.forEach(function(e){let t,n,o,i,r,s,u,c;switch(e[0]){case"dimension":if(d.dimension[e[1]]={},i=d.dimension[e[1]],i.label=e[2],i.category={},r=i.category,r.index=[],n={},o=2*e[3]+3,e.length>=o){for(t=4;t<o;t++)u=e[t],c=e[++t],Object.defineProperty(n,u,{value:c,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 o=e[t+l].split(a);r.unit[n]={},s=r.unit[n],void 0!==o[0]&&""!==o[0]&&(s.decimals=1*o[0]),void 0!==o[1]&&""!==o[1]&&(s.label=o[1]),void 0!==o[2]&&""!==o[2]&&(s.symbol=o[2]),void 0!==o[1]&&-1!==["start","end"].indexOf(o[3])&&(s.position=o[3])})))}break;case"label":case"source":case"updated":case"href":d[e[0]]=e[1]||null}}),f&&(l.time.length||delete l.time,l.geo.length||delete l.geo,l.metric.length||delete l.metric,d.role=l)}if(i=m.length,r=m[0].length,void 0!==h){for(;r--;)if(m[0][r]===h){c=r;break}if(null===c)return null}else c=r-1,h=m[0][c];if(","===p)for(r=1;r<i;r++)m[r][c]=Number(m[r][c].replace(",","."));else for(r=1;r<i;r++)m[r][c]=Number(m[r][c]);return s(m,{header:d,vlabel:h,slabel:b,type:"array",label:t.label||"",ovalue:t.ovalue||!1,ostatus:t.ostatus||!1,instance:t.instance||!1})}function c(t,n){if("object"!=typeof t)return null;if(t.hasOwnProperty("data")&&t.hasOwnProperty("meta")&&t.data.hasOwnProperty("dataSets")&&t.data.hasOwnProperty("structures")&&Array.isArray(t.data.structures)&&(t.dataSets=t.data.dataSets,t.structure=t.data.structures[0],t.header=t.meta),!t.hasOwnProperty("dataSets")||!Array.isArray(t.dataSets))return null;if(1!==t.dataSets.length)return null;t.dataSets[0].hasOwnProperty("observations")||function(e){const t=e.dataSets[0],n=t.series,l=e.structure,o=l.dimensions,i={};Object.keys(n).forEach(function(e){const t=n[e].observations;Object.keys(t).forEach(function(l){i[e+":"+l]=t[l].concat(n[e].attributes)})}),t.observations=i,delete t.series,o.observation=o.series.concat(o.observation),delete o.series,l.attributes.observation=l.attributes.observation.concat(l.attributes.series),delete l.attributes.series}(t),void 0===n?n={ovalue:!1,ostatus:!1,instance:!1}:("boolean"!=typeof n.ovalue&&(n.ovalue=!1),"boolean"!=typeof n.ostatus&&(n.ostatus=!1),"boolean"!=typeof n.instance&&(n.instance=!1));const l=t.structure,o=t.dataSets[0].observations,i=l.attributes.observation,r=l.dimensions;if(!r.hasOwnProperty("observation"))return null;if(r.hasOwnProperty("series")&&Object.keys(r.series).length)return null;let a=1,s=[],u=function(){},c=i.findIndex(function(e){return"OBS_STATUS"===e.id});const d=[],f=[],h={},b={time:[],geo:[]},p=function(e,t){const n=e.size;for(let e=n.length-t.length;e--;)t.push(0);let l=0,o=1;for(let e=0,i=n.length;e<i;e++)o*=e>0?n[i-e]:1,l+=o*t[i-e-1];return l},m=function(e){if(h[e.id]={label:e.name},e.hasOwnProperty("role"))switch(e.role){case"REF_AREA":b.geo.push(e.id);break;case"TIME_PERIOD":b.time.push(e.id)}Object.defineProperty(h[e.id],"category",{value:{index:[],label:{}},writable:!0,enumerable:!0}),d.push(e.id),f.push(e.values.length),a*=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=t.header.links?t.header.links.find(function(e){return"request"===e.rel}):null;-1!==c&&(i[c].values.length?s=i[c].values:c=-1),r.observation.forEach(m),r.hasOwnProperty("dataSet")&&r.dataSet.forEach(m);const y={version:"2.0",class:"dataset",updated:t.header.prepared||null,source:t.header.sender.name||null,label:l.name||null,id:d,size:f,dimension:h,value:n.ovalue?{}:new Array(a).fill(null)};g&&(y.link={alternate:[{type:"application/vnd.sdmx.data+json",href:g.href}]}),b.geo.length+b.time.length>0&&(b.time.length||delete b.time,b.geo.length||delete b.geo,y.role=b),-1!==c&&(y.status=n.ostatus?{}:new Array(a).fill(null),y.extension={status:{label:{}}},s.forEach(function(e){y.extension.status.label[e.id]=e.name}),u=n.ostatus?function(e,t){const n=o[e][c];null!==n&&(y.status[p(y,t)]=s[n].id)}:function(e,t){const n=o[e][c];y.status[p(y,t)]=null===n?null:s[n].id}),c++;for(let e in o){const t=e.split(":");n.ovalue&&null===o[e][0]||(y.value[p(y,t)]=o[e][0]),u(e,t)}return n.instance?e(y):y}function d(t,n){if(void 0===t||!Array.isArray(t))return null;const l=JSON.parse(JSON.stringify(t)),o=l[0];if(!o.hasOwnProperty("version")||!o.hasOwnProperty("class")||"dataset"!==o.class)return null;void 0===n&&(n={});const i=void 0===n.label?null:n.label,r=void 0===n.by?null:n.by;let a,u,c,d=[];if(null===r){for(let e=1,t=l.length;e<t;e++)d=d.concat(l[e].value);return o.value=d,null!==i&&(o.label=i),o}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};l.forEach(function(t,n){const l=e(t).Transform({status:!0}),o=t.dimension[r].category;0===n?(d=[l[0]],a=o.index,u=o.label,c=o.unit):(a=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}(a,o.index,Object.keys(a).length),u=f(u,o.label,n),c=f(c,o.unit,n)),d=d.concat(l.slice(1))});const h=s(d);return o.value=h.value,o.size=h.size,o.status=h.status||null,o.label=i||"",o.href=null,o.dimension[r].category.index=a||null,o.dimension[r].category.label=u||null,o.dimension[r].category.unit=c||null,o}function f(e,t){return null==e?"":-1!==e.indexOf(t)?'"'+e+'"':e}function h(e,l){if(void 0===e)return null;void 0===l&&(l={});const o={content:!1,field:!1},i=!0===l.rich,r=l.na||"n/a",a=l.delimiter||",",s=l.separator||"|",u=";"===a?l.decimal||",":l.decimal||".",c=!0===l.array,d=t(e,l.dsid||0),h=c?function(e){w.push(e)}:function(e){w+=e+"\n"};let b=l.content||"label",p=l.field||"label",m=i?"value":l.vlabel||"Value",g=i?"status":l.slabel||"Status",y=!0===l.status,v=c?[]:"",w=c?[]:"",O=c?function(e){v.push(e)}:function(e){v+=e+"\n"};if(!n(d))return null;i?y=null!==d.status:("[id] label"===b&&(o.content=!0,b="id"),"[id] label"===p&&(o.field=!0,p="id"));const E=!i&&l.by&&-1!==d.id.indexOf(l.by)?l.by:null,S=E&&void 0!==l.drop&&Array.isArray(l.drop)?l.drop:null;if(E)return function(e,t){const n=[],l=t.by,o=t.idlabel,i=t.drop,r=t.na,a=t.delimiter,s=t.decimal,u=o.content?"id":t.content,c=o.field?"label":t.field,d=e.Transform({drop:i,content:u,by:l,type:"arrobj"}),h=Object.keys(d[0]),b=function(e,t,n,l,i){return e.map(e=>"label"===n?-1!==l.id.indexOf(e)?f((o.field?"["+e+"] ":"")+l.Dimension(e).label,t):f((o.field?"["+e+"] ":"")+l.Dimension(i).Category(e).label,t):f(e,t)).join(t)+"\n"}(h,a,c,e,l),p="."!==s?function(e){return null===e?f(r,a):String(e).replace(".",s)}:function(e){return null===e?f(r,a):e};return d.forEach(t=>{let l=[];h.forEach(n=>{const i=t[n],r="string"==typeof i?f(o.content?"["+i+"] "+e.Dimension(n).Category(i).label:i,a):p(i);l.push(r)}),n.push(l.join(a))}),b+n.join("\n")}(d,{drop:S,na:r,delimiter:a,decimal:u,content:b,field:p,by:E,idlabel:o});const x=d.Transform({vlabel:m,slabel:g,status:y,field:i||o.field?"id":p,content:i||o.content?"id":b,type:"array"}),A=x[0].indexOf("id"===p?"value":m),D=y?x[0].indexOf("id"===p?"status":g):-1;return x.forEach(function(e,t){e.forEach(function(n,l){const i=d.Dimension(l);t&&l===A?null===n?e[l]=f(r,a):"."!==u&&(e[l]=String(e[l]).replace(".",u)):t&&l===D&&null===n?e[l]="":(o.content&&t&&i?e[l]="["+e[l]+"] "+i.Category(e[l]).label:o.field&&0===t&&(e[l]=i?"["+e[l]+"] "+i.label:"value"===e[l]?m:g),e[l]=f(e[l],a))}),O(e.join(a))}),i&&(h("jsonstat"+a+u+a+s),["label","source","updated","href"].forEach(function(e){d[e]&&h(e+a+f(d[e],a))}),d.id.forEach(function(e,t){const n=[],l=d.Dimension(t),o=l.role;let i=!1,r="";r+="dimension"+a+f(e,a)+a+f(l.label,a)+a+l.length,"metric"===o&&l.__tree__.category.unit&&(i=!0),l.id.forEach(function(e,t){const o=[],u=l.Category(t);r+=a+f(e,a)+a+f(u.label,a),i&&(o.push(u.unit.hasOwnProperty("decimals")?u.unit.decimals:""),o.push(u.unit.label||""),u.unit.symbol&&(o.push(u.unit.symbol),o.push(u.unit.position)),n.push(f(o.join(s),a)))}),null!==o&&"classification"!==o&&(r+=a+l.role,i&&(r+=a+n.join(a))),h(r)}),v=c?w.concat(["data"],v):w+"data\n"+v),v}String.prototype.capitalize=function(){return this.charAt(0).toUpperCase()+this.slice(1)};var b="4.0.0";export{r as datalist,u as fromCSV,c as fromSDMX,s as fromTable,d as join,i as tbrowser,h as toCSV,b as version};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jsonstat-suite",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "A library of high level functions built on top of the JSON-stat
|
|
3
|
+
"version": "4.0.0",
|
|
4
|
+
"description": "A library of high level functions built on top of the JSON-stat JavaScript Toolkit (JJT)",
|
|
5
5
|
"homepage": "https://jsonstat.com",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"util",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
},
|
|
25
25
|
"repository": {
|
|
26
26
|
"type": "git",
|
|
27
|
-
"url": "https://github.com/jsonstat/suite.git"
|
|
27
|
+
"url": "git+https://github.com/jsonstat/suite.git"
|
|
28
28
|
},
|
|
29
29
|
"main": "main.cjs",
|
|
30
30
|
"module": "module.mjs",
|
|
@@ -36,14 +36,13 @@
|
|
|
36
36
|
"test": "node test/nodejs.js"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"jsonstat-toolkit": "^
|
|
39
|
+
"jsonstat-toolkit": "^2.2.2"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@rollup/plugin-json": "^6.
|
|
42
|
+
"@rollup/plugin-json": "^6.1.0",
|
|
43
43
|
"@rollup/plugin-node-resolve": "^15.0.1",
|
|
44
|
-
"@rollup/plugin-
|
|
45
|
-
"rollup": "^
|
|
46
|
-
"@rollup/plugin-terser": "^0.4.0"
|
|
44
|
+
"@rollup/plugin-terser": "^1.0.0",
|
|
45
|
+
"rollup": "^4.59.0"
|
|
47
46
|
},
|
|
48
47
|
"license": "Apache-2.0",
|
|
49
48
|
"files": [
|
package/rollup.config.mjs
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import json from '@rollup/plugin-json';
|
|
2
|
-
import replace from '@rollup/plugin-replace';
|
|
3
2
|
import terser from "@rollup/plugin-terser";
|
|
4
3
|
import resolve from '@rollup/plugin-node-resolve';
|
|
5
4
|
import { readFileSync } from "fs";
|
|
@@ -30,10 +29,6 @@ export default [
|
|
|
30
29
|
}
|
|
31
30
|
],
|
|
32
31
|
plugins: [
|
|
33
|
-
replace({
|
|
34
|
-
preventAssignment: true,
|
|
35
|
-
__IE__: 'Array.prototype.reduce||Object.defineProperty(Array.prototype,"reduce",{value:function(r){if(null===this)throw new TypeError("Array.prototype.reduce called on null or undefined");if("function"!=typeof r)throw new TypeError(r+" is not a function");var e,t=Object(this),n=t.length>>>0,o=0;if(arguments.length>=2)e=arguments[1];else{for(;o<n&&!(o in t);)o++;if(o>=n)throw new TypeError("Reduce of empty array with no initial value");e=t[o++]}for(;o<n;)o in t&&(e=r(e,t[o],o,t)),o++;return e}}),Array.prototype.find||Object.defineProperty(Array.prototype,"find",{value:function(r){if(null==this)throw TypeError(\'"this" is null or not defined\');var e=Object(this),t=e.length>>>0;if("function"!=typeof r)throw TypeError("predicate must be a function");for(var n=arguments[1],o=0;o<t;){var i=e[o];if(r.call(n,i,o,e))return i;o++}},configurable:!0,writable:!0}),Array.prototype.findIndex||Object.defineProperty(Array.prototype,"findIndex",{value:function(r){if(null==this)throw new TypeError(\'"this" is null or not defined\');var e=Object(this),t=e.length>>>0;if("function"!=typeof r)throw new TypeError("predicate must be a function");for(var n=arguments[1],o=0;o<t;){var i=e[o];if(r.call(n,i,o,e))return o;o++}return-1},configurable:!0,writable:!0});'
|
|
36
|
-
}),
|
|
37
32
|
...plugins
|
|
38
33
|
]
|
|
39
34
|
},
|
|
@@ -53,10 +48,6 @@ export default [
|
|
|
53
48
|
},
|
|
54
49
|
],
|
|
55
50
|
plugins: [
|
|
56
|
-
replace({
|
|
57
|
-
preventAssignment: true,
|
|
58
|
-
__IE__: ''
|
|
59
|
-
}),
|
|
60
51
|
...plugins
|
|
61
52
|
]
|
|
62
53
|
},
|
|
@@ -67,10 +58,6 @@ export default [
|
|
|
67
58
|
format: "esm",
|
|
68
59
|
},
|
|
69
60
|
plugins: [
|
|
70
|
-
replace({
|
|
71
|
-
preventAssignment: true,
|
|
72
|
-
__IE__: ''
|
|
73
|
-
}),
|
|
74
61
|
resolve({mainFields: ["browser"]}),
|
|
75
62
|
...plugins
|
|
76
63
|
]
|