Benthos.js

ES2015 Rich Test Data Generator

Setup

Install Benthos with NPM

npm install benthos

Usage

Benthos written in ES2015, there are couple of ways using Benthos in your project.

In browser, include the bundle file which exposed a global variable benthos

<script src="node_modules/benthos/dist/benthos.min.js"></script>
<script>
    var email = benthos.email();
</script>

In CommonJS, use require

var benthos = require('benthos');
var email = benthos.email();

With ES2015 module system

import { email } from 'benthos';
let email = email();

Guides

1. Generate data from template

By using compile, you could generate data from templates, compile takes a handlebars-liked template string and return the compiled data for the given template, for example:

import {compile} from 'benthos'

const template = 'Hi! This is {{name}}, follow my on twitter {{tag("@")}}';

// Hi! This is John Smith, follow me on twitter @djcivk
let result = compile(template);

As you can see, you could call the built-in APIs within the template, if no parameters provided ({{name}}), the compiler would try to parse the template with API default parameters

If you needed, you could also pass your own properties/functions into compile:

import {compile} from 'benthos'

const template = 'Hi! This is {{myName}}, here is my blog {{myBlog}}';

// Hi! This is Marko, here is my blog http://markocen.com/blog
let result = compile(template, {
    myName: 'Marko',
    myBlog: function(){
        // all provided functions bind to benthos built-in APIs
        return this.url({host: 'markocen.com', path: 'blog' })
    }
});

2. Generate complex data structure from schema

Rather than simple data, schema method gives you the ability to generate much more complex data structures

For example, we would like to generate random user profiles, each profile should contain the following properties:

We could build the user schema, and each time we want a new test user, we just need to call the schema

import {schema} from 'benthos'

const User = schema({
    id: '{{uuid}}',
    name: '{{name}}',
    gender: '{{gender}}',
    age: '{{integer(21, 100)}}',
    address: '{{address}}',
    email: '{{email("google.com")}}',
    phone: '{{phone}}',
    ssn: '{{serial(***-**-****)}}'
})

let user = User();

When calling a schema, you could also provide your own properties/functions

import {schema} from 'benthos'

const Bio = schema({
    bio: 'Hi! This is {{myName}}, here is my blog {{myBlog}}'
})

let myBio = Bio({
    myName: 'Marko',
    myBlog: function(){
        // all provided functions bind to benthos built-in APIs
        return this.url({host: 'markocen.com', path: 'blog' })
    }
});

let amyBio = Bio({
    myName: 'Amy',
    myBlog: function(){
        return this.url()
    }
});

API Reference

Basic
Contact
Internet
Others


API Description Example
address() Return a random address in US mailing address format
//473 Bunny Street, Bluds, United States 384912
let address = benthos.address();
boolean() Return a boolean value, TRUE or FALSE
let val = benthos.boolean();
color([format:string]) Return random color for given format, default in hex format
import { color } from 'benthos';

// #e3d1f3
let hexColor = color();

// #d9c341
let hexColor2 = color('hex');

// rgb(231, 103, 43)
let rgbColor = color('rgb');

// hsl(30%, 40%, 80%)
let hslColor = color('hsl'); 
country([isCode:bool]) Return a country name or country code
// Brazil
let country = benthos.country();

// BR
let country = benthos.country(true);
date([after:Date, before:Date]) Return a random date object which greater than <after> date and less than <before> date

The default <after> date is new Date(1970, 1, 1)

The default <before> date is new Date(Date.now())
let after = new Date(2000, 1, 1);
let before = new Date(2010, 1, 1);

// Wed Nov 23 2004 10:17:06 GMT-0500
let date = benthos.date(after, before);
dateIn([range:string]) Return a random date object within given range, the default <range> is 'week',

<range> could be 'day', 'week', 'month' or 'year'
import { dateIn } from 'benthos';

// get a date within current week
let dateInThisWeek = dateIn('week');

// get a date within current month
let dateInThisMonth = dateIn('month');
domain() Return a random domain
// jdixc.org
let domain = benthos.domain()
email([domain:string]) Return an email address with given domain,

Random domain would be used if no domain provided
// diaocix12@benthos.com
let email = benthos.email('benthos.com')
firstName([gender:string]) Return a random first name

If gender provided, only return those first names for the given gender
// John or Lisa
let first = benthos.firstName()

// Lisa
let femaleFirstName = benthos.firstName('female')

// John
let maleFirstName = benthos.firstName('male')
float([precision:int, radix:string|int]) Return a float number between 0 and 1 with specified precision

The default <precision> is 2 and the default <radix> is 10

