UNPKG

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