Repository navigation
fix[split]: keep surrogate pairs and combining marks together for an empty separator - #2171
Merged
Merged
Conversation
…for an empty separator
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
KooYS
marked this pull request as ready for review
October 28, 2026 06:47
raon0211
reviewed
Oct 30, 2026
Co-authored-by: Sojin Park <raon0211@gmail.com>
raon0211
approved these changes
Oct 30, 2026
1 task
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
splitin compat breaks emoji and accented characters when the separator is''.It just calls
String.prototype.split, so the string is split by UTF-16 code units.lodash handles this case differently: if the string has multi-byte characters, it splits by characters instead.
split('a😀b', '')['a', '😀', 'b']['a', '\ud83d', '\ude00', 'b']['a', '😀', 'b']split('Ame\u0301lie', '')'é'kept'\u0301'alone'é'keptsplit('👨👩👧', '')['👨👩👧']['👨👩👧']split('🇰🇷', '')['🇰🇷']['🇰🇷']split('a😀b', '', 2)['a', '😀']['a', '\ud83d']['a', '😀']This call type-checks with
@types/lodashin strict mode.Grist uses it to highlight autocomplete matches, and the comment there says it expects diacritics to stay combined:
https://github.com/gristlabs/grist-core/blob/897d07997d0d101b7fe5ecfaca61bb5f4e3a6ce0/app/client/lib/ACIndex.ts#L273
With the current compat, highlighting the first 3 characters of "Ame\u0301lie" gives
'Ame'and'\u0301lie', so the accent ends up in the wrong part.Changes
I reused the
reUnicodepattern that is already inunicodeSize.ts(it is the same one lodash uses), and added_internal/unicodeToArray.tsthat uses it, like lodash's_unicodeToArray.splitnow uses it only when the separator is''and the string has multi-byte characters. Other inputs work the same as before.Tests
Added a test that fails before this change. The compat tests pass.
I also compared the results with lodash 4.18.1 for 308 input combinations, and they all match.
Benchmark results
Run with
vitest benchon Node 22.14 (ops/sec).The first row is the existing
split.bench.ts. For the other two rows I used a temporary bench file, which is not included in this PR.'-'separator''''The ascii cases are about the same. The unicode case is slower than
main, butmainwas returning broken strings there, and the speed is now close to lodash.Bundle size of
compat/splitgoes from 388 B to 1,028 B because of thereUnicodepattern.compat/sizealready uses the same pattern, and lodash-essplitis 4,219 B.I also tried writing it as a loop instead of a regex. It matched lodash on 2,000,000 random strings, but the bundle was bigger (1,118 B), so I kept the regex.
The existing bundle-size snapshots don't change.