@travi/any
Version:
random data generator for when test data is insignificant
136 lines (128 loc) • 4.21 kB
JavaScript
import Chance from 'chance';
import MersenneTwister from 'mersenne-twister';
import debugLibrary from 'debug';
import _ from 'lodash';
function _defineProperty(e, r, t) {
return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
value: t,
enumerable: !0,
configurable: !0,
writable: !0
}) : e[r] = t, e;
}
function ownKeys(e, r) {
var t = Object.keys(e);
if (Object.getOwnPropertySymbols) {
var o = Object.getOwnPropertySymbols(e);
r && (o = o.filter(function (r) {
return Object.getOwnPropertyDescriptor(e, r).enumerable;
})), t.push.apply(t, o);
}
return t;
}
function _objectSpread2(e) {
for (var r = 1; r < arguments.length; r++) {
var t = null != arguments[r] ? arguments[r] : {};
r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
_defineProperty(e, r, t[r]);
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
});
}
return e;
}
function _toPrimitive(t, r) {
if ("object" != typeof t || !t) return t;
var e = t[Symbol.toPrimitive];
if (void 0 !== e) {
var i = e.call(t, r || "default");
if ("object" != typeof i) return i;
throw new TypeError("@@toPrimitive must return a primitive value.");
}
return ("string" === r ? String : Number)(t);
}
function _toPropertyKey(t) {
var i = _toPrimitive(t, "string");
return "symbol" == typeof i ? i : i + "";
}
const debug = debugLibrary('any');
const generator = new MersenneTwister();
// Multiply the random seed to match chance.js
const seed = process.env.ANY_SEED || generator.random() * Math.pow(10, 13);
debug(`randomness seed: ${seed}`);
function isPrimitive(value) {
return value !== Object(value);
}
const chance = new Chance(seed);
const integer = options => chance.natural(!isPrimitive(options) ? options : undefined);
const float = options => chance.floating(!isPrimitive(options) ? options : undefined);
const string = options => chance.string(!isPrimitive(options) ? options : undefined);
const sentence = options => chance.sentence(!isPrimitive(options) ? options : undefined);
const paragraph = options => chance.paragraph(!isPrimitive(options) ? options : undefined);
const url = options => chance.url(!isPrimitive(options) ? options : undefined);
const boolean = options => chance.bool(!isPrimitive(options) ? options : undefined);
const email = options => chance.email(!isPrimitive(options) ? options : undefined);
const date = options => chance.date(_objectSpread2({
string: true
}, options));
const fromList = list => chance.pickone(list);
const subList = (list, {
size
}) => chance.pickset(list, size);
function word(options = {}) {
return options.length ? chance.word(options) : chance.word(_objectSpread2({
syllables: 3
}, !isPrimitive(options) ? options : undefined));
}
const DEFAULT_SIZE_RANGE = {
max: 20,
min: 1
};
function listOf(factory, options = {}) {
const listSize = options.size || integer(_objectSpread2(_objectSpread2({}, DEFAULT_SIZE_RANGE), options));
if (options.uniqueOn) {
const uniqueValues = {};
while (Object.keys(uniqueValues).length < listSize) {
const item = factory(Object.keys(uniqueValues).length);
uniqueValues[item[options.uniqueOn]] = item;
}
return _.values(uniqueValues);
}
const list = [];
for (let i = 0; i < listSize; i += 1) {
list.push(factory(i));
}
return list;
}
function simpleObject () {
const object = {};
const size = integer(DEFAULT_SIZE_RANGE);
for (let i = 0; i < size; i += 1) {
object[word()] = string();
}
return object;
}
function objectWithKeys (keys, options = {}) {
return keys.map((key, index) => options.factory ? [key, options.factory(key, index)] : [key, string()]).reduce((acc, [key, value]) => _objectSpread2(_objectSpread2({}, acc), {}, {
[key]: value
}), {});
}
var index = {
string,
word,
sentence,
paragraph,
integer,
float,
boolean,
url,
email,
date,
simpleObject,
objectWithKeys,
listOf,
fromList,
subList
};
export { index as default };
//# sourceMappingURL=any.mjs.map