samplesGenerator.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. 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; }; }();
  2. 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; }
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. import { isObject } from './../helpers/object';
  5. import { rangeEach } from './../helpers/number';
  6. import { stringify } from './../helpers/mixed';
  7. /**
  8. * @class SamplesGenerator
  9. * @util
  10. */
  11. var SamplesGenerator = function () {
  12. _createClass(SamplesGenerator, null, [{
  13. key: 'SAMPLE_COUNT',
  14. /**
  15. * Number of samples to take of each value length.
  16. *
  17. * @type {Number}
  18. */
  19. get: function get() {
  20. return 3;
  21. }
  22. }]);
  23. function SamplesGenerator(dataFactory) {
  24. _classCallCheck(this, SamplesGenerator);
  25. /**
  26. * Samples prepared for calculations.
  27. *
  28. * @type {Map}
  29. * @default {null}
  30. */
  31. this.samples = null;
  32. /**
  33. * Function which give the data to collect samples.
  34. *
  35. * @type {Function}
  36. */
  37. this.dataFactory = dataFactory;
  38. /**
  39. * Custom number of samples to take of each value length.
  40. *
  41. * @type {Number}
  42. * @default {null}
  43. */
  44. this.customSampleCount = null;
  45. /**
  46. * `true` if duplicate samples collection should be allowed, `false` otherwise.
  47. *
  48. * @type {Boolean}
  49. * @default {false}
  50. */
  51. this.allowDuplicates = false;
  52. }
  53. /**
  54. * Get the sample count for this instance.
  55. *
  56. * @returns {Number}
  57. */
  58. _createClass(SamplesGenerator, [{
  59. key: 'getSampleCount',
  60. value: function getSampleCount() {
  61. if (this.customSampleCount) {
  62. return this.customSampleCount;
  63. }
  64. return SamplesGenerator.SAMPLE_COUNT;
  65. }
  66. }, {
  67. key: 'setSampleCount',
  68. /**
  69. * Set the sample count.
  70. *
  71. * @param {Number} sampleCount Number of samples to be collected.
  72. */
  73. value: function setSampleCount(sampleCount) {
  74. this.customSampleCount = sampleCount;
  75. }
  76. /**
  77. * Set if the generator should accept duplicate values.
  78. *
  79. * @param {Boolean} allowDuplicates `true` to allow duplicate values.
  80. */
  81. }, {
  82. key: 'setAllowDuplicates',
  83. value: function setAllowDuplicates(allowDuplicates) {
  84. this.allowDuplicates = allowDuplicates;
  85. }
  86. /**
  87. * Generate samples for row. You can control which area should be sampled by passing `rowRange` object and `colRange` object.
  88. *
  89. * @param {Object|Number} rowRange
  90. * @param {Object} colRange
  91. * @returns {Object}
  92. */
  93. }, {
  94. key: 'generateRowSamples',
  95. value: function generateRowSamples(rowRange, colRange) {
  96. return this.generateSamples('row', colRange, rowRange);
  97. }
  98. /**
  99. * Generate samples for column. You can control which area should be sampled by passing `colRange` object and `rowRange` object.
  100. *
  101. * @param {Object} colRange Column index.
  102. * @param {Object} rowRange Column index.
  103. * @returns {Object}
  104. */
  105. }, {
  106. key: 'generateColumnSamples',
  107. value: function generateColumnSamples(colRange, rowRange) {
  108. return this.generateSamples('col', rowRange, colRange);
  109. }
  110. /**
  111. * Generate collection of samples.
  112. *
  113. * @param {String} type Type to generate. Can be `col` or `row`.
  114. * @param {Object} range
  115. * @param {Object|Number} specifierRange
  116. * @returns {Map}
  117. */
  118. }, {
  119. key: 'generateSamples',
  120. value: function generateSamples(type, range, specifierRange) {
  121. var _this = this;
  122. var samples = new Map();
  123. if (typeof specifierRange === 'number') {
  124. specifierRange = { from: specifierRange, to: specifierRange };
  125. }
  126. rangeEach(specifierRange.from, specifierRange.to, function (index) {
  127. var sample = _this.generateSample(type, range, index);
  128. samples.set(index, sample);
  129. });
  130. return samples;
  131. }
  132. /**
  133. * Generate sample for specified type (`row` or `col`).
  134. *
  135. * @param {String} type Samples type `row` or `col`.
  136. * @param {Object} range
  137. * @param {Number} specifierValue
  138. * @returns {Map}
  139. */
  140. }, {
  141. key: 'generateSample',
  142. value: function generateSample(type, range, specifierValue) {
  143. var _this2 = this;
  144. var samples = new Map();
  145. var sampledValues = [];
  146. var length = void 0;
  147. rangeEach(range.from, range.to, function (index) {
  148. var value = void 0;
  149. if (type === 'row') {
  150. value = _this2.dataFactory(specifierValue, index);
  151. } else if (type === 'col') {
  152. value = _this2.dataFactory(index, specifierValue);
  153. } else {
  154. throw new Error('Unsupported sample type');
  155. }
  156. if (isObject(value)) {
  157. length = Object.keys(value).length;
  158. } else if (Array.isArray(value)) {
  159. length = value.length;
  160. } else {
  161. length = stringify(value).length;
  162. }
  163. if (!samples.has(length)) {
  164. samples.set(length, {
  165. needed: _this2.getSampleCount(),
  166. strings: []
  167. });
  168. }
  169. var sample = samples.get(length);
  170. if (sample.needed) {
  171. var duplicate = sampledValues.indexOf(value) > -1;
  172. if (!duplicate || _this2.allowDuplicates) {
  173. var computedKey = type === 'row' ? 'col' : 'row';
  174. sample.strings.push(_defineProperty({ value: value }, computedKey, index));
  175. sampledValues.push(value);
  176. sample.needed--;
  177. }
  178. }
  179. });
  180. return samples;
  181. }
  182. }]);
  183. return SamplesGenerator;
  184. }();
  185. export default SamplesGenerator;