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

feat: Initial implementation of limit option #30

Merged
merged 2 commits into from
Oct 11, 2022
Merged

feat: Initial implementation of limit option #30

merged 2 commits into from
Oct 11, 2022

Conversation

justinvdm
Copy link
Collaborator

@justinvdm justinvdm commented Oct 8, 2022

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:

copycat.email('real@email.org')
// => 'Era_Kunde245@gmail.com'

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:

// generated result will be <= 20 characters
copycat.email('real@example.org', { limit: 20 })

Approach

Copycat makes use of composition to generate values. For example, copycat.email() makes use of copycat.firstName() and copycat.lastName().

The idea is to have each component in each layer of composition be aware of the character limit. For composites like email, allocate a limit to each component. For example, if the limit is 25, then give 5 as a limit for firstName.

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 of oneOf(), and uses it to pick from an array of first names (provided by faker). So this PR replaces this oneOf() usage with a new oneOfString() function, which will only pick from the list of values that are within that limit. If none are found, it defaults to using copycat.word().

For more context on the approach, take a look at the video: https://www.loom.com/share/b94094839ca04524ab661b8837eebf6e

Copy link
Contributor

@CarelFdeWaal CarelFdeWaal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool. I like the "tiny email test" :). This is going to be useful!

@justinvdm justinvdm merged commit 8babbfa into main Oct 11, 2022
@peterp
Copy link
Collaborator

peterp commented Oct 11, 2022

This is awesome!

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

Successfully merging this pull request may close these issues.

3 participants