Reputation: 393
I'm currently using chance.js
to generate test data. For example, I can generate a random email and use it to test my models.
My problem is that I need to ensure some fields on my models are unique e.g. email on the user model. Does chance
ensure that it doesn't generate the same email twice?
I'd be willing to use faker
as an alternative but I couldn't find out if faker
offered this functionality.
Upvotes: 1
Views: 1195
Reputation: 4201
There is a unique function at chance.js
which also support comparator functionality From the change.js docs :
The comparator used to determine whether a generated item is in the list of already generated items. By default the comparator just checks to see if the newly generated item is in the array of already generated items. This works for most simple cases (such as chance.state()) but will not work if the generated item is an object (because the Array.prototype.indexOf() method will not work on an object since 2 objects will not be strictly equal, ===, unless they are references to the same object).
chance.unique(chance.currency, 2, {
comparator: function(err, val) {
return arr.reduce(function(acc, item) {
return acc || (item.code === val.code);
}, false);
}
});
check the docs for more details...
Upvotes: 2