UNPKG

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