-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: 코드 효율 개선 #124
fix: 코드 효율 개선 #124
Conversation
🦋 Changeset detectedLatest commit: 5e122b2 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
@@ -20,25 +20,13 @@ import { excludeLastElement } from './_internal'; | |||
* removeLastHangulCharacter('신세계') // 신세ㄱ | |||
*/ | |||
export function removeLastHangulCharacter(words: string) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 파일에 대해서 개선은, 성능 뿐만 아니라, 가독성도 크게 개선되었네요!
src/utils.ts
Outdated
if (lastChar == null) { | ||
return false; | ||
} | ||
const charCode = lastChar.charCodeAt(0); | ||
const isCompleteHangul = COMPLETE_HANGUL_START_CHARCODE <= charCode && charCode <= COMPLETE_HANGUL_END_CHARCODE; | ||
|
||
if (!isCompleteHangul) { | ||
return false; | ||
} | ||
|
||
const disassembled = disassembleHangul(lastChar); | ||
return disassembled.length === 3; | ||
const batchimCode = (charCode - COMPLETE_HANGUL_START_CHARCODE) % NUMBER_OF_JONGSUNG; | ||
return HANGUL_CHARACTERS_BY_LAST_INDEX[batchimCode].length === 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 부분은 어떠한 이유로 성능 개선이 있는지 궁금해요.
코드의 복잡도 향상에 비해 성능 개선이 더 큰 이점인가요~?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hasSingleBatchim 에서 hasBatchim 을 이용하는데, disassembleHangul 을 반복 호출하게 됩니다.
그게 아까워서 고쳐본건데, 리턴 타입 등 사용방법 자체를 바꾸는건 안될 것 같아서 이렇게 했습니다.
리턴 타입을 바꿀 수 있다면, hasBatchim 의 리턴을 int 로 하고 받침의 개수로 리턴하게 하는것을 제안합니다.
=== 1 이면 hasSingleBatchim 이고 > 0 이면 hasBatchim 인거죠.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
자세하게 답변주셔서 감사해요! 이해했습니다~!
return disassembleHangulToGroups(word).reduce((chosung, [consonant]) => { | ||
return `${chosung}${consonant}`; | ||
}, ''); | ||
return word.normalize('NFD') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 부분은 어떠한 이유로 성능 개선이 있는지 궁금해요.
코드의 복잡도 향상에 비해 성능 개선이 더 큰 이점인가요~?
word.normalize라는 메서드도 생각보다 내부적으로 복잡하게 구현되어있다면 성능의 이점이 있을까해서요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
js normalize 성능은 믿어도 될겁니다. :)
src/utils.ts
Outdated
if (lastChar == null) { | ||
return false; | ||
} | ||
const charCode = lastChar.charCodeAt(0); | ||
const isCompleteHangul = COMPLETE_HANGUL_START_CHARCODE <= charCode && charCode <= COMPLETE_HANGUL_END_CHARCODE; | ||
|
||
if (!isCompleteHangul) { | ||
return false; | ||
} | ||
|
||
const disassembled = disassembleHangul(lastChar); | ||
return disassembled.length === 3; | ||
const batchimCode = (charCode - COMPLETE_HANGUL_START_CHARCODE) % NUMBER_OF_JONGSUNG; | ||
return HANGUL_CHARACTERS_BY_LAST_INDEX[batchimCode].length === 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
자세하게 답변주셔서 감사해요! 이해했습니다~!
src/utils.ts
Outdated
.replace(/[^\u1100-\u1112\u3130-\u314e\s]+/ug, '') // NFD ㄱ-ㅎ, NFC ㄱ-ㅎ 외 문자 삭제 | ||
.replace(/[\u1100-\u1112]/g, $0 => HANGUL_CHARACTERS_BY_FIRST_INDEX[$0.charCodeAt(0) - 0x1100]); // NFD to NFC |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
요, 숫자들이 지금은 이해하기 쉽지만, 나중에 이 코드를 유지보수 하시는 분들은 이해하기 어려울 것 같은데요, 주석이 있긴 하지만요. 주석 대신 상수화를 통해 명확히 표현하는 것은 어떤가요?
const ㄱ유니코드 = 0x1100;
const ㅎ유니코드 = 0x1112;
const ㄱNFC유니코드 = 0x3130;
const ㅎNFC유니코드 = 0x314e;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
마지막 0x1100 외에는 정규식이라 new RegExp() 로 바꿔야 상수를 사용할 수 있을 것 같은데,
거기까지는 안하고 상수 정의까지만 하는것도 좋을 것 같네요.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
constants 에 있는것과 형식을 맞춰보려 했는데, 이렇게 하는게 맞을까요? ;;;
export const HANGUL_NFD_CHOSUNG_START_CHARCODE = '가'.normalize('NFD').charCodeAt(0); // 'ㄱ' 0x1100
export const HANGUL_NFD_CHOSUNG_END_CHARCODE = '하'.normalize('NFD').charCodeAt(0); // 'ㅎ' 0x1112
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
요렇게 생각해봤어요!
리팩토링해도, 여전히 복잡한 부분은 있지만, 각 절차가 상수나 함수명에 드러나서 보다 읽기 편한 것 같아용!
const HANGUL_UNICODE_RANGE = {
NFD_START: 0x1100,
NFD_END: 0x1112,
NFC_START: 0x3130,
NFC_END: 0x314e,
};
const NFD_REGEX = new RegExp(
`[^\\u${HANGUL_UNICODE_RANGE.NFD_START.toString(16)}-\\u${HANGUL_UNICODE_RANGE.NFD_END.toString(16)}\\u${HANGUL_UNICODE_RANGE.NFC_START.toString(16)}-\\u${HANGUL_UNICODE_RANGE.NFC_END.toString(16)}\\s]+`,
'ug'
);
const NFD_TO_NFC_REGEX = new RegExp(
`[\\u${HANGUL_UNICODE_RANGE.NFD_START.toString(16)}-\\u${HANGUL_UNICODE_RANGE.NFD_END.toString(16)}]`,
'g'
);
export function getChosung(word: string) {
return word
.normalize('NFD')
.replace(NFD_REGEX, '') // NFD ㄱ-ㅎ, NFC ㄱ-ㅎ 외 문자 삭제
.replace(
NFD_TO_NFC_REGEX,
$0 => HANGUL_CHARACTERS_BY_FIRST_INDEX[$0.charCodeAt(0) - HANGUL_UNICODE_RANGE.NFD_START]
); // NFD to NFC
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
range 부분은 constants.ts 에 맞춰서 넣어보려는데 어떤가요?
const _JASO_HANGUL_NFD = [...'각힣'.normalize('NFD')].map(char => char.charCodeAt(0)); // NFC 에 정의되지 않은 문자는 포함하지 않음
export const JASO_HANGUL_NFD = {
START_CHOSUNG: _JASO_HANGUL_NFD[0], // ㄱ
START_JUNGSUNG: _JASO_HANGUL_NFD[1], // ㅏ
START_JONGSUNG: _JASO_HANGUL_NFD[2], // ㄱ
END_CHOSUNG: _JASO_HANGUL_NFD[3], // ㅎ
END_JUNGSUNG: _JASO_HANGUL_NFD[4], // ㅣ
END_JONGSUNG: _JASO_HANGUL_NFD[5], // ㅎ
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
추후에도 자주 사용 될 것 같은 상수들이라면 좋은 것 같아요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
적용했습니다.
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #124 +/- ##
==========================================
- Coverage 99.56% 98.72% -0.85%
==========================================
Files 14 14
Lines 230 235 +5
Branches 51 53 +2
==========================================
+ Hits 229 232 +3
- Misses 1 3 +2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
감사합니다!
Overview
정규식 효율개선, 불필요한 계산의 생략을 했습니다.
각각의 commit 메세지를 참조해주세요.
PR Checklist