diff --git a/src/chatUtils.test.ts b/src/chatUtils.test.ts index 4c86f904..6d683919 100644 --- a/src/chatUtils.test.ts +++ b/src/chatUtils.test.ts @@ -70,9 +70,15 @@ test('isAllowedChatCharacter', () => { expect(isAllowedChatCharacter('a')).toBe(true) expect(isAllowedChatCharacter('§')).toBe(false) expect(isAllowedChatCharacter(' ')).toBe(true) - expect(isAllowedChatCharacter('ツ')).toBe(true) - expect(isStringAllowed('🟢')).toMatchObject({ + expect(isStringAllowed('a§b')).toMatchObject({ + valid: false, + clean: 'ab', + invalid: ['§'] + }) + expect(isStringAllowed('aツ')).toMatchObject({ + valid: true, + }) + expect(isStringAllowed('a🟢')).toMatchObject({ valid: true, - invalid: null }) }) diff --git a/src/chatUtils.ts b/src/chatUtils.ts index 6253569e..384dbdae 100644 --- a/src/chatUtils.ts +++ b/src/chatUtils.ts @@ -137,14 +137,24 @@ export function isAllowedChatCharacter (char: string): boolean { export const isStringAllowed = (str: string) => { const invalidChars = new Set() - for (const char of str) { + for (const [i, char] of [...str].entries()) { + const isSurrogatePair = str.codePointAt(i) !== str['charCodeAt'](i) + if (isSurrogatePair) continue + if (!isAllowedChatCharacter(char)) { invalidChars.add(char) } } + const valid = invalidChars.size === 0 + if (valid) { + return { + valid: true + } + } + return { - valid: invalidChars.size === 0, + valid, clean: [...str].filter(c => !invalidChars.has(c)).join(''), invalid: [...invalidChars] }