Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Intl.Segmenter example to split #194

Merged
merged 5 commits into from Mar 9, 2023
Merged

Conversation

102
Copy link
Collaborator

@102 102 commented Mar 1, 2023

Recently found out that Intl.Segmenter provides a way to split the string to graphemes just like the lodash.split does.

No more ['f', 'o', 'o', '\uD83D', '\uDC68', '‍', '\uD83D', '\uDC68', '‍', '\uD83D', '\uDC66', '‍', '\uD83D', '\uDC66'], yay!

@102 102 requested a review from cedmax March 1, 2023 08:22
@codecov
Copy link

codecov bot commented Mar 1, 2023

Codecov Report

Merging #194 (5c38c68) into production (0256fbb) will not change coverage.
The diff coverage is 100.00%.

@@              Coverage Diff               @@
##           production      #194     +/-   ##
==============================================
  Coverage      100.00%   100.00%             
==============================================
  Files             490       530     +40     
  Lines            4426      1984   -2442     
  Branches          396        85    -311     
==============================================
- Hits             4426      1984   -2442     
Impacted Files Coverage Δ
src/content/lodash/function/after/vanilla.js 100.00% <ø> (ø)
src/content/lodash/function/before/vanilla.js 100.00% <ø> (ø)
src/content/lodash/function/debounce/lodash.js 100.00% <ø> (ø)
src/content/lodash/function/delay/lodash.js 100.00% <ø> (ø)
src/content/lodash/function/once/lodash.js 100.00% <ø> (ø)
...ntent/date-fns/08-month/lastDayOfMonth/date-fns.js 100.00% <100.00%> (ø)
...ontent/date-fns/08-month/lastDayOfMonth/vanilla.js 100.00% <100.00%> (ø)
src/content/lodash/array/difference/vanilla.js 100.00% <100.00%> (ø)
src/content/lodash/array/differenceBy/vanilla.js 100.00% <100.00%> (ø)
src/content/lodash/array/flatMap/lodash.js 100.00% <100.00%> (ø)
... and 544 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@cedmax
Copy link
Owner

cedmax commented Mar 1, 2023

Hey, nice to see you back 😊😊
This looks quite cool, I'm wondering if it would make sense to abstract this as the solution even for simple strings, explaining why the String#split wouldn't make the cut for emojiis in a comment 🤔🤔

What do you think?

@102
Copy link
Collaborator Author

102 commented Mar 1, 2023

Hey, nice to see you back 😊😊 This looks quite cool, I'm wondering if it would make sense to abstract this as the solution even for simple strings, explaining why the String#split wouldn't make the cut for emojiis in a comment 🤔🤔

What do you think?

It still seems to me that for 99% of cases String#split would be a better option, you don't usually need to think this deep into emoji or other symbols with multiple charcodes. I'll add a comment with explanations though.

And the Intl.Segmenter API is quite cool, and heavily outweigh the _.split, i.e. it can split sentences by words with respect to the given locale, like in example for Japanese from mdn, but this actually has no counterpart in lodash 🙂

const str = "吾輩は猫である。名前はたぬき。";
const segmenterJa = new Intl.Segmenter("ja-JP", { granularity: "word" });

const segments = segmenterJa.segment(str);
console.table(Array.from(segments));
// [{segment: '吾輩', index: 0, input: '吾輩は猫である。名前はたぬき。', isWordLike: true},
// etc.
// ]

And it also seems that Intl.Segmenter is not yet available in Firefox, so it might make sense to keep the original one.

@cedmax
Copy link
Owner

cedmax commented Mar 9, 2023

Sorry I didn't approve it yet, I've been quite busy at work 😅
I'm also wondering if we should bring in something that is not supported in all the browsers 🤔 Would it be worth mentioning the polyfill?

@102 102 merged commit d2039b1 into cedmax:production Mar 9, 2023
9 checks passed
@102 102 deleted the production branch March 9, 2023 12:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants