danibram / mocker-data-generator

A simplified way to generate masive mock data based on a schema, you can use the awesome fake/random data generators like (FakerJs, ChanceJs, CasualJs and RandExpJs)
https://danibram.github.io/mocker-data-generator/
MIT License
426 stars 45 forks source link

Feature: Include Array Length in Generator "function" for a Collection #17

Closed zamnuts closed 7 years ago

zamnuts commented 7 years ago

When using the function type in the generator as part of an array in the schema, currently the iteration index is provided, but the target length of the final array is unknown to the consumer. For example (from the README):

var group = {
    users: [{
        function: function(index) { // index is provided
            return this.faker.random.arrayElement(this.db.user).username + index.toString() // index is used
        },
        length: 10,
        fixedLength: false
    }]
};

This capability exists due to: https://github.com/danibram/mocker-data-generator/blob/v2.0.2/src/lib/Schema.ts#L72-L74

let length = fieldArrayCalcLength(fieldConfig, na.length, this)
let array = Array.from(new Array(length)).map((el, index) => this.generateField(fieldConfig, index))

I propose that length be included in the invocation:

let length = fieldArrayCalcLength(fieldConfig, na.length, this)
let array = Array.from(new Array(length)).map((el, index) => this.generateField(fieldConfig, index, length))

I'm not in a position to provide a PR right now, hence the formal feature request. I can provide a PR later if desired.


Additionally, I was considering making the array available to the generator function as it was being built, but cannot determine an elegant implementation at this time.

danibram commented 7 years ago

I added all this features, thanks for the improvements