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

slump

v3.0.2

Published

create random json

Downloads

157

Readme

slump

Create random json.

npm Node version Build Status JavaScript Style Guide

Ported from node-random-json.

Install

$ npm install slump --save

Usage

Generate a random json object:

const random = require('slump')
console.log(JSON.stringify(random.json(), null, 2))
[
  [
    null,
    0.7102621181796653,
    []
  ]
]

Api

random.bytes([size])

Returns a buffer of random bytes of size size, which defaults to one.

random.byte()

Returns a single random byte.

random.integer([signed])

Returns a 32 bit integer. If signed is trueish both negative and positive values are generated. Default is non signed integers.

random.float()

Returns a float as a result of division of two random integers. Random floats are always signed.

random.string([options[, length]])

Returns a random string in utf8 encoding.

If length is omitted the string length is a random integer between 0 and 100. So the string can be empty.

Randomizes a much longer string of bytes and cuts it off to appropriate length.

options can be used for different encodings:

  • options.enc (string) Defaults to utf8. Valid encodings are utf8, ascii, hex, base64 and base58
  • options.length (number) Length of string.
  • options.values (array) Array of predetermined strings.
const random = require('slump')
// random string with random length
const s1 = random.string()
// random string with length 20
const s2 = random.string(20)
// random base58 encoded string with length 30
const s3 = random.string({ enc: 'base58', length: 30 })
// randomize between predetermined values
const values = [ 'apples', 'oranges', 'bananas' ]
const s4 = random.string({ values: values })

random.array([length])

Returns a fixed length random array where the elements are random json values, i.e. the elements can be anything from null, false, true, another json object etc.

If length is omitted the string length is a random integer between 0 and 10. So the array can be empty.

random.obj([size])

Returns a random object with size number of random keys and values. Each property is a random.string() (with random length) and each value is a random.json().

If size is omitted the number of properties is a random integer between 0 and 10. So the object can be empty.

random.json()

Generates a random json object value, i.e. either of the following:

  • false
  • true
  • null
  • random.integer()
  • random.float()
  • random.string()
  • random.array()
  • random.obj()

Todo

  • Configuration for changing default behaviors and also per single operations, i.e. default lengths of strings etc.
  • More advanced schemas. It could be useful to generate random data that still follows a defined structure, perhaps you want arrays of only strings, number intervals and things like that.

License

All code, unless stated otherwise, is licensed under the WTFPL.