npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

random-input-generator

v1.0.0

Published

Random JavaScript value generator for testing or seed data.

Downloads

6

Readme

Travis Codecov Coverage NPM Version License

Introduction

random-input-generator is a simple, easily-configurable module that generates common JavaScript values for quick and easy seed or testing data.

Issues

If you happen to find any bugs or would like to recommend suggestions for future updates, please feel free to do so in the github issue tracker.

Usage

To install the module, you can run npm install random-input-generator from your command line or you can add "random-input-generator" to your package.json and run npm install.

Documentation

For your generation pleasure, this module provides methods for generating randomized strings, booleans, numbers, objects, and arrays. Primitive-generating methods accept multiple arguments, whereas objects accept configuration objects.

To get started, simply require in the module: var randomInput = require('random-input-generator');

If you're using ES6/ES2015, feel free to use destructuring syntax: const { generateNumber, generateString, generateBoolean, generateObject, generateArray, defaultGenerator } = require('random-input-generator');

generateNumber

Parameters

  • min number? Optional number representing the minimum (inclusive) randomly generated number. (optional, default 0)
  • max number? Optional number representing the maximum (inclusive) randomly generated number. Must be greater than min. (optional, default 10000)
  • rounded boolean? Optional true/false determining whether to return an integer or a number with a decimal value. (optional, default true)

Examples

  var randomInput = require('random-input-generator');
  var generateNumber = randomInput.generateNumber;

  //with default parameters, method will return integer between 0 and 10000
  generateNumber(); //1538

  //can return a number within a specific range, or decimal value
  generateNumber(0,5,false); //4.633396897573343

generateString

Parameters

  • minLength number? Optional number representing the minimum (inclusive) length of randomly generated string. (optional, default 4)
  • maxLength number? Optional number representing the maximum (inclusive) length of a randomly generated string. (optional, default 12)
  • nonLetters boolean? Optional true/false determining whether to include non-letter characters between Unicode values 32-127. (optional, default true)
  • casing string? Optional string, either "upper" or "lower", provided if either uppercase or lowercase string is preferred. (optional)

Examples

  var randomInput = require('random-input-generator');
  var generateString = randomInput.generateString;

  //with default parameters, method will return string between 4-12 characters with non-letters
  generateString(); //'XnB%tudFH>'

  //can return a string within a specific length range
  generateString(1,4,false); //'"SX'

  //can return a string with only letters
  generateString(6,10,false); //'SHqIHoF'

  //can enforce casing
  generateString(4,8,false,"upper"); //'CTEQTUE'

generateBoolean

Parameters

  • weightPercentage number? Optional decimal value between 0 and 1 representing a weight towards either a return value of false or true. (optional, default .5)

Examples

  var randomInput = require('random-input-generator');
  var generateBoolean = randomInput.generateBoolean;

  //with default parameters, theoretically equally likely to return true or false
  generateBoolean(); //true

  //with weight of .3, theoretically <30% likely to return true
  generateBoolean(.3); //false

For non-primitive values, each generator function accepts a configuration object as its sole argument. While each specification is optional, if provided, each must conform to a specific type.

generateObject

Parameters

  • options Object? configuration Object
    • options.keyValPairs number? A specific number of key-value pairs of which the object will be comprised. (optional)
    • options.optionalSkeleton (Array<string>|Object)? An object on which to generate new Object. If Array is provided, each element will be used as a key in the generated Object and array's length will set the total number of key-value pairs in the generated Object. If Object is provided, each key-value pair will be used to extend an empty object, with any additional key-value pairs added according to options.keyValPairs or minKeyValPairs, maxKeyValPairs. (optional)
    • options.valPreference Array<string>? An array containing desired string value types for the generated Object. All strings in valPreference array must conform to one of the following value types ["string", "number", "boolean", "object", "array"]. Strings contained in valPreference will be chosen at random to generate values. Duplicate types are permitted and will effectively weight random selection towards one of these value types. (optional, default [])
    • options.minKeyValPairs number? Number representing the minimum (inclusive) possible number of key-value pairs populated in generated Object. If options.optionalSkeleton is provided, minKeyValPairs must be greater than or equal to the length of skeleton array or number of key-value pairs in skeleton Object. (optional, default 2)
    • options.maxKeyValPairs number? Number representing the maximum (inclusive) possible number of key-value pairs populated in generated Object. (optional, default 6)
    • options.maxDepth number? As randomly generating objects can potentially exceed the maximum call stack, a maxDepth option is provided to prevent object spelunking. maxDepth will dictate the depth of the deepest non-object value, at which point the generator will only be allowed to produce primitive values for key-value pairs. (optional, default 3)

