Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including mockdata-generator with all npm packages installed. Try it out:

var mockdataGenerator = require("mockdata-generator")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

mockdata-generator v0.2.0

Collection of data generators to create complex data structures and any number of records

Generator

A library of mock data object generator. Allows developers and users to define real life looking mock data.

Installation

npm install mockdata-generator --save-dev

Usage

The following is a complete program that generates 10 records of data. Each of the valid dataTypes is included in the example


    /**
     * This will get the constructor of the main class
     * The constructor accepts a configuration object with these properties
     * 'name': An optional name of the object
     * 'metadata': An array of descriptions for each of the attributes of the object
     **/
    "use strict";

    var Generator,
        meta,
        gen,
        pretty,
        value;

    Generator = require('./index.js').Generator;
    pretty = require('js-object-pretty-print').pretty;

    meta = {
        'name': 'Mockdata test set',
        'metadata': [
            {
                'attributeName': 'creditCards',
                'dataType': 'Array',
                'metadata': [
                    {
                        'attributeName': 'ccNumber',
                        'dataType': 'Pattern',
                        'pattern': '####-####-####-####'
                    },
                    {
                        'attributeName': 'cvv',
                        'dataType': 'Pattern',
                        'pattern': '###'
                    },
                    {
                        'attributeName': 'expiration',
                        'dataType': 'Date',
                        'min': 30,
                        'max': 1000
                    },
                    {
                        'attributeName': 'limit',
                        'dataType': 'OneOfList',
                        'list': [500, 1000, 2000, 5000, 10000],
                        'mode': 'shuffle'
                    }
                ],
                'min': 2,
                'max': 5
            },
            {
                'attributeName': 'balance',
                'dataType': 'Currency',
                'min': 50,
                'max': 1000
            },
            {
                'attributeName': 'txDate',
                'dataType': 'Date',
                'min': 10,
                'max': 30
            },
            {
                'attributeName': 'id',
                'dataType': 'Guid',
                'prefix': 'test'
            },
            {
                'attributeName': 'branches',
                'dataType': 'Integer',
                'min': 10,
                'max': 30
            },
            {
                'attributeName': 'description',
                'dataType': 'Ipsum',
                'paragraphNum': 1,
                'wordMin': 10,
                'wordMax': 30
            },
            {
                'attributeName': 'accountOwner',
                'dataType': 'Name',
                'order': 'fl'
            },
            {
                'attributeName': 'beneficiaryContact',
                'dataType': 'Name',
                'order': 'lf'
            },
            {
                'attributeName': 'averageTxPerDay',
                'dataType': 'Number',
                'min': 5,
                'max': 20
            },
            {
                'attributeName': 'beneficiary',
                'dataType': 'OneOfList',
                'listName': 'ListBase.Beneficiaries',
                'mode': 'sequence',
                'index': 0
            },
            {
                'attributeName': 'clothesCategory',
                'dataType': 'OneOfList',
                'list': ['Accessories', 'Blouses', 'Cardigans', 'Dresses', 'Skirts'],
                'mode': 'shuffle'
            },
            {
                'attributeName': 'accountNumber',
                'dataType': 'Pattern',
                'pattern': '###-AA-###-aaa-####'
            },
            {
                'attributeName': 'accountName',
                'dataType': 'String',
                'min': 6,
                'max': 30
            }
        ]
    };

    gen = new Generator(meta);
    value = gen.getValue(10);
    process.stdout.write(pretty(value));

Available dataTypes

mockdata-generator provides a way to generate data in the following dataTypes

  • Address
  • Array
  • Bank
  • Currency
  • Date
  • Guid
  • Integer
  • Ipsum
  • Name
  • Number
  • Object
  • One Of List
  • Pattern
  • String

Base

The base clase for all the other generators. It implements several attributes and methods. Base is not used to generate data and it provides all the common functionality for the other data types

AttributeDefaultDescription
attributeNamen/aIdentifies the attribute inside the object. It must be unique for the object
dataTypeThe data type of the attribute. Any of the names in the dataType list above
min0The minimum value of a range. Not all the data types require a min
max1The maximum value of a range. Not all the data types require a max

Address

Generates a random address in the United States. It uses preloaded lists of street names, cities, and states. It generates a random street number a random zip code in the format #####-####. The result is an object like

{
    street: "10th",
    town: "Oakland",
    state: "Massachusetts",
    stateAbbr: "MA",
    streetNumber: 2548,
    streetType: "Street",
    streetPosition: undefined,
    zipCode: "31494-4275",
    formatted: "2548 10th Street, Oakland, 31494-4275 Massachusetts"
}

The constructor uses the following attribute:

AttributeDefaultDescription
addressTemplate{streetNumber} {street} {streetPosition}{streetType}, {town}, {zipCode} {state}Allows to define how the address is formatted

Array

Generates a number of object values in the range of (min, max).

AttributeDefaultDescription
metadatan/aAn array of metadata descriptors for the attributes in each object
min1The minimum number of values to generate
max1The maximum number of values to generate