<radix> should be an integer between 1 and 100 (inclusive), and for convenience, it could also be a radix string, like 'hex', 'bin' and 'oct'
// 0.12
let float = benthos.float();

// 0.73215
let float5 = benthos.float(5)

// 0.bb6e2eb1c432c8
let floatHex = benthos.float(5, 'hex')

// 0.ndn2tce46b4
let float32 = benthos.float(5, 32);
gender([isNum:bool]) Return a gender string

If <isNum> set to true, the result would be a number, 0 (female) or 1 (male)
// Male or Female
let gender = benthos.gender();

// 0 or 1
let genderCode = benthos.gender(true);
hash([content:string]) Return the hash for given content

If no <content> provided, a random string would be used
// 99162322
let hash = benthos.hash('Hello');
identicon([hash:string, size:int]) Return an identicon image base64 string for given hash string by using identicon.js

If no <hash> provided, a random hash string would be used

The default <size> is 200
import {hash, identicon} from 'benthos';

let hash = hash('Hello');
let idt = identicon(hash);
let image = new Image();
image.src = idt;
image([width:int, height:int]) Return an random image url provide by unsplash.it with given width and height

The default <width> and <height> are 500
// https://unsplash.it/300/400?random&id=dsio9c0dm3
let imageSrc = benthos.image(300, 400);
integer([min:int, max:int, radix:string|int]) Return an integer between <min> and <max> with given <radix>

The default <min> is 0, the default <max> is 100 and the default <radix> is 10

<radix> should be an integer between 1 and 100 (inclusive), and for convenience, it could also be a radix string, like 'hex', 'bin' and 'oct'
import {integer} from 'benthos';

// integer between 100 and 1000
let int1 = integer(100, 1000);

// integer between 0 and 1000
let int2 = integer(1000);

// integer between 0 and 100 in hexadecimal string
let int3 = integer('hex');

// integer between 100 and 1000 in hexadecimal string
let int4 = integer(100, 1000, 16);
ipv4() Return a random ipv4 address
// 192.158.32.11
let ip = benthos.ipv4();
ipv6() Return a random ipv6 address
// 2001:0db8:0000:0000:0000:ff00:0042:8329
let ip = benthos.ipv6();
lastName() Return a random last name
// Freeman
let last = benthos.lastName()
md5([content:string]) Return md5 string for given content

If no <content> provided, a random string would be used
// 5d41402abc4b2a76b9719d911017c592
let md5 = benthos.md5('hello');
name([gender:string]) Return a random name

If gender provided, only return those first names for the given gender
// John Freeman or Lisa Freeman
let name = benthos.name()

// Lisa Freeman
let femaleName = benthos.name('female')

// John Freeman
let maleName = benthos.name('male')
phone([format:string]) Return a phone number with given format

The default format is (***)***-****
// (010)010-0101
let usPhone = benthos.phone()

// +861010101010
let cnPhone = benthos.phone('+8610********')
serial([format:string]) Return a serial number with given format

The default format is ***-**-****
// 010-01-0101
let serial = benthos.serial()

// SN1234
let snSerial = benthos.serial('SN****')
street() Return a random street adress
// 312 Hill Street
let street = benthos.street()
string([length:int, regex:RegExp|string]) Return a random string with given length, and each character would be matched given regex

The default <length> is 10, and the default <regex> is /[a-zA-Z0-9]/g

<regex> could be a RegExp object or a RegExp liked string
// diAXo9s0dI
let s1 = benthos.string();

// 33df4Q
let s2 = benthos.string(6);

// 381934
let s3 = benthos.string(6, '[0-9]')
tag([symbol:string]) Return a social tag start with given symbol

the default <symbol> is #
// #bibendum
let tag = benthos.tag();

// @bibendum
let at = benthos.tag('@');
url([options:object]) Return a random url with given options

the <options> could contain following properties:

host: define the hostname, a random domain would be used if this property not provided
protocol: define the protocol, default is 'http'
port: define the port in url, default is null
hash: define the hash in url, default is null
path: define the pathname in url, default is null
query: an object contains all queries in url, default is null
// http://doicidmd.info
let url = benthos.url();

// https://benthos.com:8080/docs
let url = benthos.url({
    host: 'benthos.com',
    protocol: 'https',
    port: 8080,
    path: 'docs'
});
uuid() Return a RFC v4 uuid
// 174d7f63-a6c2-4947-b41a-ebd5dbf66d11
let id = benthos.uuid();
words([count:int]) Return dummy text for given counts

The default <count> is 10
// lorem ipsum ut dictum vel
let text = benthos.words(5);