UNPKG

3.93 kBJavaScriptView Raw
1import Chance from 'chance';
2import MersenneTwister from 'mersenne-twister';
3import _ from 'lodash';
4
5function ownKeys(object, enumerableOnly) {
6 var keys = Object.keys(object);
7
8 if (Object.getOwnPropertySymbols) {
9 var symbols = Object.getOwnPropertySymbols(object);
10 enumerableOnly && (symbols = symbols.filter(function (sym) {
11 return Object.getOwnPropertyDescriptor(object, sym).enumerable;
12 })), keys.push.apply(keys, symbols);
13 }
14
15 return keys;
16}
17
18function _objectSpread2(target) {
19 for (var i = 1; i < arguments.length; i++) {
20 var source = null != arguments[i] ? arguments[i] : {};
21 i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
22 _defineProperty(target, key, source[key]);
23 }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
24 Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
25 });
26 }
27
28 return target;
29}
30
31function _defineProperty(obj, key, value) {
32 if (key in obj) {
33 Object.defineProperty(obj, key, {
34 value: value,
35 enumerable: true,
36 configurable: true,
37 writable: true
38 });
39 } else {
40 obj[key] = value;
41 }
42
43 return obj;
44}
45
46const debug = require('debug')('any');
47
48const generator = new MersenneTwister(); // Multiply the random seed to match chance.js
49
50const seed = process.env.ANY_SEED || generator.random() * Math.pow(10, 13);
51debug(`randomness seed: ${seed}`);
52
53function isPrimitive(value) {
54 return value !== Object(value);
55}
56
57const chance = new Chance(seed);
58const integer = options => chance.natural(!isPrimitive(options) ? options : undefined);
59const float = options => chance.floating(!isPrimitive(options) ? options : undefined);
60const string = options => chance.string(!isPrimitive(options) ? options : undefined);
61const sentence = options => chance.sentence(!isPrimitive(options) ? options : undefined);
62const paragraph = options => chance.paragraph(!isPrimitive(options) ? options : undefined);
63const url = options => chance.url(!isPrimitive(options) ? options : undefined);
64const boolean = options => chance.bool(!isPrimitive(options) ? options : undefined);
65const email = options => chance.email(!isPrimitive(options) ? options : undefined);
66const date = () => chance.date({
67 string: true
68});
69const fromList = list => chance.pickone(list);
70const subList = (list, {
71 size
72}) => chance.pickset(list, size);
73function word(options = {}) {
74 return options.length ? chance.word(options) : chance.word(_objectSpread2({
75 syllables: 3
76 }, !isPrimitive(options) ? options : undefined));
77}
78const DEFAULT_SIZE_RANGE = {
79 max: 20,
80 min: 1
81};
82
83function listOf(factory, options = {}) {
84 const listSize = options.size || integer(_objectSpread2(_objectSpread2({}, DEFAULT_SIZE_RANGE), options));
85
86 if (options.uniqueOn) {
87 const uniqueValues = {};
88
89 while (Object.keys(uniqueValues).length < listSize) {
90 const item = factory(Object.keys(uniqueValues).length);
91 uniqueValues[item[options.uniqueOn]] = item;
92 }
93
94 return _.values(uniqueValues);
95 }
96
97 const list = [];
98
99 for (let i = 0; i < listSize; i += 1) {
100 list.push(factory(i));
101 }
102
103 return list;
104}
105
106function simpleObject () {
107 const object = {};
108 const size = integer(DEFAULT_SIZE_RANGE);
109
110 for (let i = 0; i < size; i += 1) {
111 object[word()] = string();
112 }
113
114 return object;
115}
116
117function objectWithKeys (keys, options = {}) {
118 return keys.map((key, index) => options.factory ? [key, options.factory(key, index)] : [key, string()]).reduce((acc, [key, value]) => _objectSpread2(_objectSpread2({}, acc), {}, {
119 [key]: value
120 }), {});
121}
122
123var index = {
124 string,
125 word,
126 sentence,
127 paragraph,
128 integer,
129 float,
130 boolean,
131 url,
132 email,
133 date,
134 simpleObject,
135 objectWithKeys,
136 listOf,
137 fromList,
138 subList
139};
140
141export { index as default };
142//# sourceMappingURL=any.mjs.map