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
Closed
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
ae711d6
Added Exact Condition for String Length & Updated README.md
bevatsal1122 Aug 6, 2022
f93cf70
Fixed Small Issue
bevatsal1122 Aug 6, 2022
cdd2192
Updated test/validators.js
bevatsal1122 Aug 6, 2022
27a40c8
Fixed in test/validators.js
bevatsal1122 Aug 6, 2022
cf729d1
Updated test/validators.js for isLength()
bevatsal1122 Aug 6, 2022
be706c4
Updated test/validators.js
bevatsal1122 Aug 6, 2022
db5b0c7
Updated test/validators.js
bevatsal1122 Aug 6, 2022
2e48053
Updated test/validators.js
bevatsal1122 Aug 6, 2022
c194526
Updated test/validators.js
bevatsal1122 Aug 6, 2022
21ec415
Updated test/validators.js
bevatsal1122 Aug 6, 2022
59abfc7
Fixed Small Issue
bevatsal1122 Aug 6, 2022
e1bb810
Fixed Small Issue
bevatsal1122 Aug 6, 2022
7c2c0cf
Fixed Small Issue
bevatsal1122 Aug 6, 2022
3ee58c0
Fixed Small Issue
bevatsal1122 Aug 6, 2022
d3e6ca5
Fixed Small Issue
bevatsal1122 Aug 6, 2022
b850b84
Fixed Small Issue
bevatsal1122 Aug 6, 2022
acfe071
Updated test/validators.js
bevatsal1122 Aug 6, 2022
cdb06a5
Updated test/validators.js
bevatsal1122 Aug 6, 2022
70775b4
Fixed Small Issue
bevatsal1122 Aug 6, 2022
a71b7ff
Renamed isPerfect to isValid
bevatsal1122 Aug 8, 2022
dc42e7f
Removed Backwards Compatibility for exact Feature
bevatsal1122 Aug 13, 2022
fb0def3
Fixed Minor Bug
bevatsal1122 Aug 13, 2022
ec1aa91
Fixed Small Issue
bevatsal1122 Aug 13, 2022
73dc505
Fixed Small Issue
bevatsal1122 Aug 13, 2022
c7605bc
Updated README.md, isLength.js & validators.js Files
bevatsal1122 Aug 13, 2022
93ac65f
Updated validators.js in test Folder
bevatsal1122 Aug 13, 2022
bc693dc
Included exact out of range Test
bevatsal1122 Aug 13, 2022
a90171f
Added Additional Test in validators.js for isLength
bevatsal1122 Aug 14, 2022
57a5633
Update src/lib/isLength.js
bevatsal1122 Nov 12, 2022
642e603
Update isLength.js
bevatsal1122 Nov 12, 2022
bb02daf
Updated discreteLengths only to be array
bevatsal1122 Nov 12, 2022
2a0a428
Resolved minor merged conflicts
bevatsal1122 Nov 12, 2022
1bf1ee4
Fixed minor bug
bevatsal1122 Nov 12, 2022
9fd65ed
Fixed minor bug
bevatsal1122 Nov 12, 2022
fd90691
Fixed tests for isLength
bevatsal1122 Nov 12, 2022
d09738c
Fixed minor bug
bevatsal1122 Nov 12, 2022
11aa19f
Minor test bug fixed
bevatsal1122 Nov 13, 2022
e63e7a5
Fixed minor bug in test/validator.js
bevatsal1122 Nov 13, 2022
3ef2588
Merge branch 'validatorjs:master' into MergeProposal
bevatsal1122 Nov 13, 2022
3b5cbd9
Update src/lib/isLength.js
bevatsal1122 Nov 18, 2022
2897ef9
Added blank lines between statements
bevatsal1122 Nov 18, 2022
22bb2f0
Updated isLength.js
bevatsal1122 Nov 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

