chancejs
chancejs copied to clipboard
Relational generation of random data
To save lazy people like me from writing a class for this job; in a users document once chance.name()
is used to generate "Albert Hodges"
then the next call to chance.gender()
should have the option to follow a previously used gender related method. Such as we sould be able to invoke chance.gender({follow: "name"})
and then chance.prefix({follow: "gender"})
kind of.
I'm having trouble understanding the approach. Could you provide a "current" and "propose" snippet of code?
for example:
// currently I have to do this
const name = chance.name()
const gender = chance.gender()
...
// to get this:
...
// I wish I could just do this:
const name = chance.name()
const gender = chance.gender({follow:'name'})
const prefix= chance.prefix({follow:'gender'})
// to get... ???
He meant that if the generated name is female the chance.gender() will give him a female; then the chance.prefix () will follow the gender and generate a female prefix.
However, I don't think the library should have such implementation.
Why ? Because We will need to change the return value of the functions. ie:
const name = chance.name()
will return a string, But he wants it to return the options object (or part of it) to be used in gender and so on.
Please correct me if I;m wrong @kedicesur .
So if this is the case, then one approach is to develop some sort of contextual mixin. The challenge is the context is, well, contextual. Is a customer based on gender? location? age?
A contexual mixin function could have this context.
For example:
chance.mixin({
'user': function() {
return chance.genderContext({ // creates the user based on a random gender
first: chance.first(),
last: chance.last(),
gender: chance.gender()
});
},
'fullAddress': function() {
return chance.countryContext({ // creates an address based on a random country
address: chance.address(),
province: chance.province(),
country: chance.country(),
postal_code: chance.postcode()
})
}
});
I agree tho that this format would be way too opinionated for a core functionality. I suggest waiting for Chance to mature to support a plugin model.