Examples

  var randomInput = require('random-input-generator');
  var generateObject = randomInput.generateObject;

  //with default parameters, will return object with 2-5 randomized key-value pairs
  generateObject();
  /* {
    kzypax: true,
    jqbf: { xjjwy: '0>>+1 i5F', wncre: ' o"HYUp>`}' },
    lllhs: [ { hmdrx: false, cyflbq: '3=HQ3' }, 2398, 1877 ],
    rcauk: true
  } */

  //with optionalSkeleton array and valPreference
  generateObject({optionalSkeleton: ["dogs", "cats"], valPreference: ["number"]});
  /* {
    dogs: 6816,
    cats: 7775
  } */

  //with optionalSkeleton object and keyValuePairs
  var generateString = randomInput.generateString;
  var userConfig = {username: generateString(8,20,false,"lower"), password: generateString(15,20,true)}

  generateObject({keyValPairs: 4, optionalSkeleton: userConfig});
  /* {
    username: 'igsxcjotuqcqenebp',
    password: 'Iq}H^H?}BS[JB$rdjiAj',
    zfzrwv: 3609,
    gptci: { crcebn: [ 'Tlg>o:uW' ], ayly: 4987 }
  } */

generateArray

Parameters

  • options Object? configuration Object (optional)
    • options.setLength number? Optional number that will set a mandatory length for generated Array. (optional)
    • options.minLength number? Optional number representing the minimum (inclusive) length for a generated Array of random length. (optional, default 0)
    • options.maxLength number? Optional number representing the maximum (inclusive) length for a generated Array of random length. (optional, default 5)
    • options.valTypes Array<string>? An array containing desired string value types for the generated Array. All strings in valTypes array must conform to one of the following value types ["string", "number", "boolean", "object", "array"]. (optional, default [])
    • options.templateArray Array<string>? An optional base array to which generateArray will add values according to either options.setLength or options.minLength, options.maxLength
    • options.valueGenerator (Function|truthyValue)? Function or value to be used to populate the generated array. If an Array of identical truthy values is desired, options.valueGenerator should be assigned a truthyValue. If an Array of randomized values or identical falsey values is desired, a options.valueGenerator should be assigned a callback that returns a randomized value. Will take precedence over options.valTypes array. (optional)

Examples

  var randomInput = require('random-input-generator');
  var generateArray = randomInput.generateArray;

  //with setLength and valueGenerator as
  generateArray({setLength: 2, valueGenerator: true});
  // [ true, true ]

  //with minLength, maxLength, templateArray, valTypes
  generateArray({minLength: 4, maxLength: 6, valTypes: ['number'], templateArray:['r', 2, 'd']});
  //[ 'r', 2, 'd', 2516, 8386, 7019 ]

  //with valueGenerator as a function
  var generateString = randomInput.generateString;
  var contactGenerator = function() {
    return {
      firstName: generateString(1, 1, false, "upper") + generateString(5, 8,false, "lower"),
      lastName: generateString(1, 1, false, "upper") + generateString(4, 12,false, "lower")
    }
  };

  generateArray({setLength: 3, valueGenerator: contactGenerator});
  /* [ { firstName: 'Jvocobshd', lastName: 'Fhigzrfyio' },
  { firstName: 'Kapplphvx', lastName: 'Xmvamyoshcm' },
  { firstName: 'Hzmgbllq', lastName: 'Qyirmdecur' } ] */

License

Licensed under the MIT license.