/
samplesGenerator.js
182 lines (158 loc) · 4.71 KB
/
samplesGenerator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { isObject } from './../helpers/object';
import { rangeEach } from './../helpers/number';
import { stringify } from './../helpers/mixed';
/**
* @class SamplesGenerator
*/
class SamplesGenerator {
/**
* Number of samples to take of each value length.
*
* @type {number}
*/
static get SAMPLE_COUNT() {
return 3;
}
constructor(dataFactory) {
/**
* 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}
*/
getSampleCount() {
if (this.customSampleCount) {
return this.customSampleCount;
}
return SamplesGenerator.SAMPLE_COUNT;
}
/**
* Set the sample count.
*
* @param {number} sampleCount Number of samples to be collected.
*/
setSampleCount(sampleCount) {
this.customSampleCount = sampleCount;
}
/**
* Set if the generator should accept duplicate values.
*
* @param {boolean} allowDuplicates `true` to allow duplicate values.
*/
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 The rows range to generate the samples.
* @param {object} colRange The column range to generate the samples.
* @returns {object}
*/
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}
*/
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 The range to generate the samples.
* @param {object|number} specifierRange The range to generate the samples.
* @returns {Map}
*/
generateSamples(type, range, specifierRange) {
const samples = new Map();
const { from, to } = typeof specifierRange === 'number' ?
{ from: specifierRange, to: specifierRange } : specifierRange;
rangeEach(from, to, (index) => {
const 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 The range to generate the samples.
* @param {number} specifierValue The range to generate the samples.
* @returns {Map}
*/
generateSample(type, range, specifierValue) {
if (type !== 'row' && type !== 'col') {
throw new Error('Unsupported sample type');
}
const samples = new Map();
const computedKey = type === 'row' ? 'col' : 'row';
const sampledValues = [];
rangeEach(range.from, range.to, (index) => {
const { value, bundleSeed } = type === 'row' ?
this.dataFactory(specifierValue, index) : this.dataFactory(index, specifierValue);
const hasCustomBundleSeed = typeof bundleSeed === 'string' && bundleSeed.length > 0;
let seed;
if (hasCustomBundleSeed) {
seed = bundleSeed;
} else if (isObject(value)) {
seed = `${Object.keys(value).length}`;
} else if (Array.isArray(value)) {
seed = `${value.length}`;
} else {
seed = `${stringify(value).length}`;
}
if (!samples.has(seed)) {
samples.set(seed, {
needed: this.getSampleCount(),
strings: [],
});
}
const sample = samples.get(seed);
if (sample.needed) {
const duplicate = sampledValues.indexOf(value) > -1;
if (!duplicate || this.allowDuplicates || hasCustomBundleSeed) {
sample.strings.push({ value, [computedKey]: index });
sampledValues.push(value);
sample.needed -= 1;
}
}
});
return samples;
}
}
export default SamplesGenerator;