@antv/data-wizard
Version:
A js/ts library for data processing
78 lines (77 loc) • 2.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebRandom = void 0;
var tslib_1 = require("tslib");
var text_random_1 = require("./text-random");
var database_1 = require("./database");
var utils_1 = require("./utils");
/**
* Generator for web
* @public
*/
var WebRandom = /** @class */ (function (_super) {
tslib_1.__extends(WebRandom, _super);
function WebRandom() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.database = tslib_1.__assign(tslib_1.__assign({}, database_1.getTextDB()), database_1.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 = utils_1.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;
}(text_random_1.TextRandom));
exports.WebRandom = WebRandom;