datafixture.js#
Generates random data based on a defined template model.
jsfiddle example implementing all of its features:
http://jsfiddle.net/acatl/fmvEj/
Jasmine tests results
http://jsfiddle.net/acatl/D2vmE/embedded/result/
Method: DataFixture.generate
DataFixture;
templateObject can be a string with patterns or an object with at least one property.
numberOfResults
optional, is the number of results we want to be generated, if numberOfResults
is == 0 it outputs an object. if > 0 it outputs an array of objects.
Generate an object with a randomly set numeric property
var template = like: "0...100" /* generate a number between 0 an 100, with no decimals */;DataFixture; // possible output like: 23
Generate an Array with a randomly set numeric property
var template = like: "0...100" /* generate a number between 0 an 100, with no decimals */;DataFixture; /* second parameter indicates the number of elements */ // possible output like: 23 like: 3 like: 2 like: 22 like: 10 ...
Generate numbers with decimals
var template = like: "0...100:2" /* generate a number between 0 an 100, with 2 decimals */;DataFixture; // possible output like: 1269
Generate a string from a defined set of values
var template = names: "Ian""Polo" "Oscar" "Fabien" "Rafael" /* Create string with any of the values from the array */;DataFixture; // possible output names: "Rafael"
Generate a string from a defined set of values with mixed types
var template = names: "Ian"2 104 true "Andros" /* Create string with any of the values from the array */;DataFixture; // possible output names: 104
One level template
var template = score: "0...100:2" /* generate a number between 0 an 100, with 2 decimals */ names: "Paula" "Darek" "Ian" "Monica" "Andros" "Kenji" /* Create string with any of the values from the array */;DataFixture; // possible output score: 4567 names: "Andros" score: 573 names: "Ian" score: 3445 names: "Paula" score: 5376 names: "Darek" score: 9928 names: "Monica" score: 012 names: "Kenji"
Nested levels template
var template = values: "0...100:2" names: "Ian"2 104 true "Andros" complex: PO:"20000...30000" country: "Mexico" "Canada" "US" ;DataFixture; // possible output names: 104 values: 1378 complex: PO: 27882 country: "Canada"
Nested template with more than one result
var template = values: "0...100:2" names: "Ian"2 104 true "Andros" complex: // '#' is a reserved property name, indicates number of instances '#': '1...3' // this will generate from 1 to 3 array elements PO:"20000...30000" country: "Mexico" "Canada" "US" ;DataFixture; // possible output values: 9762 names: 104 complex: PO: 22468 country: 'US' PO: 27431 country: 'Canada' PO: 20893 country: 'Canada' PO: 25644 country: 'Canada'
Values of properties can also be functions
var df = DataFixture; // possible output coordinates: '15:7' coordinates: '14:11' coordinates: '19:15' coordinates: '15:9' coordinates: '17:2' coordinates: '25:3' coordinates: '4:1' coordinates: '12:9' coordinates: '15:2' coordinates: '7:8'
Create Lorem Ipsum paragraphs with random lengths
var x = DataFixture; //output lorem: 'Lorem' lorem: 'Lorem ipsum' lorem: 'Lorem ipsum' lorem: 'Lorem ipsum' lorem: 'Lorem ipsum dolor' lorem: 'Lorem ipsum' lorem: 'Lorem' lorem: 'Lorem ipsum dolor' lorem: 'Lorem' lorem: 'Lorem ipsum'
Multiple values on a string
note: currently only support string, lorem, or numeric ranges
var x = DataFixture; //output time: '15:53:36' time: '13:54:7' time: '4:37:46'
Array Values may also be objects
Array values may also have objects in them, object's properties may also have parsable values.
var x = DataFixture; //output "item": 2 "item": "nunc nec accumsan" "item": "porttitor bibendum" "item": "type": "ad" "item": "type": "video" "url": "http://some.domain.com/videos/?id=168f5078-0eda-b021-fc93-c621a26a4e2e"
enjoy!
All feedback is welcomed