Skip to content

Commit

Permalink
feat: 숫자 사이에 콤마를 찍어주는 유틸 함수 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave352 committed Nov 5, 2023
1 parent 54abf6a commit 255a564
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/calm-pets-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@musma/react-utils": patch
---

feat: 숫자 유틸 함수작성
4 changes: 2 additions & 2 deletions packages/react-utils/src/utils/LodashUtil.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { uniqueId, isEmpty, debounce, isUndefined, findIndex } from 'lodash-es'
import { uniqueId, isEmpty, debounce, isUndefined, findIndex, isNumber } from 'lodash-es'

export { uniqueId, isEmpty, debounce, isUndefined, findIndex }
export { uniqueId, isEmpty, debounce, isUndefined, findIndex, isNumber }
18 changes: 18 additions & 0 deletions packages/react-utils/src/utils/NumberUtil.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isNumber } from './LodashUtil'

/**
* @description
* 숫자를 전달받아 null 또는 undefined 일 경우 0을 리턴해줍니다.
Expand All @@ -6,3 +8,19 @@
export function convertEmptyNumber(num?: number | null) {
return num ?? 0
}

/**
* 숫자를 문자열로 변환하고 콤마를 찍어줍니다.
* 그리고 이상한 숫자 데이터인 4294967295라는 값이 오는 경우가 생겼을 때, '-'를 표시해줍니다.
*
* @returns string ("0", "-", "1,000")
*/
export const formatNumberWithCommas = (_number?: number | null) => {
const number = isNumber(_number) ? _number : '0'

if (+number > 4200000000) {
return '-'
}

return number.toLocaleString()
}

0 comments on commit 255a564

Please sign in to comment.