@reactual/handsontable
Version:
Spreadsheet-like data grid editor
223 lines (185 loc) • 6.31 kB
JavaScript
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 _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
import { isObject } from './../helpers/object';
import { rangeEach } from './../helpers/number';
import { stringify } from './../helpers/mixed';
/**
* @class SamplesGenerator
* @util
*/
var SamplesGenerator = function () {
_createClass(SamplesGenerator, null, [{
key: 'SAMPLE_COUNT',
/**
* Number of samples to take of each value length.
*
* @type {Number}
*/
get: function get() {
return 3;
}
}]);
function SamplesGenerator(dataFactory) {
_classCallCheck(this, SamplesGenerator);
/**
* Samples prepared for calculations.
*
* @type {Map}
* @default {null}
*/
this.samples = null;
/**
* Function which give the data to collect samples.
*
* @type {Function}
*/
this.dataFactory = dataFactory;
/**
* Custom number of samples to take of each value length.
*
* @type {Number}
* @default {null}
*/
this.customSampleCount = null;
/**
* `true` if duplicate samples collection should be allowed, `false` otherwise.
*
* @type {Boolean}
* @default {false}
*/
this.allowDuplicates = false;
}
/**
* Get the sample count for this instance.
*
* @returns {Number}
*/
_createClass(SamplesGenerator, [{
key: 'getSampleCount',
value: function getSampleCount() {
if (this.customSampleCount) {
return this.customSampleCount;
}
return SamplesGenerator.SAMPLE_COUNT;
}
}, {
key: 'setSampleCount',
/**
* Set the sample count.
*
* @param {Number} sampleCount Number of samples to be collected.
*/
value: function setSampleCount(sampleCount) {
this.customSampleCount = sampleCount;
}
/**
* Set if the generator should accept duplicate values.
*
* @param {Boolean} allowDuplicates `true` to allow duplicate values.
*/
}, {
key: 'setAllowDuplicates',
value: function setAllowDuplicates(allowDuplicates) {
this.allowDuplicates = allowDuplicates;
}
/**
* Generate samples for row. You can control which area should be sampled by passing `rowRange` object and `colRange` object.
*
* @param {Object|Number} rowRange
* @param {Object} colRange
* @returns {Object}
*/
}, {
key: 'generateRowSamples',
value: function generateRowSamples(rowRange, colRange) {
return this.generateSamples('row', colRange, rowRange);
}
/**
* Generate samples for column. You can control which area should be sampled by passing `colRange` object and `rowRange` object.
*
* @param {Object} colRange Column index.
* @param {Object} rowRange Column index.
* @returns {Object}
*/
}, {
key: 'generateColumnSamples',
value: function generateColumnSamples(colRange, rowRange) {
return this.generateSamples('col', rowRange, colRange);
}
/**
* Generate collection of samples.
*
* @param {String} type Type to generate. Can be `col` or `row`.
* @param {Object} range
* @param {Object|Number} specifierRange
* @returns {Map}
*/
}, {
key: 'generateSamples',
value: function generateSamples(type, range, specifierRange) {
var _this = this;
var samples = new Map();
if (typeof specifierRange === 'number') {
specifierRange = { from: specifierRange, to: specifierRange };
}
rangeEach(specifierRange.from, specifierRange.to, function (index) {
var sample = _this.generateSample(type, range, index);
samples.set(index, sample);
});
return samples;
}
/**
* Generate sample for specified type (`row` or `col`).
*
* @param {String} type Samples type `row` or `col`.
* @param {Object} range
* @param {Number} specifierValue
* @returns {Map}
*/
}, {
key: 'generateSample',
value: function generateSample(type, range, specifierValue) {
var _this2 = this;
var samples = new Map();
var sampledValues = [];
var length = void 0;
rangeEach(range.from, range.to, function (index) {
var value = void 0;
if (type === 'row') {
value = _this2.dataFactory(specifierValue, index);
} else if (type === 'col') {
value = _this2.dataFactory(index, specifierValue);
} else {
throw new Error('Unsupported sample type');
}
if (isObject(value)) {
length = Object.keys(value).length;
} else if (Array.isArray(value)) {
length = value.length;
} else {
length = stringify(value).length;
}
if (!samples.has(length)) {
samples.set(length, {
needed: _this2.getSampleCount(),
strings: []
});
}
var sample = samples.get(length);
if (sample.needed) {
var duplicate = sampledValues.indexOf(value) > -1;
if (!duplicate || _this2.allowDuplicates) {
var computedKey = type === 'row' ? 'col' : 'row';
sample.strings.push(_defineProperty({ value: value }, computedKey, index));
sampledValues.push(value);
sample.needed--;
}
}
});
return samples;
}
}]);
return SamplesGenerator;
}();
export default SamplesGenerator;