Skip to content
Snippets Groups Projects
Unverified Commit 67b076ae authored by Jamie Magee's avatar Jamie Magee Committed by GitHub
Browse files

refactor: safely parse emoji shortcodes (#20814)

parent 01a1af5d
No related branches found
No related tags found
No related merge requests found
......@@ -7,8 +7,10 @@ import {
} from 'emojibase';
import emojibaseEmojiRegex from 'emojibase-regex/emoji.js';
import SHORTCODE_REGEX from 'emojibase-regex/shortcode.js';
import { z } from 'zod';
import type { RenovateConfig } from '../config/types';
import dataFiles from '../data-files.generated';
import { logger } from '../logger';
import { regEx } from './regex';
let unicodeEmoji = true;
......@@ -17,13 +19,25 @@ let mappingsInitialized = false;
const shortCodesByHex = new Map<string, string>();
const hexCodesByShort = new Map<string, string>();
const EmojiShortcodesSchema = z.record(
z.string(),
z.union([z.string(), z.array(z.string())])
);
function lazyInitMappings(): void {
if (!mappingsInitialized) {
const table: Record<string, string | string[]> = JSON.parse(
const result = EmojiShortcodesSchema.safeParse(
JSON.parse(
dataFiles.get('node_modules/emojibase-data/en/shortcodes/github.json')!
)
);
for (const [hex, val] of Object.entries(table)) {
const shortCodes: string[] = is.array<string>(val) ? val : [val];
// istanbul ignore if: not easily testable
if (!result.success) {
logger.warn({ error: result.error }, 'Unable to parse emoji shortcodes');
return;
}
for (const [hex, val] of Object.entries(result.data)) {
const shortCodes = is.array(val) ? val : [val];
shortCodesByHex.set(hex, `:${shortCodes[0]}:`);
shortCodes.forEach((shortCode) => {
hexCodesByShort.set(`:${shortCode}:`, hex);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment