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

randoc

v1.1.0

Published

Generates random documents based on a simple schema for your test or stub data

Downloads

13

Readme

Randoc

Generates random documents based on a simple schema using Chance.js functions.

npm version Build Status Dependency Status Open Source Love

Handy for creating test / stub data.

Getting started

Install

$ npm install --save randoc

Create one random document

const { randomDocument } = require('randoc');

const oneDoc = randomDocument({ name: 'name', age: 'age' });

// { name: 'Jorge Floyd', age: 60 }

Where name and age are both Chance.js functions.

Create many random documents

const { randomDocuments } = require('randoc');

const lots = randomDocuments({ name: 'first', employed: 'bool' }, 7);

/* [ { name: 'Chase', employed: false },
  { name: 'Alejandro', employed: true },
  { name: 'Lewis', employed: true },
  { name: 'Lulu', employed: true },
  { name: 'Ora', employed: false },
  { name: 'Tony', employed: false },
  { name: 'Nellie', employed: false }]
*/

Schema

The schema types randoc uses loosely map to functions offered by Chance.js, with a few additional options.

Simple Chance.js types

The simplest schema looks something like:

const schema = { isMonday: 'bool' };
const doc = randomDocument(schema);
// { isMonday: false }

Passing an argument to the Chance.js function

const schema = { isMonday: { _type: 'bool', args: { likelihood: 1/7 } } };
const doc = randomDocument(schema);
// { isMonday: false }

Another example:

randomDocument({ name: 'name', age: { _type: 'natural', args: { max: 80 } } });
// { name: 'Norman McCoy', age: 9 }

Nested objects

randoc supports nested objects:

const schema = { isMonday: 'bool', weather: { rain: 'bool', snow: 'bool' } };
const doc = randomDocument(schema);
// { isMonday: false, weather: { rain: true, snow: true } }

Arrays of Chance.js types

By default, the array will have a length of 1.

const schema = { professions: { _type: 'profession', _array: true } },
};
const doc = randomDocument(schema);
// { professions: ['Teacher'] }

You can specify the length of the array and the likelihood of it being empty. The example below has a 30% chance of including an empty array. Otherwise it will be an array of length 3.

const schema = { professions: { _type: 'profession', _array: { empty: 30, length: 3 } } },
};
const doc = randomDocument(schema);
// { professions: [] }
// or
// { professions: ['Software Developer', 'Recreational Director' 'Landscape Architect'] }

Arrays of objects

If provided with an array, randoc will create a random document for each element.

const schema = { days: [ { isMonday: 'bool' } ] };
const doc = randomDocument(schema);
// { days: [ { isMonday: false } ] }
const schema = { days: new Array(10).fill({ isMonday: 'bool' }) };
const doc = randomDocument(schema);
/* { days:
   [ { isMonday: false },
     { isMonday: true },
     { isMonday: true },
     { isMonday: false },
     { isMonday: false },
     { isMonday: false },
     { isMonday: true },
     { isMonday: false },
     { isMonday: false },
     { isMonday: true } ] }
  */

Special types

These are types not offered directly by Chance.js functions.

Enum

You may want to pick a value from a list of options:

const schema = { status: { _type: 'enum', options: ['new', 'available', 'expired'] } };
const doc = randomDocument(schema);
// { status: 'available' }

Properties that may not exist

The example below has a 70% chance of including a status property:

const schema = { name: 'name', status: { _type: 'enum', _exists: 70, options: ['new', 'available', 'expired'] } };
const doc = randomDocument(schema);
//

Here is an example for an array of Chance.js generated cities which has a 30% chance of being empty or otherwise a length of three.

const schema = { cities: { _type: 'city', _exists: 30, _arrayOf: 3 } };
const doc = randomDocument(schema);
// cities: []
// or
// cities: ['Johannesburg', 'London', 'Singapore']

Note that _exists is currently only available for "special" types and arrays.

Unknown types

If there is no Chance.js function for the _type you've provided, randoc will default to using chance.string().

A more complete example

Here's an example schema that showcases some of the available functionality:

const schema = {
  widget: {
    name: 'string',
    storeId: {
      _type: 'enum',
      options: [543, 999, 1232, 110],
    },
    deleted: {
      _type: 'bool',
      args: {
        likelihood: 5,
      },
    },
    startDate: 'date',
    outOfStock: {
      _type: 'bool',
      args: {
        likelihood: 10,
      },
    },
    discountable: {
      _type: 'bool',
      args: {
        likelihood: 90,
      },
    },
  },
  status: {
    _type: 'enum',
    options: ['new', 'active', 'cancelled', ''],
  },
};
/*
{ widget:
   { name: 'vU9SpLn3ZfsW3hud%DT',
     storeId: 1232,
     deleted: false,
     startDate: 2103-10-13T14:26:33.440Z,
     outOfStock: false,
     discountable: true },
  status: 'new' }
*/

License

MIT