-
-
Notifications
You must be signed in to change notification settings - Fork 114
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(size): allow to specify string length of random values #316
Conversation
Run & review this pull request in StackBlitz Codeflow. |
3c41bd5
to
8243873
Compare
README.md
Outdated
You can specify the length of elements you want to generate. Below is an example of generating 10 emails with length equal or smaller than 20 characters. | ||
|
||
```ts | ||
const emails = randEmail({ length: 10, size: 20 }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should go we charSize
to be more explicit. We need to check if we already use this name in other functions.
@va-stefanek any progress here? |
I think the name should mention that the result can be less than |
maxCharCount looks good to me |
Will work on it this week |
8243873
to
b2ae2a0
Compare
You can specify the length of elements you want to generate. Below is an example of generating 10 emails with length equal or smaller than 20 characters. | ||
|
||
```ts | ||
const emails = randEmail({ length: 10, maxCharCount: 20 }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The returned size can differ from the length
value, right? I wonder if this is the desired result.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't that length is just quantity of items and maxChartCount is length of that items?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But what happens when you request 20 items and only 10 passes the maxCharCount predicate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Than there will be some duplicate values in return
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add a test for this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is such a test, should I increase the length and decrease maxCharCount?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, please. In a new spec.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@NetanelBasal done
b2ae2a0
to
89cf2a9
Compare
packages/falso/src/lib/core/core.ts
Outdated
? () => | ||
randElement( | ||
data.filter((fakeData) => | ||
options?.maxCharCount && typeof fakeData === 'string' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@NetanelBasal Does it make sense that we have arrays with mixed data types? otherwise, I would test the array element and decide whether we need to filter.
We might want to add some more restrictions in the future, WDYT about creating an abstraction that applies these restrictions on the data set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@NetanelBasal any update?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the late response.
Does it make sense that we have arrays with mixed data types?
No
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shaharkazaz what is your suggestion based on the response from Netanel?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shaharkazaz up
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that we only have one data type per array we can move options?.maxCharCount && typeof fakeData === 'string'
to only be tested once and not inside the filter loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure I understand it properly.
Do you suggest to code it like that
randElement(
options?.maxCharCount && typeof data[0] === 'string' ?
data.filter(fakeData => fakeData.length <= options.maxCharCount!)
: data
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const dataSource = (data as FactoryFunction<T>);
if (Array.isArray(data)) {
let resolvedData = data;
if (options?.maxCharCount && isString(data[0])) {
resolvedData = data.filter((item) => item.length <= options.maxCharCount);
}
dataSource = () => randElement(resolvedData);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shaharkazaz done
89cf2a9
to
d756f5b
Compare
export const isString = (value: any) => { | ||
return typeof value === 'string'; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rename the file to utils/validators.utils
.
export const isString = (value: any) => { | |
return typeof value === 'string'; | |
}; | |
export function isString(value: any): value is string { | |
return typeof value === 'string'; | |
} |
d756f5b
to
90ce4d0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last ones 🙂
result.forEach((movie) => expect(movie.length).toBeLessThanOrEqual(8)); | ||
}); | ||
|
||
it('should return 10 random movies with name length equal or smaller than 1', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it('should return 10 random movies with name length equal or smaller than 1', () => { | |
it('should return 50 random movies with name length equal or smaller than 1', () => { |
result.forEach((movie) => expect(movie.length).toBeLessThanOrEqual(1)); | ||
}); | ||
|
||
it('should return 10 random movies with name length equal or smaller than 20', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it('should return 10 random movies with name length equal or smaller than 20', () => { | |
it('should return 20 random movies with name length equal or smaller than 20', () => { |
90ce4d0
to
aa72a6e
Compare
@NetanelBasal up :) |
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: #299