yod
Fantasy data generator.
Use yod-mock for more pre-defined types and modifiers..
Usage
Create a String type to generate random string.
yodtype'String' { len = len || Math; var pool = 'abcdefghigklmnopqrstuvwxyz'; var poolLen = poollength; var result = ''; for var i = 0; i < poolLen; i++ result += poolMath return result;};
Create a Int type to generate random integer.
yodtype'Int' { return Math;};
Create a User type.
yodtype'User' name: '@String(5)' age: '@Int';
And then you can generate random User by calling yod('@User')
.
; // Will create a random object like: `{name: 'atx', age: 30}`.
Terminology
-
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.
API
yod(generator)
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.
Yod example:
// Execute javascript, wrapped in "`" (just calling `eval` to get the result); // => 4; // => 'a3' // Use caller string; // => {a: "a", b: "a's value is a"} // get config valueyod; // At first, use yod.config set a value; // => {b: "1"}
yod.type(name, generator [, aliases...])
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.
Type example:
// Create a Bool type, and alias Boolean. it will random return true or false.yodtype'Bool' { return Math % 2 === 0; } 'Boolean'; // Call your new type.; // return true or false.; // return true or false.
yod.types
All defined types.
yod.isTypeNameExists(name)
Check if the type name exists.
yod.isTypeNameValid(name)
Check if the type name is a valid.
yod.emptyTypes()
Remove all defined types.
yod.modifier([filters,] name, modifierFn)
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
Modifier example:
Create a value modifier: index —— Get array's index item
yod; // Use it ;// => {a: ['a', 'b', 'c'], b: 'b'}
Create a function modifier: repeat —— Generate value array using generator function
yod; // Use it (@Bool is defined in yod.type area) ;// Will generator a array like this: [true, false, false]. the boolean value is random.
yod.modifiers
All defined modifiers.
yod.isModifierNameExists(name)
Check if the modifier name exists.
yod.isModifierNameValid(name)
Check if the modifier name is a valid.
yod.emptyModifiers()
Remove all defined modifiers.
yod.config(key [, val] [, meta])
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: ...}
Config example:
// Setyod;yod; // Getyod; // => {b: 'ab', c: 'ac'}yod; // => {val: {b: 'ab', c: 'ac'}, meta: undefined}yod; // => 'ac'yod; // => {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); // => {b: 'ab', c: 'ac'}; // => 'ac'
yod.config.all
Type: Object
All config data.
yod.config.meta
Type: Object
All meta data.
Wrong user case
-
Caller string's arguments can not include ")".
@String// Will parsed to `@String.replace('"')` -
Object generator can not recycle depends.
;// Will throw error. -
Child object can't depend on it parents, similar to recycle depends.
;// Will throw error.
Install
Browser
bower install yod --save-dev
Node
npm install yod --save
Contributing
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.
License
Copyright (c) 2015 Zhonglei Qiu. Licensed under the MIT license.