Fix ZWJ sequence handling in calculateRating function

Replaced [...pattern] iteration with Intl.Segmenter to properly handle ZWJ sequences like the pirate flag emoji 🏴‍☠️. This ensures that complex emojis are treated as single grapheme clusters rather than being split into component Unicode code points, fixing the issue where pirate flag emojis were getting a rating of 0 instead of the correct value.

This brings the calculateRating function in line with the rest of the codebase which already uses Intl.Segmenter for proper Unicode handling.
This commit is contained in:
Filip Noetzel 2025-09-06 15:47:35 +02:00
parent c8b6605e7a
commit fb5c9496f1

View file

@ -6,8 +6,9 @@ import { SymbolSet } from '../types';
*/
export function calculateRating(pattern: string, symbolSet: SymbolSet): number {
let rating = 0;
// Use array spread to properly iterate over Unicode characters
for (const char of [...pattern]) {
// Use Intl.Segmenter to properly iterate over grapheme clusters (handles ZWJ sequences)
const segmenter = new Intl.Segmenter('en', { granularity: 'grapheme' });
for (const {segment: char} of segmenter.segment(pattern)) {
if (char === symbolSet.full) rating += 1.0;
else if (symbolSet.half && char === symbolSet.half) rating += 0.5;
}