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

feat: 숫자를 날짜를 나타내는 순우리말로 바꿔주는 함수 중 days를 추가 #228

Merged
merged 11 commits into from
Aug 9, 2024
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
BO-LIKE-CHICKEN marked this conversation as resolved.
Show resolved Hide resolved
---

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('지원하지 않는 숫자입니다.');
});
});
});
});
Comment on lines +3 to +36
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍👍👍👍👍👍👍👍

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