Skip to content

Commit

Permalink
feat(isLength): Fix linting and merge errors for #2019 (#2474)
Browse files Browse the repository at this point in the history
Co-authored-by: Falk Schieber <[email protected]>
Co-authored-by: Vatsal <[email protected]>
  • Loading branch information
3 people authored Oct 16, 2024
1 parent 66ddd9c commit 12b27a2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
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 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'`.
**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.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5394,12 +5394,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: [],
});
test({
validator: 'isLength',
valid: ['a', '', 'asds'],
Expand Down

0 comments on commit 12b27a2

Please sign in to comment.