This module allows you to generate completely random variables, which is sometimes useful in testing. There are situations when you want to write a little property-based test, but you don't want to use a full framework for that.
That's where Anything comes in.
Anything is a tiny, zero-dependency utility for generating random stuff.
Install it with yarn:
yarn add -D any-thing
Or, if you use npm:
npm install --save-dev any-thing
An example of usage with Jest:
import _ from 'lodash'
import { anything } from 'any-thing'
test('makeOkResponse', () => {
/* a tiny property-based test */
_.times(100, () => {
const thing = anything()
expect(makeOkResponse(thing)).toEqual({ status: "OK", payload: thing })
})
})
Anything includes 9 generators, which produce, respectively:
- Arrays
- Booleans
- Functions
- Integers
- Numbers (floats)
- Objects
- Strings
null
undefined
Arrays, functions and objects may also be nested.
Generates anything. You may provide options for each generator. Options is an object that contains options for all (or some) generators. The overall shape is:
{
array: { /* array options */ },
boolean: { /* boolean options */ },
function: { /* function options */ },
integer: { /* integer options */ },
/* and so on for other generators */
}
See also: Generator options.
Generates anything, but does not use specified generators. For example, anything.but(Array, Object)
will generate anything but an array or an object.
Alias for anything.but
.
Generates anything using a whitelist of generators. For example, anything.from(null, undefined)
will return either null
or undefined
.
There's also a function for each generator:
anyArray()
anyBoolean()
anyFunction()
anyInteger()
anyNumber()
anyObject()
anyString()
These functions accept options, which are specific for each generator.
minLength
: minimum length of the array. Default: 0maxLength
: maximum length of the array. Default: 10maxDepth
: maximum depth ("nestedness") of the array. Default: 3
Example:
anyArray({ minLength: 1, maxLength: 2, maxDepth: 2 })
// => [ -923712 ]
This generator doesn't have any options.
Example:
anyBoolean()
// => true
maxDepth
: maximum depth of the function. Default: 3
Example:
anyFunction({ maxDepth: 2 })
// => function () { return 23123 }
min
: minimum value. Default: -1,000,000max
: maximum value. Default: 1,000,000
Example:
anyInteger({ min: 0, max: 42 })
// => 42
// i knew I'd get 42
// what else could it be
min
: minimum value. Default: -1,000,000max
: maximum value. Default: 1,000,000
Example:
anyNumber({ min: 0, max: 42 })
// => 30.172931281536908
minLength
: minimum length. Default: 0maxLength
: maximum length. Default: 10alphabet
: characters to use when generating a stirng. May be a string or an array. Default:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Example:
anyString({ minLength: 10, maxLength: 30, alphabet: '(!*@#!@ )' })
// => '! @ !#*(!*@)('
minLength
: minimum object length (number of entries). Default: 0maxLength
: maximum object length. Default: 10maxDepth
: maximum object depth. Default: 3minStringLength
: minimum length of keys. Default: 1maxStringLength
: maximum length of keys. Default: 20alphabet
: alphabet to use when generating keys. Defaults to String generator's default
Example:
anyObject({
minLength: 0,
maxLength: 2,
maxDepth: 1,
minStringLength: 1,
maxStringLength: 4,
alphabet: '!@#$%^'
})
// => {}
// of course that's what I get
// rng often seems like it's not random at all