0.0.0 • Published 8 years ago
mock-compiler v0.0.0
mock-compiler
Compile template to mock date.
Table of Content
Install
$ npm install --save mock-compiler
Usage
var compiler = require('mock-compiler');
var template = {
raw : 'Mock loves you',
'number|1-10': 1,
'bool|1-10' : true,
'string|2' : 'foobar',
'array|-2' : [1, 2, 3, 4, 5],
'object|2' : { a: 1, b: 2, c: 3, d: 4 }
'function' : function () { return 'mock' },
'placeholder': '@int(1, 10)'
};
var result = compiler.compile(template);
// =>
// {
// raw : 'Mock loves you',
// number: 6,
// bool : false,
// string: 'foobarfoobar',
// array : [3, 5],
// object: { b: 2, c: 3 }
// function : 'mock',
// placeholder: 3
// }
API
compile(template, data, helpers)
Compile the given template
into random result, with optional data
and helpers
.
template
- Data template with special syntax.data
- Optional data that can be used in placeholders.helpers
- Optional runtime placeholders, which are only available in this compiling.
registerPlaceholder(name, fn, overwrite)
Register custom placeholders to the compiler, these placeholders are shared in all compiling:
// register single placeholder
compiler.registerPlaceholder('getData', function(key) {
return this.data[key];
});
// register placeholders
compiler.registerPlaceholder({
add: function (a) {
return this.data.seed + a;
},
sub: function (a) {
return this.data.seed - a;
},
rand: function () {
// use the internal placeholders
return this.int(1, 10);
}
});
Use the placeholders:
var template = {
aaa: '@getData("foo")',
bbb: '@add(2)',
ccc: '@sub(2)',
ddd: '@rand()',
eee: '@rand', // brackets can be omitted when do not take any arguments.
fff: '@add(@sub(2))', // nested placeholders
ggg: '@test("Runtime")' // use runtime placeholder
};
var result = compiler.compile(template, { foo: 'mock loves you!!', seed: 10 }, {
// runtime placeholder
test: function (name) {
return name + ' is cool.'
}
});
// =>
//{
// aaa: 'mock loves you!!'
// bbb: 12
// ccc: 8
// ddd: 6
// eee: 9
// fff: 18
// ggg: 'Runtime is cool.'
//}
Template Syntax
Every key-value in the template can be composed of three parts:
'name|rule': value
key
- name of the key.rule
- optional,key
andrule
should be separated by|
.value
- indicate the value, value type or initial value.
Rules are closely related with the type of value
. Now, let's go through rules with a variety of value types:
Rules for Number
name|[+-]step: seed
Base on seed
, increase/decrease by step
on every compiling:
{
'num1|+1' : 1,
'num2|-1' : 1,
'num3|+1.5': 3
}
name|number: value
Randomly generate a number between 0
and number
, value
indicate whether the result is an Integer or Float and it's precision:
{
'num1|99' : 1, // => 23
'num2|99' : 1.23, // => 57.82
'num2|-99': 1 // => -73
}
name|min-max: value
name|min-max.min-max: value
Rules for Boolean
Rules for Array
Rules for Object
Rules for Function
Rules for String
Placeholders
Internal Placeholders
About this
Nested Usage
Contributing
Pull requests and stars are highly welcome.
For bugs and feature requests, please create an issue.
0.0.0
8 years ago