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

Couldn't generate zero entity using 'hasMany' even though min attribute is 0 #47

Closed swatjoshi closed 6 years ago

swatjoshi commented 6 years ago

Hi While working with 'hasMany', I wanted to generate an array with 0 or 1 entity , but I could not get zero entity. Below is the example on which I was working. `var user = { email : { faker : 'internet.email' }, org : { values : ['deloitte', 'tcs', 'infosys', 'wipro'] }, name : { faker : 'name.findName' }, _id : { function : function () { return Math.floor(Math.random() * 100); } } } var finalSchema = { userDetails : { hasMany : 'users', min : 0, max : 1 }, date : { faker : 'date.past' } }

mocker() .schema('users', user, 2) .schema('finalObject', finalSchema, 2)`

Finally, I tried debugging and came up with certain changes in Generator.prototype.hasMany(cfg) of Generator.zip

mocker_data_generator/build/main/lib/Generator.js file to meet my requirement of getting 0 entity as well when I mention both min and max attributes. Please find the zipped file attachment. Kindly, correct me if I am going wrong.. If you find it worth updating this changes in your module, please do it so that others get benefited.

Thank You

danibram commented 6 years ago

Hi, thank you for the issue. Yes i will add this to the library, but seriously!Did you debug the JS of the library?Wow!thats really hard men! This library is written in Typescript, so you dont need to modify or read any JS code, simply git clone this repository, modify the TS class and make me a pull request, i thinks its easy than debug shitty compiled JS. This is the method of the class in TS:

hasMany(cfg) {
        let amount = 1
        let db = this.DB

        let min = cfg.min ? cfg.min : 1
        let max = cfg.max ? cfg.max : db[cfg.hasMany].length

        if (cfg.amount) {
            amount = cfg.amount
        } else {
            amount = Math.floor(Math.random() * (max - min + 1)) + min
        }

        let newCfg = {
            hasOne: cfg.hasMany
        }

        if (cfg.get) {
            newCfg['get'] = cfg.get
        }

        return Array.from(new Array(amount)).map(() => this.hasOne(newCfg))
    }

Cleaner!Dont think so? ;) I will add you changes in the next version! Thanks

swatjoshi commented 6 years ago

Yeah absolutely!! Cloning git repository , modifying the required thing and making a pull request seems much better than debugging. Eagerly waiting for your library's next version !! Thank you :)

danibram commented 6 years ago

Hi! Now with 2.5.2 you can generate also with minimum 0. But you have to specify minimum to 0, by default it takes minimum to 1. so i will close this