make-mp-data 1.1.19 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/utils.js CHANGED
@@ -9,16 +9,20 @@ dayjs.extend(utc);
9
9
 
10
10
  function pick(items) {
11
11
  if (!Array.isArray(items)) {
12
- try {
13
- const choice = chance.pickone(this);
14
- return choice;
15
- }
16
- catch (e) {
17
- return null;
12
+ if (typeof items === 'function') {
13
+ const selection = items();
14
+ if (Array.isArray(selection)) {
15
+ return chance.pickone(selection);
16
+ }
17
+ else {
18
+ return selection;
19
+ }
18
20
  }
21
+ return items;
22
+
19
23
  }
20
24
  return chance.pickone(items);
21
- }
25
+ };
22
26
 
23
27
  function date(inTheLast = 30, isPast = true, format = 'YYYY-MM-DD') {
24
28
  const now = dayjs.utc();
@@ -48,7 +52,7 @@ function date(inTheLast = 30, isPast = true, format = 'YYYY-MM-DD') {
48
52
  if (!format) return now?.toISOString();
49
53
  }
50
54
  };
51
- }
55
+ };
52
56
 
53
57
  function dates(inTheLast = 30, numPairs = 5, format = 'YYYY-MM-DD') {
54
58
  const pairs = [];
@@ -57,7 +61,7 @@ function dates(inTheLast = 30, numPairs = 5, format = 'YYYY-MM-DD') {
57
61
  }
58
62
  return pairs;
59
63
 
60
- }
64
+ };
61
65
 
62
66
  function day(start, end) {
63
67
  const format = 'YYYY-MM-DD';
@@ -74,7 +78,7 @@ function day(start, end) {
74
78
  };
75
79
  };
76
80
 
77
- }
81
+ };
78
82
 
79
83
  function choose(value) {
80
84
  if (typeof value === 'function') {
@@ -85,13 +89,13 @@ function choose(value) {
85
89
  }
86
90
 
87
91
  return value;
88
- }
92
+ };
89
93
 
90
94
  function exhaust(arr) {
91
95
  return function () {
92
96
  return arr.shift();
93
97
  };
94
- }
98
+ };
95
99
 
96
100
 
97
101
  function integer(min, max) {
@@ -114,7 +118,7 @@ function integer(min, max) {
114
118
  }
115
119
 
116
120
  return 0;
117
- }
121
+ };
118
122
 
119
123
  function makeHashTags() {
120
124
  const popularHashtags = [
@@ -138,7 +142,7 @@ function makeHashTags() {
138
142
  hashtags.push(chance.pickone(popularHashtags));
139
143
  }
140
144
  return hashtags;
141
- }
145
+ };
142
146
 
143
147
  function makeProducts() {
144
148
  let categories = ["Device Accessories", "eBooks", "Automotive", "Baby Products", "Beauty", "Books", "Camera & Photo", "Cell Phones & Accessories", "Collectible Coins", "Consumer Electronics", "Entertainment Collectibles", "Fine Art", "Grocery & Gourmet Food", "Health & Personal Care", "Home & Garden", "Independent Design", "Industrial & Scientific", "Accessories", "Major Appliances", "Music", "Musical Instruments", "Office Products", "Outdoors", "Personal Computers", "Pet Supplies", "Software", "Sports", "Sports Collectibles", "Tools & Home Improvement", "Toys & Games", "Video, DVD & Blu-ray", "Video Games", "Watches"];
@@ -172,7 +176,7 @@ function makeProducts() {
172
176
  }
173
177
 
174
178
  return data;
175
- }
179
+ };
176
180
 
177
181
  // Box-Muller transform to generate standard normally distributed values
178
182
  function boxMullerRandom() {
@@ -180,7 +184,7 @@ function boxMullerRandom() {
180
184
  while (u === 0) u = Math.random();
181
185
  while (v === 0) v = Math.random();
182
186
  return Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v);
183
- }
187
+ };
184
188
 
185
189
  // Apply skewness to the value
186
190
  function applySkew(value, skew) {
@@ -188,12 +192,12 @@ function applySkew(value, skew) {
188
192
  // Adjust the value based on skew
189
193
  let sign = value < 0 ? -1 : 1;
190
194
  return sign * Math.pow(Math.abs(value), skew);
191
- }
195
+ };
192
196
 
193
197
  // Map standard normal value to our range
194
198
  function mapToRange(value, mean, sd) {
195
199
  return Math.round(value * sd + mean);
196
- }
200
+ };
197
201
 
