Fantasy data generator.
Use yod-mock for more pre-defined types and modifiers..
Create a String type to generate random string.
yod.type('String', function(len) {
len = len || Math.round(Math.random() * 10);
var pool = 'abcdefghigklmnopqrstuvwxyz';
var poolLen = pool.length;
var result = '';
for (var i = 0; i < poolLen; i++) {
result += pool[Math.round(Math.random() * (poolLen - 1))]
}
return result;
});
Create a Int type to generate random integer.
yod.type('Int', function() {
return Math.round(Math.random() * 100);
});
Create a User type.
yod.type('User', {
name: '@String(5)',
age: '@Int'
});
And then you can generate random User by calling yod('@User')
.
yod('@User');
// Will create a random object like: `{name: 'atx', age: 30}`.
-
Caller: Prepend with a
"@"
character, follow by series string which like function calling.e.g:
@Bool
,@String.repeat(3).join(",")
,@Self.someKey
. -
Generator: The source that can generate other thing, in
yod
, generator can be anything.
Parameter: generator
, Type: *
Use the generator
as a generator to get other generated value.
In generator
, you can use caller string, you can execute javascript, you can get config value.
// Execute javascript, wrapped in "`" (just calling `eval` to get the result)
yod("` 1 + 3 `"); // => 4
yod("` 'a' + 3 `"); // => 'a3'
// Use caller string
yod({ a: "a", b: "a's value is @Self.a" }); // => {a: "a", b: "a's value is a"}
// get config value
yod.config("a.b", "1"); // At first, use yod.config set a value
yod("@Config.a"); // => {b: "1"}
Parameter: name
, Type: String
Parameter: generator
, Type: *
Parameter: aliases
, Type: String
or String Array
, optional
Create a new type, so that you can use it in caller string.
// Create a Bool type, and alias Boolean. it will random return true or false.
yod.type('Bool', function() {
return Math.round(Math.random() * 100) % 2 === 0;
}, 'Boolean');
// Call your new type.
yod('@Bool'); // return true or false.
yod('@Boolean'); // return true or false.
All defined types.
Check if the type name exists.
Check if the type name is a valid.
Remove all defined types.
Parameter: filters
, Type: String
or Function
or Array of String/Function
, optional
Parameter: name
, Type: String
Parameter: modifierFn
, Type: Function
Create a new modifier.
There are two type or modifier
- Value modifier: modifier generator value.
- Function modifier: modifier generator function. Create it with ":" prepend to
name
Create a value modifier: index —— Get array's index item
yod.modifier('Array', 'index', function(val, index) {
return val[index];
});
// Use it
yod({
a: ['a', 'b', 'c']
b: '@Self.a.index[1]'
});
// => {a: ['a', 'b', 'c'], b: 'b'}
Create a function modifier: repeat —— Generate value array using generator function
yod.modifier('repeat', function(generatorFn, times) {
var result = [];
for (var i = 0; i < times; i++) {
result.push(generatorFn());
}
return result;
});
// Use it (@Bool is defined in yod.type area)
yod('@Bool.repeat(3)');
// Will generator a array like this: [true, false, false]. the boolean value is random.
All defined modifiers.
Check if the modifier name exists.
Check if the modifier name is a valid.
Remove all defined modifiers.
Parameter: key
, Type: String
Parameter: val
, Type: *
Parameter: meta
, Type: *
Get or set a config key. When set a key, you can also set a meta on this key.
If you want get the value and the meta, you can append a string ":meta" to the key
,
then the result will be something like this: {val: ..., meta: ...}
// Set
yod.config('a.b', 'ab');
yod.config('a.c', 'ac', 'ac-meta');
// Get
yod.config('a'); // => {b: 'ab', c: 'ac'}
yod.config('a:meta'); // => {val: {b: 'ab', c: 'ac'}, meta: undefined}
yod.config('a.c'); // => 'ac'
yod.config('a.c:meta'); // => {val: 'ac', meta: 'ac-meta'}
// Using in caller string. (You can't get meta data in this way, but you can get it by adding a modifier)
yod('@Config.a'); // => {b: 'ab', c: 'ac'}
yod('@Config.a.c'); // => 'ac'
Type: Object
All config data.
Type: Object
All meta data.
-
Caller string's arguments can not include ")".
@String.replace(")", "") // Will parsed to `@String.replace('"')`
-
Object generator can not recycle depends.
yod({ a: '@Self.b', b: '@Self.a' }); // Will throw error.
-
Child object can't depend on it parents, similar to recycle depends.
yod({ a: { b: { c: '@Parent.Parent.a' } } }); // Will throw error.
bower install yod --save-dev
npm install yod --save
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using gulp.
Copyright (c) 2015 Zhonglei Qiu. Licensed under the MIT license.