UNPKG

random-object-generator

Version:

Generate random values for javascript objects

69 lines (57 loc) 1.55 kB
### Random Object Generator Generate random javascript objects ----------------------------------- first, install the package: ```shell npm i random-object-generator ``` next require the package in the file you want to use the object generator ```javascript var random = require('random-object-generator'); ``` To generate a random object, first define a javascript object. Define the types of the properties by assigning them values. supported types are: * object * "id" * "int" * "string" * "bool" * "date" * "period" The object generator works recursively. To generate an array of a type, simply assign an array to a property, the first item in the array will be the type of the generated array. ["id"] will generate an array if id's. This can be a function or a hard-coded object: ```javascript function testObject() { this.id = "id"; this.number = "int"; this.description = "string"; this.anotherObject = [new anotherTestObject()]; this.intArray= ["int"]; } function anotherTestObject() { this.testId = "id"; } ``` To generate random values for an object, use the randomObject function. ```javascript var object = random.randomObject(new testObject()); console.log(object); ``` This will output the following: ```javascript { id: 'id-5981', number: 231, description: 'f)z', anotherObject: [ { testId: 'testId-6774' }, { testId: 'testId-7310' }, { testId: 'testId-2681' } ], intArray: [ 722, 179, 477 ] } ```