**isLicensePlate(str [, locale])** | check if string matches the format of a country's license plate.<br/><br/>(locale is one of `['cs-CZ', 'de-DE', 'de-LI', 'fi-FI', 'pt-BR', 'pt-PT', 'sq-AL', 'sv-SE', 'en-IN', 'hi-IN', 'gu-IN', 'as-IN', 'bn-IN', 'kn-IN', 'ml-IN', 'mr-IN', 'or-IN', 'pa-IN', 'sa-IN', 'ta-IN', 'te-IN', 'kok-IN']` or `any`)
**isLocale(str)** | check if the string is a locale
**isLowercase(str)** | check if the string is lowercase.
Expand Down
28 changes: 27 additions & 1 deletion src/lib/isLength.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,41 @@ export default function isLength(str, options) {
assertString(str);
let min;
let max;
let exact;
let result;
let isValid = false;
bevatsal1122 marked this conversation as resolved.
Show resolved Hide resolved
if (typeof (options) === 'object') {
min = options.min || 0;
max = options.max;
exact = options.exact;
} else { // backwards compatibility: isLength(str, min [, max])
min = arguments[1] || 0;
max = arguments[2];
}
const presentationSequences = str.match(/(\uFE0F|\uFE0E)/g) || [];
const surrogatePairs = str.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g) || [];
const len = str.length - presentationSequences.length - surrogatePairs.length;
return len >= min && (typeof max === 'undefined' || len <= max);
result = len >= min && (typeof max === 'undefined' || len <= max);
if (result === false || typeof exact === 'undefined') return result;
bevatsal1122 marked this conversation as resolved.
Show resolved Hide resolved
if (Array.isArray(exact) === true) {
for (let oneElem of exact) {
if (len === oneElem) {
isValid = true;
break;
}
}
} else if (typeof exact === 'number') {
if (len === exact) {
isValid = true;
}
} else if (typeof exact === 'object') {
for (let key in exact) {
if (len === exact[key]) {
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.

isValid = false;
}
return isValid;
}
48 changes: 48 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.

validator: 'isLength',
args: [2, 8],
valid: ['Helloo', 'Laptop', 'ab'],
invalid: ['', 'a'],
});
});

it('should validate isLocale codes', () => {
Expand Down Expand Up @@ -4698,12 +4704,54 @@ describe('Validators', () => {
valid: ['abc', 'de', 'a', ''],
invalid: ['abcd'],
});
test({
validator: 'isLength',
args: [{ max: 6, exact: 5 }],
valid: ['abcde', 'fffde', 'allkk'],
invalid: ['abcd', 'abcdef', 'vfd', 'ff', '', 'k'],
});
test({
validator: 'isLength',
args: [{ max: 6, exact: { } }],
valid: [],
invalid: ['abcd', 'abcdef', 'vfd', 'ff', '', 'k'],
});
test({
validator: 'isLength',
args: [{ min: 2, max: 6, exact: 5 }],
valid: ['abcde', 'fffde', 'allkk'],
invalid: ['bsa', 'vfvd', 'ff', '', 'k'],
});
test({
validator: 'isLength',
args: [{ exact: 2 }],
valid: ['fg', 'ff', 'po'],
invalid: ['bsa', 'vfvd', '', 'k'],
});
test({
validator: 'isLength',
args: [{ max: 0 }],
valid: [''],
invalid: ['a', 'ab'],
});
test({
validator: 'isLength',
args: [{ min: 5, max: 10, exact: [6, 8, 9] }],
valid: ['helloguy', 'shopping', 'validator', 'length'],
invalid: ['abcde', 'abcdefg'],
});
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] }].

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 }].

validator: 'isLength',
args: [{ exact: '9' }],
valid: [],
invalid: ['a', 'abcd', 'abcdefghijkl'],
});
bevatsal1122 marked this conversation as resolved.
Show resolved Hide resolved
test({
validator: 'isLength',
valid: ['a', '', 'asds'],
Expand Down