Skip to content

Commit

Permalink
Merge pull request #1967 from ikkyu-3/master
Browse files Browse the repository at this point in the history
fix(isLength): added process to subtract Presentation Sequences characters
  • Loading branch information
rubiin authored Jul 18, 2022
2 parents 8233ff1 + 95345ae commit 49c3abe
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/lib/isLength.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export default function isLength(str, options) {
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 - surrogatePairs.length;
const len = str.length - presentationSequences.length - surrogatePairs.length;
return len >= min && (typeof max === 'undefined' || len <= max);
}
5 changes: 5 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -4669,6 +4669,11 @@ describe('Validators', () => {
validator: 'isLength',
valid: ['a', '', 'asds'],
});
test({
validator: 'isLength',
args: [{ max: 8 }],
valid: ['👩🦰👩👩👦👦🏳️🌈', '⏩︎⏩︎⏪︎⏪︎⏭︎⏭︎⏮︎⏮︎'],
});
});

it('should validate strings by byte length', () => {
Expand Down

0 comments on commit 49c3abe

Please sign in to comment.