Wondering what’s next for npm?Check out our public roadmap! »

randoc

1.1.0 • Public • Published

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

Install

npm i randoc

DownloadsWeekly Downloads

16

Version

1.1.0

License

MIT

Unpacked Size

10.6 kB

Total Files

7

Last publish

Collaborators

  • avatar