Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
mununki committed Feb 3, 2021
0 parents commit 992eb84
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
yarn.lock

# common
.DS_Store
node_modules
logs/
*.log
../.idea

# ocaml, reason
lib
.bsb.lock
.merlin
*.bs.js
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Greenlabs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# bs-num-to-kor

`bs-num-to-kor`은 숫자를 한글 숫자로 변환해주는 리스트립트(ReScript) 모듈 입니다.

###
```
1234 -> 1,234
1234567890 -> 12억 3,456만 7,890
1234567890 -> 12억 3,456만 // 만 이하 절삭
```

## 설치하기

1. 모듈 설치

```shell
npm i bs-num-to-kor
or
yarn add bs-num-to-kor
```

2. `bsconfig.json` 의존성 추가하기

```json
"bs-dependencies": [
"bs-num-to-kor"
]
```

## 사용방법

1. `fromInt`

```reason
NumToKorean.fromInt(1234, ())
// 1,234
```

2. `fromFloat`

```reason
NumToKorean.fromFloat(1234567890.1, ~drop=1, ())
// 12억 3,456만
```

3. `fromString`

```reason
NumToKorean.fromString("1234567890987654321", ~drop=1, ())
// 123경 4,567조 8,909억 8,765만
```

## 테스트

```shell
npm run test
or
yarn test
```
56 changes: 56 additions & 0 deletions __tests__/numToKorean_test.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
open Jest;

describe(`1만 이하 표시`, () => {
open Expect;

test(`NumToKorean.fromInt`, () => {
let result = NumToKorean.fromInt(1234, ());
expect(result) |> toBe(`1,234`);
});
test(`NumToKorean.fromFloat`, () => {
let result = NumToKorean.fromFloat(152212525110.0, ());
expect(result) |> toBe(`1,522억 1,252만 5,110`);
});
test(`NumToKorean.fromInt64`, () => {
let result =
NumToKorean.fromInt64(Int64.of_string("1234567890987654321"), ());
expect(result) |> toBe(`123경 4,567조 8,909억 8,765만 4,321`);
});
});

describe(`1만 이하는 절삭`, () => {
open Expect;

test(`1234 => ""`, () => {
let result = NumToKorean.fromInt(1234, ~drop=1, ());
expect(result) |> toBe(``);
});
test(`1234567 => "123만"`, () => {
let result = NumToKorean.fromInt(1234567, ~drop=1, ());
expect(result) |> toBe(`123만`);
});
test(`1234567890 => "12억 3,456만"`, () => {
let result = NumToKorean.fromInt(1234567890, ~drop=1, ());
expect(result) |> toBe(`12억 3,456만`);
});
test(`1234567890.1 => "12억 3,456만"`, () => {
let result = NumToKorean.fromFloat(1234567890.1, ~drop=1, ());
expect(result) |> toBe(`12억 3,456만`);
});
test(`152212525110 => "1,522억 1,252만"`, () => {
let result = NumToKorean.fromFloat(152212525110.0, ~drop=1, ());
expect(result) |> toBe(`1,522억 1,252만`);
});
test(`152212525110 => "1,522억"`, () => {
let result = NumToKorean.fromFloat(152200005110.0, ~drop=1, ());
expect(result) |> toBe(`1,522억`);
});
test(`220000152212525110.0 => "22경 1,522억 1,252만"`, () => {
let result = NumToKorean.fromFloat(220000152212525110.0, ~drop=1, ());
expect(result) |> toBe(`22경 1,522억 1,252만`);
});
test(`1234567890987654321 => "123경 4,567조 8,909억 8,765만"`, () => {
let result = NumToKorean.fromString("1234567890987654321", ~drop=1, ());
expect(result) |> toBe(`123경 4,567조 8,909억 8,765만`);
});
});
27 changes: 27 additions & 0 deletions bsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "bs-num-to-kor",
"bsc-flags": [
"-bs-super-errors"
],
"sources": [
{
"dir": "src"
},
{
"dir": "__tests__",
"type": "dev"
}
],
"package-specs": [
{
"module": "commonjs",
"in-source": true
}
],
"suffix": ".bs.js",
"namespace": true,
"refmt": 3,
"bs-dev-dependencies": [
"@glennsl/bs-jest"
]
}
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "bs-num-to-kor",
"version": "0.1.0",
"description": "ReScript module to convert the number to Korean",
"scripts": {
"re:watch": "bsb -make-world -w",
"re:build": "bsb -make-world",
"re:clean": "bsb -clean-world",
"test": "jest"
},
"repository": {
"type": "git",
"url": "git+https://github.com/green-labs/bs-num-to-kor.git"
},
"keywords": [
"ReScript",
"ReasonML",
"BuckleScript"
],
"author": "Greenlabs Dev <developer@greenlabs.co.kr>",
"license": "MIT",
"bugs": {
"url": "https://github.com/green-labs/bs-num-to-kor/issues"
},
"homepage": "https://github.com/green-labs/bs-num-to-kor#readme",
"devDependencies": {
"@glennsl/bs-jest": "^0.6.0",
"bs-platform": "^8.4.2"
}
}
38 changes: 38 additions & 0 deletions src/NumToKorean.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
@bs.send external toLocaleString: (int, string) => string = "toLocaleString";

open Belt;

let unitsInKorean = list{``, `만`, `억`, `조`, `경`};
let mann = Int64.of_int(10000);
let rec split = divided =>
Int64.equal(divided, Int64.zero)
? list{} : list{Int64.rem(divided, mann), ...split(Int64.div(divided, mann))};

let fromInt64 = (num, ~drop=0, ()) => {
split(num)
->List.zipBy(unitsInKorean, (num, unit) => {
Int64.equal(num, Int64.zero)
? "" : num->Int64.to_int->toLocaleString("ko-KR") ++ unit
})
->List.drop(drop)
->Option.getWithDefault(list{})
->List.keep(i => i != "")
->List.reverse
->List.toArray
->Js.Array.joinWith(" ", _);
};

let fromInt = (num, ~drop=0, ()) => {
fromInt64(Int64.of_int(num), ~drop, ());
};
let fromFloat = (num, ~drop=0, ()) => {
fromInt64(Int64.of_float(ceil(num)), ~drop, ());
};

/**
* JSNumber의 limit을 넘는 수를 BigInt로 생성하기 전에 number로 다루는 것 자체가
* 에러를 발생할 수 있기 때문에, fromString이 필요하여 추가
*/
let fromString = (num, ~drop=0, ()) => {
fromInt64(Int64.of_string(num), ~drop, ());
};

0 comments on commit 992eb84

Please sign in to comment.