Skip to content

Commit

Permalink
fix(isLength) added process to subtract Presentation Sequences charac…
Browse files Browse the repository at this point in the history
…ters
  • Loading branch information
ikkyu-3 committed May 15, 2022
1 parent cfcf911 commit 95345ae
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 @@ -4586,6 +4586,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 95345ae

Please sign in to comment.