UNPKG

4.33 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 enumerableOnly && (symbols = symbols.filter(function (sym) {
19 return Object.getOwnPropertyDescriptor(object, sym).enumerable;
20 })), keys.push.apply(keys, symbols);
21 }
22
23 return keys;
24}
25
26function _objectSpread2(target) {
27 for (var i = 1; i < arguments.length; i++) {
28 var source = null != arguments[i] ? arguments[i] : {};
29 i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
30 _defineProperty(target, key, source[key]);
31 }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
32 Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
33 });
34 }
35
36 return target;
37}
38
39function _defineProperty(obj, key, value) {
40 if (key in obj) {
41 Object.defineProperty(obj, key, {
42 value: value,
43 enumerable: true,
44 configurable: true,
45 writable: true
46 });
47 } else {
48 obj[key] = value;
49 }
50
51 return obj;
52}
53
54const debug = require('debug')('any');
55
56const generator = new MersenneTwister__default["default"](); // 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__default["default"](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 ___default["default"].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
149module.exports = index;
150//# sourceMappingURL=any.js.map