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

Added Exact Value/s Condition for String Length & Tests required with README.md Update #2019

Closed
wants to merge 42 commits into from

Conversation

bevatsal1122
Copy link
Contributor

@bevatsal1122 bevatsal1122 commented Aug 6, 2022

feat(isLength): Added Conditions for checking if String's length is equal to exact if exact provided

If the string length falls in the range and if exact number/array/object is provided, then exact conditions are checked. If not provided, then true is returned is len>=min & len<=max and false is returned is not in the range [min, max]. README.md File is also updated for the same.

Checklist

  • README updated (where applicable)
  • PR contains only changes related; no stray files, etc.
  • Tests written (where applicable)

@codecov
Copy link

codecov bot commented Aug 6, 2022

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (531dc7f) 100.00% compared to head (3ef2588) 100.00%.

❗ Current head 3ef2588 differs from pull request most recent head 22bb2f0. Consider uploading reports for the commit 22bb2f0 to get more accurate results

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #2019   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          104       104           
  Lines         2308      2311    +3     
  Branches       578       579    +1     
=========================================
+ Hits          2308      2311    +3     
Impacted Files Coverage Δ
src/lib/isLength.js 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@bevatsal1122
Copy link
Contributor Author

Hello, I have added the required feature. If the string length falls in the range and if exact number/array/object is mentioned, then exact conditions are checked. If not provided, then true is returned is len>=min & len<=max and false is returned is not in range [min, max]. All the checks have also been passed :)
PR#2019 - #2019

} else { // backwards compatibility: isLength(str, min [, max])
min = arguments[1] || 0;
max = arguments[2];
exact = arguments[3];
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need backwards compatibility for a new feature

Copy link
Member

Choose a reason for hiding this comment

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

That also removes the need for some of the tests btw

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ohh I thought if at all a User wants to check if it's exact to a bunch of values rather than a single value, so I configured the exact value to be an array or an object or a number. Should I remove the backwards compatibility feature ??

Copy link
Member

Choose a reason for hiding this comment

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

It's just this line that you can remove (and the tests associated to this line). This else statement is only here for the people that use the old syntax for isLength. And since this is a new feature there is no need to implement it in both the old and new syntax.

That you configured the exact value to be an array or object or number is fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okkay!! I made the required changes & also updated the Tests for the same... Hence, now this new Feature exact is implemented only in the new Syntax of isLength.

@@ -5,15 +5,42 @@ export default function isLength(str, options) {
assertString(str);
let min;
let max;
let exact;
let result;
let isPerfect = false;
Copy link
Member

Choose a reason for hiding this comment

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

Maybe refactor this to isValid?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yess!! Renamed isPerfect to isValid

@bevatsal1122 bevatsal1122 requested a review from WikiRik August 11, 2022 04:13
Copy link
Member

@WikiRik WikiRik left a comment

Choose a reason for hiding this comment

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

Thanks for the updates! I have some additional comments

And I'll tag @rubiin here as well for the more official review and approval for having exact be either a number, a array or an object of numbers since I'm not a maintainer of this library

README.md Outdated
@@ -140,7 +140,7 @@ Validator | Description
**isJSON(str [, options])** | check if the string is valid JSON (note: uses JSON.parse).<br/><br/>`options` is an object which defaults to `{ allow_primitives: false }`. If `allow_primitives` is true, the primitives 'true', 'false' and 'null' are accepted as valid JSON values.
**isJWT(str)** | check if the string is valid JWT token.
**isLatLong(str [, options])** | check if the string is a valid latitude-longitude coordinate in the format `lat,long` or `lat, long`.<br/><br/>`options` is an object that defaults to `{ checkDMS: false }`. Pass `checkDMS` as `true` to validate DMS(degrees, minutes, and seconds) latitude-longitude format.
**isLength(str [, options])** | check if the string's length falls in a range.<br/><br/>`options` is an object which defaults to `{min:0, max: undefined}`. Note: this function takes into account surrogate pairs.
**isLength(str [, options])** | check if the string's length falls in a range and equal to exactValue if provided.<br/><br/>`options` is an object which defaults to `{min:0, max: undefined, exact: undefined}`. Note: this function takes into account surrogate pairs.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add here that exact can be a single number, an array or an object of several numbers?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!! Updated the README.md.

@@ -4629,6 +4629,12 @@ describe('Validators', () => {
valid: [''],
invalid: ['a', 'ab'],
});
test({
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 can remove this new test since it does not test the new functionality

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yess!! Removed that test.

valid: ['helloguy', 'shopping', 'validator', 'length'],
invalid: ['abcde', 'abcdefg', 'abcdefghij'],
});
test({
Copy link
Member

Choose a reason for hiding this comment

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

We can remove this test since it tests the same as the test of { exact: 2 }.

Copy link
Member

Choose a reason for hiding this comment

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

Or adjust this test to test the combination of a min and exact values.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, the later one with exact: { first: 6, second: 8, third: 9 } checks for the condition involving the exact being an object. Hence, I have adjusted the previous one with {exact: 2} to test the combination of a min and exact values.

Copy link
Member

Choose a reason for hiding this comment

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

I was wrong about this test, the difference is that this is using a string of '9' instead of the number 9. That makes this a different test to { exact: 2 }. So the combination of min and exact should be an additional test

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So I have included the combination of a min and exact values in a test with args: [{ min: 1, exact: 2 }].

});
test({
validator: 'isLength',
args: [{ min: 5, max: 10, exact: { first: 6, second: 8, third: 9 } }],
Copy link
Member

Choose a reason for hiding this comment

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

What would happen if one of the exact numbers is lower than min or higher than max? Should we add tests for that behaviour as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since the exact condition only runs when the Length of the String falls inside [min, max] range, if at all exact integer or one of the exact numbers is out of range [min, max], the equality check for that case will return false and isValid won't be set to true.
Also, I have included the test condition for that behavior in the already present test with args: [{ min: 5, max: 10, exact: [2, 6, 8, 9] }].

src/lib/isLength.js Outdated Show resolved Hide resolved
isValid = true;
}
}
} else {
Copy link
Member

Choose a reason for hiding this comment

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

This else statement is not needed since you already define isValid as false at the beginning.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yess!! Removed the else statement.

Copy link
Contributor

@pixelbucket-dev pixelbucket-dev left a comment

Choose a reason for hiding this comment

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

LGTM, just a few comments :).

src/lib/isLength.js Outdated Show resolved Hide resolved
src/lib/isLength.js Outdated Show resolved Hide resolved
@pixelbucket-dev
Copy link
Contributor

I was just thinking: A future improvement could be to improve the options API to have consistent fields. This is because, with discreteLengths, we have a new field, and the API might become less intuitive to use now.

Something like:

{
	range: {
		min: 0,
		max: undefined
	}, // optional
	discreteLengths: undefined // optional
}

With the above proposal, it also becomes clear, that there is another permutation of the rules set:

  1. range defined AND discreteLengths undefined ⇒ implemented by this PR
  2. range undefined AND discreteLengths undefined ⇒ implemented by this PR
  3. range defined AND discreteLengths defined ⇒ implemented by this PR
  4. range undefined AND discreteLengths defined ⇒ NOT implemented by this PR

So basically, we would have to discuss if this API should allow checking against discrete lengths w/o using the range rule/option. Currently, to check discreteLengths, min and max, i.e. range, are also considered.

Just some food for thoughts 😄.

Co-authored-by: Falk Schieber <[email protected]>
@bevatsal1122
Copy link
Contributor Author

I was just thinking: A future improvement could be to improve the options API to have consistent fields. This is because, with discreteLengths, we have a new field, and the API might become less intuitive to use now.

Something like:

{
	range: {
		min: 0,
		max: undefined
	}, // optional
	discreteLengths: undefined // optional
}

With the above proposal, it also becomes clear, that there is another permutation of the rules set:

  1. range defined AND discreteLengths undefined ⇒ implemented by this PR
  2. range undefined AND discreteLengths undefined ⇒ implemented by this PR
  3. range defined AND discreteLengths defined ⇒ implemented by this PR
  4. range undefined AND discreteLengths defined ⇒ NOT implemented by this PR

So basically, we would have to discuss if this API should allow checking against discrete lengths w/o using the range rule/option. Currently, to check discreteLengths, min and max, i.e. range, are also considered.

Just some food for thoughts 😄.

Yeahh that's a really good idea. Making it segregated into key-value pairs would be easier to use and understand.
@WikiRik @rubiin What do you think ??

@rubiin rubiin requested review from profnandaa, pixelbucket-dev, rubiin, WikiRik and tux-tn and removed request for pixelbucket-dev and WikiRik November 19, 2022 08:09
@pixelbucket-dev
Copy link
Contributor

pixelbucket-dev commented Nov 21, 2022

I was just thinking: A future improvement could be to improve the options API to have consistent fields. This is because, with discreteLengths, we have a new field, and the API might become less intuitive to use now.
Something like:

{
	range: {
		min: 0,
		max: undefined
	}, // optional
	discreteLengths: undefined // optional
}

With the above proposal, it also becomes clear, that there is another permutation of the rules set:

  1. range defined AND discreteLengths undefined ⇒ implemented by this PR
  2. range undefined AND discreteLengths undefined ⇒ implemented by this PR
  3. range defined AND discreteLengths defined ⇒ implemented by this PR
  4. range undefined AND discreteLengths defined ⇒ NOT implemented by this PR

So basically, we would have to discuss if this API should allow checking against discrete lengths w/o using the range rule/option. Currently, to check discreteLengths, min and max, i.e. range, are also considered.
Just some food for thoughts 😄.

Yeahh that's a really good idea. Making it segregated into key-value pairs would be easier to use and understand. @WikiRik @rubiin What do you think ??

It would be a breaking change! So I'd suggest keep the existing API (min and max), like with the other PRs here, and add the new fields range and discreteLengths. min and max should probably be prioritised if defined, for now. You can document the new syntax already in this PR and deprecate min and max. With the next major version, min and max will be removed.

@WikiRik
Copy link
Member

WikiRik commented Dec 2, 2022

@rubiin any reason why you approved this while the GitHub Actions have not passed?

@pixelbucket-dev
Copy link
Contributor

@bevatsal1122 could you please fix the formatting issues that break the CI? View the “Files Changed” tab so see details about the linting issues.

N.B. we have to discuss better linter integration. Errors like this should never arrive in the PR, but rather fixed at commit time or push time by running commit hooks.

@profnandaa profnandaa added 🧹 needs-update For PRs that need to be updated before landing and removed ready-to-land For PRs that are reviewed and ready to be landed ✅ LGTM labels Jan 22, 2023
@bevatsal1122 bevatsal1122 reopened this Jul 14, 2023
@2021eo3ar
Copy link

@bevatsal1122 could you please assign this issue to me

rubiin pushed a commit that referenced this pull request Oct 16, 2024
@rubiin
Copy link
Member

rubiin commented Oct 16, 2024

closing in favor of #2474

@rubiin rubiin closed this Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants