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 lint and prettier #17

Merged
merged 3 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build
.eslintignore
16 changes: 16 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"plugins": [
"matrix-org",
"prettier"
],
"parserOptions": {
"project": ["./tsconfig.json"]
},
"extends": [
"plugin:matrix-org/typescript",
"prettier"
],
"rules": {
"prettier/prettier": "error"
}
}
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,21 @@
"scripts": {
"build": "tsc",
"start": "ts-node ./src/build.ts",
"prepare": "yarn build && yarn start"
"prepare": "yarn build && yarn start",
"lint": "eslint ."
},
"devDependencies": {
"@types/node": "^20.4.7",
"@typescript-eslint/eslint-plugin": "^8.7.0",
"@typescript-eslint/parser": "^8.7.0",
"eslint": "8.57.1",
"eslint-config-google": "^0.14.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.30.0",
"eslint-plugin-matrix-org": "^1.2.1",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-unicorn": "^55.0.0",
"prettier": "^3.3.3",
"ts-node": "^10.9.1",
"typescript": "^5.1.6"
},
Expand Down
17 changes: 9 additions & 8 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import * as fs from "fs";

import { DATA_BY_CATEGORY } from "./emoji";
import * as fs from 'fs'

const json = JSON.stringify(DATA_BY_CATEGORY)
fs.writeFile("build/emojibase.json", json, function(err) {
if (err) {
console.log(err);
} else {
console.log("Success.")
}
const json = JSON.stringify(DATA_BY_CATEGORY);
fs.writeFile("build/emojibase.json", json, function (err) {
if (err) {
console.log(err);
} else {
console.log("Success.");
}
});
141 changes: 73 additions & 68 deletions src/emoji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,92 +19,97 @@ import SHORTCODES from "emojibase-data/en/shortcodes/iamcal.json";
import { CompactEmoji } from "emojibase";

export interface Emoji extends Omit<CompactEmoji, "shortcodes"> {
// We generate a shortcode based on the label if none exist in the dataset
shortcodes: string[];
// We generate a shortcode based on the label if none exist in the dataset
shortcodes: string[];
}

// The unicode is stored without the variant selector
const UNICODE_TO_EMOJI = new Map<string, Emoji>(); // not exported as gets for it are handled by getEmojiFromUnicode
export const EMOTICON_TO_EMOJI = new Map<string, Emoji>();

export const getEmojiFromUnicode = (unicode: string): Emoji | undefined =>
UNICODE_TO_EMOJI.get(stripVariation(unicode));
UNICODE_TO_EMOJI.get(stripVariation(unicode));

const isRegionalIndicator = (x: string): boolean => {
// First verify that the string is a single character. We use Array.from
// to make sure we count by characters, not UTF-8 code units.
return (
Array.from(x).length === 1 &&
// Next verify that the character is within the code point range for
// regional indicators.
// http://unicode.org/charts/PDF/Unicode-6.0/U60-1F100.pdf
x >= "\u{1f1e6}" &&
x <= "\u{1f1ff}"
);
// First verify that the string is a single character. We use Array.from
// to make sure we count by characters, not UTF-8 code units.
return (
Array.from(x).length === 1 &&
// Next verify that the character is within the code point range for
// regional indicators.
// http://unicode.org/charts/PDF/Unicode-6.0/U60-1F100.pdf
x >= "\u{1f1e6}" &&
x <= "\u{1f1ff}"
);
};

const EMOJIBASE_GROUP_ID_TO_CATEGORY = [
"people", // smileys
"people", // actually people
"control", // modifiers and such, not displayed in picker
"nature",
"foods",
"places",
"activity",
"objects",
"symbols",
"flags",
"people", // smileys
"people", // actually people
"control", // modifiers and such, not displayed in picker
"nature",
"foods",
"places",
"activity",
"objects",
"symbols",
"flags",
];

export const DATA_BY_CATEGORY: Record<string, Emoji[]> = {
people: [],
nature: [],
foods: [],
places: [],
activity: [],
objects: [],
symbols: [],
flags: [],
people: [],
nature: [],
foods: [],
places: [],
activity: [],
objects: [],
symbols: [],
flags: [],
};

// Store various mappings from unicode/emoticon/shortcode to the Emoji objects
export const EMOJI: Emoji[] = EMOJIBASE.map((emojiData) => {
// If there's ever a gap in shortcode coverage, we fudge it by
// filling it in with the emoji's CLDR annotation
const shortcodeData = SHORTCODES[emojiData.hexcode] ?? [emojiData.label.toLowerCase().replace(/\W+/g, "_")];

const emoji: Emoji = {
...emojiData,
// Homogenize shortcodes by ensuring that everything is an array
shortcodes: typeof shortcodeData === "string" ? [shortcodeData] : shortcodeData,
};

// We manually include regional indicators in the symbols group, since
// Emojibase intentionally leaves them uncategorized
const categoryId =
EMOJIBASE_GROUP_ID_TO_CATEGORY[emoji.group!] ?? (isRegionalIndicator(emoji.unicode) ? "symbols" : null);

if (DATA_BY_CATEGORY.hasOwnProperty(categoryId)) {
DATA_BY_CATEGORY[categoryId].push(emoji);
}

// Add mapping from unicode to Emoji object
// The 'unicode' field that we use in emojibase has either
// VS15 or VS16 appended to any characters that can take
// variation selectors. Which one it appends depends
// on whether emojibase considers their type to be 'text' or
// 'emoji'. We therefore strip any variation chars from strings
// both when building the map and when looking up.
UNICODE_TO_EMOJI.set(stripVariation(emoji.unicode), emoji);

if (emoji.emoticon) {
// Add mapping from emoticon to Emoji object
Array.isArray(emoji.emoticon)
? emoji.emoticon.forEach((x) => EMOTICON_TO_EMOJI.set(x, emoji))
: EMOTICON_TO_EMOJI.set(emoji.emoticon, emoji);
}

return emoji;
// If there's ever a gap in shortcode coverage, we fudge it by
// filling it in with the emoji's CLDR annotation
const shortcodeData = SHORTCODES[emojiData.hexcode] ?? [
emojiData.label.toLowerCase().replace(/\W+/g, "_"),
];

const emoji: Emoji = {
...emojiData,
// Homogenize shortcodes by ensuring that everything is an array
shortcodes:
typeof shortcodeData === "string" ? [shortcodeData] : shortcodeData,
};

// We manually include regional indicators in the symbols group, since
// Emojibase intentionally leaves them uncategorized
const categoryId =
EMOJIBASE_GROUP_ID_TO_CATEGORY[emoji.group!] ??
(isRegionalIndicator(emoji.unicode) ? "symbols" : null);

if (DATA_BY_CATEGORY.hasOwnProperty(categoryId)) {
DATA_BY_CATEGORY[categoryId].push(emoji);
}

// Add mapping from unicode to Emoji object
// The 'unicode' field that we use in emojibase has either
// VS15 or VS16 appended to any characters that can take
// variation selectors. Which one it appends depends
// on whether emojibase considers their type to be 'text' or
// 'emoji'. We therefore strip any variation chars from strings
// both when building the map and when looking up.
UNICODE_TO_EMOJI.set(stripVariation(emoji.unicode), emoji);

if (emoji.emoticon) {
// Add mapping from emoticon to Emoji object
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to fix this in a subsequent pr

Array.isArray(emoji.emoticon)
? emoji.emoticon.forEach((x) => EMOTICON_TO_EMOJI.set(x, emoji))
: EMOTICON_TO_EMOJI.set(emoji.emoticon, emoji);
}

return emoji;
});

/**
Expand All @@ -116,5 +121,5 @@ export const EMOJI: Emoji[] = EMOJIBASE.map((emojiData) => {
* @returns {string} stripped string
*/
function stripVariation(str: string): string {
return str.replace(/[\uFE00-\uFE0F]$/, "");
return str.replace(/[\uFE00-\uFE0F]$/, "");
}
Loading
Loading