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
"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
Attribute | Default | Description |
---|
attributeName | n/a | Identifies the attribute inside the object. It must be unique for the object |
dataType | | The data type of the attribute. Any of the names in the dataType list above |
min | 0 | The minimum value of a range. Not all the data types require a min |
max | 1 | The 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:
Attribute | Default | Description |
---|
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).
Attribute | Default | Description |
---|
metadata | n/a | An array of metadata descriptors for the attributes in each object |
min | 1 | The minimum number of values to generate |
max | 1 | The 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'
}
Attribute | Default | Description |
---|
currencyCode | ['AUD', 'CAD', 'CHF', 'DKK', 'EUR', 'GBP', 'HKD', 'JPY', 'MXN', 'NZD', 'PHP', 'SEK', 'SGD', 'THB', 'USD', 'ZAR'] | A list of the currencies to select. |
decimalPlaces | 2 | Number of decimal places |
decimalSeparator | "." | Character to separate decimal fractions |
thousandsSeparator | "," | Character to separate thousands |
min | 0 | The minimum value in range |
max | 500 | The 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
}
Attribute | Default | Description |
---|
dateSeed | today = new Date() | Javascript date instance to use as the base for the computation. Defaults to today |
min | 0 | The minimum value in range, relative to dateSeed |
max | 30 | The maximum value in range, relative to dateSeed |
format | %c | Any 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
Attribute | Default | Description |
---|
prefix | 'mockdata-generator' | Arbitrary string to prefix the guid. Default value is 'mockdata-generator' |
Integer
Generates a random integer in the range (min, max)
Attribute | Default | Description |
---|
min | 1 | The minimum value in range |
max | 10 | The maximum value in range |
Ipsum
Generates a random string using words taken from "Ipsum Lorem". It can generate any size of strings
Attribute | Default | Description |
---|
phraseMin | 6 | Minimum number of words in a phrase |
phraseMax | 10 | Maximum 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 |
paragraphNum | 1 | Number of paragraphs to generate |
wordMin | 50 | Minimum number of words in a paragraph |
wordMax | 150 | Maximum 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
Attribute | Default | Description |
---|
order | fl | The 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)
Attribute | Default | Description |
---|
min | 0 | The minimum value in range |
max | 1 | The 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.
Attribute | Default | Description |
---|
metadata | n/a | An 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.
Attribute | Default | Description |
---|
listName | n/a | A 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 |
list | n/a | A comma separated list of values. This attribute supersedes listName |
mode | shuffle | Either sequence of shuffle |
index | n/a | Specifies 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
Attribute | Default | Description |
---|
pattern | n/a | A sequence of characters to be replaced when generating a value. See table below |
Character replacement rules
Character | What is generated |
---|
a | Generates a lower case letter (a-z) |
A | Generates 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 character | Will be rendered as is |
Examples (values are only representative, actual rendering is random within the category)
Pattern | Renders |
---|
aaa | kwt |
AAA | UYT |
@@@ | UmY |
| 832
(###) ###-#### | (716) 837-0932
\A-AAA | A-UER
String
Renders a string of random characters. The length of the string is a number in the range of (min, max)
Attribute | Default | Description |
---|
min | 1 | The minimum value in range |
max | 10 | The 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