diff --git a/src/lib/isLength.js b/src/lib/isLength.js index 4d5d52546..56d8e3fbf 100644 --- a/src/lib/isLength.js +++ b/src/lib/isLength.js @@ -14,7 +14,7 @@ export default function isLength(str, options) { max = arguments[2]; } - const presentationSequences = str.match(/(\uFE0F|\uFE0E)/g) || []; + const presentationSequences = str.match(/[^\uFE0F\uFE0E][\uFE0F\uFE0E]/g) || []; const surrogatePairs = str.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g) || []; const len = str.length - presentationSequences.length - surrogatePairs.length; const isInsideRange = len >= min && (typeof max === 'undefined' || len <= max); diff --git a/test/validators.test.js b/test/validators.test.js index 5196c6fe9..c5ea4dc99 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -5591,32 +5591,6 @@ describe('Validators', () => { }); }); - it('should validate strings by length (deprecated api)', () => { - test({ - validator: 'isLength', - args: [2], - valid: ['abc', 'de', 'abcd'], - invalid: ['', 'a'], - }); - test({ - validator: 'isLength', - args: [2, 3], - valid: ['abc', 'de'], - invalid: ['', 'a', 'abcd'], - }); - test({ - validator: 'isLength', - args: [2, 3], - valid: ['干𩸽', '𠮷野家'], - invalid: ['', '𠀋', '千竈通り'], - }); - test({ - validator: 'isLength', - args: [0, 0], - valid: [''], - invalid: ['a', 'ab'], - }); - }); it('should validate isLocale codes', () => { test({ @@ -5695,77 +5669,6 @@ describe('Validators', () => { }); }); - it('should validate strings by length', () => { - test({ - validator: 'isLength', - args: [{ min: 2 }], - valid: ['abc', 'de', 'abcd'], - invalid: ['', 'a'], - }); - test({ - validator: 'isLength', - args: [{ min: 2, max: 3 }], - valid: ['abc', 'de'], - invalid: ['', 'a', 'abcd'], - }); - test({ - validator: 'isLength', - args: [{ min: 2, max: 3 }], - valid: ['干𩸽', '𠮷野家'], - invalid: ['', '𠀋', '千竈通り'], - }); - test({ - validator: 'isLength', - args: [{ max: 3 }], - 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'], - }); - test({ - validator: 'isLength', - args: [{ max: 8 }], - valid: ['👩🦰👩👩👦👦🏳️🌈', '⏩︎⏩︎⏪︎⏪︎⏭︎⏭︎⏮︎⏮︎'], - }); - }); it('should validate strings by byte length', () => { test({ diff --git a/test/validators/isLength.test.js b/test/validators/isLength.test.js new file mode 100644 index 000000000..5c1444002 --- /dev/null +++ b/test/validators/isLength.test.js @@ -0,0 +1,144 @@ +import test from '../testFunctions'; + +describe('isLength', () => { + it('should return false for a string with length greater than the max', () => { + test({ + validator: 'isLength', + args: [{ max: 3 }], + invalid: ['test'], + }); + }); + + it('should return true for a string with length equal to the max', () => { + test({ + validator: 'isLength', + args: [{ max: 4 }], + valid: ['test'], + }); + }); + + it('should correctly calculate the length of a string with presentation sequences', () => { + test({ + validator: 'isLength', + args: [{ max: 4 }], + valid: ['test\uFE0F'], + }); + + test({ + validator: 'isLength', + args: [{ min: 5, max: 5 }], + valid: ['test\uFE0F\uFE0F'], + }); + + test({ + validator: 'isLength', + args: [{ min: 5, max: 5 }], + valid: ['\uFE0Ftest'], + }); + + test({ + validator: 'isLength', + args: [{ min: 9, max: 9 }], + valid: ['test\uFE0F\uFE0F\uFE0F\uFE0F\uFE0F\uFE0F'], + }); + }); + + it('should validate strings by length (deprecated api)', () => { + test({ + validator: 'isLength', + args: [2], + valid: ['abc', 'de', 'abcd'], + invalid: ['', 'a'], + }); + test({ + validator: 'isLength', + args: [2, 3], + valid: ['abc', 'de'], + invalid: ['', 'a', 'abcd'], + }); + test({ + validator: 'isLength', + args: [2, 3], + valid: ['干𩸽', '𠮷野家'], + invalid: ['', '𠀋', '千竈通り'], + }); + test({ + validator: 'isLength', + args: [0, 0], + valid: [''], + invalid: ['a', 'ab'], + }); + }); + + it('should validate strings by length', () => { + test({ + validator: 'isLength', + args: [{ min: 2 }], + valid: ['abc', 'de', 'abcd'], + invalid: ['', 'a'], + }); + test({ + validator: 'isLength', + args: [{ min: 2, max: 3 }], + valid: ['abc', 'de'], + invalid: ['', 'a', 'abcd'], + }); + test({ + validator: 'isLength', + args: [{ min: 2, max: 3 }], + valid: ['干𩸽', '𠮷野家'], + invalid: ['', '𠀋', '千竈通り'], + }); + test({ + validator: 'isLength', + args: [{ max: 3 }], + 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'], + }); + test({ + validator: 'isLength', + args: [{ max: 8 }], + valid: ['👩🦰👩👩👦👦🏳️🌈', '⏩︎⏩︎⏪︎⏪︎⏭︎⏭︎⏮︎⏮︎'], + }); + }); +});