-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 숫자를 날짜를 나타내는 순우리말로 바꿔주는 함수 중 days를 추가 (#228)
* 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
1 parent
08adfc9
commit 0633b9f
Showing
7 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"es-hangul": minor | ||
--- | ||
|
||
feat: 숫자를 날짜를 나타내는 순우리말로 바꿔주는 함수 중 days를 추가 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('지원하지 않는 숫자입니다.'); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('지원하지 않는 숫자입니다.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters