Detalhes do pacote

random-object-generator

agilians186ISC1.0.2

Generate random values for javascript objects

random, agilians, abao-test

readme (leia-me)

Random Object Generator

Generate random javascript objects

first, install the package:

npm i random-object-generator

next require the package in the file you want to use the object generator

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:

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.

var object = random.randomObject(new testObject());
console.log(object);

This will output the following:

{ 
    id: 'id-5981',
    number: 231,
    description: 'f)z',
    anotherObject:
    [ 
        { testId: 'testId-6774' },
        { testId: 'testId-7310' },
        { testId: 'testId-2681' } 
    ],
    intArray: [ 722, 179, 477 ] 
}