UNPKG

@antv/data-wizard

Version:
75 lines (74 loc) 2.63 kB
import { __assign, __extends } from "tslib"; import { TextRandom } from './text-random'; import { getWebDB, getTextDB } from './database'; import { initOptions } from './utils'; /** * Generator for web * @public */ var WebRandom = /** @class */ (function (_super) { __extends(WebRandom, _super); function WebRandom() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.database = __assign(__assign({}, getTextDB()), getWebDB()); return _this; } /** * Return a random top-level domain ({@link https://en.wikipedia.org/wiki/Top-level_domain | TLD}) * @remarks return one of database.tlds */ WebRandom.prototype.tld = function () { return this.pickone(this.database.tld); }; /** * Return a random domain * @param options - the params */ WebRandom.prototype.domain = function (options) { return this.word() + "." + ((options && options.tld) || this.tld()); }; /** * Return a random url * @param options - the params */ WebRandom.prototype.url = function (options) { var opts = initOptions({ protocol: 'http', domain: this.domain(options), domainPrefix: '', path: this.word(), extensions: [], }, options); var protocol = opts.protocol, domainPrefix = opts.domainPrefix, path = opts.path; var extension = opts.extensions.length > 0 ? "." + this.pickone(opts.extensions) : ''; var domain = domainPrefix ? domainPrefix + ".opts.domain" : opts.domain; return protocol + "://" + domain + "/" + path + extension; }; /** * Return a random ip v4 */ WebRandom.prototype.ipv4 = function () { return this.natural({ min: 1, max: 254 }) + "." + this.natural({ max: 255 }) + "." + this.natural({ max: 255, }) + "." + this.natural({ min: 1, max: 254 }); }; /** * Return a random ip v6 */ WebRandom.prototype.ipv6 = function () { var _this = this; var hash = function () { return _this.n(function () { return _this.natural({ min: 0, max: 15 }).toString(16); }, 4).join(''); }; var ipAddr = this.n(hash, 8); return ipAddr.join(':'); }; /** * Return a random email * @param options - the params */ WebRandom.prototype.email = function (options) { if (options === void 0) { options = {}; } return this.word({ length: options.length }) + "@" + (options.domain || this.domain()); }; return WebRandom; }(TextRandom)); export { WebRandom };