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(size): allow to specify string length of random values #316

Merged
merged 1 commit into from
Aug 20, 2023

Conversation

va-stefanek
Copy link
Contributor

@va-stefanek va-stefanek commented Nov 13, 2022

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] Other... Please describe:

What is the current behavior?

Issue Number: #299

@stackblitz
Copy link

stackblitz bot commented Nov 13, 2022

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

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 });
Copy link
Member

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.

@NetanelBasal
Copy link
Member

@va-stefanek any progress here?

@PumpedSardines
Copy link
Contributor

I think the name should mention that the result can be less than size. isn't maxStringLength or maxCharCount better?

@NetanelBasal
Copy link
Member

maxCharCount looks good to me

@va-stefanek
Copy link
Contributor Author

Will work on it this week

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 });
Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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?

Copy link
Contributor Author

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

Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://github.com/ngneat/falso/pull/316/files#diff-b93ef17757be75c3c90f47b1c860c47f9eaa31865b28df81874fe2043686cfe5R12

There is such a test, should I increase the length and decrease maxCharCount?

Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

? () =>
randElement(
data.filter((fakeData) =>
options?.maxCharCount && typeof fakeData === 'string'
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@NetanelBasal any update?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

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

Copy link
Contributor Author

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

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.

Copy link
Contributor Author

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
        )

Copy link
Contributor

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);
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment on lines 1 to 3
export const isString = (value: any) => {
return typeof value === 'string';
};
Copy link
Contributor

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.

Suggested change
export const isString = (value: any) => {
return typeof value === 'string';
};
export function isString(value: any): value is string {
return typeof value === 'string';
}

Copy link
Contributor

@shaharkazaz shaharkazaz left a 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', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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', () => {

@va-stefanek
Copy link
Contributor Author

@NetanelBasal up :)

@shaharkazaz shaharkazaz merged commit 4989449 into ngneat:main Aug 20, 2023
1 check passed
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.

None yet

4 participants