camusjs
A extensible random objects generator
What is this?
You can translate templates into objects with full random objects.
This:
"name": "*": "name" "age": "*": "integer" "args": "min": 12 "max": 40
Will generate some like this:
"name": "Jordan Barnes" "age": 18
If you want some more complex:
"name": "*": "name" "pets": "*": "array" "minLength": 1 "maxLength": 3 "types": "chanceToAppear": 80 "definition": "*": "object" "definition": "name": "*": "first" "type": "Dog" "chanceToAppear": 20 "definition": "*": "object" "definition": "name": "*": "first" "type": "Cat"
It will generate a person, with a name and 1 to 3 pets. Each pet has a name and type. Dogs has 80% of chance to appear. Cats, 20%.
The result is some like this:
"name": "Alvin Ramsey" "pets": "name": "Jane" "type": "Dog" "name": "Harry" "type": "Dog" "name": "Ina" "type": "Cat"
It can handle only certain path if you want:
store: country: "Brazil" employees: "*": "name"
Is translated to some like:
"store": "country": "Brazil" "employees": "Maud Moran"
How can i use this?
First, install it: npm install camusjs
var camusjs = var myTemplate = "myProperty": "*": "type of the generator" var generated = camusjs // done
ChanceJS
You can use any CHANCE JS(VIEW HERE) method to generate values.
Let me show you a sample: The method guid:
chance;// 'f0d8368d-85e2-54fb-73c4-2d60374295e3'
You can add to your templates using:
"id": "*": "guid"
And that's it!
If the function you want to call has arguments:
chance;// -7
"myRandomInt": "*": "integer" "args": "min": 1 "max": 10
Nested Objects
If you want to generate nested objects, use the object
template:
"name": "*": "name" "job": "*": "object" "definition": "title": "*": "pickone" "args": "developer" "qa" "manager"
It will generate some like this:
"name": "Andre Ortega" "job": "title": "developer"
Array
If you want a collection of objects, use the array
template:
"myArray": "*": "array" "minLength": 2 // the minimum size of the array "maxLength": 6 // the maximum size of the array "types": // the types of content of the array // if you have multimple types of templates in the array, // set the percentage of the template appear. "chanceToAppear": 90 "definition": // you can use template here. "*": "object" "definition": "myPropName": "*": "name" "chanceToAppear": 10 "definition": "*": "integer"
In this sample, we generate an array with misc objects and numbers. The result must be some like this:
"myArray": "myPropName": "Georgie Patterson" 6303461331173376 "myPropName": "Anne Jefferson" "myPropName": "Lucinda Jordan"
Allowing NULLS
If you want to some template returns null
in some case, use the chanceToBeNull
modifier:
"id": "*": "guid" "chanceToBeNull": 15 // in this case, 15% of the times, the id will returns null.
All templates accept this modifier =).
Using static values
You can use fixed informed values using the option_value
and options
:
var template = id: "*": "option_value" property: 'myId' var generated = camusjs
String Replacing
If your property has a string value and you want to replace some of this string with a random or option value, use the string_replace
template:
var template = url: "*": "string_replace" value: 'http://api/authentication/%CPF%(/.*)?' // the raw string searchFor: '%CPF%' // what will be replaced replaceWith: // the value that be replacer, you can use templates here =). "*": "option_value" property: "cpf" var generated = camusjs // 'http://api/authentication/00011122244(/.*)?'
Advanced
Adding new Parsers
You can create parsers and append them to the core. For this, create the following:
var camusjs = var newParser = "*": "my_name_of_generator" { // return your value... return null } camusjs
Now you can use:
"myProperty": "*": "my_name_of_generator"
Sample of parser that generate a sword:
/* want to use: { "*": "sword" } and get { "name": "any name of sword", "damage": 12, "price": 300.51 }*/ var camusjs = var swordParser = "*": "sword" { return camusjs } camusjs var template = "item": "*": "sword" var generated = camusjs /* { "item": { "name": "Mitchell Mason", "damage": 9, "price": 257.21 } }*/
Adding new middlewares
Middlewares are parsers that runs BEFORE the registered parsers (chanceToBeNull
is a middleware).
You can register new middlewares:
var camusjs = var myMiddleware = '#': 'name_of_my_middleware' isGlobal: true // if must run before ALL transformers { // do what you want here var newValue = {} // my new value var mustContinue = true //must execute the next middleware or stops here and return value } camusjs
For localized middlewares, you can set isGlobal
to false and add it manually to your parsers:
var camusjs = var myMiddleware = '#': 'name_of_my_middleware' isGlobal: false // if must run before ALL transformers { // do what you want here var newValue = {} // my new value var mustContinue = true //must execute the next middleware or stops here and return value } camusjs var newParser = "*": "my_name_of_generator" middlewares: 'name_of_my_middleware' { // return your value... return null } camusjs
Changelog
2.0.1
- Fix: fixing wrong template manipulation in loop generation.
2.0.0
- Breaking Change: Removing
parseValue
method. Useparse
only for all.
1.3.1
- Fixing null exception on parse object
1.3.0
- Improve object parsing
1.2.0
- Feat: adding
string_replace