Getting unique values when specifying length
juliendelort opened this issue ยท 14 comments
Description
Currently, when specifying a length, duplicates can be returned.
Here is an example call:
randCat({ length: 10 });
result:
["Thai","LaPerm","Bombay","Siberian","Himalayan","Himalayan","Himalayan","Ocicat","Thai","Serengeti"]
Is there a way to only get unique results?
Proposed solution
Maybe an extra option:
randCat({ length: 10, unique: true });
Alternatives considered
No response
Do you want to create a pull request?
No
Hi @juliendelort , I have a doubt in this implementation.
What if the length provided in the rand
function is greater than the available number of entries in the data ?
- If
unique:true
is prioritized, then the output will have less number of entries than specified in length - If
length
is prioritized, then the output will not be unique
Either way, the function is not returning an output which is compliant to the specified input options
That makes sense, thank you for the reply!
How about using a more explicit option (like tryUniques: 10
for example) and letting the user choose between strategies #1 and #2 above using another option (like priority: 'length' | 'unicity'
):
// Returns 10 items for sure with possible duplicates (duplicates only if there are less than 10 entries in the data)
randCat({ tryUniques: 10, priority: 'length' });
// If there are less than 10 entries in the data, the below would just return all the data entries (less than 10)
randCat({ tryUniques: 10, priority: 'unicity' });
(Different option names can be used)
@shaharkazaz @theryansmee any thoughts?
I really like the idea of ensuring data is unique, and I think the priority
is a really interesting concept.
We need to think about:
- How this param will work with randomly generated content rather than JSON data (Credit card number etc)
- How we clearly document to a user that if the select
unicity
their array length may not be what they initially expecting. And the returned length could increase within a minor release if we add more items to a JSON file (Not that that's a massive deal) - Should
priority
just work with thelength
param that already exists
All that said, i've already run into issues in my unit tests with duplicated data being returned from Falso, so this would be a blessing for me ๐
@shaharkazaz @NetanelBasal - Did you guys have any more thoughts on this? The more I think about it, the more I think it would be cool
I'm ok with the proposed API. It should be easy because we have the proxy function.
I'm going to have a little crack at it then :)
Any ETA for this feature to be implemented?
It depends on @theryansmee :)
It depends on @theryansmee :)
Hi. Sorry, i've not looked at this in months. Last time I looked there was a butt load of merge conflicts with my PR and I haven't had any time to solve them. I will try and see if I can get them fixed this week so we can finally get this work in.
Sorry again for the radio silence
Hello, any chance of giving this one some new love? I see a lot of effort has been done on the PR last year by @theryansmee , would be a shame to waste that i.m.o. Anything I can do to help?
@kfrederix yes, I'll take a look at it this week ๐
Hopefully we can close this, it would really come in handy!
+1