First commit.

Signed-off-by: Chen Xiao <abigwc@gmail.com>
This commit is contained in:
Chen Xiao
2026-05-08 14:43:16 +08:00
commit 0b64e2de94
10989 changed files with 2253791 additions and 0 deletions
+34
View File
@@ -0,0 +1,34 @@
/**
* Create regular expression instance
*/
declare function createEmojiRegExp(regexp: string): RegExp;
/**
* Match
*/
interface EmojiRegexMatch {
match: string;
sequence: number[];
keyword: string;
regexp: number;
}
/**
* Add prev/next
*/
interface PrevMatch {
match: EmojiRegexMatch;
prev: string;
}
interface PrevNextMatch extends PrevMatch {
next: string;
}
/**
* Find emojis in text
*
* Returns only one entry per match
*/
declare function getEmojiMatchesInText(regexp: string | RegExp | (string | RegExp)[], content: string): EmojiRegexMatch[];
/**
* Sort emojis, get prev and next text
*/
declare function sortEmojiMatchesInText(content: string, matches: EmojiRegexMatch[]): PrevNextMatch[];
export { EmojiRegexMatch, createEmojiRegExp, getEmojiMatchesInText, sortEmojiMatchesInText };
+92
View File
@@ -0,0 +1,92 @@
import "../data.js";
import { convertEmojiSequenceToUTF32 } from "../convert.js";
import { getEmojiSequenceKeyword } from "../format.js";
/**
* Create regular expression instance
*/
function createEmojiRegExp(regexp) {
return new RegExp(regexp, "g");
}
/**
* Find emojis in text
*
* Returns only one entry per match
*/
function getEmojiMatchesInText(regexp, content) {
const results = [];
const found = /* @__PURE__ */ new Set();
(regexp instanceof Array ? regexp : [regexp]).forEach((regexp, index) => {
const matches = content.match(typeof regexp === "string" ? createEmojiRegExp(regexp) : regexp);
if (matches) for (let i = 0; i < matches.length; i++) {
const match = matches[i];
if (found.has(match)) continue;
found.add(match);
const sequence = [];
for (const codePoint of match) {
const num = codePoint.codePointAt(0);
if (num !== 65039) sequence.push(num);
}
results.push({
match,
sequence,
keyword: getEmojiSequenceKeyword(convertEmojiSequenceToUTF32(sequence)),
regexp: index
});
}
});
results.sort((a, b) => {
const match1 = a.match;
const match2 = b.match;
if (match2.length === match1.length) return match1.localeCompare(match2);
return match2.length - match1.length;
});
return results;
}
/**
* Sort emojis, get prev and next text
*/
function sortEmojiMatchesInText(content, matches) {
const ranges = [];
const check = (start, end) => {
for (let i = 0; i < ranges.length; i++) if (start < ranges[i].end && end > ranges[i].start) return false;
return true;
};
for (let i = 0; i < matches.length; i++) {
const match = matches[i];
const search = match.match;
let startFrom = 0;
let start;
while ((start = content.indexOf(search, startFrom)) !== -1) {
const end = start + search.length;
startFrom = end;
if (check(start, end)) ranges.push({
start,
end,
match
});
}
}
ranges.sort((a, b) => a.start - b.start);
const list = [];
let prevRange;
let lastEnd;
for (let i = 0; i < ranges.length; i++) {
const range = ranges[i];
const prev = content.slice(prevRange ? prevRange.end : 0, range.start);
list.push({
match: range.match,
prev
});
prevRange = range;
lastEnd = range.end;
}
if (!lastEnd) return [];
return list.map((item, index) => {
const nextItem = list[index + 1];
return {
...item,
next: nextItem ? nextItem.prev : content.slice(lastEnd)
};
});
}
export { createEmojiRegExp, getEmojiMatchesInText, sortEmojiMatchesInText };
+14
View File
@@ -0,0 +1,14 @@
import { EmojiRegexMatch } from "./find.js";
/**
* Callback for replacing emoji in text
*
* Returns text to replace emoji with, undefined to skip replacement
*/
type FindAndReplaceEmojisInTextCallback = (match: EmojiRegexMatch, prev: string) => string | undefined;
/**
* Find and replace emojis in text
*
* Returns null if nothing was replaced
*/
declare function findAndReplaceEmojisInText(regexp: string | RegExp | (string | RegExp)[], content: string, callback: FindAndReplaceEmojisInTextCallback): string | null;
export { FindAndReplaceEmojisInTextCallback, findAndReplaceEmojisInText };
+26
View File
@@ -0,0 +1,26 @@
import { getEmojiMatchesInText, sortEmojiMatchesInText } from "./find.js";
/**
* Find and replace emojis in text
*
* Returns null if nothing was replaced
*/
function findAndReplaceEmojisInText(regexp, content, callback) {
const matches = getEmojiMatchesInText(regexp, content);
if (!matches.length) return null;
const sortedMatches = sortEmojiMatchesInText(content, matches);
let result = "";
let replaced = false;
for (let i = 0; i < sortedMatches.length; i++) {
const item = sortedMatches[i];
result += item.prev;
const replacement = callback({ ...item.match }, result);
if (replacement === void 0) result += item.match.match;
else {
result += replacement;
replaced = true;
}
}
result += sortedMatches[sortedMatches.length - 1].next;
return replaced ? result : null;
}
export { findAndReplaceEmojisInText };