Install Benthos with NPM
npm install benthos
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();
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' })
}
});
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 | Description | Example |
address() |
Return a random address in US mailing address format |
|
boolean() |
Return a boolean value, TRUE or FALSE |
|
color([format:string]) |
Return random color for given format, default in hex format |
|
country([isCode:bool]) |
Return a country name or country code |
|
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())
|
|
dateIn([range:string]) |
Return a random date object within given range, the default <range> is 'week', <range> could be 'day', 'week', 'month' or 'year' |
|
domain() |
Return a random domain |
|
email([domain:string]) |
Return an email address with given domain, Random domain would be used if no domain provided |
|
firstName([gender:string]) |
Return a random first name If gender provided, only return those first names for the given gender |
|
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' |
|
gender([isNum:bool]) |
Return a gender string If <isNum> set to true, the result would be a number, 0 (female) or 1 (male) |
|
hash([content:string]) |
Return the hash for given content If no <content> provided, a random string would be used |
|
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 |
|
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 |
|
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' |
|
ipv4() |
Return a random ipv4 address |
|
ipv6() |
Return a random ipv6 address |
|
lastName() |
Return a random last name |
|
md5([content:string]) |
Return md5 string for given content If no <content> provided, a random string would be used |
|
name([gender:string]) |
Return a random name If gender provided, only return those first names for the given gender |
|
phone([format:string]) |
Return a phone number with given format
The default format is (***)***-**** |
|
serial([format:string]) |
Return a serial number with given format
The default format is ***-**-**** |
|
street() |
Return a random street adress |
|
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 |
|
tag([symbol:string]) |
Return a social tag start with given symbol
the default <symbol> is # |
|
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 |
|
uuid() |
Return a RFC v4 uuid |
|
words([count:int]) |
Return dummy text for given counts
The default <count> is 10 |
|