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 all 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 @@ -139,7 +139,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 any of the integers of the `discreteLengths` array if provided.<br/><br/>`options` is an object which defaults to `{min:0, max: undefined, discreteLengths: undefined}`. Note: this function takes into account surrogate pairs.
**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
10 changes: 9 additions & 1 deletion src/lib/isLength.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@ export default function isLength(str, options) {
assertString(str);
let min;
let max;

if (typeof (options) === 'object') {
min = options.min || 0;
max = options.max;
} 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);
const isInsideRange = len >= min && (typeof max === 'undefined' || len <= max);

if (isInsideRange && Array.isArray(options?.discreteLengths)){
return options.discreteLengths.some(discreteLen => discreteLen === len);
}

return isInsideRange;
}
30 changes: 30 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -4816,12 +4816,42 @@ describe('Validators', () => {
valid: ['abc', 'de', 'a', ''],
invalid: ['abcd'],
});
test({
validator: 'isLength',
args: [{ max: 6, discreteLengths: 5 }],
valid: ['abcd', 'vfd', 'ff', '', 'k'],
invalid: ['abcdefgh', 'hfjdksks'],
});
test({
validator: 'isLength',
args: [{ min: 2, max: 6, discreteLengths: 5 }],
valid: ['bsa', 'vfvd', 'ff'],
invalid: ['', ' ', 'hfskdunvc'],
});
test({
validator: 'isLength',
args: [{ min: 1, discreteLengths: 2 }],
valid: [' ', 'hello', 'bsa'],
invalid: [''],
});
test({
validator: 'isLength',
args: [{ max: 0 }],
valid: [''],
invalid: ['a', 'ab'],
});
test({
validator: 'isLength',
args: [{ min: 5, max: 10, discreteLengths: [2, 6, 8, 9] }],
valid: ['helloguy', 'shopping', 'validator', 'length'],
invalid: ['abcde', 'abcdefg'],
});
test({
validator: 'isLength',
args: [{ discreteLengths: '9' }],
valid: ['a', 'abcd', 'abcdefghijkl'],
invalid: [],
});
bevatsal1122 marked this conversation as resolved.
Show resolved Hide resolved
test({
validator: 'isLength',
valid: ['a', '', 'asds'],
Expand Down