Skip to content

Commit d13cf14

Browse files
committed
Fix(isLength): Correctly handle Unicode variation selectors
1 parent f2e3633 commit d13cf14

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/lib/isLength.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default function isLength(str, options) {
1414
max = arguments[2];
1515
}
1616

17-
const presentationSequences = str.match(/(\uFE0F|\uFE0E)/g) || [];
17+
const presentationSequences = str.match(/[^\uFE0F\uFE0E][\uFE0F\uFE0E]/g) || [];
1818
const surrogatePairs = str.match(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g) || [];
1919
const len = str.length - presentationSequences.length - surrogatePairs.length;
2020
const isInsideRange = len >= min && (typeof max === 'undefined' || len <= max);

test/validators/isLength.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import test from '../testFunctions';
2+
3+
describe('isLength', () => {
4+
it('should return false for a string with length greater than the max', () => {
5+
test({
6+
validator: 'isLength',
7+
args: [{ max: 3 }],
8+
invalid: ['test'],
9+
});
10+
});
11+
12+
it('should return true for a string with length equal to the max', () => {
13+
test({
14+
validator: 'isLength',
15+
args: [{ max: 4 }],
16+
valid: ['test'],
17+
});
18+
});
19+
20+
it('should correctly calculate the length of a string with presentation sequences', () => {
21+
test({
22+
validator: 'isLength',
23+
args: [{ max: 4 }],
24+
valid: ['test\uFE0F'],
25+
});
26+
27+
test({
28+
validator: 'isLength',
29+
args: [{ min: 5, max: 5 }],
30+
valid: ['test\uFE0F\uFE0F'],
31+
});
32+
33+
test({
34+
validator: 'isLength',
35+
args: [{ min: 5, max: 5 }],
36+
valid: ['\uFE0Ftest'],
37+
});
38+
39+
test({
40+
validator: 'isLength',
41+
args: [{ min: 9, max: 9 }],
42+
valid: ['test\uFE0F\uFE0F\uFE0F\uFE0F\uFE0F\uFE0F'],
43+
});
44+
});
45+
});

0 commit comments

Comments
 (0)