diff --git a/.changeset/fix-remove-last-character-surrogate.md b/.changeset/fix-remove-last-character-surrogate.md new file mode 100644 index 00000000..14c14bde --- /dev/null +++ b/.changeset/fix-remove-last-character-surrogate.md @@ -0,0 +1,5 @@ +--- +'es-hangul': patch +--- + +fix: `removeLastCharacter`가 이모지처럼 코드 유닛 두 개로 된 문자를 반쪽만 지우던 문제를 수정합니다. (예: `removeLastCharacter('안녕😀')` → `'안녕'`) diff --git a/src/core/removeLastCharacter/removeLastCharacter.spec.ts b/src/core/removeLastCharacter/removeLastCharacter.spec.ts index ded3c260..a52566d8 100644 --- a/src/core/removeLastCharacter/removeLastCharacter.spec.ts +++ b/src/core/removeLastCharacter/removeLastCharacter.spec.ts @@ -34,4 +34,9 @@ describe('removeLastCharacter', () => { it('빈 문자열일 경우 빈 문자열을 반환한다.', () => { expect(removeLastCharacter('')).toBe(''); }); + it('마지막 문자가 이모지처럼 코드 유닛 두 개로 된 문자인 경우 문자 전체를 지운다.', () => { + expect(removeLastCharacter('😀')).toBe(''); + expect(removeLastCharacter('안녕😀')).toBe('안녕'); + expect(removeLastCharacter('𠮷')).toBe(''); + }); }); diff --git a/src/core/removeLastCharacter/removeLastCharacter.ts b/src/core/removeLastCharacter/removeLastCharacter.ts index e885155c..cf812f38 100644 --- a/src/core/removeLastCharacter/removeLastCharacter.ts +++ b/src/core/removeLastCharacter/removeLastCharacter.ts @@ -21,7 +21,8 @@ import { combineCharacter } from '../combineCharacter'; * removeLastCharacter('신세계') // 신세ㄱ */ export function removeLastCharacter(words: string) { - const lastCharacter = words[words.length - 1]; + // 이모지처럼 코드 유닛 두 개로 된 문자를 반쪽만 지우지 않도록 코드 포인트 단위로 자른다. + const lastCharacter = Array.from(words).pop(); if (lastCharacter == null) { return ''; } @@ -45,5 +46,5 @@ export function removeLastCharacter(words: string) { } })(); - return [words.substring(0, words.length - 1), result].join(''); + return [words.substring(0, words.length - lastCharacter.length), result].join(''); }