198
202
  function weightedRange(min, max, size = 100, skew = 1) {
199
203
  const mean = (max + min) / 2;
@@ -214,14 +218,12 @@ function weightedRange(min, max, size = 100, skew = 1) {
214
218
  }
215
219
 
216
220
  return array;
217
- }
221
+ };
218
222
 
219
223
  function progress(thing, p) {
220
224
  readline.cursorTo(process.stdout, 0);
221
225
  process.stdout.write(`${thing} processed ... ${comma(p)}`);
222
- }
223
-
224
-
226
+ };
225
227
 
226
228
  function range(a, b, step = 1) {
227
229
  step = !step ? 1 : step;
@@ -241,7 +243,7 @@ function openFinder(path, callback) {
241
243
  p.kill();
242
244
  return callback(err);
243
245
  });
244
- }
246
+ };
245
247
 
246
248
  function getUniqueKeys(data) {
247
249
  const keysSet = new Set();
@@ -249,7 +251,7 @@ function getUniqueKeys(data) {
249
251
  Object.keys(item).forEach(key => keysSet.add(key));
250
252
  });
251
253
  return Array.from(keysSet);
252
- }
254
+ };
253
255
 
254
256
  //makes a random-sized array of emojis
255
257
  function generateEmoji(max = 10, array = false) {
@@ -264,74 +266,8 @@ function generateEmoji(max = 10, array = false) {
264
266
  if (!array) return arr.join(', ');
265
267
  return "🤷";
266
268
  };
267
- }
268
-
269
- function generateName() {
270
- var adjs = [
271
- "autumn", "hidden", "bitter", "misty", "silent", "empty", "dry", "dark",
272
- "summer", "icy", "delicate", "quiet", "white", "cool", "spring", "winter",
273
- "patient", "twilight", "dawn", "crimson", "wispy", "weathered", "blue",
274
- "billowing", "broken", "cold", "damp", "falling", "frosty", "green",
275
- "long", "late", "lingering", "bold", "little", "morning", "muddy", "old",
276
- "red", "rough", "still", "small", "sparkling", "throbbing", "shy",
277
- "wandering", "withered", "wild", "black", "young", "holy", "solitary",
278
- "fragrant", "aged", "snowy", "proud", "floral", "restless", "divine",
279
- "polished", "ancient", "purple", "lively", "nameless", "gentle", "gleaming", "furious", "luminous", "obscure", "poised", "shimmering", "swirling",
280
- "sombre", "steamy", "whispering", "jagged", "melodic", "moonlit", "starry", "forgotten",
281
- "peaceful", "restive", "rustling", "sacred", "ancient", "haunting", "solitary", "mysterious",
282
- "silver", "dusky", "earthy", "golden", "hallowed", "misty", "roaring", "serene", "vibrant",
283
- "stalwart", "whimsical", "timid", "tranquil", "vast", "youthful", "zephyr", "raging",
284
- "sapphire", "turbulent", "whirling", "sleepy", "ethereal", "tender", "unseen", "wistful"
285
- ];
286
-
287
- var nouns = [
288
- "waterfall", "river", "breeze", "moon", "rain", "wind", "sea", "morning",
289
- "snow", "lake", "sunset", "pine", "shadow", "leaf", "dawn", "glitter",
290
- "forest", "hill", "cloud", "meadow", "sun", "glade", "bird", "brook",
291
- "butterfly", "bush", "dew", "dust", "field", "fire", "flower", "firefly",
292
- "feather", "grass", "haze", "mountain", "night", "pond", "darkness",
293
- "snowflake", "silence", "sound", "sky", "shape", "surf", "thunder",
294
- "violet", "water", "wildflower", "wave", "water", "resonance", "sun",
295
- "wood", "dream", "cherry", "tree", "fog", "frost", "voice", "paper",
296
- "frog", "smoke", "star", "glow", "wave", "riverbed", "cliff", "deluge", "prairie", "creek", "ocean",
297
- "peak", "valley", "starlight", "quartz", "woodland", "marsh", "earth", "canopy",
298
- "petal", "stone", "orb", "gale", "bay", "canyon", "watercourse", "vista", "raindrop",
299
- "boulder", "grove", "plateau", "sand", "mist", "tide", "blossom", "leaf", "flame",
300
- "shade", "coil", "grotto", "pinnacle", "scallop", "serenity", "abyss", "skyline",
301
- "drift", "echo", "nebula", "horizon", "crest", "wreath", "twilight", "balm", "glimmer"
302
- ];
303
-
304
- var verbs = [
305
- "dancing", "whispering", "flowing", "shimmering", "swirling", "echoing", "sparkling", "glistening",
306
- "cascading", "drifting", "glowing", "rippling", "quivering", "singing", "twinkling", "radiating",
307
- "enveloping", "enchanting", "captivating", "embracing", "embracing", "illuminating", "pulsating", "gliding",
308
- "soaring", "wandering", "meandering", "dazzling", "cuddling", "embracing", "caressing", "twisting",
309
- "twirling", "tumbling", "surging", "glimmering", "gushing", "splashing", "rolling", "splintering",
310
- "splintering", "crescendoing", "whirling", "bursting", "shining", "gushing", "emerging", "revealing",
311
- "emerging", "unfolding", "unveiling", "emerging", "surrounding", "unveiling", "materializing", "revealing"
312
- ];
313
-
314
- var adverbs = [
315
- "gracefully", "softly", "smoothly", "gently", "tenderly", "quietly", "serenely", "peacefully",
316
- "delicately", "effortlessly", "subtly", "tranquilly", "majestically", "silently", "calmly", "harmoniously",
317
- "elegantly", "luminously", "ethereally", "mysteriously", "sublimely", "radiantly", "dreamily", "ethereally",
318
- "mesmerizingly", "hypnotically", "mystically", "enigmatically", "spellbindingly", "enchantingly", "fascinatingly",
319
- "bewitchingly", "captivatingly", "entrancingly", "alluringly", "rapturously", "seductively", "charismatically",
320
- "seductively", "envelopingly", "ensnaringly", "entrancingly", "intoxicatingly", "irresistibly", "transcendentally",
321
- "envelopingly", "rapturously", "intimately", "intensely", "tangibly", "vividly", "intensely", "deeply"
322
- ];
323
-
324
-
325
- // ? http://stackoverflow.com/a/17516862/103058
326
- var adj = adjs[Math.floor(Math.random() * adjs.length)];
327
- var noun = nouns[Math.floor(Math.random() * nouns.length)];
328
- var verb = verbs[Math.floor(Math.random() * verbs.length)];
329
- var adverb = adverbs[Math.floor(Math.random() * adverbs.length)];
330
-
331
-
332
- return adj + '-' + noun + '-' + verb + '-' + adverb;
269
+ };
333
270
 
