UNPKG

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