feat: Initial implementation of limit
option
#30
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Video with context
https://www.loom.com/share/b94094839ca04524ab661b8837eebf6e
Problem
At the moment, copycat gives you the ability to generate some fake value corresponding to some input value, for example:
However, there is currently no way to restrict the generated values to be within a given character limit.
At snaplet, we make use of copycat to replace real values in a database with fake values.
This is where the problem comes in: the real values in the database are within some character limit. However, the fake values generated by copycat that we're replacing them with are not always within this same character limit. As a result, we aren't able to use these fake values.
Solution
Add a
limit
option to each copycat API method that generates a string, for example:Approach
Copycat makes use of composition to generate values. For example,
copycat.email()
makes use ofcopycat.firstName()
andcopycat.lastName()
.The idea is to have each component in each layer of composition be aware of the character limit. For composites like
email
, allocate alimit
to each component. For example, if the limit is25
, then give5
as a limit forfirstName
.Then, for each primitive/leaf (the end of the chain of composition), have it restrict its output to be within that limit. For example,
firstName
makes use ofoneOf()
, and uses it to pick from an array of first names (provided by faker). So this PR replaces thisoneOf()
usage with a newoneOfString()
function, which will only pick from the list of values that are within that limit. If none are found, it defaults to usingcopycat.word()
.For more context on the approach, take a look at the video: https://www.loom.com/share/b94094839ca04524ab661b8837eebf6e