Skip to content

Commit 41bc4b4

Browse files
committed
絵文字の正規表現を大幅に軽量化
see https://github.com/slevithan/emoji-regex-xs
1 parent 2648486 commit 41bc4b4

File tree

4 files changed

+6
-25
lines changed

4 files changed

+6
-25
lines changed

package-lock.json

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@
3434
"typescript": "5.8.3",
3535
"vitest": "^3.2.4"
3636
},
37-
"dependencies": {
38-
"@twemoji/parser": "16.0.0"
39-
},
4037
"files": [
4138
"built",
4239
"CHANGELOG.md"

src/@types/twemoji.d.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/internal/parser.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
// NOTE:
2-
// tsdのテストでファイルを追加しているにも関わらず「@twemoji/parser/dist/lib/regex」の型定義ファイルがないとエラーが出るため、
3-
// このエラーを無視する。
4-
/* eslint @typescript-eslint/ban-ts-comment: 1 */
5-
// @ts-ignore
6-
import twemojiRegex from '@twemoji/parser/dist/lib/regex';
7-
81
import * as M from '..';
92
import * as P from './core';
103
import { mergeText } from './util';
@@ -17,6 +10,11 @@ const space = P.regexp(/[\u0020\u3000\t]/);
1710
const alphaAndNum = P.regexp(/[a-z0-9]/i);
1811
const newLine = P.alt([P.crlf, P.cr, P.lf]);
1912

13+
// taken from https://github.com/slevithan/emoji-regex-xs
14+
const r = String.raw;
15+
const base = r`\p{Emoji}(?:\p{EMod}|[\u{E0020}-\u{E007E}]+\u{E007F}|\uFE0F?\u20E3?)`;
16+
const emojiRegex = new RegExp(r`\p{RI}{2}|(?![#*\d](?!\uFE0F?\u20E3))${base}(?:\u200D${base})*`, 'gu');
17+
2018
function seqOrText<Parsers extends P.Parser<unknown>[]>(...parsers: Parsers): P.Parser<SeqParseResult<Parsers> | string> {
2119
return new P.Parser<SeqParseResult<Parsers> | string>((input, index, state) => {
2220
// TODO: typesafe implementation
@@ -457,8 +455,7 @@ export const language = P.createLanguage<TypeTable>({
457455
},
458456

459457
unicodeEmoji: () => {
460-
const emoji = RegExp(twemojiRegex.source);
461-
return P.regexp(emoji).map(content => {
458+
return P.regexp(emojiRegex).map(content => {
462459
// 異体字セレクタ(U+FE0F)の場合は文字として返す
463460
return content === '\uFE0F' ? content : M.UNI_EMOJI(content);
464461
});

0 commit comments

Comments
 (0)