Skip to content
Merged
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 src/lib/isLength.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
97 changes: 0 additions & 97 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand Down
144 changes: 144 additions & 0 deletions test/validators/isLength.test.js
Original file line number Diff line number Diff line change
@@ -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: ['👩🦰👩👩👦👦🏳️🌈', '⏩︎⏩︎⏪︎⏪︎⏭︎⏭︎⏮︎⏮︎'],
});
});
});