@antv/data-wizard
Version:
A js/ts library for data processing
300 lines (299 loc) • 11 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TextRandom = void 0;
var tslib_1 = require("tslib");
var utils_1 = require("../utils");
var database_1 = require("./database");
var utils_2 = require("./utils");
var basic_random_1 = require("./basic-random");
/**
* Generator for string
* @public
*/
var TextRandom = /** @class */ (function (_super) {
tslib_1.__extends(TextRandom, _super);
function TextRandom() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.database = database_1.getTextDB();
return _this;
}
/**
* Return a random char
*
* @param options -
*/
TextRandom.prototype.character = function (options) {
var opts = utils_2.initOptions({ lower: true, upper: true, symbols: true, numeric: true }, options);
var pool = '';
if (opts.pool) {
pool = opts.pool;
}
else {
if (opts.lower) {
pool += this.database.character.lower;
}
if (opts.upper) {
pool += this.database.character.upper;
}
if (opts.symbols) {
pool += this.database.character.symbol;
}
if (opts.numeric) {
pool += this.database.character.number;
}
}
return pool.charAt(this.natural({ max: pool.length - 1 }));
};
/**
* Return a random string
*
* @param options - the params
*
* @public
*/
TextRandom.prototype.string = function (options) {
var opts = utils_2.initOptions({ length: this.natural({ min: 5, max: 20 }) }, options);
utils_1.assert(opts.length >= 0, 'Length cannot be less than zero.');
var length = opts.length;
var text = this.n(this.character, length, opts);
return text.join('');
};
/**
* Return a random syllable
* @param options - the params
* @internal
*/
TextRandom.prototype.syllable = function (options) {
var length = utils_2.initOptions({ length: this.natural({ min: 2, max: 3 }) }, options).length;
var _a = this.database.syllable, consonants = _a.consonants, vowels = _a.vowels; // consonants except hard to speak ones
var all = consonants + vowels; // all
var text = '';
var chr = '';
// I'm sure there's a more elegant way to do this, but this works
// decently well.
for (var i = 0; i < length; i += 1) {
if (i === 0) {
// First character can be anything
chr = this.character({ pool: all });
}
else if (consonants.indexOf(chr) === -1) {
// Last character was a vowel, now we want a consonant
chr = this.character({ pool: consonants });
}
else {
// Last character was a consonant, now we want a vowel
chr = this.character({ pool: vowels });
}
text += chr;
}
if (options && options.capitalize) {
text = utils_2.capitalize(text);
}
return text;
};
/**
* return a random word
* @param options - the params
*/
TextRandom.prototype.word = function (options) {
if (options === void 0) { options = {}; }
utils_1.assert(!options.syllables || !options.length, 'Cannot specify both syllables AND length.');
var syllables = options.syllables || this.natural({ min: 1, max: 3 });
var text = '';
if (options.length) {
// Either bound word by length
do {
text += this.syllable();
} while (text.length < options.length);
text = text.substring(0, options.length);
}
else {
// Or by number of syllables
for (var i = 0; i < syllables; i += 1) {
text += this.syllable();
}
}
if (options && options.capitalize)
text = utils_2.capitalize(text);
return text;
};
/**
* return a random sentence
* @param options - the params
*/
TextRandom.prototype.sentence = function (options) {
var opts = utils_2.initOptions({ words: this.natural({ min: 12, max: 18 }), punctuation: true }, options);
var words = opts.words, punctuation = opts.punctuation;
var wordArray = this.n(this.word, words);
var text;
text = wordArray.join(' ');
// Capitalize first letter of sentence
text = utils_2.capitalize(text);
// Make sure punctuation has a usable value
if (punctuation === true) {
text += this.pickone(this.database.sentence.punctuations.split(''));
}
else if (typeof punctuation === 'string') {
text += punctuation;
}
return text;
};
/**
* return a random paragraph
* @param options - the params
*/
TextRandom.prototype.paragraph = function (options) {
var sentences = utils_2.initOptions({ sentences: this.natural({ min: 3, max: 7 }) }, options).sentences;
var sentenceArray = this.n(this.sentence, sentences, { punctuation: '.' });
return sentenceArray.join(' ');
};
/**
* return a random english name
*
* @param options - the params
*/
TextRandom.prototype.name = function (options) {
return this.givenName(options) + " " + this.surname();
};
/**
* return a random english surname
*/
TextRandom.prototype.surname = function () {
return this.pickone(this.database.surname);
};
/**
* return a random english given name
* @param options - the params
*/
TextRandom.prototype.givenName = function (options) {
var gender = utils_2.initOptions({}, options).gender;
utils_1.assert(!gender || gender === 'female' || gender === 'male', 'Gender must be one of female or male');
var _a = this.database.givenName, male = _a.male, female = _a.female;
var pool = gender ? this.database.givenName[gender] : [].concat(male).concat(female);
return this.pickone(pool);
};
/**
* return a random phone number
* @param options - the params
*/
TextRandom.prototype.phone = function (options) {
var _a = utils_2.initOptions({
mobile: true,
formatted: false,
asterisk: false,
startNum: '',
}, options), mobile = _a.mobile, formatted = _a.formatted, asterisk = _a.asterisk, startNum = _a.startNum;
var exp;
if (mobile) {
// add **** in mobile numbers
if (asterisk) {
// specify the first three digits
if (startNum.length === 3) {
exp = formatted ? new RegExp(startNum + "-\\*{4}-\\d{4}") : new RegExp(startNum + "\\*{4}\\d{4}");
}
else {
exp = formatted ? /1[345789]\d-\*{4}-\d{4}/ : /1[345789]\d\*{4}\d{4}/;
}
}
else if (startNum.length === 3) {
exp = formatted ? new RegExp(startNum + "-\\d{4}-\\d{4}") : new RegExp(startNum + "\\d{8}");
}
else {
exp = formatted ? /1[345789]\d-\d{4}-\d{4}/ : /1[345789]\d{9}/;
}
}
else if (asterisk) {
// add ** or *** in landline numbers
if (startNum.length === 3) {
exp = formatted
? new RegExp(startNum + "-\\d{3}\\*{2,3}\\d{2}")
: new RegExp(startNum + "\\d{3}\\*{2,3}\\d{2}");
}
else {
exp = formatted ? /\d{3,4}-\d{3}\*{2,3}\d{2}/ : /\d{3,4}\d{3}\*{2,3}\d{2}/;
}
}
else if (startNum.length === 3) {
exp = formatted ? new RegExp(startNum + "-\\d{7,8}") : new RegExp(startNum + "\\d{7,8}");
}
else {
exp = formatted ? /\d{3,4}-\d{7,8}/ : /\d{10,12}/;
}
return this.randexp(exp);
};
/**
* return a random Chinese character
*/
TextRandom.prototype.cCharacter = function (options) {
var pool = utils_2.initOptions({}, options).pool;
return this.pickone((pool || this.database.cCharacter.chars).split(''));
};
/**
* return a random Chinese word
* @param options - the params
*/
TextRandom.prototype.cWord = function (options) {
var length = utils_2.initOptions({ length: this.natural({ min: 2, max: 6 }) }, options).length;
return this.n(this.cCharacter, length, options).join('');
};
/**
* return a random Chinese sentence
* @param options - the params
*/
TextRandom.prototype.cSentence = function (options) {
var opts = utils_2.initOptions({ min: 10, max: 18 }, options);
var length = this.natural(opts);
return this.n(this.cWord, length).join('') + "\u3002";
};
/**
* return a random Chinese paragraph
* @param options - the params
*/
TextRandom.prototype.cParagraph = function (options) {
var opts = utils_2.initOptions({ min: 3, max: 18 }, options);
var length = this.natural(opts);
return this.n(this.cSentence, length).join('');
};
/**
* return a random Chinese name
* @param options - the params
*/
TextRandom.prototype.cName = function (options) {
return "" + this.cSurname() + this.cGivenName(options);
};
/**
* return a random Chinese surname
* @param options - the params
*/
TextRandom.prototype.cSurname = function () {
return this.pickone(this.database.cSurname);
};
/**
* return a random Chinese given name
*/
TextRandom.prototype.cGivenName = function (options) {
var _a = utils_2.initOptions({ length: this.natural({ min: 1, max: 2 }) }, options), length = _a.length, gender = _a.gender;
utils_1.assert(!gender || gender === 'female' || gender === 'male', 'Gender must be one of female or male');
var _b = this.database.cGivenName, male = _b.male, female = _b.female;
var pool = gender ? this.database.cGivenName[gender] : male + female;
return this.pickset(pool.split(''), length).join('');
};
/**
* return a random Chinese zodiac
* @param options - the params
*
* @example
* ```javascript
* const R = new TextRandom();
* const cZodiacCn = R.cZodiac(); // '鼠', '牛', '虎'...
* const cZodiacEn = R.cZodiac({ locale: 'en-US' }); // 'Rat','OX','Tiger'...
* ```
*/
TextRandom.prototype.cZodiac = function (options) {
var locale = utils_2.initOptions({ locale: 'zh-CN' }, options).locale;
var list = this.database.cZodiac[locale];
return this.pickone(list);
};
return TextRandom;
}(basic_random_1.BasicRandom));
exports.TextRandom = TextRandom;