-
Notifications
You must be signed in to change notification settings - Fork 1
/
iife.js
2 lines (2 loc) · 6.21 KB
/
iife.js
1
2
// jsonstat-euro v2.1.1 Copyright 2023 Xavier Badosa https://jsonstat.com
var EuroJSONstat=function(e,t){"use strict";t=t&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t;function r(e){return(r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function a(e){var t=e.lang||"en",a=e.version||"1.0",n=e.dataset||null,i=e.filter||null,l=e.label||null,o={class:"query",lang:t,version:a,dataset:n};return i&&"object"===r(i)&&0!==Object.keys(i).length&&(Object.keys(i).forEach((function(e){var t=i[e];Array.isArray(t)||(i[e]=[t]),0===i[e].length&&delete i[e]})),o.filter=i),l&&"object"===r(l)&&0!==Object.keys(l).length&&(o.label=l),o}function n(e,t){return Object.prototype.hasOwnProperty.call(e,t)}function i(e,t,r,i){var l,o=JSON.parse(JSON.stringify(e));if("string"==typeof i&&o.filter.hasOwnProperty("geo")&&-1!==o.filter.geo.indexOf(i)||(i=null),n(o,"filter")){switch(r){case"last":l=function(e){o.filter[e]=o.filter[e].slice(-1)};break;case"middle":l=function(e){var t=Math.round(o.filter[e].length/2);o.filter[e]=o.filter[e].slice(t-1,t)};break;default:l=function(e){o.filter[e]=o.filter[e].slice(0,1)}}Object.keys(o.filter).forEach(l),!0===t&&(delete o.filter.time,o.filter.lastTimePeriod=["1"]),i&&(o.filter.geo=[i])}return n(o,"label")&&n(o.label,"category")&&(Object.keys(o.label.category).forEach((function(e){o.label.category[e]=o.label.category[e].slice(0,1)})),!0===t&&delete o.label.category.time),a(o)}function l(e){var t=JSON.parse(JSON.stringify(e));return n(t,"filter")?(delete t.filter.time,t.filter.lastTimePeriod=["1"]):t.filter={lastTimePeriod:["1"]},a(t)}function o(e,t,r){void 0===r&&(r=Object.keys(t),t=a({dataset:null,filter:t}));var i=JSON.parse(JSON.stringify(e)),l=n(t,"filter"),o=n(t,"label")&&n(t.label,"category");return r.forEach((function(e){l&&n(t.filter,e)&&(n(i,"filter")||(i.filter={}),i.filter[e]=t.filter[e]),o&&n(t.label.category,e)&&(n(i,"label")?n(i.label,"category")||(i.label.category={}):i.label={},i.label.category[e]=t.label.category[e])})),a(i)}function s(e,t){var r=JSON.parse(JSON.stringify(e)),i=n(r,"filter"),l=n(r,"label"),o=l&&n(r.label,"category"),s=l&&n(r.label,"dimension");return t.forEach((function(e){i&&delete r.filter[e],l&&(o&&delete r.label.category[e],s&&delete r.label.dimension[e])})),a(r)}function c(e){if("string"==typeof e){if(/https?:\/\//i.test(e))return e;e={dataset:e}}if("object"===r(e)&&null!==e&&!Array.isArray(e)&&e.dataset){e=a(e);var t="".concat("https://ec.europa.eu/eurostat/api/dissemination/statistics/").concat(e.version,"/data/").concat(e.dataset,"?lang=").concat(e.lang),n=[],i=e.filter||null;return i&&0!==Object.keys(i).length&&(Object.keys(i).forEach((function(e){i[e].forEach((function(t){n.push("".concat(e,"=").concat(t))}))})),t+="&"+n.join("&")),t}return null}function f(e){e.role={geo:[],time:[],metric:[],classification:[]},e.id.forEach((function(t){switch(e.Dimension(t).role="time"===t||"geo"===t?t:"classification",t){case"geo":case"time":e.role[t].push(t);break;case"unit":case"s_adj":case"indic_co":case"na_item":case"indic":e.role.metric.push(t);break;default:e.role.classification.push(t)}})),e.role.geo.length||delete e.role.geo,e.role.time.length||delete e.role.time,e.role.metric.length||delete e.role.metric,e.role.classification.length||delete e.role.classification}function u(e){if(e)return t(c(e)).then((function(e){return"dataset"===e.class?(f(e),e):e}),(function(e){var t=e.message.slice(0,3),r=isNaN(t)?"418":t;return{class:"error",status:r,label:"418"!==r?e.message.slice(4):e.message}}))}function y(e,t){"string"==typeof e&&(e={dataset:e});var r=!1!==t?l(e):e;return u(r).then((function(e){if("error"===e.class)return e;if(e.error)return{class:"error",status:e.error.status,label:e.error.label};var t={},a={},n={};return e.id.forEach((function(r){var i=e.Dimension(r);a[r]=i.label,t[r]=i.id,n[r]=i.Category().map((function(e){return e.label}))})),{class:"query",dataset:r.dataset,filter:t,label:{dataset:e.label,dimension:a,category:n},lang:r.lang||"en",version:r.version||"1.0"}}))}return e.addParamQuery=o,e.fetchDataset=u,e.fetchEmptyDataset=function(e,r){function a(e){if(!e.ok)throw new Error(e.status+" "+e.statusText);return e.json()}if(e){if("string"==typeof e){if(/https?:\/\//i.test(e))return new Promise((function(e){e({class:"error",status:400,label:"A URL was passed to fetchEmptyDataset. Only dataset codes and queries are accepted. "})}));e={dataset:e}}return e.filter={time_period:["null"]},fetch(c(e)).then(a).then((function(n){var i=t(n),l=n.id.indexOf("time"),o={};return i.id.forEach((function(e){"time"!==e&&(o[e]=[i.Dimension(e).id[0]])})),n.value=[],e.filter=o,fetch(c(e)).then(a).then((function(e){if(n.size[l]=e.size[l],n.dimension.time.category=e.dimension.time.category,r){var a=t(n);return"dataset"===a.class?(f(a),a):a}return n}))}),(function(e){var t=e.message.slice(0,3),r=isNaN(t)?"418":t;return{class:"error",status:r,label:"418"!==r?e.message.slice(4):e.message}}))}},e.fetchFullQuery=function(e,t,r,a){"string"==typeof e&&(e={dataset:e}),"boolean"!=typeof t&&(t=!1),"string"!=typeof r&&(r="first"),"string"!=typeof a&&(a=null);var l=n(e,"filter")?e.filter:null;if(l&&0!==Object.keys(l).length){var c=Object.keys(l);return y(o(e,l),!1).then((function(e){return"error"===e.class?e:y(s(i(e,t,r,a),c),!1).then((function(t){return"error"===t.class?t:o(e,t,c)}))}))}return y(e,!1).then((function(e){return e}))},e.fetchQuery=y,e.getEmptyDataset=function(e){var r=n(e,"lang")?e.lang:"en",a=n(e,"dataset")?e.dataset:null,i={lang:r.toUpperCase(),datasetId:a},l=Object.keys(e.filter),o=l.map((function(t){return e.filter[t].length})),s={};l.forEach((function(t){s[t]={label:e.label.dimension[t],category:{index:e.filter[t],label:{}}},e.filter[t].forEach((function(r,a){Object.defineProperty(s[t].category.label,r,{value:e.label.category[t][a]})}))}));var c={version:"2.0",class:"dataset",label:e.label.dataset,extension:i,id:l,size:o,dimension:s,value:[]},u=t(c);return f(u),u},e.getStatusLabel=function(e,t){return e.extension.status.label[t]},e.getURL=c,e.lastPeriodQuery=l,e.removeParamQuery=s,e.removeTimeQuery=function(e){return s(e,["time","lastTimePeriod","sinceTimePeriod"])},e.setRole=f,e.simpleQuery=i,e.version="2.1.1",e}({},JSONstat);