ab04d6f90e731d5531c7adb6da08edfb1573556604c5733825b02ecbc6e5d4db578d39d82d3b378a906fc6aeca0946a54f6f8c06677b084a9b37511972d944 6.3 KB

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