jiminy
Version:
Library for inferring which type of data visualization can be rendered from a JSON dataset, and with which data field(s)
88 lines (73 loc) • 2.76 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var TYPES = ['quantitative', 'nominal', 'ordinal', 'temporal'];
var StatType = function () {
/* An instance can be created or with a type (number, string, etc.) and stats
* about that column, or directly the name of the statistical type */
function StatType(type, stats) {
_classCallCheck(this, StatType);
this._types = TYPES;
if (type && stats && stats.hasOwnProperty('atLeast5DistinctValues')) {
this._name = this._inferType(type, stats);
} else {
throw new Error('Jiminy: Statistical types must be instanciated with the ' + 'field\'s type and the stats.');
}
}
_createClass(StatType, [{
key: '_inferType',
/* Infer the statistical type depending on the type of data and the statistics
* of the field */
value: function _inferType(type, stats) {
var statType = this._types[2];
if (type.isBoolean || type.isString) {
statType = this._types[1];
} else if (type.isDate) {
statType = this._types[3];
} else if ((type.isNumber || type.isInteger) && stats.atLeast5DistinctValues) {
statType = this._types[0];
}
return statType;
}
}, {
key: 'equals',
value: function equals(o) {
return o.constructor && o.constructor === this.constructor && o.name === this._name;
}
}, {
key: 'name',
get: function get() {
return this._name;
}
}, {
key: 'isQuantitative',
get: function get() {
return this._name === this._types[0];
}
}, {
key: 'isNominal',
get: function get() {
return this._name === this._types[1];
}
}, {
key: 'isOrdinal',
get: function get() {
return this._name === this._types[2];
}
}, {
key: 'isTemporal',
get: function get() {
return this._name === this._types[3];
}
}], [{
key: 'types',
get: function get() {
return TYPES;
}
}]);
return StatType;
}();
exports.default = StatType;