Skip to content
Open
Changes from all commits
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
23 changes: 12 additions & 11 deletions shared/utils/emoji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1905,15 +1905,16 @@ const emojis = {
'wales': '🏴󠁧󠁢󠁷󠁬󠁳󠁿',
}

const emojisKeysRegex = new RegExp(
Object.keys(emojis)
.map(key => `:${key}:`)
.join('|'),
'g',
)
export function convertToEmoji(text: string): string {
return text.replace(emojisKeysRegex, match => {
const key = match.slice(1, -1) as keyof typeof emojis
return emojis[key] || match
})
export function convertToEmoji(html: string): string {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this previously took in text, but now html - is this definitely the case everywhere? and if not I guess it doesn't matter?

return html.replace(
/(<code[\s>][\s\S]*?<\/code>|<pre[\s>][\s\S]*?<\/pre>)|(:[\w+-]+:)/gi,
(match, codeBlock: string | undefined, shortcode: string | undefined) => {
if (codeBlock) return codeBlock
if (shortcode) {
const key = shortcode.slice(1, -1) as keyof typeof emojis
return emojis[key] || shortcode
Comment on lines +1914 to +1915
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify TypeScript strictness settings and locate assertion-based lookup in this utility.
fd -i 'tsconfig*.json' --exec sh -c 'echo "=== {} ==="; cat "{}"' \;
echo
rg -n --type=ts -C2 'as keyof typeof emojis|const emojis\s*:\s*Record<string,\s*string>|emojis\[key\]' shared/utils/emoji.ts

Repository: npmx-dev/npmx.dev

Length of output: 1366


🏁 Script executed:

# Check file size first
wc -l shared/utils/emoji.ts

# Read the area around the disputed lines and the emojis definition
head -n 100 shared/utils/emoji.ts | tail -n 50

# Also check the beginning to find the emojis object definition
head -n 50 shared/utils/emoji.ts

Repository: npmx-dev/npmx.dev

Length of output: 2543


🏁 Script executed:

# Read the area around the disputed lines
sed -n '1900,1920p' shared/utils/emoji.ts

# Also check the end of the file to see if there's an export
tail -n 30 shared/utils/emoji.ts

# Check for how many entries are in the emojis object
grep -n "^const emojis\|^}" shared/utils/emoji.ts | head -n 5

Repository: npmx-dev/npmx.dev

Length of output: 1512


Remove assertion-based key narrowing in emoji lookup to improve type safety.

Line 1914 uses as keyof typeof emojis, which unsafely asserts the key is valid. Instead, annotate the emojis object as Record<string, string> to allow string-indexed lookup without assertion. This approach is more permissive in the right way—string keys are valid on string-indexed maps—and aligns with strict TypeScript requirements.

Suggested refactor
-const emojis = {
+const emojis: Record<string, string> = {
   '100': '💯',
   '1234': '🔢',
   ...
 }
@@
-        const key = shortcode.slice(1, -1) as keyof typeof emojis
-        return emojis[key] || shortcode
+        const key = shortcode.slice(1, -1)
+        return emojis[key] ?? shortcode

}
return match
},
)
}
Loading