mockJSON icon indicating copy to clipboard operation
mockJSON copied to clipboard

Order of array in custom data is still random even though mockJSON.random = false

Open mykone opened this issue 10 years ago • 0 comments

Hello, I would like to thank you very much for a great library. I do have a small request though, if you could make a small change to it to make array types keep their sorted value in the mockJSON.data. This is done in the getRandomnData() function.

For example, I provided a custom data like: COMPANY: ["ABC Inc", "DAB Co", "AACC Inc"] but when I set the random = false flag, it returns random data. To fix that let me show you what I had to do:

function getRandomData(key) { key = key.substr(1); // remove "@"

//var params = key.match(/\(((\d+),?)+\)/g) || [];
var params = key.match(/\(([^\)]+)\)/g) || [];

if (!(key in $.mockJSON.data)) {

// console.log(key); // console.log(params); return key; }

var a = $.mockJSON.data[key];

switch (type(a)) {
    case 'array':
        if($.mockJSON.workingKeys[key] == null){
            $.mockJSON.workingKeys[key] = 0;
        }
        else{
            var index = $.mockJSON.workingKeys[key];
            if(index >= a.length)
                $.mockJSON.workingKeys[key] = 0;
        }

// var index = Math.floor(a.length * rand()); return a[$.mockJSON.workingKeys[key]++];

    case 'function':
        return a();
}

}

When I use the changes shown above, it keeps the order of my custom data arrays. Please let me know what you think.

mykone avatar Mar 30 '14 18:03 mykone