Skip to content

Commit 12b27a2

Browse files
Suven-ppixelbucket-devbevatsal1122
authored
feat(isLength): Fix linting and merge errors for #2019 (#2474)
Co-authored-by: Falk Schieber <[email protected]> Co-authored-by: Vatsal <[email protected]>
1 parent 66ddd9c commit 12b27a2

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ Validator | Description
139139
**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.
140140
**isJWT(str)** | check if the string is valid JWT token.
141141
**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.
142-
**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.
142+
**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.
143143
**isLicensePlate(str, locale)** | check if the string matches the format of a country's license plate.<br/><br/>`locale` is one of `['cs-CZ', 'de-DE', 'de-LI', 'en-IN', 'en-SG', 'en-PK', 'es-AR', 'hu-HU', 'pt-BR', 'pt-PT', 'sq-AL', 'sv-SE']` or `'any'`.
144144
**isLocale(str)** | check if the string is a locale.
145145
**isLowercase(str)** | check if the string is lowercase.

src/lib/isLength.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,23 @@ export default function isLength(str, options) {
55
assertString(str);
66
let min;
77
let max;
8+
89
if (typeof (options) === 'object') {
910
min = options.min || 0;
1011
max = options.max;
1112
} else { // backwards compatibility: isLength(str, min [, max])
1213
min = arguments[1] || 0;
1314
max = arguments[2];
1415
}
16+
1517
const presentationSequences = str.match(/(\uFE0F|\uFE0E)/g) || [];
1618
const surrogatePairs = str.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g) || [];
1719
const len = str.length - presentationSequences.length - surrogatePairs.length;
18-
return len >= min && (typeof max === 'undefined' || len <= max);
20+
const isInsideRange = len >= min && (typeof max === 'undefined' || len <= max);
21+
22+
if (isInsideRange && Array.isArray(options?.discreteLengths)) {
23+
return options.discreteLengths.some(discreteLen => discreteLen === len);
24+
}
25+
26+
return isInsideRange;
1927
}

test/validators.test.js

+30
Original file line numberDiff line numberDiff line change
@@ -5394,12 +5394,42 @@ describe('Validators', () => {
53945394
valid: ['abc', 'de', 'a', ''],
53955395
invalid: ['abcd'],
53965396
});
5397+
test({
5398+
validator: 'isLength',
5399+
args: [{ max: 6, discreteLengths: 5 }],
5400+
valid: ['abcd', 'vfd', 'ff', '', 'k'],
5401+
invalid: ['abcdefgh', 'hfjdksks'],
5402+
});
5403+
test({
5404+
validator: 'isLength',
5405+
args: [{ min: 2, max: 6, discreteLengths: 5 }],
5406+
valid: ['bsa', 'vfvd', 'ff'],
5407+
invalid: ['', ' ', 'hfskdunvc'],
5408+
});
5409+
test({
5410+
validator: 'isLength',
5411+
args: [{ min: 1, discreteLengths: 2 }],
5412+
valid: [' ', 'hello', 'bsa'],
5413+
invalid: [''],
5414+
});
53975415
test({
53985416
validator: 'isLength',
53995417
args: [{ max: 0 }],
54005418
valid: [''],
54015419
invalid: ['a', 'ab'],
54025420
});
5421+
test({
5422+
validator: 'isLength',
5423+
args: [{ min: 5, max: 10, discreteLengths: [2, 6, 8, 9] }],
5424+
valid: ['helloguy', 'shopping', 'validator', 'length'],
5425+
invalid: ['abcde', 'abcdefg'],
5426+
});
5427+
test({
5428+
validator: 'isLength',
5429+
args: [{ discreteLengths: '9' }],
5430+
valid: ['a', 'abcd', 'abcdefghijkl'],
5431+
invalid: [],
5432+
});
54035433
test({
54045434
validator: 'isLength',
54055435
valid: ['a', '', 'asds'],

0 commit comments

Comments
 (0)