UNPKG

jiminy

Version:

Library for inferring which type of data visualization can be rendered from a JSON dataset, and with which data field(s)

2 lines 14.9 kB
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("datalib")):"function"==typeof define&&define.amd?define(["datalib"],t):"object"==typeof exports?exports.Jiminy=t(require("datalib")):e.Jiminy=t(e.dl)}(this,function(e){return function(e){function t(i){if(n[i])return n[i].exports;var r=n[i]={exports:{},id:i,loaded:!1};return e[i].call(r.exports,r,r.exports,t),r.loaded=!0,r.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t,n){"use strict";function i(e){return e&&e.__esModule?e:{default:e}}function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var a=function(){function e(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}return function(t,n,i){return n&&e(t.prototype,n),i&&e(t,i),t}}(),s=n(5),u=i(s),o=n(7),l=i(o),c=n(4),f=i(c),h=function(){function e(t,n){r(this,e),this._dataset=new u.default(t),this._fields=new l.default(this._dataset),this._charts=new f.default(n)}return a(e,[{key:"recommendation",value:function(e){var t=[],n=!1;if(null!==e&&void 0!==e){if(!Array.isArray(e))throw new Error("Jiminy: recommendation should be called without any parameter or with an array of column names.");e.length&&(n=!0,t=this._fields.get(e))}t.length||(t=this._fields.fields);var i={allInclusive:n};return this._availableCharts=this._charts.getAvailable(t,i),this._availableCharts.map(function(e){return e.name})}},{key:"columns",value:function(e,t){if(!e)throw new Error("Jiminy: columns expects the name of the chart as first argument.");var n=this._charts.getChart(e);if(!n)throw new Error(e+" isn't a valid chart name. Check the documentation to see existing types of charts.");if(null!==t&&void 0!==t&&"string"!=typeof t)throw new Error("Jiminy: The second parameter of columns must be a string.");if(!t)return n.computeUsefulFields(this._fields.fields).map(function(e){return e.name});if(!~this._dataset.getColumnNames().indexOf(t))throw new Error('Unable to find the column "'+t+'" inside the dataset');var i=this._fields.get([t])[0];return n.computeUsefulFields(this._fields.fields,i).map(function(e){return e.name})}},{key:"name",get:function(){return"Jiminy"}},{key:"version",get:function(){return"0.1.1"}}]),e}();t.default=h,e.exports=t.default},function(e,t){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var i=function(){function e(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}return function(t,n,i){return n&&e(t.prototype,n),i&&e(t,i),t}}(),r=["quantitative","nominal","ordinal","temporal"],a=function(){function e(t,i){if(n(this,e),this._types=r,!(t&&i&&i.hasOwnProperty("atLeast5DistinctValues")))throw new Error("Jiminy: Statistical types must be instanciated with the field's type and the stats.");this._name=this._inferType(t,i)}return i(e,[{key:"_inferType",value:function(e,t){var n=this._types[2];return e.isBoolean||e.isString?n=this._types[1]:e.isDate?n=this._types[3]:(e.isNumber||e.isInteger)&&t.atLeast5DistinctValues&&(n=this._types[0]),n}},{key:"equals",value:function(e){return e.constructor&&e.constructor===this.constructor&&e.name===this._name}},{key:"name",get:function(){return this._name}},{key:"isQuantitative",get:function(){return this._name===this._types[0]}},{key:"isNominal",get:function(){return this._name===this._types[1]}},{key:"isOrdinal",get:function(){return this._name===this._types[2]}},{key:"isTemporal",get:function(){return this._name===this._types[3]}}],[{key:"types",get:function(){return r}}]),e}();t.default=a},function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.default={isNaN:function(e){function t(t){return e.apply(this,arguments)}return t.toString=function(){return e.toString()},t}(function(e){return Number.isNaN?Number.isNaN(e):"number"==typeof e&&isNaN(e)})}},function(e,t){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var i=function(){function e(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}return function(t,n,i){return n&&e(t.prototype,n),i&&e(t,i),t}}(),r=function(){function e(t){if(n(this,e),!(t&&t.hasOwnProperty("name")&&t.hasOwnProperty("acceptedStatTypes")))throw new Error("Jiminy: Charts must be instanciated with a correct configuration ie. with a name and a list of accepted statistical types.");this._name=t.name,this._acceptedStatTypes=t.acceptedStatTypes}return i(e,[{key:"isAvailable",value:function(e,t){var n=!1;t=t||{};for(var i=0,r=this._acceptedStatTypes.length;i<r;i++){var a=this._acceptedStatTypes[i];if((!t.allInclusive||a.length===e.length)&&(n=1===a.length?this._existFields(e,a[0],1)&&(!t.allInclusive||t.allInclusive&&!this._existFields(e,a[0],2)):a[0]===a[1]?this._existFields(e,a[0],2)&&(!t.allInclusive||t.allInclusive&&!this._existFields(e,a[0],3)):this._existFields(e,a[0],1)&&this._existFields(e,a[1],1)&&(!t.allInclusive||t.allInclusive&&!this._existFields(e,a[0],2)&&!this._existFields(e,a[1],2))))break}return n}},{key:"_capitalize",value:function(e){return e?e[0].toUpperCase()+e.slice(1,e.length):e}},{key:"_existFields",value:function(e,t,n){var i=this;return e.filter(function(e){return e.statType["is"+i._capitalize(t)]}).length>=n}},{key:"computeUsefulFields",value:function(e,t){var n=this;return e.filter(function(i){for(var r=!1,a=0,s=n._acceptedStatTypes.length;a<s;a++){var u=n._acceptedStatTypes[a];if(r=1===u.length?!t&&u[0]===i.statType.name:u[0]===u[1]?u[0]===i.statType.name&&n._existFields(e,u[0],2)&&(!t||t&&t.name!==i.name):t?u[0]===i.statType.name&&n._existFields(e,u[1],1)&&t.statType.name===u[1]||u[1]===i.statType.name&&n._existFields(e,u[0],1)&&t.statType.name===u[0]:u[0]===i.statType.name&&n._existFields(e,u[1],1))break}return r})}},{key:"equals",value:function(e){return e.constructor&&e.constructor===this.constructor&&e.name===this._name&&e.acceptedStatTypes===this._acceptedStatTypes}},{key:"name",get:function(){return this._name}},{key:"acceptedStatTypes",get:function(){return this._acceptedStatTypes}}]),e}();t.default=r},function(e,t,n){"use strict";function i(e){return e&&e.__esModule?e:{default:e}}function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var a=function(){function e(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}return function(t,n,i){return n&&e(t.prototype,n),i&&e(t,i),t}}(),s=n(3),u=i(s),o=n(1),l=i(o),c=function(){function e(t){if(r(this,e),!t||!Array.isArray(t)||!t.length)throw new Error("Jiminy: You must pass a non-empty chart configuration.");this._config=this._parseConfig(t),this._charts=this._createCharts()}return a(e,[{key:"_createCharts",value:function(){for(var e=[],t=0,n=this._config.length;t<n;t++)e.push(new u.default(this._config[t]));return e}},{key:"_parseConfig",value:function(e){for(var t=[],n=function(n,i){var r=e[n];if(!r.name||"string"!=typeof r.name||!r.name.length)return console.warn("Jiminy: Each chart must have a name."),"continue";if(!r.acceptedStatTypes||!Array.isArray(r.acceptedStatTypes)||!r.acceptedStatTypes.length)return console.warn("Jiminy: Each chart must have a set of rules."),"continue";for(var a=[],s=0,u=r.acceptedStatTypes.length;s<u;s++){var o=r.acceptedStatTypes[s];if(o&&Array.isArray(o)&&o.length)if(o.length>2)console.warn('Jiminy: A rule for chart "'+r.name+'" has been ignored because it owns more than two statistical types.');else{for(var c=0,f=0,h=o.length;f<h;f++)"string"==typeof o[f]&&~l.default.types.indexOf(o[f])?c++:console.warn('Jiminy: A rule for chart "'+r.name+"\" has been ignored because a statistical type isn't valid.");c===o.length&&a.push(o.slice(0))}else console.warn('Jiminy: A rule for chart "'+r.name+'" has been ignored because invalid.')}if(a.length>0){if(t.filter(function(e){return e.name===r.name}).length)return console.warn("Jiminy: A chart has been ignored because it "+('already exists another with name "'+r.name+'".')),"continue";t.push({name:r.name,acceptedStatTypes:a})}},i=0,r=e.length;i<r;i++){n(i,r)}if(!t.length)throw new Error("Jiminy: The chart configuration is empty or malformed.");return t}},{key:"getAvailable",value:function(e,t){var n=[];if(t=t||{},!e||!e.length)throw new Error("Jiminy: At least one field is required to compute the available charts.");for(var i=0,r=this._charts.length;i<r;i++){var a={};a.allInclusive&&(a.allInclusive=!0),this._charts[i].isAvailable(e,a)&&n.push(this._charts[i])}return n}},{key:"getChart",value:function(e){for(var t=0,n=this._charts.length;t<n;t++)if(this._charts[t].name===e)return this._charts[t];return null}},{key:"charts",get:function(){return this._charts}}]),e}();t.default=c},function(e,t,n){"use strict";function i(e){return e&&e.__esModule?e:{default:e}}function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var a="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},s=function(){function e(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}return function(t,n,i){return n&&e(t.prototype,n),i&&e(t,i),t}}(),u=n(2),o=i(u),l=function(){function e(t){if(r(this,e),this._data=t,!this.valid)throw new Error("Jiminy: The dataset must be a non-empty array.")}return s(e,[{key:"getFirstValidValue",value:function(e){for(var t=0,n=this._data.length;t<n;t++){var i=this._data[t][e];if(this._validValue(i))return i}return null}},{key:"atLeast5DistinctValues",value:function(e){for(var t=[],n=0,i=this._data.length;n<i;n++){var r=this._data[n][e];if(this._validValue(r)&&!~t.indexOf(r)&&t.push(r),t.length>=5)return!0}return!1}},{key:"getColumnNames",value:function(){var e=this._data[0],t=[];for(var n in e)t.push(n);return t}},{key:"_validValue",value:function(e){return!(null===e||void 0===e||o.default.isNaN(e)||Array.isArray(e)||"object"===("undefined"==typeof e?"undefined":a(e)))}},{key:"data",get:function(){return this._data}},{key:"valid",get:function(){return this._data&&Array.isArray(this._data)&&this._data.length>0}}]),e}();t.default=l},function(e,t,n){"use strict";function i(e){return e&&e.__esModule?e:{default:e}}function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var a=function(){function e(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}return function(t,n,i){return n&&e(t.prototype,n),i&&e(t,i),t}}(),s=n(8),u=i(s),o=n(1),l=i(o),c=function(){function e(t,n){if(r(this,e),!t||!n||!n.valid)throw new Error("Jiminy: Fields must be instanciated with a name and with the dataset");this._name=t,this._type=new u.default(t,n),this._stats=this._computeStats(n),this._statType=new l.default(this._type,this._stats)}return a(e,[{key:"_computeStats",value:function(e){return{atLeast5DistinctValues:e.atLeast5DistinctValues(this._name)}}},{key:"equals",value:function(e){return e.constructor&&e.constructor===this.constructor&&e.name===this._name&&e.type.equals(this._type)&&e.statType.equals(this._statType)}},{key:"name",get:function(){return this._name}},{key:"type",get:function(){return this._type}},{key:"statType",get:function(){return this._statType}}]),e}();t.default=c},function(e,t,n){"use strict";function i(e){return e&&e.__esModule?e:{default:e}}function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var a=function(){function e(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}return function(t,n,i){return n&&e(t.prototype,n),i&&e(t,i),t}}(),s=n(6),u=i(s),o=function(){function e(t){if(r(this,e),!t||!t.valid)throw new Error("Jiminy: Fields expects a valid dataset");this._fields=this.computeFields(t)}return a(e,[{key:"computeFields",value:function(e){for(var t=e.getColumnNames(),n=[],i=0,r=t.length;i<r;i++)n.push(new u.default(t[i],e));return n}},{key:"get",value:function(e){for(var t=[],n=0,i=e.length;n<i;n++){var r=e[n],a=this._getField(r);a?t.push(a):console.warn('Jiminy: Unable to find the column "'+r+'" inside the dataset.')}return t}},{key:"_getField",value:function(e){for(var t=0,n=this._fields.length;t<n;t++)if(this.fields[t].name===e)return this._fields[t];return null}},{key:"fields",get:function(){return this._fields}}]),e}();t.default=o},function(e,t,n){"use strict";function i(e){return e&&e.__esModule?e:{default:e}}function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var a=function(){function e(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}return function(t,n,i){return n&&e(t.prototype,n),i&&e(t,i),t}}(),s=n(9),u=i(s),o=n(2),l=i(o),c=["number","integer","string","date","boolean"],f=function(){function e(t,n){if(r(this,e),this._types=c,!u.default)throw new Error("Jiminy: Make sure to include the dependency 'datalib'");if(!(t&&n&&n.valid))throw new Error("Jiminy: A type requires the dataset and the field name.");this._name=this._inferType(t,n)}return a(e,[{key:"_inferType",value:function(e,t){var n=t.getFirstValidValue(e);return null!==n?u.default.type.infer([n]):this._types[2]}},{key:"_validValue",value:function(e){return null!==e&&void 0!==e&&!l.default.isNaN(e)}},{key:"equals",value:function(e){return e.constructor&&e.constructor===this.constructor&&e.name===this._name}},{key:"name",get:function(){return this._name}},{key:"isNumber",get:function(){return 0===this._types.indexOf(this._name)}},{key:"isInteger",get:function(){return 1===this._types.indexOf(this._name)}},{key:"isString",get:function(){return 2===this._types.indexOf(this._name)}},{key:"isDate",get:function(){return 3===this._types.indexOf(this._name)}},{key:"isBoolean",get:function(){return 4===this._types.indexOf(this._name)}}]),e}();t.default=f},function(t,n){t.exports=e}])}); //# sourceMappingURL=jiminy.js.map