@helpscout/helix
Version:
A Faker-powered fixture generator for Javascript
145 lines (111 loc) • 5.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _faker = _interopRequireDefault(require("faker"));
var _lodash = require("lodash");
var _log = _interopRequireDefault(require("../utils/log"));
var _generateSpecs = _interopRequireDefault(require("./generateSpecs"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
/**
* Class for a HelixSpec definition. This class contains methods to
* generate the final fixture object, as well as methods to seed Faker
* fixture data.
*
* @param object $shape Fixture shape, that contains Faker render API
*
* @returns class
*/
var HelixSpec = /*#__PURE__*/function () {
function HelixSpec(shape) {
_classCallCheck(this, HelixSpec);
this.shape = shape;
this.seedValue = undefined;
this._afterGenerate = function (props) {
return props;
};
return this;
}
_createClass(HelixSpec, [{
key: "extend",
value: function extend() {
for (var _len = arguments.length, specs = new Array(_len), _key = 0; _key < _len; _key++) {
specs[_key] = arguments[_key];
}
this.shape = Object.assign.apply(Object, [this.shape].concat(specs));
return this;
}
}, {
key: "beforeGenerate",
value: function beforeGenerate(callback) {
if (!(0, _lodash.isFunction)(callback)) {
throw (0, _log.default)('HelixSpec.beforeGenerate()', 'Argument must be a valid function.');
}
this.shape = callback(this.shape);
return this;
}
}, {
key: "afterGenerate",
value: function afterGenerate(callback) {
if (!(0, _lodash.isFunction)(callback)) {
throw (0, _log.default)('HelixSpec.afterGenerate()', 'Argument must be a valid function.');
}
this._afterGenerate = callback;
return this;
}
}, {
key: "generate",
value: function generate(count, max) {
var _this = this;
if (!(0, _lodash.isNumber)(count) && count !== undefined) {
throw (0, _log.default)('HelixSpec.generate()', 'Argument must be a valid number.');
}
if (max !== undefined) {
if (!(0, _lodash.isNumber)(max)) {
throw (0, _log.default)('HelixSpec.generate()', 'Max argument must be a valid number.');
}
if (max <= count) {
throw (0, _log.default)('HelixSpec.generate()', 'Max argument must be larger than count argument.');
}
count = _faker.default.datatype.number({
min: count,
max: max
});
}
var isArray = (0, _lodash.isNumber)(count);
var _seedValue = this.seedValue;
var generatedSpecs = isArray ? _toConsumableArray(Array(count)).map(function (n, index) {
// Respect seed value for multi-generated specs
_this.seed(_seedValue + index);
return (0, _generateSpecs.default)(_this.shape, _this.seedValue);
}) : (0, _generateSpecs.default)(this.shape, this.seedValue);
this.seedValue = undefined;
return this._afterGenerate(generatedSpecs);
}
}, {
key: "seed",
value: function seed(seedValue) {
if (seedValue !== undefined && !(0, _lodash.isNumber)(seedValue)) {
throw new _log.default('HelixSpec.seed()', 'Seed value must be a valid number.');
}
if (seedValue !== undefined) {
this.seedValue = seedValue;
_faker.default.seed(this.seedValue);
}
return this;
}
}]);
return HelixSpec;
}();
var _default = HelixSpec;
exports.default = _default;