Skip to content
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

[Feature]: Add Functions for Hangul Detection #154

Open
youngjae99 opened this issue Jun 30, 2024 · 1 comment
Open

[Feature]: Add Functions for Hangul Detection #154

youngjae99 opened this issue Jun 30, 2024 · 1 comment

Comments

@youngjae99
Copy link
Contributor

Description

This feature request proposes the addition of two new functions, isHangul and isOnlyHangul, to enhance our text processing capabilities. These functions will allow for the detection of Hangul characters and ensure that a string contains only Hangul characters, respectively.

Possible Solution

  1. isHangul(value: string): checks if a given string contains any Hangul characters.
export function isHangul(value: string) {
  const hangulRegex = /[ㄱ-ㅎㅏ-ㅣ가-힣]/g;
  return hangulRegex.test(value);
}
  1. isOnlyHangul(value: string): checks if a given string contains only Hangul characters, ensuring there are no English letters or numbers present.
export function isOnlyHangul(value: string) {
  const hangulRegex = /[ㄱ-ㅎㅏ-ㅣ가-힣]/g;
  const englishNumberRegex = /[a-zA-Z0-9]/g;

  // 문자열에 영어나 숫자가 포함되어 있으면 한글이 아님
  if (englishNumberRegex.test(value)) {
    return false;
  }

  // 문자열에 영어나 숫자가 없으면 한글임
  return hangulRegex.test(value);
}

etc.

No response

@po4tion
Copy link
Contributor

po4tion commented Jun 30, 2024

https://github.com/toss/es-hangul/blob/main/src/_internal/hangul.ts
해당 파일에는 내부에서 사용하는 isHangul이라는 함수가 있는데요. 얻고자 하는 것은 같지만 그 과정이 좀 다르네요.

@youngjae99 님이 고안해주신 isHangul의 경우에는 한글과 그 외의 문자가 섞여있더라도 한글이 있다면 "한글"로 판단하고 true 값을 반환해주는데요. 입력 값으로 "고양이는 cat"이라고 한다면, 이것은 한글일까요 영어일까요? 이럴 경우는 한영혼용체라고 한다고 합니다.

안내해주신 기능으로 isHangul 네이밍을 사용하기 위해서는 먼저 한영혼용체 또는 그외의 한글과 혼합된 문자열이 입력값일 경우 한글로 판단한다는 전제가 깔려있어야 할 것 같아요.
그래서 isHangul이라는 네이밍 보다는 isMixedHangul, isCombinedHangul, containHangul 등의 네이밍과 더 어울리는 기능이라고 생각해요.

그리고 isOnlyHangul의 경우에도 조금 모호한 부분이 있는데요. 영어나 숫자만 판단하면 함수 네이밍에 비해 기능이 너무 좁다는게 제 의견인데요. 이스케이프 문자 또는 영어말고 일본어가 한글과 같이 입력된다면 어떻게 될까요? isOnlyHangul 이지만 true로 반환됩니다. 그래서 isOnlyHangul이라는 네이밍 보다는 extractNumberAndEnglish가 더 어울리는 것 같아요.

어떻게 생각하고 계신지 의견을 듣고 싶어요.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants