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

fix(utils): formatNumberWithUnits, formatNumberWithCurrency 로직 개선 및 인터페이스 변경 #666

Merged
merged 4 commits into from
Jan 12, 2025

Conversation

ssi02014
Copy link
Contributor

@ssi02014 ssi02014 commented Jan 8, 2025

Overview

fix(utils): formatNumberWithUnits, formatNumberWithCurrency 로직 개선 및 인터페이스 변경

Breaking Change

formatNumberByUnits -> formatNumberWithUnits 네이밍 및 기능 변경
formatNumberCurrency -> formatNumberWithCurrency 네이밍 및 기능 변경

formatNumberWithUnits

숫자 혹은 숫자로 이루어진 문자열을 주어진 단위 별로 포맷팅하는 함수입니다.

기본 동작

import { formatNumberWithUnits } from '@modern-kit/utils';

const KRW_UNITS = [
  { unit: '조', value: 1_000_000_000_000 },
  { unit: '억', value: 100_000_000 },
  { unit: '만', value: 10_000 },
] as const;

// 기본 동작
formatNumberWithUnits(1234567) // "1,234,567"

formatNumberWithUnits(1234567, { units: KRW_UNITS }) // "123만 4,567"
formatNumberWithUnits(-1234567, { units: KRW_UNITS }) // "-123만 4,567", 음수 처리

formatNumberWithUnits('1234567', { units: KRW_UNITS }) // "123만 4,567", 숫자로 이루어진 문자열 허용

옵션 사용

import { formatNumberWithUnits } from '@modern-kit/utils';

const KRW_UNITS = [
  { unit: '조', value: 1_000_000_000_000 },
  { unit: '억', value: 100_000_000 },
  { unit: '만', value: 10_000 },
] as const;

// 단위 사이 공백 추가 (기본값: true)
formatNumberWithUnits(1234567, { units: KRW_UNITS, space: true }) // "123만 4,567"
formatNumberWithUnits(1234567, { units: KRW_UNITS, space: false }) // "123만4,567"

// 쉼표 사용 여부 (기본값: true)
formatNumberWithUnits(1234567, { units: KRW_UNITS, commas: false }) // "123만 4567"
formatNumberWithUnits(1234567, { units: KRW_UNITS, commas: true }) // "123만 4,567"

// 버림 단위 (기본값: 1)
formatNumberWithUnits(1234567, { units: KRW_UNITS, floorUnit: 10000 }) // "123만"

// 소수점 자리수 (기본값: 0)
formatNumberWithUnits(1234567.123, { units: KRW_UNITS, decimal: 2 }) // "123만 4,567.12"

formatNumberCurrency

숫자 혹은 숫자로 이루어진 문자열을 주어진 통화 기호를 추가하는 함수입니다.

기본 동작

import { formatNumberWithCurrency } from '@modern-kit/utils';

// 기본 동작
formatNumberWithCurrency(1000) // '1,000'

// 통화 기호 추가 (기본 값: '')
formatNumberWithCurrency(1000, { symbol: '원' }) // '1,000원'
formatNumberWithCurrency(1000, { symbol: '$', position: 'prefix' }) // '$1,000'

// 숫자로 이루어진 문자열도 허용
formatNumberWithCurrency('1000', { symbol: '원' }) // '1,000원'
formatNumberWithCurrency('1000', { symbol: '$', position: 'prefix' }) // '$1,000'

// 음수 처리
formatNumberWithCurrency(-1000, { symbol: '$', position: 'prefix' }) // '-$1,000'
formatNumberWithCurrency(-1000, { symbol: '원', position: 'suffix' }) // '-1,000원'

옵션 사용

import { formatNumberWithCurrency } from '@modern-kit/utils';

// 통호 기호 위치 변경 (기본값: 'suffix')
formatNumberWithCurrency(1000, { symbol: '$', position: 'prefix' }) // '$1,000'

// 공백 추가 (기본값: false)
formatNumberWithCurrency(1000, { symbol: '$', position: 'prefix', space: false }) // '$1000'
formatNumberWithCurrency(1000, { symbol: '$', position: 'prefix', space: true }) // '$ 1000'

// 천의 단위 구분 여부 (기본값: true)
formatNumberWithCurrency(1000, { symbol: '원', commas: false }) // '1000원'
formatNumberWithCurrency(1000, { symbol: '원', commas: true }) // '1,000원'

// locale 사용 
// locale 옵션이 있으면 commas 옵션을 제외한 나머지 옵션들은 무시됩니다.
formatNumberWithCurrency(1000, { locale: 'USD' }) // '$1,000'
formatNumberWithCurrency(1000, { locale: 'KRW' }) // '1,000원'
formatNumberWithCurrency(1000, { locale: 'KRW_SYMBOL' }) // '1,000₩'
formatNumberWithCurrency(1000, { locale: 'JPY' }) // '¥1,000'
formatNumberWithCurrency(1000, { locale: 'CNY' }) // '¥1,000'
formatNumberWithCurrency(1000, { locale: 'EUR' }) // '€1,000'

PR Checklist

  • All tests pass.
  • All type checks pass.
  • I have read the Contributing Guide document.
    Contributing Guide

@ssi02014 ssi02014 added @modern-kit/utils @modern-kit/utils fix 버그 수정 및 코드 개선 labels Jan 8, 2025
@ssi02014 ssi02014 self-assigned this Jan 8, 2025
Copy link

changeset-bot bot commented Jan 8, 2025

🦋 Changeset detected

Latest commit: b26fdb4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@modern-kit/utils Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

codecov bot commented Jan 8, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.22%. Comparing base (33cf068) to head (b26fdb4).
Report is 149 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #666      +/-   ##
==========================================
+ Coverage   97.41%   98.22%   +0.81%     
==========================================
  Files         164      179      +15     
  Lines        1470     1580     +110     
  Branches      361      417      +56     
==========================================
+ Hits         1432     1552     +120     
+ Misses         34       26       -8     
+ Partials        4        2       -2     
Components Coverage Δ
@modern-kit/react 96.95% <ø> (+1.73%) ⬆️
@modern-kit/utils 99.60% <ø> (-0.40%) ⬇️

@ssi02014 ssi02014 force-pushed the feat/format branch 2 times, most recently from f82ffdc to 1e14a87 Compare January 9, 2025 09:42
@ssi02014 ssi02014 force-pushed the feat/format branch 3 times, most recently from 86b9349 to 472725e Compare January 12, 2025 08:32
@ssi02014 ssi02014 merged commit 57ff5c1 into main Jan 12, 2025
3 checks passed
@ssi02014 ssi02014 deleted the feat/format branch January 12, 2025 09:21
@github-actions github-actions bot mentioned this pull request Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix 버그 수정 및 코드 개선 @modern-kit/utils @modern-kit/utils
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant