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 | 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 |
|
26 | function _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 |
|
39 | function _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 |
|
54 | const debug = require('debug')('any');
|
55 |
|
56 | const generator = new MersenneTwister__default["default"]();
|
57 |
|
58 | const seed = process.env.ANY_SEED || generator.random() * Math.pow(10, 13);
|
59 | debug(`randomness seed: ${seed}`);
|
60 |
|
61 | function isPrimitive(value) {
|
62 | return value !== Object(value);
|
63 | }
|
64 |
|
65 | const chance = new Chance__default["default"](seed);
|
66 | const integer = options => chance.natural(!isPrimitive(options) ? options : undefined);
|
67 | const float = options => chance.floating(!isPrimitive(options) ? options : undefined);
|
68 | const string = options => chance.string(!isPrimitive(options) ? options : undefined);
|
69 | const sentence = options => chance.sentence(!isPrimitive(options) ? options : undefined);
|
70 | const paragraph = options => chance.paragraph(!isPrimitive(options) ? options : undefined);
|
71 | const url = options => chance.url(!isPrimitive(options) ? options : undefined);
|
72 | const boolean = options => chance.bool(!isPrimitive(options) ? options : undefined);
|
73 | const email = options => chance.email(!isPrimitive(options) ? options : undefined);
|
74 | const date = () => chance.date({
|
75 | string: true
|
76 | });
|
77 | const fromList = list => chance.pickone(list);
|
78 | const subList = (list, {
|
79 | size
|
80 | }) => chance.pickset(list, size);
|
81 | function word(options = {}) {
|
82 | return options.length ? chance.word(options) : chance.word(_objectSpread2({
|
83 | syllables: 3
|
84 | }, !isPrimitive(options) ? options : undefined));
|
85 | }
|
86 | const DEFAULT_SIZE_RANGE = {
|
87 | max: 20,
|
88 | min: 1
|
89 | };
|
90 |
|
91 | function 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 |
|
114 | function 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 |
|
125 | function 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 |
|
131 | var 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 |
|
149 | module.exports = index;
|
150 |
|