Skip to content

Commit

Permalink
feat: add utils package (#7)
Browse files Browse the repository at this point in the history
* hasOnlyNumberAndEnglish, hasOnlyNumberAndEnglishAndHangul 추가

* fix: 문자열 닫기 추가

* fix: hasOnlyNumberAndEnglishAndHangul.ko.md 오작성된 함수 이름 수정

* feat: 테스트 코드 작성 및 hasOnlyNumberAndEnglishAndHangul 함수 수정
  • Loading branch information
owl1753 authored May 29, 2024
1 parent 8892e12 commit 6171a65
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/utils/src/hasOnlyNumberAndEnglish.ko.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# hasOnlyNumberAndEnglish

주어진 문자열이 오직 숫자와 영어 알파벳(대소문자)으로만 구성되어 있는지 확인하는 함수입니다.

## 매개변수

- `value`: 검사할 문자열입니다.

## 반환값

문자열이 오직 숫자와 영어 알파벳으로만 구성되어 있으면 `true`를 반환하고, 그렇지 않으면 `false`를 반환합니다.

## Example

```typescript
console.log(hasOnlyNumberAndEnglish('Hello123')); // true
console.log(hasOnlyNumberAndEnglish('123')); // true
console.log(hasOnlyNumberAndEnglish('Hello')); // true
console.log(hasOnlyNumberAndEnglish('Hello123!')); // false
console.log(hasOnlyNumberAndEnglish('@#$%')); // false
```
37 changes: 37 additions & 0 deletions packages/utils/src/hasOnlyNumberAndEnglish.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, it, expect } from 'vitest';

import { hasOnlyNumberAndEnglish } from './hasOnlyNumberAndEnglish';

describe('hasOnlyNumberAndEnglish', () => {
it('should return true for a string with only numbers', () => {
expect(hasOnlyNumberAndEnglish('123456')).toBe(true);
});

it('should return true for a string with only uppercase English letters', () => {
expect(hasOnlyNumberAndEnglish('ABCDEF')).toBe(true);
});

it('should return true for a string with only lowercase English letters', () => {
expect(hasOnlyNumberAndEnglish('abcdef')).toBe(true);
});

it('should return true for a string with mixed numbers and English letters', () => {
expect(hasOnlyNumberAndEnglish('abc123DEF')).toBe(true);
});

it('should return false for a string with special characters', () => {
expect(hasOnlyNumberAndEnglish('abc123!@#')).toBe(false);
});

it('should return false for a string with spaces', () => {
expect(hasOnlyNumberAndEnglish('abc 123')).toBe(false);
});

it('should return false for a string with non-English letters', () => {
expect(hasOnlyNumberAndEnglish('abc123한글')).toBe(false);
});

it('should return true for an empty string', () => {
expect(hasOnlyNumberAndEnglish('')).toBe(true);
});
});
7 changes: 7 additions & 0 deletions packages/utils/src/hasOnlyNumberAndEnglish.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* 주어진 문자열이 오직 숫자와 영어 알파벳(대소문자)으로만 구성되어 있는지 확인하는 함수입니다.
*
* @param value - 검사할 문자열입니다.
* @returns 문자열이 오직 숫자와 영어 알파벳으로만 구성되어 있으면 true를 반환하고, 그렇지 않으면 false를 반환합니다.
*/
export const hasOnlyNumberAndEnglish = (value: string) => /^[a-zA-Z0-9]*$/.test(value);
21 changes: 21 additions & 0 deletions packages/utils/src/hasOnlyNumberAndEnglishAndHangul.ko.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# hasOnlyNumberAndEnglishAndHangul

주어진 문자열이 오직 영어 알파벳(대문자 및 소문자), 한글(자모 및 음절), 숫자로만 구성되어 있는지 확인하는 함수입니다.

## 매개변수

- `value`: 검사할 문자열입니다.

## 반환값

문자열이 오직 영어 알파벳, 한글, 숫자로만 구성되어 있으면 `true`를 반환하고, 그렇지 않으면 `false`를 반환합니다.

## Example

```typescript
console.log(hasOnlyNumberAndEnglishAndHangul('Hello123')); // true
console.log(hasOnlyNumberAndEnglishAndHangul('안녕하세요')); // true
console.log(hasOnlyNumberAndEnglishAndHangul('123')); // true
console.log(hasOnlyNumberAndEnglishAndHangul('Hello123안녕하세요')); // true
console.log(hasOnlyNumberAndEnglishAndHangul('@#$%')); // false
```
49 changes: 49 additions & 0 deletions packages/utils/src/hasOnlyNumberAndEnglishAndHangul.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { describe, it, expect } from 'vitest';

import { hasOnlyNumberAndEnglishAndHangul } from './hasOnlyNumberAndEnglishAndHangul';

describe('hasOnlyNumberAndEnglishAndHangul', () => {
it('should return true for a string with only numbers', () => {
expect(hasOnlyNumberAndEnglishAndHangul('123456')).toBe(true);
});

it('should return true for a string with only uppercase English letters', () => {
expect(hasOnlyNumberAndEnglishAndHangul('ABCDEF')).toBe(true);
});

it('should return true for a string with only lowercase English letters', () => {
expect(hasOnlyNumberAndEnglishAndHangul('abcdef')).toBe(true);
});

it('should return true for a string with mixed numbers and English letters', () => {
expect(hasOnlyNumberAndEnglishAndHangul('abc123DEF')).toBe(true);
});

it('should return true for a string with only Hangul syllables', () => {
expect(hasOnlyNumberAndEnglishAndHangul('한글테스트')).toBe(true);
});

it('should return true for a string with mixed Hangul and numbers', () => {
expect(hasOnlyNumberAndEnglishAndHangul('한글123')).toBe(true);
});

it('should return true for a string with mixed Hangul and English letters', () => {
expect(hasOnlyNumberAndEnglishAndHangul('한글abcDEF')).toBe(true);
});

it('should return false for a string with special characters', () => {
expect(hasOnlyNumberAndEnglishAndHangul('abc123!@#')).toBe(false);
});

it('should return false for a string with spaces', () => {
expect(hasOnlyNumberAndEnglishAndHangul('한글 abc 123')).toBe(false);
});

it('should return false for a string with non-Hangul, non-English letters', () => {
expect(hasOnlyNumberAndEnglishAndHangul('abc123漢字')).toBe(false);
});

it('should return true for an empty string', () => {
expect(hasOnlyNumberAndEnglishAndHangul('')).toBe(true);
});
});
9 changes: 9 additions & 0 deletions packages/utils/src/hasOnlyNumberAndEnglishAndHangul.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* 주어진 문자열이 오직 영어 알파벳(대문자 및 소문자), 한글(자모 및 음절), 숫자로만 구성되어 있는지 확인하는 함수입니다.
*
* @param value - 검사할 문자열입니다.
* @returns 문자열이 오직 영어 알파벳, 한글, 숫자로만 구성되어 있으면 true를 반환하고,
* 그렇지 않으면 false를 반환합니다.
*/
export const hasOnlyNumberAndEnglishAndHangul = (value: string) =>
/^[--a-zA-Z0-9]*$/.test(value);
2 changes: 2 additions & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { hasOnlyNumberAndEnglish } from './hasOnlyNumberAndEnglish';
export { hasOnlyNumberAndEnglishAndHangul } from './hasOnlyNumberAndEnglishAndHangul.ts';

0 comments on commit 6171a65

Please sign in to comment.