Skip to content

Commit

Permalink
feat: 숫자를 날짜를 나타내는 순우리말로 바꿔주는 함수 중 days를 추가 (#228)
Browse files Browse the repository at this point in the history
* feat: days 구현

* test: days에 대한 테스트 작성

* feat: 테스트를 통과하도록 미구현부 구현

* feat: index 에서 export 하도록 수정

* chore: 코드 정리

* chore: 불필요한 주석 제거

* docs: date mdx 파일 추가

* test: 테스트를 date로 감싸고 invaildNumber에서 101보다 가까운 31로 수정

* docs: 잘못된 예제 수정

* refactor: date의 확장을 고려해 폴더구조를 변경

* Create spicy-teachers-admire.md

---------

Co-authored-by: 박찬혁 <[email protected]>
  • Loading branch information
BO-LIKE-CHICKEN and okinawaa authored Aug 9, 2024
1 parent 08adfc9 commit 0633b9f
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/spicy-teachers-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"es-hangul": minor
---

feat: 숫자를 날짜를 나타내는 순우리말로 바꿔주는 함수 중 days를 추가
48 changes: 48 additions & 0 deletions docs/src/pages/docs/api/date.en.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
title: date
---

import { Sandpack } from '@/components/Sandpack';

# date

Convert numbers to native Korean words representing dates. The given number is valid when it is greater than 0 and less than or equal to 30.

## days

Convert numbers to native Korean words for days.

```typescript
function days(
// Number to convert
num: number
): string;
```

### Examples

```typescript
days(1); // '하루'
days(2); // '이틀'
days(3); // '사흘'
days(10); // '열흘'
days(11); // '열하루'
days(20); // '스무날'
days(29); // '스무아흐레'
days(30); // '서른날'
```

### Demo

<br />

<Sandpack>

```ts index.ts
import { days } from 'es-hangul';

console.log(days(3));
console.log(days(4));
```

</Sandpack>
48 changes: 48 additions & 0 deletions docs/src/pages/docs/api/date.ko.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
title: date
---

import { Sandpack } from '@/components/Sandpack';

# date

숫자를 날짜를 나타내는 순우리말로 바꿔줍니다. 주어진 숫자가 0보다 크고 30 이하일 때 유효합니다.

## days

숫자를 순우리말 날로 바꿔줍니다.

```typescript
function days(
// 변환할 숫자
num: number
): string;
```

### Examples

```typescript
days(1); // '하루'
days(2); // '이틀'
days(3); // '사흘'
days(10); // '열흘'
days(11); // '열하루'
days(20); // '스무날'
days(29); // '스무아흐레'
days(30); // '서른날'
```

### 사용해보기

<br />

<Sandpack>

```ts index.ts
import { days } from 'es-hangul';

console.log(days(3));
console.log(days(4));
```

</Sandpack>
36 changes: 36 additions & 0 deletions src/date/date.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { days } from '.';

describe('date', () => {
describe('days', () => {
const validNumbers = [
{ num: 1, word: '하루' },
{ num: 2, word: '이틀' },
{ num: 3, word: '사흘' },
{ num: 4, word: '나흘' },
{ num: 5, word: '닷새' },
{ num: 6, word: '엿새' },
{ num: 7, word: '이레' },
{ num: 8, word: '여드레' },
{ num: 9, word: '아흐레' },
{ num: 10, word: '열흘' },
{ num: 11, word: '열하루' },
{ num: 20, word: '스무날' },
{ num: 21, word: '스무하루' },
{ num: 30, word: '서른날' },
];

const invalidNumbers = [0, -1, 31, 1.1, -1.1, Infinity, -Infinity, NaN];

validNumbers.forEach(({ num, word }) => {
it(`${num} - 순 우리말 날짜 ${word}로 바꿔 반환해야 한다.`, () => {
expect(days(num)).toBe(word);
});
});

invalidNumbers.forEach(num => {
it(`유효하지 않은 숫자 ${num}에 대해 오류를 발생시켜야 한다.`, () => {
expect(() => days(num)).toThrow('지원하지 않는 숫자입니다.');
});
});
});
});
19 changes: 19 additions & 0 deletions src/date/days.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const DAYS_MAP = {
1: '하루',
2: '이틀',
3: '사흘',
4: '나흘',
5: '닷새',
6: '엿새',
7: '이레',
8: '여드레',
9: '아흐레',
10: '열',
20: '스무',
} as const;

export const DAYS_ONLY_TENS_MAP = {
10: '열흘',
20: '스무날',
30: '서른날',
} as const;
28 changes: 28 additions & 0 deletions src/date/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { hasProperty } from '../_internal';
import { DAYS_MAP, DAYS_ONLY_TENS_MAP } from './days.constants';

export function days(num: number): string {
return getNumberWord(num);
}

function getNumberWord(num: number): string {
validateNumber(num);

const tens = Math.floor(num / 10) * 10;
const ones = num % 10;

if (ones === 0 && hasProperty(DAYS_ONLY_TENS_MAP, tens)) {
return DAYS_ONLY_TENS_MAP[tens];
}

const tensWord = hasProperty(DAYS_MAP, tens) ? DAYS_MAP[tens] : '';
const onesWord = hasProperty(DAYS_MAP, ones) ? DAYS_MAP[ones] : '';

return `${tensWord}${onesWord}`;
}

function validateNumber(num: number): void {
if (Number.isNaN(num) || num <= 0 || num > 30 || !Number.isInteger(num) || !Number.isFinite(num)) {
throw new Error('지원하지 않는 숫자입니다.');
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { assemble } from './assemble';
export { combineCharacter, combineVowels } from './combineCharacter';
export { convertQwertyToHangul, convertQwertyToAlphabet } from './convertQwertyToAlphabet';
export { days } from './date';
export { disassemble, disassembleToGroups } from './disassemble';
export { disassembleCompleteCharacter } from './disassembleCompleteCharacter';
export { josa } from './josa';
Expand Down

0 comments on commit 0633b9f

Please sign in to comment.