jiminy
Version:
Library for inferring which type of data visualization can be rendered from a JSON dataset, and with which data field(s)
98 lines (72 loc) • 3.25 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
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; }; }();
var _utils = require('utils');
var _utils2 = _interopRequireDefault(_utils);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Dataset = function () {
function Dataset(dataset) {
_classCallCheck(this, Dataset);
this._data = dataset;
if (!this.valid) {
throw new Error('Jiminy: The dataset must be a non-empty array.');
}
}
_createClass(Dataset, [{
key: 'getFirstValidValue',
/* Return the first valid value of the column named fieldName */
value: function getFirstValidValue(fieldName) {
for (var i = 0, j = this._data.length; i < j; i++) {
var value = this._data[i][fieldName];
if (this._validValue(value)) return value;
}
return null;
}
/* Return true if the column contains at least 5 different valid values */
}, {
key: 'atLeast5DistinctValues',
value: function atLeast5DistinctValues(fieldName) {
var values = [];
for (var i = 0, j = this._data.length; i < j; i++) {
var value = this._data[i][fieldName];
if (this._validValue(value) && !~values.indexOf(value)) values.push(value);
if (values.length >= 5) return true;
}
return false;
}
/* Return the name of the columns from the first row */
}, {
key: 'getColumnNames',
value: function getColumnNames() {
var row = this._data[0];
var columns = [];
for (var column in row) {
columns.push(column);
}
return columns;
}
/* Return true if the passed value is different from null, undefined and
* NaN */
}, {
key: '_validValue',
value: function _validValue(value) {
return value !== null && value !== undefined && !_utils2.default.isNaN(value) && !Array.isArray(value) && !((typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object');
}
}, {
key: 'data',
get: function get() {
return this._data;
}
}, {
key: 'valid',
get: function get() {
return this._data && Array.isArray(this._data) && this._data.length > 0;
}
}]);
return Dataset;
}();
exports.default = Dataset;
;