Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mykone opened this issue Mar 30, 2014 · 0 comments

Comments

@mykone
Copy link

mykone commented Mar 30, 2014

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant