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

any-thing

v1.0.0

Published

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.

Downloads

1,114

Readme

Anything ¯\_(ツ)_/¯

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.

Anything in a nutshell

Example

Installation

Install it with yarn:

yarn add -D any-thing

Or, if you use npm:

npm install --save-dev any-thing

Usage

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.

API

anything(options = {})

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.

anything.but(generators..., options = {})

Generates anything, but does not use specified generators. For example, anything.but(Array, Object) will generate anything but an array or an object.

anything.except(generators..., options = {})

Alias for anything.but.

anything.from(generators..., options = {})

Generates anything using a whitelist of generators. For example, anything.from(null, undefined) will return either null or undefined.

Specific generators

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.

Generator options

Array

  • minLength: minimum length of the array. Default: 0
  • maxLength: maximum length of the array. Default: 10
  • maxDepth: maximum depth ("nestedness") of the array. Default: 3

Example:

anyArray({ minLength: 1, maxLength: 2, maxDepth: 2 })
// => [ -923712 ]

Boolean

This generator doesn't have any options.

Example:

anyBoolean()
// => true

Function

  • maxDepth: maximum depth of the function. Default: 3

Example:

anyFunction({ maxDepth: 2 })
// => function () { return 23123 }

Integer

  • min: minimum value. Default: -1,000,000
  • max: maximum value. Default: 1,000,000

Example:

anyInteger({ min: 0, max: 42 })
// => 42
// i knew I'd get 42
// what else could it be

Number (float)

  • min: minimum value. Default: -1,000,000
  • max: maximum value. Default: 1,000,000

Example:

anyNumber({ min: 0, max: 42 })
// => 30.172931281536908

String

  • minLength: minimum length. Default: 0
  • maxLength: maximum length. Default: 10
  • alphabet: characters to use when generating a stirng. May be a string or an array. Default: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789

Example:

anyString({ minLength: 10, maxLength: 30, alphabet: '(!*@#!@ )' })
// => '! @ !#*(!*@)('

Object

  • minLength: minimum object length (number of entries). Default: 0
  • maxLength: maximum object length. Default: 10
  • maxDepth: maximum object depth. Default: 3
  • minStringLength: minimum length of keys. Default: 1
  • maxStringLength: maximum length of keys. Default: 20
  • alphabet: 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