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

isExact feature along with its tests and readme changes #1938

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 can receive an extra argument that is an array of numbers used to ask the string's length is exact of one of these elements.<br/><br/>`options` is an object which defaults to `{min:0, max: undefined, exact:[]}`. 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']` or `any`)
**isLocale(str)** | check if the string is a locale
**isLowercase(str)** | check if the string is lowercase.
Expand Down
20 changes: 17 additions & 3 deletions src/lib/isLength.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
import assertString from './util/assertString';
import assertString from './assertString';

/* eslint-disable prefer-rest-params */
export default function isLength(str, options) {
assertString(str);
let min;
let max;
let exact;
let match = false;
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];
exact = arguments[3];

}
const surrogatePairs = str.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g) || [];
const len = str.length - surrogatePairs.length;
return len >= min && (typeof max === 'undefined' || len <= max);
}
if (exact && (len >= min && (typeof max === 'undefined' || len <= max))) {
exact.forEach(element => {
if(len === element){
match = true;
}
})
} else {
return len >= min && (typeof max === 'undefined' || len <= max);
}
return match;
}
16 changes: 14 additions & 2 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -4578,8 +4578,20 @@ describe('Validators', () => {
});
test({
validator: 'isLength',
args: [{ max: 0 }],
valid: [''],
args: [{ min: 2, max: 8, exact: [4, 5, 6] }],
valid: ['abcd', 'abcde', 'abcdef'],
invalid: ['ab', 'abc'],
});
test({
validator: 'isLength',
args: [{ min: 5, max: 8, exact: [3, 4, 8] }],
valid: ['abcdefgh'],
invalid: ['abc', 'abcd'],
});
test({
validator: 'isLength',
args: [{ min: 3, max: 4, exact: [3, 4] }],
valid: ['abc', 'abcd', 'abcde'],
invalid: ['a', 'ab'],
});
test({
Expand Down