Bank

Generates an object that describes a bank. The result is an object like

{
    name: "FirstFed Financial Corp.",
    address: "1951 Jefferson Street",
    city: "Los Angeles",
    state: "CA",
    aba: "48871-322"
}

Currency

Generates a random numeric value in the (min, max) range, assign one of the listed currencies and generates an object with three attributes: currency, value and formatted value, for instance

{
    currency: 'MXN',
    value: 943.9473666250706,
    formatted: '943.95 MXN'
}
AttributeDefaultDescription
currencyCode['AUD', 'CAD', 'CHF', 'DKK', 'EUR', 'GBP', 'HKD', 'JPY', 'MXN', 'NZD', 'PHP', 'SEK', 'SGD', 'THB', 'USD', 'ZAR']A list of the currencies to select.
decimalPlaces2Number of decimal places
decimalSeparator"."Character to separate decimal fractions
thousandsSeparator","Character to separate thousands
min0The minimum value in range
max500The maximum value in range

Date

Generates a random date in the range of (min, max) days. To create a date in the past the range can be negative. For instance (-60, 0) will generate a date in the last 60 days. The result is an object like this:

expiration: {
    value: "Thu Jun 09 2016 14:33:26 GMT-0700 (Pacific Daylight Time)",
    formatted: "Thu, Jun 09, 2016  2:33:26 PM PDT",
    days: 666
}
AttributeDefaultDescription
dateSeedtoday = new Date()Javascript date instance to use as the base for the computation. Defaults to today
min0The minimum value in range, relative to dateSeed
max30The maximum value in range, relative to dateSeed
format%cAny strftime string is supported, such as "%I:%M:%S %p". strftime has several format specifiers defined by the Open group at Open group strftime PHP added a few of its own, defined at PHP strftime. You can also see the list in the YUI3 site

%c is the preferred date and time representation for the current locale

Guid

Generates a quasi global unique identifier. It uses the YUI.guid() method and then replaces some characters with a provided prefix

AttributeDefaultDescription
prefix'mockdata-generator'Arbitrary string to prefix the guid. Default value is 'mockdata-generator'

Integer

Generates a random integer in the range (min, max)

AttributeDefaultDescription
min1The minimum value in range
max10The maximum value in range

Ipsum

Generates a random string using words taken from "Ipsum Lorem". It can generate any size of strings

AttributeDefaultDescription
phraseMin6Minimum number of words in a phrase
phraseMax10Maximum number of words in a phrase. Once the generator determines that a phrase is complete it will make sure that the first character is capitalized and that a punctuation mark is appended to the end of it
paragraphNum1Number of paragraphs to generate
wordMin50Minimum number of words in a paragraph
wordMax150Maximum number of words in a paragraph

Name

Generates a random name (first last) or (last, first). It uses the list of the most popular names for men and women, as well as the most frequent last names in the United States

AttributeDefaultDescription
orderflThe order in which the name is rendered. Use "fl" for (first last) and "lf" for (last, first)

Number

Generates a random number in the range (min, max)

AttributeDefaultDescription
min0The minimum value in range
max1The maximum value in range

Object

Generates an object with the attributes described in the metadata. This is the entry point for the module, and it can be used at any level.

AttributeDefaultDescription
metadatan/aAn array of metadata descriptors for the attributes in each object

One Of List

Chooses a value out of a list. It has two modes:

  • shuffle. It will choose at random every time it is invoked
  • sequence. It will set the initial value when instantiated, then return the values in order. It will start with the first if the list is exhausted.
AttributeDefaultDescription
listNamen/aA list to be found inside of the YUI instance object, dot notation is used to fully identify the list. For instance the name ListBase.Places will be resolved if Y.ListBase.Places exist
listn/aA comma separated list of values. This attribute supersedes listName
modeshuffleEither sequence of shuffle
indexn/aSpecifies the first element in the list to be returned if the shuffle mode is specified. If not provided a random integer in the range (0, list length - 1) will be chosen

Pattern

Generates a string that follows the defined pattern

AttributeDefaultDescription
patternn/aA sequence of characters to be replaced when generating a value. See table below

Character replacement rules

CharacterWhat is generated
aGenerates a lower case letter (a-z)
AGenerates an upper case letter (A-Z)
#Generates a digit (0-9)
@Generates a letter (A-Z) and (a-z)
\The next character will not be replaced. Useful to insert a, A, # or @ in the pattern
Any other characterWill be rendered as is

Examples (values are only representative, actual rendering is random within the category)

PatternRenders
aaakwt
AAAUYT
@@@UmY
###832
(###) ###-####(716) 837-0932
\A-AAAA-UER

String

Renders a string of random characters. The length of the string is a number in the range of (min, max)

AttributeDefaultDescription
min1The minimum value in range
max10The maximum value in range

Release History

  • 0.1.0 Initial Release
  • 0.1.1 Full code
  • 0.1.2 Full documentation
  • 0.1.3 Replace columnName for attributeName
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free