334
- }
335
271
 
336
272
  function person(bornDaysAgo = 30) {
337
273
  //names and photos
@@ -373,25 +309,55 @@ function person(bornDaysAgo = 30) {
373
309
  anonymousIds,
374
310
  sessionIds
375
311
  };
312
+ };
313
+
314
+
315
+ function weighList(items, mostChosenIndex) {
316
+ if (mostChosenIndex > items.length) mostChosenIndex = items.length;
317
+ return function () {
318
+ const weighted = [];
319
+ for (let i = 0; i < 10; i++) {
320
+ if (chance.bool({ likelihood: integer(10, 35) })) {
321
+ if (chance.bool({ likelihood: 50 })) {
322
+ weighted.push(items[mostChosenIndex]);
323
+ }
324
+ else {
325
+ const rand = chance.d10();
326
+ const addOrSubtract = chance.bool({ likelihood: 50 }) ? -rand : rand;
327
+ let newIndex = mostChosenIndex + addOrSubtract;
328
+ if (newIndex < 0) newIndex = 0;
329
+ if (newIndex > items.length) newIndex = items.length;
330
+ weighted.push(items[newIndex]);
331
+ }
332
+ }
333
+ else {
334
+ weighted.push(chance.pickone(items));
335
+ }
336
+ }
337
+ return weighted;
338
+
339
+ };
376
340
  }
377
341
 
378
342
  module.exports = {
379
- weightedRange,
380
343
  pick,
344
+ date,
345
+ dates,
381
346
  day,
347
+ choose,
348
+ exhaust,
382
349
  integer,
350
+ makeHashTags,
383
351
  makeProducts,
384
- date,
352
+ boxMullerRandom,
353
+ applySkew,
354
+ mapToRange,
355
+ weightedRange,
385
356
  progress,
386
- choose,
387
357
  range,
388
- exhaust,
389
358
  openFinder,
390
- applySkew,
391
- boxMullerRandom,
392
- generateEmoji,
393
359
  getUniqueKeys,
394
- makeHashTags,
395
- generateName,
396
- person
360
+ generateEmoji,
361
+ person,
362
+ weighList
397
363
  };