1 | 'use strict';
|
2 |
|
3 | var Chance = require('chance');
|
4 | var MersenneTwister = require('mersenne-twister');
|
5 | var _ = require('lodash');
|
6 |
|
7 | function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
8 |
|
9 | var Chance__default = _interopDefaultLegacy(Chance);
|
10 | var MersenneTwister__default = _interopDefaultLegacy(MersenneTwister);
|
11 | var ___default = _interopDefaultLegacy(_);
|
12 |
|
13 | function 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 |
|
31 | function _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 |
|
51 | function _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 |
|
66 | const debug = require('debug')('any');
|
67 |
|
68 | const generator = new MersenneTwister__default["default"]();
|
69 |
|
70 | const seed = process.env.ANY_SEED || generator.random() * Math.pow(10, 13);
|
71 | debug(`randomness seed: ${seed}`);
|
72 |
|
73 | function isPrimitive(value) {
|
74 | return value !== Object(value);
|
75 | }
|
76 |
|
77 | const chance = new Chance__default["default"](seed);
|
78 | const integer = options => chance.natural(!isPrimitive(options) ? options : undefined);
|
79 | const float = options => chance.floating(!isPrimitive(options) ? options : undefined);
|
80 | const string = options => chance.string(!isPrimitive(options) ? options : undefined);
|
81 | const sentence = options => chance.sentence(!isPrimitive(options) ? options : undefined);
|
82 | const paragraph = options => chance.paragraph(!isPrimitive(options) ? options : undefined);
|
83 | const url = options => chance.url(!isPrimitive(options) ? options : undefined);
|
84 | const boolean = options => chance.bool(!isPrimitive(options) ? options : undefined);
|
85 | const email = options => chance.email(!isPrimitive(options) ? options : undefined);
|
86 | const date = () => chance.date({
|
87 | string: true
|
88 | });
|
89 | const fromList = list => chance.pickone(list);
|
90 | const subList = (list, {
|
91 | size
|
92 | }) => chance.pickset(list, size);
|
93 | function word(options = {}) {
|
94 | return options.length ? chance.word(options) : chance.word(_objectSpread2({
|
95 | syllables: 3
|
96 | }, !isPrimitive(options) ? options : undefined));
|
97 | }
|
98 | const DEFAULT_SIZE_RANGE = {
|
99 | max: 20,
|
100 | min: 1
|
101 | };
|
102 |
|
103 | function 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 |
|
126 | function 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 |
|
137 | function 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 |
|
143 | var 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 |
|
161 | module.exports = index;
|
162 |
|